From 4c6d3cf75f7309373909c35db5f9c25479c1addd Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Thu, 30 Nov 2023 18:44:32 -0300 Subject: [PATCH] test: add failing test cases --- test/commandExecution.test.ts | 54 +++++++++++++++++++++++++++++++++++ test/helpers/myCommand.ts | 12 ++++++++ 2 files changed, 66 insertions(+) diff --git a/test/commandExecution.test.ts b/test/commandExecution.test.ts index 213bdcef..cac5ef0c 100644 --- a/test/commandExecution.test.ts +++ b/test/commandExecution.test.ts @@ -160,6 +160,60 @@ describe('toJson', () => { expect(actual.deprecatedCommandUsed).to.equal('deprecated:alias'); }); + it('does not capture flags that have a default value but were not specified by the user', async () => { + process.env.CI = 'true'; + const config = stubInterface(sandbox, {}); + + // the `test` command has `user` flag that sets a default value + // we run this command with no flags to ensure only flags specified (typed by user) are captured in telemetry + const execution = await CommandExecution.create({ + argv: [], + command: MyCommand, + config, + }); + const actual = execution.toJson(); + expect(actual.specifiedFlags).to.equal(''); + expect(actual.specifiedFlagFullNames).to.equal(''); + expect(actual.deprecatedFlagsUsed).to.equal(''); + }); + + it('captures flag if used non-deprecated alias', async () => { + process.env.CI = 'true'; + const config = stubInterface(sandbox, {}); + + // flag name: "blue" + // flag alias: "bleu" + // non-deprecated alias + const execution = await CommandExecution.create({ + argv: ['--bleu', 'test'], + command: MyCommand, + config, + }); + const actual = execution.toJson(); + expect(actual.specifiedFlags).to.equal('blue'); + expect(actual.specifiedFlagFullNames).to.equal('blue'); + expect(actual.deprecatedFlagsUsed).to.equal(''); + }); + + it('captures flag if used non-deprecated char alias', async () => { + process.env.CI = 'true'; + const config = stubInterface(sandbox, {}); + + // flag name: "red" + // flag char: "r" + // flag char alias: "e" + // non-deprecated alias + const execution = await CommandExecution.create({ + argv: ['-e', 'test'], + command: MyCommand, + config, + }); + const actual = execution.toJson(); + expect(actual.specifiedFlags).to.equal('red'); + expect(actual.specifiedFlagFullNames).to.equal('red'); + expect(actual.deprecatedFlagsUsed).to.equal(''); + }); + it('shows short help flag', async () => { process.env.CI = 'true'; const config = stubInterface(sandbox, {}); diff --git a/test/helpers/myCommand.ts b/test/helpers/myCommand.ts index 6b098349..dc8d83df 100644 --- a/test/helpers/myCommand.ts +++ b/test/helpers/myCommand.ts @@ -17,6 +17,18 @@ export class MyCommand extends SfCommand { test: Flags.string({ char: 't' }), valid: Flags.string({ aliases: ['invalid', 'i'], char: 'v', deprecateAliases: true }), newflag: Flags.string({ aliases: ['oldflag', 'o'], char: 'n', deprecateAliases: true }), + user: Flags.string({ + aliases: ['targetuser'], + deprecateAliases: true, + default: async () => 'test', + }), + blue: Flags.string({ + aliases: ['bleu'], + }), + red: Flags.string({ + charAliases: ['e'], + char: 'r', + }), }; public static args = {};