Skip to content

Commit edfe67c

Browse files
committed
Test terminal can set dimensions
- ShellTest now has fields which can be used to change terminal default widht/height. - Backed by properties so can be used with `spring.shell.test.terminal-width` and `spring.shell.test.terminal-height`. - Backport #656 - Fixes #659
1 parent 3ed49d1 commit edfe67c

File tree

10 files changed

+234
-7
lines changed

10 files changed

+234
-7
lines changed

spring-shell-docs/src/main/asciidoc/using-shell-testing-basics.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[[using-shell-testing-basics]]
2-
==== Basics
2+
=== Basics
33
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
44

55
Spring Shell provides a number of utilities and annotations to help when testing your application.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[[using-shell-testing-settings]]
2+
=== Settings
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
Built in emulation uses terminal width 80 and height 24 on default.
6+
Changing dimensions is useful if output would span into multiple
7+
lines and you don't want to handle those cases in a tests.
8+
9+
These can be changed using properties `spring.shell.test.terminal-width`
10+
or `spring.shell.test.terminal-height`.
11+
12+
====
13+
[source, java, indent=0]
14+
----
15+
include::{snippets}/TestingSnippets.java[tag=testing-shelltest-dimensions-props]
16+
----
17+
====
18+
19+
`ShellTest` annotation have fields `terminalWidth` and `terminalHeight`
20+
which can also be used to change dimensions.
21+
22+
====
23+
[source, java, indent=0]
24+
----
25+
include::{snippets}/TestingSnippets.java[tag=testing-shelltest-dimensions-field]
26+
----
27+
====

spring-shell-docs/src/main/asciidoc/using-shell-testing.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ NOTE: Testing support is currently under development and will be
1616
unstable for various parts.
1717

1818
include::using-shell-testing-basics.adoc[]
19+
20+
include::using-shell-testing-settings.adoc[]

spring-shell-docs/src/test/java/org/springframework/shell/docs/TestingSnippets.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,23 @@ void test() {
8181
}
8282
}
8383
// end::testing-shelltest-noninteractive[]
84+
85+
class Dump1 {
86+
87+
// tag::testing-shelltest-dimensions-props[]
88+
@ShellTest(properties = {
89+
"spring.shell.test.terminal-width=120",
90+
"spring.shell.test.terminal-height=40"
91+
})
92+
class ShellSettingsSample {}
93+
// end::testing-shelltest-dimensions-props[]
94+
}
95+
class Dump2 {
96+
97+
// tag::testing-shelltest-dimensions-field[]
98+
@ShellTest(terminalWidth = 120, terminalHeight = 40)
99+
class ShellSettingsSample {}
100+
// end::testing-shelltest-dimensions-field[]
101+
}
102+
84103
}

spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellAutoConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import org.springframework.boot.autoconfigure.AutoConfiguration;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.shell.boot.JLineShellAutoConfiguration;
3031
import org.springframework.shell.boot.TerminalCustomizer;
@@ -33,6 +34,7 @@
3334
import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession;
3435

3536
@AutoConfiguration(before = JLineShellAutoConfiguration.class)
37+
@EnableConfigurationProperties(SpringShellTestProperties.class)
3638
public class ShellAutoConfiguration {
3739

3840
@Bean
@@ -55,8 +57,8 @@ TtyConnector ttyConnector(TerminalStreams terminalStreams) {
5557
}
5658

5759
@Bean
58-
TerminalSession terminalSession(TtyConnector ttyConnector) {
59-
JediTermWidget widget = new JediTermWidget(80, 24);
60+
TerminalSession terminalSession(TtyConnector ttyConnector, SpringShellTestProperties properties) {
61+
JediTermWidget widget = new JediTermWidget(properties.getTerminalWidth(), properties.getTerminalHeight());
6062
widget.setTtyConnector(ttyConnector);
6163
return widget;
6264
}

spring-shell-test-autoconfigure/src/main/java/org/springframework/shell/test/autoconfigure/ShellTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
import org.springframework.boot.autoconfigure.SpringBootApplication;
2929
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
3030
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
31+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
32+
import org.springframework.boot.test.autoconfigure.properties.SkipPropertyMapping;
3133
import org.springframework.context.annotation.ComponentScan.Filter;
3234
import org.springframework.core.annotation.AliasFor;
3335
import org.springframework.core.env.Environment;
@@ -61,6 +63,22 @@
6163
*/
6264
String[] properties() default {};
6365

66+
/**
67+
* Set emulated terminal width.
68+
*
69+
* @return emulated terminal width
70+
*/
71+
@PropertyMapping(value = "spring.shell.test.terminal-width", skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
72+
int terminalWidth() default 80;
73+
74+
/**
75+
* Set emulated terminal height.
76+
*
77+
* @return emulated terminal height
78+
*/
79+
@PropertyMapping(value = "spring.shell.test.terminal-height", skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
80+
int terminalHeight() default 24;
81+
6482
/**
6583
* Determines if default filtering should be used with
6684
* {@link SpringBootApplication @SpringBootApplication}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.test.autoconfigure;
17+
18+
import org.springframework.boot.context.properties.ConfigurationProperties;
19+
20+
/**
21+
* Configuration properties for shell test.
22+
*
23+
* @author Janne Valkealahti
24+
*/
25+
@ConfigurationProperties(prefix = "spring.shell.test")
26+
public class SpringShellTestProperties {
27+
28+
/**
29+
* Width of an emulated terminal.
30+
*/
31+
private int terminalWidth = 80;
32+
33+
/**
34+
* Height of an emulated terminal.
35+
*/
36+
private int terminalHeight = 24;
37+
38+
public int getTerminalWidth() {
39+
return terminalWidth;
40+
}
41+
42+
public void setTerminalWidth(int terminalWidth) {
43+
this.terminalWidth = terminalWidth;
44+
}
45+
46+
public int getTerminalHeight() {
47+
return terminalHeight;
48+
}
49+
50+
public void setTerminalHeight(int terminalHeight) {
51+
this.terminalHeight = terminalHeight;
52+
}
53+
}

spring-shell-test-autoconfigure/src/test/java/org/springframework/shell/test/autoconfigure/ShellTestPropertiesIntegrationTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.core.env.Environment;
2222
import org.springframework.shell.test.autoconfigure.app.ExampleShellApplication;
23+
import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession;
2324
import org.springframework.test.context.ContextConfiguration;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,15 +31,25 @@
3031
*
3132
* @author Janne Valkealahti
3233
*/
33-
@ShellTest(properties = "spring.profiles.active=test")
34+
@ShellTest(properties = { "spring.profiles.active=test", "spring.shell.test.terminal-width=81",
35+
"spring.shell.test.terminal-height=25" })
3436
@ContextConfiguration(classes = ExampleShellApplication.class)
3537
public class ShellTestPropertiesIntegrationTests {
3638

3739
@Autowired
3840
private Environment environment;
3941

42+
@Autowired
43+
private TerminalSession session;
44+
4045
@Test
4146
void environmentWithNewProfile() {
4247
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
4348
}
49+
50+
@Test
51+
void dimensionsSet() {
52+
assertThat(session.getTerminal().getTerminalWidth()).isEqualTo(81);
53+
assertThat(session.getTerminal().getTerminalHeight()).isEqualTo(25);
54+
}
4455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.test.autoconfigure;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.shell.test.autoconfigure.app.ExampleShellApplication;
22+
import org.springframework.shell.test.jediterm.terminal.ui.TerminalSession;
23+
import org.springframework.test.context.ContextConfiguration;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@ShellTest(terminalWidth = 81, terminalHeight = 25)
28+
@ContextConfiguration(classes = ExampleShellApplication.class)
29+
class ShellTestTerminalDimensionsIntegrationTests {
30+
31+
@Autowired
32+
private TerminalSession session;
33+
34+
@Test
35+
void dimensionsSet() {
36+
assertThat(session.getTerminal().getTerminalWidth()).isEqualTo(81);
37+
assertThat(session.getTerminal().getTerminalHeight()).isEqualTo(25);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.test.autoconfigure;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
class SpringShellTestPropertiesTests {
26+
27+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
28+
29+
@Test
30+
public void defaultNoPropertiesSet() {
31+
this.contextRunner
32+
.withUserConfiguration(Config1.class)
33+
.run((context) -> {
34+
SpringShellTestProperties properties = context.getBean(SpringShellTestProperties.class);
35+
assertThat(properties.getTerminalWidth()).isEqualTo(80);
36+
assertThat(properties.getTerminalHeight()).isEqualTo(24);
37+
});
38+
}
39+
40+
@Test
41+
public void setProperties() {
42+
this.contextRunner
43+
.withPropertyValues("spring.shell.test.terminal-width=81")
44+
.withPropertyValues("spring.shell.test.terminal-height=25")
45+
.withUserConfiguration(Config1.class)
46+
.run((context) -> {
47+
SpringShellTestProperties properties = context.getBean(SpringShellTestProperties.class);
48+
assertThat(properties.getTerminalWidth()).isEqualTo(81);
49+
assertThat(properties.getTerminalHeight()).isEqualTo(25);
50+
});
51+
}
52+
53+
@EnableConfigurationProperties({ SpringShellTestProperties.class })
54+
private static class Config1 {
55+
}
56+
}

0 commit comments

Comments
 (0)