Add base shell test system

- NOTE: very much wip and unstable
- This commit is a first step to provide boot style
  @ShellTest annotation
- New modules spring-shell-test and spring-shell-test-autoconfigure
- Focus is to autoconfigure context without shell runners so that
  we can create "sessions" and hook to configures jline terminal
  with custom in/out streams.
- Skeleton fork from jediterm to provide basic terminal emulation
  to part of a control amd escape characters working.
- ShellTestClient is a concept user can use to interact with a shell
  in a same way user would use a "real" shell.
- Fixes #489
This commit is contained in:
Janne Valkealahti
2022-11-30 09:09:35 +00:00
parent 2bfcf99e96
commit d3e897f6d9
82 changed files with 9694 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
/**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Shell tests.
* Most tests should consider using {@link ShellTest @ShellTest} rather than using this
* annotation directly.
*
* @author Janne Valkealahti
* @see ShellTest
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration
public @interface AutoConfigureShell {
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
/**
* Annotation that can be applied to a test class to enable and configure
* auto-configuration of shell client.
*
* @author Janne Valkealahti
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration
public @interface AutoConfigureShellTestClient {
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.boot.JLineShellAutoConfiguration;
import org.springframework.shell.boot.TerminalCustomizer;
import org.springframework.shell.test.jediterm.terminal.TtyConnector;
import org.springframework.shell.test.jediterm.terminal.ui.JediTermWidget;
import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession;
@AutoConfiguration(before = JLineShellAutoConfiguration.class)
public class ShellAutoConfiguration {
@Bean
TerminalCustomizer terminalStreamsTerminalCustomizer(TerminalStreams terminalStreams) {
return builder -> {
builder.streams(terminalStreams.input, terminalStreams.output)
.jansi(false)
.jna(false);
};
}
@Bean
TerminalStreams terminalStreams() {
return new TerminalStreams();
}
@Bean
TtyConnector ttyConnector(TerminalStreams terminalStreams) {
return new TestTtyConnector(terminalStreams.myReader, terminalStreams.myWriter);
}
@Bean
TerminalSession terminalSession(TtyConnector ttyConnector) {
JediTermWidget widget = new JediTermWidget(80, 24);
widget.setTtyConnector(ttyConnector);
return widget;
}
public static class TerminalStreams {
PipedInputStream input;
PipedOutputStream output;
InputStreamReader myReader;
OutputStreamWriter myWriter;
public TerminalStreams() {
input = new PipedInputStream();
output = new PipedOutputStream();
try {
myReader = new InputStreamReader(new PipedInputStream(this.output));
myWriter = new OutputStreamWriter(new PipedOutputStream(this.input));
} catch (IOException e) {
}
}
}
private static class TestTtyConnector implements TtyConnector {
private final static Logger log = LoggerFactory.getLogger(TestTtyConnector.class);
InputStreamReader myReader;
OutputStreamWriter myWriter;
TestTtyConnector(InputStreamReader myReader, OutputStreamWriter myWriter) {
this.myReader = myReader;
this.myWriter = myWriter;
}
@Override
public boolean init() {
return true;
}
@Override
public void close() {
}
@Override
public String getName() {
return null;
}
@Override
public int read(char[] buf, int offset, int length) throws IOException {
log.trace("read1");
int read = this.myReader.read(buf, offset, length);
log.trace("read2 {}", read);
return read;
}
@Override
public void write(byte[] bytes) throws IOException {
log.trace("write1 {}", bytes);
this.myWriter.write(new String(bytes));
this.myWriter.flush();
log.trace("write2");
}
@Override
public boolean isConnected() {
return true;
}
@Override
public void write(String string) throws IOException {
this.write(string.getBytes());
}
@Override
public int waitFor() throws InterruptedException {
return 0;
}
@Override
public boolean ready() throws IOException {
log.trace("ready1");
boolean ready = myReader.ready();
log.trace("ready2 {}", ready);
return ready;
}
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.env.Environment;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Annotation that can be used for a Shell test that focuses
* <strong>only</strong> on Shell components.
*
* @author Janne Valkealahti
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(ShellTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
@OverrideAutoConfiguration(enabled = false)
@TypeExcludeFilters(ShellTypeExcludeFilter.class)
@AutoConfigureShell
@AutoConfigureShellTestClient
@ImportAutoConfiguration
public @interface ShellTest {
/**
* Properties in form {@literal key=value} that should be added to the Spring
* {@link Environment} before the test runs.
*
* @return the properties to add
*/
String[] properties() default {};
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}.
*
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used
*/
boolean useDefaultFilters() default true;
/**
* A set of include filters which can be used to add otherwise filtered beans to the
* application context.
*
* @return include filters to apply
*/
Filter[] includeFilters() default {};
/**
* A set of exclude filters which can be used to filter beans that would otherwise be
* added to the application context.
*
* @return exclude filters to apply
*/
Filter[] excludeFilters() default {};
/**
* Auto-configuration exclusions that should be applied for this test.
*
* @return auto-configuration exclusions to apply
*/
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
Class<?>[] excludeAutoConfiguration() default {};
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.Shell;
import org.springframework.shell.jline.PromptProvider;
import org.springframework.shell.test.ShellTestClient;
import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession;
/**
*
* @author Janne Valkealahti
*/
@AutoConfiguration
public class ShellTestClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
ShellTestClient shellTestClient(TerminalSession widget, Shell shell, PromptProvider promptProvider,
LineReader lineReader, Terminal terminal) {
return ShellTestClient.builder(widget, shell, promptProvider, lineReader, terminal).build();
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.test.context.TestContextBootstrapper;
/**
* {@link TestContextBootstrapper} for {@link ShellTest @ShellTest} support.
*
* @author Janne Valkealahti
*/
public class ShellTestContextBootstrapper extends SpringBootTestContextBootstrapper {
@Override
protected String[] getProperties(Class<?> testClass) {
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(ShellTest.class)
.getValue("properties", String[].class).orElse(null);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.test.autoconfigure;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
import org.springframework.shell.standard.ShellComponent;
/**
* {@link TypeExcludeFilter} for {@link ShellTest @ShellTest}.
*
* @author Janne Valkealahti
*/
public class ShellTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<ShellTest> {
private static final Set<Class<?>> DEFAULT_INCLUDES;
static {
Set<Class<?>> includes = new LinkedHashSet<>();
includes.add(ShellComponent.class);
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
}
ShellTypeExcludeFilter(Class<?> testClass) {
super(testClass);
}
@Override
protected Set<Class<?>> getDefaultIncludes() {
return DEFAULT_INCLUDES;
}
}

View File

@@ -0,0 +1,16 @@
org.springframework.shell.test.autoconfigure.ShellAutoConfiguration
org.springframework.shell.test.autoconfigure.ShellTestClientAutoConfiguration
org.springframework.shell.boot.CommandCatalogAutoConfiguration
org.springframework.shell.boot.CompleterAutoConfiguration
org.springframework.shell.boot.ComponentFlowAutoConfiguration
org.springframework.shell.boot.ExitCodeAutoConfiguration
org.springframework.shell.boot.JLineAutoConfiguration
org.springframework.shell.boot.JLineShellAutoConfiguration
org.springframework.shell.boot.LineReaderAutoConfiguration
org.springframework.shell.boot.ParameterResolverAutoConfiguration
org.springframework.shell.boot.ShellContextAutoConfiguration
org.springframework.shell.boot.SpringShellAutoConfiguration
org.springframework.shell.boot.StandardAPIAutoConfiguration
org.springframework.shell.boot.StandardCommandsAutoConfiguration
org.springframework.shell.boot.ThemingAutoConfiguration
org.springframework.shell.boot.UserConfigAutoConfiguration