UNPKG

arb-convert

Version:

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

96 lines (95 loc) 4.21 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 onlySourcePo = fs_1.default.readFileSync('src/cli/__tests__/only_source.po').toString(); var withTargetPo = fs_1.default.readFileSync('src/cli/__tests__/with_target.po').toString(); var withMismatchTargetPo = fs_1.default.readFileSync('src/cli/__tests__/with_mismatch_target.po').toString(); describe('arb2po 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', 'arb2po.js']; expect(function () { return require('../arb2po'); }).toThrow('process.exit() was called'); expect(exitSpy).toHaveBeenCalledWith(0); expect(helpSpy).toHaveBeenCalled(); }); test('missing --sourcefile arg', function () { process.argv = [ 'node', 'arb2po.js', '--targetfile', 'foo', ]; expect(function () { return require('../arb2po'); }).toThrow('process.exit() was called'); expect(exitSpy).toHaveBeenCalledWith(1); expect(stdoutSpy).toHaveBeenCalledWith("error: option '--sourcefile <filename>' is required"); expect(helpSpy).not.toHaveBeenCalled(); }); test('with --sourcefile arg to stdout', function () { process.argv = [ 'node', 'arb2po.js', '--sourcefile', 'src/cli/__tests__/source.arb', ]; expect(function () { return require('../arb2po'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).toHaveBeenCalledWith(onlySourcePo); expect(helpSpy).not.toHaveBeenCalled(); }); test('with --sourcefile arg to file', function () { var outPath = tempy_1.default.file({ extension: 'po' }); process.argv = [ 'node', 'arb2po.js', '--sourcefile', 'src/cli/__tests__/source.arb', '--out', outPath, ]; expect(function () { return require('../arb2po'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).not.toHaveBeenCalled(); expect(helpSpy).not.toHaveBeenCalled(); expect(fs_1.default.readFileSync(outPath).toString()).toBe(onlySourcePo); }); test('with --sourcefile and --targetfile args', function () { process.argv = [ 'node', 'arb2po.js', '--sourcefile', 'src/cli/__tests__/source.arb', '--targetfile', 'src/cli/__tests__/target_de_DE.arb', ]; expect(function () { return require('../arb2po'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).toHaveBeenCalledWith(withTargetPo); expect(helpSpy).not.toHaveBeenCalled(); }); test('with --sourcefile and --targetfile args and mismatch of keys', function () { process.argv = [ 'node', 'arb2po.js', '--sourcefile', 'src/cli/__tests__/source.arb', '--targetfile', 'src/cli/__tests__/target_mismatch_de_DE.arb', ]; expect(function () { return require('../arb2po'); }).not.toThrow(); expect(exitSpy).not.toHaveBeenCalled(); expect(stdoutSpy).toHaveBeenCalledWith(withMismatchTargetPo); expect(helpSpy).not.toHaveBeenCalled(); }); afterAll(function () { jest.restoreAllMocks(); }); });