This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch maven-4.0.x in repository https://gitbox.apache.org/repos/asf/maven.git
commit 1acbade5f6d151980a904a1c07c0a76dbd56e7d6 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Mon Nov 2 15:12:46 2026 +0000 Ask again when mvnenc init gets an empty editable value --- .../maven/cling/invoker/mvnenc/goals/Init.java | 28 ++++++++++++---------- .../maven/cling/invoker/mvnenc/goals/InitTest.java | 26 ++++++++++++++++---- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java index 6b4233d1b7..aab776b9fa 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Init.java @@ -132,18 +132,22 @@ public int doExecute(EncryptContext context) throws Exception { Map<String, PromptResultItemIF> editMap; for (Map.Entry<String, PromptResultItemIF> editable : editables) { String template = editable.getValue().getResult(); - editMap = prompt.prompt( - context.header, - prompt.getPromptBuilder() - .createInputPrompt() - .name("edit") - .message(template) - .addPrompt() - .build()); - if (editMap.isEmpty()) { - throw new InterruptedException(); - } - String userInput = editMap.get("edit").getResult(); + String userInput; + // the template is not a usable value, so ask again until one is given + do { + editMap = prompt.prompt( + context.header, + prompt.getPromptBuilder() + .createInputPrompt() + .name("edit") + .message(template) + .addPrompt() + .build()); + if (editMap.isEmpty()) { + throw new InterruptedException(); + } + userInput = editMap.get("edit").getResult(); + } while (userInput == null || userInput.isBlank()); String prefix = template.substring(0, template.indexOf('$')); if (!prefix.isEmpty() && !userInput.startsWith(prefix)) { userInput = prefix + userInput; diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnenc/goals/InitTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnenc/goals/InitTest.java index 53f3de2e01..0040aa92dc 100644 --- a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnenc/goals/InitTest.java +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnenc/goals/InitTest.java @@ -64,6 +64,18 @@ class InitTest { "$VAR, my_var, my_var" }) void testPrefixPrependedToUserInput(String template, String userInput, String expectedValue) throws Exception { + assertEquals(expectedValue, runInit(template, userInput)); + } + + @ParameterizedTest + @CsvSource( + value = {"NULL", "''", "' '"}, + nullValues = "NULL") + void testEmptyEditableValueIsAskedAgain(String emptyInput) throws Exception { + assertEquals("env:my_var", runInit("env:$VARIABLE_NAME", emptyInput, "my_var")); + } + + private String runInit(String template, String... userInputs) throws Exception { MessageBuilderFactory messageBuilderFactory = mock(MessageBuilderFactory.class, Mockito.RETURNS_DEEP_STUBS); SecDispatcher secDispatcher = mock(SecDispatcher.class); @@ -107,16 +119,22 @@ void testPrefixPrependedToUserInput(String template, String userInput, String ex Map<String, PromptResultItemIF> configureResult = new HashMap<>(); configureResult.put("password", createResult(template)); - Map<String, PromptResultItemIF> editResult = new HashMap<>(); - editResult.put("edit", createResult(userInput)); + List<Map<String, PromptResultItemIF>> answers = new ArrayList<>(); + answers.add(configureResult); + for (String userInput : userInputs) { + Map<String, PromptResultItemIF> editResult = new HashMap<>(); + editResult.put("edit", createResult(userInput)); + answers.add(editResult); + } Map<String, PromptResultItemIF> confirmResult = new HashMap<>(); ConfirmResult confirm = mock(ConfirmResult.class); when(confirm.getConfirmed()).thenReturn(ConfirmChoice.ConfirmationValue.YES); confirmResult.put("confirm", confirm); + answers.add(confirmResult); when(mock.prompt(any(List.class), any(List.class))) - .thenReturn(dispatcherResult, configureResult, editResult, confirmResult); + .thenReturn(dispatcherResult, answers.toArray(new Map[0])); })) { init.doExecute(context); } @@ -129,7 +147,7 @@ void testPrefixPrependedToUserInput(String template, String userInput, String ex assertEquals(1, config.getProperties().size()); ConfigProperty prop = config.getProperties().get(0); assertEquals("password", prop.getName()); - assertEquals(expectedValue, prop.getValue()); + return prop.getValue(); } private PromptResultItemIF createResult(String value) {
