@fontoxml/fontoxml-development-tools
Version:
Development tools for FontoXML.
207 lines (171 loc) • 6.98 kB
JavaScript
;
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const fontoxmlDevelopmentTools = require('../');
const addModulesToApp = require('../src/addModulesToApp');
const warnIfNotInsideEditorRepository = require('../src/warnIfNotInsideEditorRepository');
const App = fontoxmlDevelopmentTools.App;
let app;
let oldCwd;
describe('App', () => {
before(() => {
oldCwd = process.cwd();
process.chdir(path.join(__dirname, './app'));
app = new App({
testMode: true,
fotnoOptions: {
silent: true,
appName: 'fontoxml-development-tools-test'
}
});
assert.strictEqual(app.catchErrors, false, 'Should not catch errors when using test mode');
});
after(() => {
process.chdir(oldCwd);
});
it('has logos configured', () => {
const appWithNonTestConfigFile = new App({
configFilename: '.fdtrc',
testMode: true,
fotnoOptions: {
silent: true,
appName: 'fontoxml-development-tools-test'
}
});
assert.ok(appWithNonTestConfigFile.config.logo);
assert.ok(Array.isArray(appWithNonTestConfigFile.config.logo.logos));
assert.ok(appWithNonTestConfigFile.config.logo.logos.length);
});
it('has logoIndex configured', () => {
assert.ok(app.config.logo);
assert.ok(app.config.logo.logoIndex !== undefined);
assert.ok(app.config.logo.logoIndex !== null);
});
it('has added fdt object to request', () => {
assert.ok(app.request.fdt);
assert.ok(typeof app.request.fdt === 'object');
});
it('has added editorRepository object to request.fdt', () => {
assert.ok(app.request.fdt.editorRepository);
assert.ok(typeof app.request.fdt.editorRepository === 'object');
});
it('has built-in core module enabled', () => {
assert.ok(app.modules.some(c => c.getInfo().name === '@fontoxml/fontoxml-development-tools-module-core'));
});
it('has built-in operations module enabled', () => {
assert.ok(app.modules.some(c => c.getInfo().name === '@fontoxml/fontoxml-development-tools-module-operations'));
});
it('has built-in schema module enabled', () => {
assert.ok(app.modules.some(c => c.getInfo().name === '@fontoxml/fontoxml-development-tools-module-schema'));
});
it('can enable modules on a per editor basis using relative paths', () => {
assert.ok(app.modules.some(c => c.getInfo().name === 'test-module-1'));
});
it('can enable modules on a per editor basis using absolute paths', () => {
const appWithoutModules = new App({
testMode: true,
skipAddModules: true,
fotnoOptions: {
silent: true,
appName: 'fontoxml-development-tools-test'
}
});
// Make sure the module is not yet loaded
assert.strictEqual(appWithoutModules.modules.some(c => c.getInfo().name === 'test-module-1'), false);
// Make path absolute, and check if it is
appWithoutModules.request.fdt.editorRepository.config.developmentToolsModules[0] =
path.resolve(appWithoutModules.request.fdt.editorRepository.config.developmentToolsModules[0]);
assert.strictEqual(path.isAbsolute(appWithoutModules.request.fdt.editorRepository.config.developmentToolsModules[0]), true);
// Add modules to app, which will load the app specific module using the absolute path
addModulesToApp(appWithoutModules, appWithoutModules.request);
// Check if the module is actually loaded
assert.ok(appWithoutModules.modules.some(c => c.getInfo().name === 'test-module-1'));
});
it('can be created without tool specific logic', () => {
process.chdir(oldCwd);
const homedirConfigFilePath = path.join(os.homedir(), '.fdttestnocreatehomedirrc');
// cleanup before test
fs.unlink(homedirConfigFilePath);
const appStripped = new App({
configFilename: '.fdttestnocreatehomedirrc',
useTestOutput: true,
skipCreateConfigFileInHomedir: true,
skipCheckForUpdates: true,
skipEnrichRequestObject: true,
skipAddModules: true,
skipWarnIfNotInsideEditorRepository: true,
fotnoOptions: {
appName: 'fontoxml-development-tools-test'
}
});
assert.ok(!fs.existsSync(homedirConfigFilePath), 'Created configuration file in homedir, while it should be skipped');
assert.ok(!appStripped.testOutput.outputContains('check for updates'), 'Outputting check for updates message, while it should be skipped');
assert.ok(!appStripped.request.fdt, 'Request object was extendend, while it should be skipped');
assert.equal(appStripped.modules.filter(module => !module.hideFromList).length, 0, 'Modules were loaded, while it sohould be skipped');
assert.ok(!appStripped.testOutput.outputContains('not running from inside an editor repository'), 'Outputting outside editor repository message, while it should be skipped');
// cleanup after test
fs.unlink(homedirConfigFilePath);
});
it('does not warn when working inside an editor repository', () => {
assert.ok(warnIfNotInsideEditorRepository(app, app.request));
});
it('can warn when not working inside an editor repository', () => {
const newCwd = process.cwd();
process.chdir(oldCwd);
const appOutsideEditorRepository = new App({
testMode: true,
// Also test else case for skipCreateConfigFileInHomedir and skipCheckForUpdates when using testMode
skipCreateConfigFileInHomedir: true,
skipCheckForUpdates: true,
skipWarnIfNotInsideEditorRepository: true,
fotnoOptions: {
silent: true,
appName: 'fontoxml-development-tools-test'
}
});
process.chdir(newCwd);
assert.ok(!warnIfNotInsideEditorRepository(appOutsideEditorRepository, appOutsideEditorRepository.request));
});
it('command does not throw an error when launched from inside an editor repository', () => {
return app.run(['module', '--dry', '-a', path.join(__dirname, 'app/test-module-1')])
.then(() => {
return app.run(['test-command-1']);
});
});
it('command can throw an error when not launched from inside an editor repository', (done) => {
const newCwd = process.cwd();
process.chdir(oldCwd);
const appOutsideEditorRepository = new App({
testMode: true,
skipWarnIfNotInsideEditorRepository: true,
fotnoOptions: {
silent: true,
appName: 'fontoxml-development-tools-test'
}
});
appOutsideEditorRepository.run(['module', '--dry', '-a', path.join(__dirname, 'app/test-module-1')])
.then(() => {
return appOutsideEditorRepository.run(['test-command-1'])
.then(() => {
process.chdir(newCwd);
done(new Error('Should have thrown'));
})
.catch(error => {
process.chdir(newCwd);
assert.ok(error.hasOwnProperty('solution'));
assert.ok(error.message === 'Not running from inside an editor repository.');
assert.ok(error.solution === 'This command needs to be run from inside an editor repository.');
done();
})
.catch(assertError => {
done(assertError);
});
})
.catch((_error) => {
process.chdir(newCwd);
done(new Error('Error while setting up test'));
});
});
});