UNPKG

arb-convert

Version:

Convert Application Resource Bundle (ARB) translation files to other translation formats and back

105 lines (104 loc) 4.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable global-require */ var fs_1 = __importDefault(require("fs")); var tempy_1 = __importDefault(require("tempy")); mockDateNow(); var sourceArb = fs_1.default.readFileSync('src/cli/__tests__/source.arb').toString(); var targetArb = fs_1.default.readFileSync('src/cli/__tests__/target_output.arb').toString(); describe('xliff2arb cli tool', function () { var exitSpy; var stdoutSpy; var helpSpy; beforeAll(function () { exitSpy = jest.spyOn(process, 'exit').mockImplementation(function () { throw new Error('process.exit() was called'); }); stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(); }); beforeEach(function () { // Make require() call work in all tests jest.resetModules(); exitSpy.mockClear(); stdoutSpy.mockClear(); var Command = require('commander').Command; helpSpy = jest.spyOn(Command.prototype, 'outputHelp').mockImplementation(); }); test('missing args', function () { process.argv = ['node', 'xliff2arb.js']; expect(function () { return require('../xliff2arb'); }).toThrow('process.exit() was called'); expect(exitSpy).toHaveBeenCalledWith(0); expect(helpSpy).toHaveBeenCalled(); }); test('missing --file arg', function () { process.argv = [ 'node', 'xliff2arb.js', '--sourceout', 'foo', ]; expect(function () { return require('../xliff2arb'); }).toThrow('process.exit() was called'); expect(exitSpy).toHaveBeenCalledWith(1); expect(stdoutSpy).toHaveBeenCalledWith("error: option '--file <filename>' is required"); expect(helpSpy).not.toHaveBeenCalled(); }); test('with --file arg to stdout', function () { process.argv = [ 'node', 'xliff2arb.js', '--file', 'src/cli/__tests__/only_source.xliff', ]; expect(function () { return require('../xliff2arb'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).toHaveBeenCalledWith(sourceArb); expect(helpSpy).not.toHaveBeenCalled(); }); test('with --file arg to file', function () { var sourceOutPath = tempy_1.default.file({ extension: 'arb' }); process.argv = [ 'node', 'xliff2arb.js', '--file', 'src/cli/__tests__/only_source.xliff', '--sourceout', sourceOutPath, ]; expect(function () { return require('../xliff2arb'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).not.toHaveBeenCalled(); expect(helpSpy).not.toHaveBeenCalled(); expect(fs_1.default.readFileSync(sourceOutPath).toString()).toBe(sourceArb); }); test('with --sourceout and --targetout args', function () { var sourceOutPath = tempy_1.default.file({ extension: 'arb' }); var targetOutPath = tempy_1.default.file({ extension: 'arb' }); process.argv = [ 'node', 'xliff2arb.js', '--file', 'src/cli/__tests__/with_target.xliff', '--sourceout', sourceOutPath, '--targetout', targetOutPath, ]; expect(function () { return require('../xliff2arb'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).not.toHaveBeenCalled(); expect(helpSpy).not.toHaveBeenCalled(); expect(fs_1.default.readFileSync(sourceOutPath).toString()).toBe(sourceArb); expect(fs_1.default.readFileSync(targetOutPath).toString()).toBe(targetArb); }); test('with --sourceout and --targetout args, but missing target strings', function () { var sourceOutPath = tempy_1.default.file({ extension: 'arb' }); var targetOutPath = tempy_1.default.file({ extension: 'arb' }); process.argv = [ 'node', 'xliff2arb.js', '--file', 'src/cli/__tests__/only_source.xliff', '--sourceout', sourceOutPath, '--targetout', targetOutPath, ]; expect(function () { return require('../xliff2arb'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).toHaveBeenCalled(); expect(helpSpy).not.toHaveBeenCalled(); expect(fs_1.default.readFileSync(sourceOutPath).toString()).toBe(sourceArb); expect(fs_1.default.existsSync(targetOutPath)).toBe(false); }); afterAll(function () { jest.restoreAllMocks(); }); });