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,25 @@
[[using-shell-testing-basics]]
==== Basics
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
Spring Shell provides a number of utilities and annotations to help when testing your application.
Test support is provided by two modules: `spring-shell-test` contains core items, and
`spring-shell-test-autoconfigure` supports auto-configuration for tests.
To test _interactive_ commands.
====
[source, java, indent=0]
----
include::{snippets}/TestingSnippets.java[tag=testing-shelltest-interactive]
----
====
To test _non-interactive_ commands.
====
[source, java, indent=0]
----
include::{snippets}/TestingSnippets.java[tag=testing-shelltest-noninteractive]
----
====

View File

@@ -0,0 +1,18 @@
[[using-shell-testing]]
== Testing
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
Testing cli application is difficult due to various reasons:
- There are differences between OS's.
- Within OS there may be different shell implementations in use.
- What goes into a shell and comes out from a shell my be totally
different what you see in shell itself due to control characters.
- Shell may feel syncronous but most likely it is not meaning when
someting is written into it, you can't assume next update in
in it is not final.
NOTE: Testing support is currently under development and will be
unstable for various parts.
include::using-shell-testing-basics.adoc[]

View File

@@ -13,3 +13,5 @@ include::using-shell-components.adoc[]
include::using-shell-customization.adoc[]
include::using-shell-execution.adoc[]
include::using-shell-testing.adoc[]

View File

@@ -0,0 +1,84 @@
/*
* 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.docs;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.test.ShellAssertions;
import org.springframework.shell.test.ShellTestClient;
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;
class TestingSnippets {
// tag::testing-shelltest-interactive[]
@ShellTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
class InteractiveTestSample {
@Autowired
ShellTestClient client;
@Test
void test() {
InteractiveShellSession session = client
.interactive()
.run();
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("shell");
});
session.write(session.writeSequence().text("help").carriageReturn().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("AVAILABLE COMMANDS");
});
}
}
// end::testing-shelltest-interactive[]
// tag::testing-shelltest-noninteractive[]
@ShellTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
class NonInteractiveTestSample {
@Autowired
ShellTestClient client;
@Test
void test() {
NonInteractiveShellSession session = client
.nonInterative("help", "help")
.run();
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("AVAILABLE COMMANDS");
});
}
}
// end::testing-shelltest-noninteractive[]
}