From 4147a5528a7775dc45296ab72377e44426a4e2cd Mon Sep 17 00:00:00 2001 From: Scott Covert Date: Mon, 26 Jun 2023 22:47:58 -0400 Subject: [PATCH] fix(cli): add api version exclusion to avoid deprecated config name (#619) --- README.md | 13 +- __tests__/unit/lib/utils/cliHelper.test.js | 291 +++++++++++---------- messages/delta.js | 4 +- src/commands/sgd/source/delta.ts | 7 + src/utils/cliHelper.js | 9 +- 5 files changed, 183 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index 45678f7b..6aef9c64 100644 --- a/README.md +++ b/README.md @@ -130,16 +130,16 @@ If you run your CI/CD jobs inside a Docker image, you can add the plugin to your ## How to use it? -* [`sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-d] [-n ] [-N ] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-sgdsourcedelta--f-string--t-string--r-filepath--i-filepath--d-filepath--s-filepath--w--o-filepath--a-number--d--n-filepath--n-filepath---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal) +* [`sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-x] [-d] [-n ] [-N ] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-sgdsourcedelta--f-string--t-string--r-filepath--i-filepath--d-filepath--s-filepath--w--o-filepath--a-number--x--d--n-filepath--n-filepath---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal) -## `sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-d] [-n ] [-N ] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]` +## `sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-x] [-d] [-n ] [-N ] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]` Generate the sfdx content in source format and destructive change from two git commits ``` USAGE $ sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] - [-o ] [-a ] [-d] [-n ] [-N ] [--json] [--loglevel + [-o ] [-a ] [-x] [-d] [-n ] [-N ] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL] OPTIONS @@ -155,7 +155,8 @@ OPTIONS -a, --api-version=api-version salesforce metadata API version, default to sfdx-project.json "sourceApiVersion" attribute or - latest version + latest version, ignored if + --exclude-api-version enabled -d, --generate-delta generate delta files in [--output] folder @@ -182,6 +183,10 @@ OPTIONS -t, --to=to [default: HEAD] commit sha to where the diff is done + -x, --exclude-api-version Execute sfdx commands without a + specified salesforce metadata API + version + --json format output as json --loglevel=(trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL) [default: warn] logging level for diff --git a/__tests__/unit/lib/utils/cliHelper.test.js b/__tests__/unit/lib/utils/cliHelper.test.js index 5b192ff7..569883de 100644 --- a/__tests__/unit/lib/utils/cliHelper.test.js +++ b/__tests__/unit/lib/utils/cliHelper.test.js @@ -331,162 +331,185 @@ describe(`test if the application`, () => { beforeAll(async () => { latestAPIVersionSupported = await getLatestSupportedVersion() }) - describe('when apiVersion parameter is set with supported value', () => { - test.each([46, 52, 55])( - 'config.apiVersion (%s) equals the parameter', - async version => { - // Arrange - const work = { - ...testConfig, - config: { - ...testConfig.config, - apiVersion: version, - }, - warnings: [], + + for(const excludeApiVersion of [false, true]) { + describe('when apiVersion parameter is set with supported value', () => { + test.each([46, 52, 55])( + 'config.apiVersion (%s) equals the parameter', + async version => { + // Arrange + const work = { + ...testConfig, + config: { + ...testConfig.config, + apiVersion: version, + excludeApiVersion, + }, + warnings: [], + } + const cliHelper = new CLIHelper(work) + + // Act + await cliHelper._handleDefault() + + // Assert + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : version + ) + expect(work.warnings.length).toEqual(0) } - const cliHelper = new CLIHelper(work) - - // Act - await cliHelper._handleDefault() - - // Assert - expect(work.config.apiVersion).toEqual(version) - expect(work.warnings.length).toEqual(0) - } - ) - }) - describe('when apiVersion parameter is set with unsupported value', () => { - test.each(['NaN', 40, 55.1, 'awesome', '1000000000', 0])( - `config.apiVersion (%s) equals the latest version `, - async version => { - // Arrange - const work = { - ...testConfig, - config: { - ...testConfig.config, - apiVersion: version, - }, - warnings: [], + ) + }) + describe('when apiVersion parameter is set with unsupported value', () => { + test.each(['NaN', 40, 55.1, 'awesome', '1000000000', 0])( + `config.apiVersion (%s) equals the latest version `, + async version => { + // Arrange + const work = { + ...testConfig, + config: { + ...testConfig.config, + apiVersion: version, + excludeApiVersion, + }, + warnings: [], + } + const cliHelper = new CLIHelper(work) + + // Act + await cliHelper._handleDefault() + + // Assert + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : latestAPIVersionSupported + ) + expect(work.warnings.length).toEqual(excludeApiVersion ? 0 : 1) } - const cliHelper = new CLIHelper(work) - - // Act - await cliHelper._handleDefault() - - // Assert - expect(work.config.apiVersion).toEqual(latestAPIVersionSupported) - expect(work.warnings.length).toEqual(1) - } - ) - }) - - describe('when apiVersion parameter is not set', () => { - describe('when sfdx-project.json file exist', () => { - describe('when "sourceApiVersion" attribute is set with supported value', () => { - test.each(['46', '52', '55', '46.0', '52.0', '55.0'])( - 'config.apiVersion (%s) equals the "sourceApiVersion" attribute', - async version => { - // Arrange - fs.__setMockFiles({ - ...mockFiles, - 'sfdx-project.json': `{"sourceApiVersion":${version}}`, - }) - - const work = { - ...testConfig, - config: { - ...testConfig.config, - apiVersion: undefined, - }, - warnings: [], + ) + }) + + describe('when apiVersion parameter is not set', () => { + describe('when sfdx-project.json file exist', () => { + describe('when "sourceApiVersion" attribute is set with supported value', () => { + test.each(['46', '52', '55', '46.0', '52.0', '55.0'])( + 'config.apiVersion (%s) equals the "sourceApiVersion" attribute', + async version => { + // Arrange + fs.__setMockFiles({ + ...mockFiles, + 'sfdx-project.json': `{"sourceApiVersion":${version}}`, + }) + + const work = { + ...testConfig, + config: { + ...testConfig.config, + apiVersion: undefined, + excludeApiVersion, + }, + warnings: [], + } + const cliHelper = new CLIHelper(work) + + // Act + await cliHelper._handleDefault() + + // Assert + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : +version + ) + expect(work.warnings.length).toEqual(0) } - const cliHelper = new CLIHelper(work) - - // Act - await cliHelper._handleDefault() - - // Assert - expect(work.config.apiVersion).toEqual(+version) - expect(work.warnings.length).toEqual(0) - } - ) - }) - describe('when "sourceApiVersion" attribute is set with unsupported value', () => { - test.each([NaN, '40', 'awesome', 1000000000, ''])( - 'config.apiVersion (%s) equals the latest version', - async version => { + ) + }) + describe('when "sourceApiVersion" attribute is set with unsupported value', () => { + test.each([NaN, '40', 'awesome', 1000000000, ''])( + 'config.apiVersion (%s) equals the latest version', + async version => { + // Arrange + fs.__setMockFiles({ + ...mockFiles, + 'sfdx-project.json': `{"sourceApiVersion":"${version}"}`, + }) + + const work = { + ...testConfig, + config: { + ...testConfig.config, + apiVersion: undefined, + excludeApiVersion, + }, + warnings: [], + } + const cliHelper = new CLIHelper(work) + + // Act + await cliHelper._handleDefault() + + // Assert + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : latestAPIVersionSupported + ) + expect(work.warnings.length).toEqual(excludeApiVersion ? 0 : 1) + } + ) + }) + + describe('when "sourceApiVersion" attribute is not set', () => { + test('config.apiVersion equals the latest version', async () => { // Arrange fs.__setMockFiles({ ...mockFiles, - 'sfdx-project.json': `{"sourceApiVersion":"${version}"}`, + 'sfdx-project.json': `{}`, }) - + const work = { ...testConfig, config: { ...testConfig.config, apiVersion: undefined, + excludeApiVersion, }, warnings: [], } const cliHelper = new CLIHelper(work) - + // Act await cliHelper._handleDefault() - + // Assert - expect(work.config.apiVersion).toEqual(latestAPIVersionSupported) - expect(work.warnings.length).toEqual(1) - } - ) - }) - - test('when "sourceApiVersion" attribute is not set', async () => { - // Arrange - fs.__setMockFiles({ - ...mockFiles, - 'sfdx-project.json': `{}`, + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : latestAPIVersionSupported + ) + expect(work.warnings.length).toEqual(excludeApiVersion ? 0 : 1) + }) }) - - const work = { - ...testConfig, - config: { - ...testConfig.config, - apiVersion: undefined, - }, - warnings: [], - } - const cliHelper = new CLIHelper(work) - - // Act - await cliHelper._handleDefault() - - // Assert - expect(work.config.apiVersion).toEqual(latestAPIVersionSupported) - expect(work.warnings.length).toEqual(1) }) - }) - describe('when sfdx-project.json file does not exist', () => { - test('config.apiVersion equals the latest version', async () => { - // Arrange - const work = { - ...testConfig, - config: { - ...testConfig.config, - apiVersion: undefined, - }, - warnings: [], - } - const cliHelper = new CLIHelper(work) - - // Act - await cliHelper._handleDefault() - - // Assert - expect(work.config.apiVersion).toEqual(latestAPIVersionSupported) - expect(work.warnings.length).toEqual(0) + describe('when sfdx-project.json file does not exist', () => { + test('config.apiVersion equals the latest version', async () => { + // Arrange + const work = { + ...testConfig, + config: { + ...testConfig.config, + apiVersion: undefined, + excludeApiVersion, + }, + warnings: [], + } + const cliHelper = new CLIHelper(work) + + // Act + await cliHelper._handleDefault() + + // Assert + expect(work.config.apiVersion).toEqual( + excludeApiVersion ? undefined : latestAPIVersionSupported + ) + expect(work.warnings.length).toEqual(0) + }) }) }) - }) + } }) }) diff --git a/messages/delta.js b/messages/delta.js index a593b190..8a2ae4aa 100644 --- a/messages/delta.js +++ b/messages/delta.js @@ -11,7 +11,9 @@ module.exports = { ignoreDestructiveFlag: 'file listing paths to explicitly ignore for any destructive actions', apiVersionFlag: - 'salesforce metadata API version, default to sfdx-project.json "sourceApiVersion" attribute or latest version', + 'salesforce metadata API version, default to sfdx-project.json "sourceApiVersion" attribute or latest version, ignored if --exclude-api-version enabled', + excludeApiVersionFlag: + 'Execute sfdx commands without a specified salesforce metadata API version', deltaFlag: 'generate delta files in [--output] folder', ignoreWhitespaceFlag: 'ignore git diff whitespace (space, tab, eol) changes', includeFlag: 'file listing paths to explicitly include for any diff actions', diff --git a/src/commands/sgd/source/delta.ts b/src/commands/sgd/source/delta.ts index 5ecd5374..09370acf 100644 --- a/src/commands/sgd/source/delta.ts +++ b/src/commands/sgd/source/delta.ts @@ -7,6 +7,7 @@ const { REPO_DEFAULT_VALUE, SOURCE_DEFAULT_VALUE, OUTPUT_DEFAULT_VALUE, + EXCLUDE_API_VERSION_DEFAULT_VALUE, } = require('../../../utils/cliHelper') // Initialize Messages with the current plugin directory @@ -62,6 +63,11 @@ export default class SourceDeltaGenerate extends SfdxCommand { char: 'a', description: messages.getMessage('apiVersionFlag'), }), + 'exclude-api-version': flags.boolean({ + char: 'x', + description: messages.getMessage('excludeApiVersionFlag'), + default: EXCLUDE_API_VERSION_DEFAULT_VALUE, + }), 'generate-delta': flags.boolean({ char: 'd', description: messages.getMessage('deltaFlag'), @@ -92,6 +98,7 @@ export default class SourceDeltaGenerate extends SfdxCommand { ignore: this.flags.ignore, ignoreDestructive: this.flags['ignore-destructive'], apiVersion: this.flags['api-version'], + excludeApiVersion: this.flags['exclude-api-version'], repo: this.flags.repo, ignoreWhitespace: this.flags['ignore-whitespace'], generateDelta: this.flags['generate-delta'], diff --git a/src/utils/cliHelper.js b/src/utils/cliHelper.js index fe611fd0..7ac66d31 100644 --- a/src/utils/cliHelper.js +++ b/src/utils/cliHelper.js @@ -136,8 +136,12 @@ class CLIHelper { } async _handleDefault() { - await this._getApiVersion() - await this._apiVersionDefault() + if (!this.config.excludeApiVersion) { + await this._getApiVersion() + await this._apiVersionDefault() + } else { + this.config.apiVersion = undefined + } } async _getApiVersion() { @@ -195,5 +199,6 @@ class CLIHelper { static OUTPUT_DEFAULT_VALUE = './output' static SOURCE_DEFAULT_VALUE = './' static REPO_DEFAULT_VALUE = './' + static EXCLUDE_API_VERSION_DEFAULT_VALUE = false } module.exports = CLIHelper