diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb1fba5..7e4a822 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: - run: npm run test env: CI: true - - run: node src/cli.js publish --slack {SLACK_MVP_URL} --title 'Unit Tests' --ci-info --chart-test-summary --junit 'results/junit.xml' + - run: node src/cli.js publish --slack '{SLACK_MVP_URL}' --title 'Unit Tests' --ci-info --chart-test-summary --junit 'results/junit.xml' if: always() env: TEST_BEATS_API_KEY: ${{ secrets.TEST_BEATS_API_KEY }} diff --git a/src/cli.js b/src/cli.js index e54990a..6b1ab61 100755 --- a/src/cli.js +++ b/src/cli.js @@ -6,12 +6,11 @@ const sade = require('sade'); const prog = sade('testbeats'); const { PublishCommand } = require('./commands/publish.command'); const logger = require('./utils/logger'); -const { ConfigBuilder } = require('./utils/config.builder'); prog .version('2.0.3') - .option('-c, --config', 'path to config file') - .option('-l, --logLevel', 'Log Level', "INFO") + .option('--config', 'path to config file') + .option('--logLevel', 'Log Level', "INFO") .option('--slack', 'slack webhook url') .option('--teams', 'teams webhook url') .option('--chat', 'chat webhook url') @@ -30,8 +29,6 @@ prog.command('publish') .action(async (opts) => { try { logger.setLevel(opts.logLevel); - const config_builder = new ConfigBuilder(opts); - config_builder.build(); const publish_command = new PublishCommand(opts); await publish_command.publish(); } catch (error) { diff --git a/src/commands/publish.command.js b/src/commands/publish.command.js index 2b2007f..ced87e5 100644 --- a/src/commands/publish.command.js +++ b/src/commands/publish.command.js @@ -3,6 +3,7 @@ const trp = require('test-results-parser'); const prp = require('performance-results-parser'); const beats = require('../beats'); +const { ConfigBuilder } = require('../utils/config.builder'); const target_manager = require('../targets'); const logger = require('../utils/logger'); const { processData } = require('../helpers/helper'); @@ -19,6 +20,7 @@ class PublishCommand { async publish() { logger.info(`🥁 TestBeats v${pkg.version}`); + this.#buildConfig(); this.#validateOptions(); this.#setConfigFromFile(); this.#processConfig(); @@ -28,6 +30,11 @@ class PublishCommand { logger.info('✅ Results published successfully!'); } + #buildConfig() { + const config_builder = new ConfigBuilder(this.opts); + config_builder.build(); + } + #validateOptions() { if (!this.opts) { throw new Error('Missing publish options'); @@ -42,8 +49,10 @@ class PublishCommand { const cwd = process.cwd(); const file_path = path.join(cwd, this.opts.config); try { - this.opts.config = require(path.join(cwd, this.opts.config)); + const config_json = require(path.join(cwd, this.opts.config)); + this.opts.config = config_json; } catch (error) { + logger.error({ error }, `Failed to read config file: '${file_path}' with error: '${error.message}'`); throw new Error(`Config file not found: ${file_path}`); } } @@ -63,7 +72,7 @@ class PublishCommand { } #validateConfig() { - logger.info("🛠️ Validating configuration...") + logger.info("🛠️ Validating configuration...") for (const config of this.configs) { this.#validateResults(config); this.#validateTargets(config); diff --git a/src/utils/config.builder.js b/src/utils/config.builder.js index 4a6fe37..f7d6e31 100644 --- a/src/utils/config.builder.js +++ b/src/utils/config.builder.js @@ -1,3 +1,6 @@ +const path = require('path'); +const logger = require('./logger'); + class ConfigBuilder { /** @@ -11,16 +14,22 @@ class ConfigBuilder { if (!this.opts) { return; } - if (this.opts.config) { + if (typeof this.opts.config === 'object') { + return + } + if (this.opts.config && typeof this.opts.config === 'string') { return; } + logger.info('🏗 Building config...') this.#buildConfig(); this.#buildBeats(); this.#buildResults(); this.#buildTargets(); this.#buildExtensions(); + logger.debug(`🛠️ Generated Config: \n${JSON.stringify(this.config, null, 2)}`); + this.opts.config = this.config; } @@ -67,7 +76,7 @@ class ConfigBuilder { this.config.results = [ { type, - files: [file] + files: [path.join(file)] } ] } diff --git a/test/cli.spec.js b/test/cli.spec.js index e6b4111..aead452 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -7,6 +7,7 @@ describe('CLI', () => { it('publish results with config file', (done) => { mock.addInteraction('post test-summary to slack'); exec('node src/cli.js publish --config test/data/configs/slack.config.json', (error, stdout, stderr) => { + console.log(stdout); assert.match(stdout, /✅ Results published successfully!/); done(); }); @@ -15,6 +16,7 @@ describe('CLI', () => { it('publish results with config builder', (done) => { mock.addInteraction('post test-summary to slack'); exec('node src/cli.js publish --slack http://localhost:9393/message --testng test/data/testng/single-suite.xml', (error, stdout, stderr) => { + console.log(stdout); assert.match(stdout, /✅ Results published successfully!/); done(); }); @@ -23,6 +25,7 @@ describe('CLI', () => { it('publish results with config builder and extension', (done) => { mock.addInteraction('post test-summary to teams with qc-test-summary', { quickChartUrl: "https://quickchart.io" }); exec('node src/cli.js publish --teams http://localhost:9393/message --testng test/data/testng/single-suite-failures.xml --chart-test-summary', (error, stdout, stderr) => { + console.log(stdout); assert.match(stdout, /✅ Results published successfully!/); done(); }); @@ -32,6 +35,7 @@ describe('CLI', () => { mock.addInteraction('post test results to beats'); mock.addInteraction('post test-summary with beats to teams'); exec('node src/cli.js publish --api-key api-key --project project-name --run build-name --teams http://localhost:9393/message --testng test/data/testng/single-suite.xml', (error, stdout, stderr) => { + console.log(stdout); assert.match(stdout, /🚀 Publishing results to TestBeats Portal/); assert.match(stdout, /✅ Results published successfully!/); done(); diff --git a/test/config.spec.js b/test/config.spec.js index 5fe03dd..1e16fb8 100644 --- a/test/config.spec.js +++ b/test/config.spec.js @@ -20,7 +20,7 @@ describe('Config', () => { } catch (err) { e = err; } - assert.equal(e.message, 'Missing publish config'); + assert.equal(e.message, 'Missing results properties in config'); }); it('should not allow missing config file', async () => {