Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.springframework.shell.core.autoconfigure;

import org.springframework.boot.EnvironmentPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.Map;

import static org.springframework.core.env.CommandLinePropertySource.DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;

/**
* This class disables the interactive shell mode by setting
* {@code spring.shell.interactive.enabled} to {@code false} when certain command-line
* arguments are passed to the application.
*
* @author David Pilar
*/
public class SpringShellEnvironmentPostProcessor implements EnvironmentPostProcessor {

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String[] args = environment.getProperty(DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME, String[].class, new String[] {});
if (args.length > 0) {
environment.getPropertySources()
.addFirst(new MapPropertySource("interactiveDisabledProps",
Map.of("spring.shell.interactive.enabled", "false")));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.EnvironmentPostProcessor=org.springframework.shell.core.autoconfigure.SpringShellEnvironmentPostProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.springframework.shell.core.autoconfigure;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.springframework.core.env.CommandLinePropertySource.DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;

/**
* @author David Pilar
*/
class SpringShellEnvironmentPostProcessorTests {

private SpringShellEnvironmentPostProcessor postProcessor;

private ConfigurableEnvironment environment;

private SpringApplication application;

@BeforeEach
void setUp() {
postProcessor = new SpringShellEnvironmentPostProcessor();
environment = new StandardEnvironment();
application = Mockito.mock(SpringApplication.class);
}

@Test
void shouldDisableInteractiveMode_whenNonOptionArgsIsPresent() {
environment.getPropertySources()
.addFirst(new MapPropertySource("test", Map.of(DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME, "some-command")));

postProcessor.postProcessEnvironment(environment, application);

assertEquals("false", environment.getProperty("spring.shell.interactive.enabled"));
}

@Test
void shouldNotModifyEnvironment_whenNonOptionArgsIsNotPresent() {
postProcessor.postProcessEnvironment(environment, application);

assertNull(environment.getProperty("spring.shell.interactive.enabled"));
}

@Test
void shouldNotModifyEnvironment_whenNonOptionArgsIsEmpty() {
environment.getPropertySources()
.addFirst(new MapPropertySource("test", Map.of(DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME, "")));

postProcessor.postProcessEnvironment(environment, application);

assertNull(environment.getProperty("spring.shell.interactive.enabled"));
}

@Test
void customPropsShouldHaveHighestPriority() {
environment.getPropertySources()
.addFirst(new MapPropertySource("existing", Map.of("spring.shell.interactive.enabled", "true",
DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME, "some-command")));

postProcessor.postProcessEnvironment(environment, application);

assertEquals("false", environment.getProperty("spring.shell.interactive.enabled"));
}

}