Skip to content

Commit 083c909

Browse files
authored
fix: Set correct exit code on incorect apify secrets usage (#1021)
- closes #953
1 parent 53b7d37 commit 083c909

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/lib/command-framework/apify-command.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
299299
}
300300

301301
if (missingRequiredArgs.size) {
302+
process.exitCode = 1;
302303
this._printMissingRequiredArgs(missingRequiredArgs);
303304
return;
304305
}
@@ -309,6 +310,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
309310
await this.run();
310311
} catch (err: any) {
311312
error({ message: err.message });
313+
process.exitCode ||= 1;
312314
} finally {
313315
// analytics
314316
if (!this.telemetryData.actorLanguage && COMMANDS_WITHIN_ACTOR.includes(this.commandString)) {
@@ -588,9 +590,15 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
588590
}
589591

590592
private _printMissingRequiredArgs(missingRequiredArgs: Map<string, TaggedArgBuilder<ArgTag, unknown>>) {
591-
const help = selectiveRenderHelpForCommand(this.ctor, {
592-
showUsageString: true,
593-
});
593+
let help: string | undefined;
594+
595+
try {
596+
help = selectiveRenderHelpForCommand(this.ctor, {
597+
showUsageString: true,
598+
});
599+
} catch {
600+
// Help renderer may not be registered (e.g. in tests)
601+
}
594602

595603
const widestArgNameLength = widestLine([...missingRequiredArgs.keys()].join('\n'));
596604

@@ -606,14 +614,18 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
606614
missingArgsStrings.push(` ${chalk.red('>')} ${indented}`);
607615
}
608616

617+
const messageParts = [
618+
`Missing ${missingRequiredArgs.size} required ${this.pluralString(missingRequiredArgs.size, 'argument', 'arguments')}:`,
619+
...missingArgsStrings,
620+
chalk.gray(' See more help with --help'),
621+
];
622+
623+
if (help) {
624+
messageParts.push('', help);
625+
}
626+
609627
error({
610-
message: [
611-
`Missing ${missingRequiredArgs.size} required ${this.pluralString(missingRequiredArgs.size, 'argument', 'arguments')}:`,
612-
...missingArgsStrings,
613-
chalk.gray(' See more help with --help'),
614-
'',
615-
help,
616-
].join('\n'),
628+
message: messageParts.join('\n'),
617629
});
618630
}
619631

test/local/commands/secrets/add.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ describe('apify secrets add', () => {
1616
}
1717
});
1818

19+
afterEach(() => {
20+
process.exitCode = undefined;
21+
});
22+
1923
it('should work', async () => {
2024
await testRunCommand(SecretsAddCommand, {
2125
args_name: SECRET_KEY,
@@ -26,6 +30,23 @@ describe('apify secrets add', () => {
2630
expect(secrets[SECRET_KEY]).to.eql(SECRET_VALUE);
2731
});
2832

33+
it('should exit with non-zero code when adding a duplicate secret', async () => {
34+
// The first add should succeed (added in the "should work" test above)
35+
await testRunCommand(SecretsAddCommand, {
36+
args_name: SECRET_KEY,
37+
args_value: SECRET_VALUE,
38+
});
39+
40+
expect(process.exitCode).to.eql(1);
41+
});
42+
43+
it('should exit with non-zero code when called without arguments', async () => {
44+
// @ts-expect-error -- We intentionally pass no args to test the missing args case
45+
await testRunCommand(SecretsAddCommand, {});
46+
47+
expect(process.exitCode).to.eql(1);
48+
});
49+
2950
afterAll(async () => {
3051
const secrets = getSecretsFile();
3152

0 commit comments

Comments
 (0)