From 2d9dfbf93aa406bd9013686730fb6f3a5865fd21 Mon Sep 17 00:00:00 2001 From: Vio Date: Thu, 25 Aug 2022 23:41:44 +0200 Subject: [PATCH 1/2] test: CLI - add success case --- test/cli.test.js | 37 ++++++++++++++++++++++++ test/cli/valid-data/relativeci.config.js | 5 ++++ test/cli/valid-data/webpack-stats.json | 30 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 test/cli/valid-data/relativeci.config.js create mode 100644 test/cli/valid-data/webpack-stats.json diff --git a/test/cli.test.js b/test/cli.test.js index f7ef09ec..4f9cf3d1 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -1,5 +1,21 @@ +const http = require('http'); const { exec } = require('child_process'); +const MOCK_RESULT = { + res: { + job: { + internalBuildNumber: 1, + }, + }, + info: { + message: { + txt: 'Hello world!', + }, + }, +}; + +const MOCK_SERVER_PORT = 5998; + describe('CLI', () => { test('should return error if config is missing', (done) => { exec('./bin/index.js', (error, stdout, sterr) => { @@ -21,4 +37,25 @@ describe('CLI', () => { done(); }); }); + + test('should run agent successfully', (done) => { + const server = http.createServer((req, res) => { + res.write(JSON.stringify(MOCK_RESULT)); + res.end(); + }).listen(MOCK_SERVER_PORT); + + exec( + `cd test/cli/valid-data && + RELATIVE_CI_ENDPOINT=http://localhost:${MOCK_SERVER_PORT}/save \ + RELATIVE_CI_SLUG=org/project \ + RELATIVE_CI_KEY=abc123 \ + ../../../bin/index.js + `, + (_, stdout, sterr) => { + expect(stdout).toContain('Job #1 done.'); + server.close(); + done(); + }, + ); + }); }); diff --git a/test/cli/valid-data/relativeci.config.js b/test/cli/valid-data/relativeci.config.js new file mode 100644 index 00000000..bbbb7f17 --- /dev/null +++ b/test/cli/valid-data/relativeci.config.js @@ -0,0 +1,5 @@ +module.exports = { + webpack: { + stats: './webpack-stats.json', + }, +}; diff --git a/test/cli/valid-data/webpack-stats.json b/test/cli/valid-data/webpack-stats.json new file mode 100644 index 00000000..c90fb19d --- /dev/null +++ b/test/cli/valid-data/webpack-stats.json @@ -0,0 +1,30 @@ +{ + "assets": [ + { + "name": "main.js", + "size": 28 + } + ], + "chunks": [ + { + "id": 179, + "entry": true, + "initial": true, + "files": [ + "main.js" + ], + "names": [ + "main" + ] + } + ], + "modules": [ + { + "name": "./src/index.js", + "size": 29, + "chunks": [ + 179 + ] + } + ] +} From 395f64fd1cbcc85fa8feb8a6333f7c46a5435cb0 Mon Sep 17 00:00:00 2001 From: Vio Date: Thu, 25 Aug 2022 23:54:39 +0200 Subject: [PATCH 2/2] feat: CLI - add support for --config-dir --- bin/index.js | 33 ++++++++++------ test/cli.test.js | 38 +++++++++++++++---- .../app/relativeci.config.js | 5 +++ .../custom-config-dir/app/webpack-stats.json | 30 +++++++++++++++ 4 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 test/cli/custom-config-dir/app/relativeci.config.js create mode 100644 test/cli/custom-config-dir/app/webpack-stats.json diff --git a/bin/index.js b/bin/index.js index 05ef286d..0c88c2ff 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +const path = require('path'); const { get } = require('lodash'); const { readJSONSync, pathExistsSync } = require('fs-extra'); const { cosmiconfigSync } = require('cosmiconfig'); @@ -11,7 +12,21 @@ const { agent } = require('../lib/agent'); const { debug } = require('../lib/utils'); const LOCALES = require('../locales/en'); -const searchConfig = cosmiconfigSync('relativeci').search(); +const args = yargs(hideBin(process.argv)) + .usage('Usage: $0 OPTIONS') + + .option('config-dir', { describe: 'Config directory', default: '', alias: 'c' }) + + .option('commit', { describe: 'Commit SHA', default: '' }) + .option('commit-message', { describe: 'Commit message', default: '', alias: 'commitMessage' }) + .option('branch', { describe: 'Branch name', default: '' }) + .option('pr', { describe: 'Pull Request number', default: '' }) + .option('slug', { describe: 'Project slug', default: '' }) + + .help() + .argv; + +const searchConfig = cosmiconfigSync('relativeci').search(args['config-dir']); debug('Config', searchConfig); if (!searchConfig) { @@ -26,7 +41,11 @@ if (!get(config, 'webpack.stats')) { process.exit(0); } -const webpackArtifactFilepath = get(config, 'webpack.stats'); +// Load webpack stats file relative to the config file +const webpackArtifactFilepath = path.join( + path.dirname(searchConfig.filepath), + get(config, 'webpack.stats'), +); if (!pathExistsSync(webpackArtifactFilepath)) { console.error(LOCALES.CLI_MISSING_STATS_FILE_ERROR); @@ -49,16 +68,6 @@ const artifactsData = [ }, ]; -const args = yargs(hideBin(process.argv)) - .usage('Usage: $0 OPTIONS') - .option('commit', { describe: 'Commit SHA', default: '' }) - .option('commit-message', { describe: 'Commit message', default: '', alias: 'commitMessage' }) - .option('branch', { describe: 'Branch name', default: '' }) - .option('pr', { describe: 'Pull Request number', default: '' }) - .option('slug', { describe: 'Project slug', default: '' }) - .help() - .argv; - debug('CLI arguments', args); agent(artifactsData, config, args); diff --git a/test/cli.test.js b/test/cli.test.js index 4f9cf3d1..73674b5c 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -16,7 +16,22 @@ const MOCK_RESULT = { const MOCK_SERVER_PORT = 5998; +const createServer = () => http.createServer((req, res) => { + res.write(JSON.stringify(MOCK_RESULT)); + res.end(); +}); + describe('CLI', () => { + let server; + + beforeAll(() => { + server = createServer().listen(MOCK_SERVER_PORT); + }); + + afterAll(() => { + server.close(); + }); + test('should return error if config is missing', (done) => { exec('./bin/index.js', (error, stdout, sterr) => { expect(sterr).toContain('relativeci.config.js file is missing!'); @@ -39,11 +54,6 @@ describe('CLI', () => { }); test('should run agent successfully', (done) => { - const server = http.createServer((req, res) => { - res.write(JSON.stringify(MOCK_RESULT)); - res.end(); - }).listen(MOCK_SERVER_PORT); - exec( `cd test/cli/valid-data && RELATIVE_CI_ENDPOINT=http://localhost:${MOCK_SERVER_PORT}/save \ @@ -51,9 +61,23 @@ describe('CLI', () => { RELATIVE_CI_KEY=abc123 \ ../../../bin/index.js `, - (_, stdout, sterr) => { + (_, stdout) => { + expect(stdout).toContain('Job #1 done.'); + done(); + }, + ); + }); + + test('should run agent successfully from parent directory', (done) => { + exec( + `cd test/cli/custom-config-dir && + RELATIVE_CI_ENDPOINT=http://localhost:${MOCK_SERVER_PORT}/save \ + RELATIVE_CI_SLUG=org/project \ + RELATIVE_CI_KEY=abc123 \ + ../../../bin/index.js --config-dir app + `, + (_, stdout) => { expect(stdout).toContain('Job #1 done.'); - server.close(); done(); }, ); diff --git a/test/cli/custom-config-dir/app/relativeci.config.js b/test/cli/custom-config-dir/app/relativeci.config.js new file mode 100644 index 00000000..bbbb7f17 --- /dev/null +++ b/test/cli/custom-config-dir/app/relativeci.config.js @@ -0,0 +1,5 @@ +module.exports = { + webpack: { + stats: './webpack-stats.json', + }, +}; diff --git a/test/cli/custom-config-dir/app/webpack-stats.json b/test/cli/custom-config-dir/app/webpack-stats.json new file mode 100644 index 00000000..c90fb19d --- /dev/null +++ b/test/cli/custom-config-dir/app/webpack-stats.json @@ -0,0 +1,30 @@ +{ + "assets": [ + { + "name": "main.js", + "size": 28 + } + ], + "chunks": [ + { + "id": 179, + "entry": true, + "initial": true, + "files": [ + "main.js" + ], + "names": [ + "main" + ] + } + ], + "modules": [ + { + "name": "./src/index.js", + "size": 29, + "chunks": [ + 179 + ] + } + ] +}