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,60 @@
/*
* 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.samples;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.shell.samples.standard.ResolvedCommands;
import org.springframework.shell.test.ShellAssertions;
import org.springframework.shell.test.ShellTestClient;
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
import org.springframework.shell.test.ShellTestClient.InteractiveShellSession;
import org.springframework.shell.test.ShellTestClient.NonInteractiveShellSession;
import org.springframework.shell.test.autoconfigure.ShellTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import static org.awaitility.Awaitility.await;
@ShellTest
@Import(ResolvedCommands.ResolvedCommandsConfiguration.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class AbstractSampleTests {
@Autowired
protected ShellTestClient client;
protected void assertScreenContainsText(BaseShellSession<?> session, String text) {
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen()).containsText(text);
});
}
protected BaseShellSession<?> createSession(String command, boolean interactive) {
if (interactive) {
InteractiveShellSession session = client.interactive().run();
session.write(session.writeSequence().command(command).build());
return session;
}
else {
String[] commands = command.split(" ");
NonInteractiveShellSession session = client.nonInterative(commands).run();
return session;
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.samples.e2e;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.shell.samples.AbstractSampleTests;
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
class RequiredValueCommandsTests extends AbstractSampleTests {
@ParameterizedTest
@CsvSource({
"e2e anno required-value,false",
"e2e reg required-value,false",
"e2e anno required-value,true",
"e2e reg required-value,true"
})
void shouldRequireOption(String command, boolean interactive) {
BaseShellSession<?> session = createSession(command, interactive);
assertScreenContainsText(session, "Missing mandatory option");
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.samples.standard;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.shell.samples.AbstractSampleTests;
import org.springframework.shell.test.ShellAssertions;
import org.springframework.shell.test.ShellTestClient.BaseShellSession;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
public class ComponentCommandsTests extends AbstractSampleTests {
@ParameterizedTest
@CsvSource({
"component single,false",
"component single,true"
})
void componentSingle(String command, boolean interactive) {
BaseShellSession<?> session = createSession(command, interactive);
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(session.screen().lines()).anySatisfy(line -> {
assertThat(line).containsPattern("[>] key1");
});
});
session.write(session.writeSequence().keyDown().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(session.screen().lines()).anySatisfy(line -> {
assertThat(line).containsPattern("[>] key2");
});
});
session.write(session.writeSequence().cr().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen()).containsText("Got value value2");
});
}
@ParameterizedTest
@CsvSource({
"component multi,false",
"component multi,true"
})
void componentMulti(String command, boolean interactive) {
BaseShellSession<?> session = createSession(command, interactive);
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(session.screen().lines()).anySatisfy(line -> {
assertThat(line).containsPattern("[>] (☐|\\[ \\]) key1");
});
});
session.write(session.writeSequence().space().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(session.screen().lines()).anySatisfy(line -> {
assertThat(line).containsPattern("[>] (☒|\\[x\\]) key1");
});
});
session.write(session.writeSequence().cr().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen()).containsText("Got value value1,value2");
});
}
}