From a2ded4e8191d49fa25b2369f319c863d7565ea2b Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 4 Apr 2022 11:04:51 +0200 Subject: [PATCH] Add tests for running from command line --- fixtures/missing-translations/package.json | 3 ++ fixtures/no-issues/package.json | 3 ++ .../remove-unused-translations/package.json | 3 ++ fixtures/unused-translations/package.json | 3 ++ package.json | 1 + test.js | 39 +++++++++++++++++++ 6 files changed, 52 insertions(+) create mode 100644 fixtures/missing-translations/package.json create mode 100644 fixtures/no-issues/package.json create mode 100644 fixtures/remove-unused-translations/package.json create mode 100644 fixtures/unused-translations/package.json diff --git a/fixtures/missing-translations/package.json b/fixtures/missing-translations/package.json new file mode 100644 index 00000000..21320dfb --- /dev/null +++ b/fixtures/missing-translations/package.json @@ -0,0 +1,3 @@ +{ + "name": "missing-translations" +} diff --git a/fixtures/no-issues/package.json b/fixtures/no-issues/package.json new file mode 100644 index 00000000..433e7bae --- /dev/null +++ b/fixtures/no-issues/package.json @@ -0,0 +1,3 @@ +{ + "name": "no-issues" +} diff --git a/fixtures/remove-unused-translations/package.json b/fixtures/remove-unused-translations/package.json new file mode 100644 index 00000000..cf20f829 --- /dev/null +++ b/fixtures/remove-unused-translations/package.json @@ -0,0 +1,3 @@ +{ + "name": "remove-unused-translations" +} diff --git a/fixtures/unused-translations/package.json b/fixtures/unused-translations/package.json new file mode 100644 index 00000000..b6ecb8ed --- /dev/null +++ b/fixtures/unused-translations/package.json @@ -0,0 +1,3 @@ +{ + "name": "unused-translations" +} diff --git a/package.json b/package.json index 2724614e..f2c2db9c 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "eslint-config-prettier": "8.5.0", "eslint-plugin-node": "11.1.0", "eslint-plugin-prettier": "4.0.0", + "execa": "^5.0.0", "jest": "27.5.1", "lerna-changelog": "2.2.0", "prettier": "2.6.2", diff --git a/test.js b/test.js index 95a6227b..6edd46ab 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,4 @@ +const execa = require('execa'); const fs = require('fs'); const { run, generateFileList } = require('./index'); @@ -59,3 +60,41 @@ describe('generateFileList', () => { expect(() => generateFileList([])).toThrow('Unexpected empty file list'); }); }); + +describe('Running from cli', () => { + test('without unused translations', async () => { + let { stdout } = await execa('node', ['../../bin/cli'], { + cwd: `${__dirname}/fixtures/no-issues`, + }); + + expect(stdout).toMatch('No unused translations'); + }); + + test('with unused translations', async () => { + expect( + execa('node', ['../../bin/cli'], { cwd: `${__dirname}/fixtures/unused-translations` }) + ).rejects.toThrowError('Found 2 unused translations'); + }); + + test('with missing translations', async () => { + expect( + execa('node', ['../../bin/cli'], { cwd: `${__dirname}/fixtures/missing-translations` }) + ).rejects.toThrowError('Found 2 missing translations'); + }); + + describe('with auto-fix', () => { + afterEach(async function () { + await execa('git', ['checkout', 'HEAD', 'fixtures/remove-unused-translations/translations'], { + cwd: __dirname, + }); + }); + + test('with unused translations', async () => { + let { stdout } = await execa('node', ['../../bin/cli', '--fix'], { + cwd: `${__dirname}/fixtures/remove-unused-translations`, + }); + + expect(stdout).toMatch('All unused translations were removed'); + }); + }); +});