diff --git a/e2e/spring-shell-e2e-tests/test/sample-e2e-boolean-arity1-default-true.test.ts b/e2e/spring-shell-e2e-tests/test/sample-e2e-boolean-arity1-default-true.test.ts new file mode 100644 index 00000000..e694c0ab --- /dev/null +++ b/e2e/spring-shell-e2e-tests/test/sample-e2e-boolean-arity1-default-true.test.ts @@ -0,0 +1,200 @@ +import 'jest-extended'; +import waitForExpect from 'wait-for-expect'; +import { Cli } from 'spring-shell-e2e'; +import { + nativeDesc, + jarDesc, + jarCommand, + nativeCommand, + jarOptions, + waitForExpectDefaultTimeout, + waitForExpectDefaultInterval, + testTimeout +} from '../src/utils'; + +describe('e2e commands boolean-arity1-default-true', () => { + let cli: Cli; + let command: string; + let options: string[] = []; + + /** + * testBooleanArity1DefaultTrue + */ + const annoDefaultWithoutOverwriteReturnsTrueDesc = 'default without overwrite returns true (anno)'; + const annoDefaultWithoutOverwriteCommand = ['e2e anno boolean-arity1-default-true']; + const annoDefaultWithoutOverwriteReturnsTrue = async (cli: Cli) => { + cli.run(); + await waitForExpect(async () => { + const screen = cli.screen(); + expect(screen).toEqual(expect.arrayContaining([expect.stringContaining('Hello true')])); + }); + await expect(cli.exitCode()).resolves.toBe(0); + }; + + /** + * testBooleanArity1DefaultTrueRegistration + */ + const regDefaultWithoutOverwriteReturnsTrueDesc = 'default without overwrite returns true (reg)'; + const regDefaultWithoutOverwriteCommand = ['e2e reg boolean-arity1-default-true']; + const regOptionalWithoutOverwriteReturnsTrue = async (cli: Cli) => { + cli.run(); + await waitForExpect(async () => { + const screen = cli.screen(); + expect(screen).toEqual(expect.arrayContaining([expect.stringContaining('Hello true')])); + }); + await expect(cli.exitCode()).resolves.toBe(0); + }; + + /** + * testBooleanArity1DefaultTrue + */ + const annoDefaultWithOverwriteFalseReturnsFalseDesc = 'default with overwrite false returns false (anno)'; + const annoDefaultWithOverwriteFalseCommand = ['e2e anno boolean-arity1-default-true --overwrite false']; + const annoDefaultWithOverwriteFalseReturnsFalse = async (cli: Cli) => { + cli.run(); + await waitForExpect(async () => { + const screen = cli.screen(); + expect(screen).toEqual(expect.arrayContaining([expect.stringContaining('Hello false')])); + }); + await expect(cli.exitCode()).resolves.toBe(0); + }; + + /** + * testBooleanArity1DefaultTrueRegistration + */ + const regDefaultWithOverwriteFalseReturnsFalseDesc = 'default with overwrite false returns false (reg)'; + const regDefaultWithOverwriteFalseCommand = ['e2e reg boolean-arity1-default-true --overwrite false']; + const regOptionalWithOverwriteFalseReturnsFalse = async (cli: Cli) => { + cli.run(); + await waitForExpect(async () => { + const screen = cli.screen(); + expect(screen).toEqual(expect.arrayContaining([expect.stringContaining('Hello false')])); + }); + await expect(cli.exitCode()).resolves.toBe(0); + }; + + beforeEach(async () => { + waitForExpect.defaults.timeout = waitForExpectDefaultTimeout; + waitForExpect.defaults.interval = waitForExpectDefaultInterval; + }, testTimeout); + + afterEach(async () => { + cli?.dispose(); + }, testTimeout); + + /** + * fatjar commands + */ + describe(jarDesc, () => { + beforeAll(() => { + command = jarCommand; + options = jarOptions; + }); + + it( + annoDefaultWithoutOverwriteReturnsTrueDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...annoDefaultWithoutOverwriteCommand] + }); + await annoDefaultWithoutOverwriteReturnsTrue(cli); + }, + testTimeout + ); + + it( + regDefaultWithoutOverwriteReturnsTrueDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...regDefaultWithoutOverwriteCommand] + }); + await regOptionalWithoutOverwriteReturnsTrue(cli); + }, + testTimeout + ); + + it( + annoDefaultWithOverwriteFalseReturnsFalseDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...annoDefaultWithOverwriteFalseCommand] + }); + await annoDefaultWithOverwriteFalseReturnsFalse(cli); + }, + testTimeout + ); + + it( + regDefaultWithOverwriteFalseReturnsFalseDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...regDefaultWithOverwriteFalseCommand] + }); + await regOptionalWithOverwriteFalseReturnsFalse(cli); + }, + testTimeout + ); + }); + + /** + * native commands + */ + describe(nativeDesc, () => { + beforeAll(() => { + command = nativeCommand; + options = []; + }); + + it( + annoDefaultWithoutOverwriteReturnsTrueDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...annoDefaultWithoutOverwriteCommand] + }); + await annoDefaultWithoutOverwriteReturnsTrue(cli); + }, + testTimeout + ); + + it( + regDefaultWithoutOverwriteReturnsTrueDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...regDefaultWithoutOverwriteCommand] + }); + await regOptionalWithoutOverwriteReturnsTrue(cli); + }, + testTimeout + ); + + it( + annoDefaultWithOverwriteFalseReturnsFalseDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...annoDefaultWithOverwriteFalseCommand] + }); + await annoDefaultWithOverwriteFalseReturnsFalse(cli); + }, + testTimeout + ); + + it( + regDefaultWithOverwriteFalseReturnsFalseDesc, + async () => { + cli = new Cli({ + command: command, + options: [...options, ...regDefaultWithOverwriteFalseCommand] + }); + await regOptionalWithOverwriteFalseReturnsFalse(cli); + }, + testTimeout + ); + }); +}); diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java index 5cdc6e96..f8999085 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/standard/E2ECommands.java @@ -17,6 +17,7 @@ package org.springframework.shell.samples.standard; import org.springframework.context.annotation.Bean; import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.command.CommandRegistration.OptionArity; import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.ShellOption; @@ -77,4 +78,32 @@ public class E2ECommands { .and() .build(); } + + @ShellMethod(key = "e2e anno boolean-arity1-default-true") + public String testBooleanArity1DefaultTrue( + @ShellOption(value = "--overwrite", arity = 1, defaultValue = "true") Boolean overwrite + ) { + return "Hello " + overwrite; + } + + @Bean + public CommandRegistration testBooleanArity1DefaultTrueRegistration() { + return CommandRegistration.builder() + .command("e2e", "reg", "boolean-arity1-default-true") + .group("E2E Commands") + .withOption() + .longNames("overwrite") + .type(Boolean.class) + .defaultValue("true") + .arity(OptionArity.ZERO_OR_ONE) + .and() + .withTarget() + .function(ctx -> { + Boolean overwrite = ctx.getOptionValue("overwrite"); + return "Hello " + overwrite; + }) + .and() + .build(); + } + }