Skip to content

Commit 60fcfaf

Browse files
committed
Improve help and help-tests
1 parent 5aa4c1d commit 60fcfaf

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

src/main/java/org/utplsql/cli/UtplsqlPicocliCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
subcommands = {
99
RunPicocliCommand.class,
1010
VersionInfoCommand.class,
11-
ReportersCommand.class
11+
ReportersCommand.class,
12+
CommandLine.HelpCommand.class
1213
})
1314
public class UtplsqlPicocliCommand {
1415

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
package org.utplsql.cli;
22

33
import org.junit.jupiter.api.Test;
4+
import org.utplsql.cli.util.SystemCapturer;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
48

59
class CliHelpTest {
610

11+
private SystemCapturer capturer;
12+
713
@Test
8-
void showBasicHelp() {
14+
void show_basic_help_on_help_command() {
15+
capturer = new SystemCapturer.SystemOutCapturer();
16+
capturer.start();
917
TestHelper.runApp("help");
18+
String output = capturer.stop();
19+
20+
assertTrue(output.contains("Usage:"));
21+
}
22+
23+
@Test
24+
void write_help_to_error_out_on_unknown_command() {
25+
capturer = new SystemCapturer.SystemErrCapturer();
26+
capturer.start();
27+
int exitCode = TestHelper.runApp("wtfhappens");
28+
String output = capturer.stop();
29+
30+
assertTrue(output.contains("Usage:"));
31+
assertEquals(1, exitCode);
1032
}
1133
}

src/test/java/org/utplsql/cli/HelpCommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.junit.jupiter.api.AfterEach;
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
6-
import org.utplsql.cli.util.SystemOutCapturer;
6+
import org.utplsql.cli.util.SystemCapturer;
77

88
import java.io.IOException;
99

@@ -13,11 +13,11 @@
1313
class HelpCommandTest {
1414

1515

16-
private SystemOutCapturer capturer;
16+
private SystemCapturer capturer;
1717

1818
@BeforeEach
1919
void setupCaptureSystemOut() {
20-
capturer = new SystemOutCapturer();
20+
capturer = new SystemCapturer.SystemOutCapturer();
2121
}
2222

2323
@Test

src/test/java/org/utplsql/cli/VersionInfoCommandIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import org.junit.jupiter.api.AfterEach;
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
6-
import org.utplsql.cli.util.SystemOutCapturer;
6+
import org.utplsql.cli.util.SystemCapturer;
77

88
import java.util.Arrays;
99

1010
import static org.junit.jupiter.api.Assertions.assertEquals;
1111

1212
class VersionInfoCommandIT {
1313

14-
private SystemOutCapturer capturer;
14+
private SystemCapturer capturer;
1515

1616
@BeforeEach
1717
void setupCaptureSystemOut() {
18-
capturer = new SystemOutCapturer();
18+
capturer = new SystemCapturer.SystemOutCapturer();
1919
}
2020

2121
private int getNonEmptyLines(String content) {

src/test/java/org/utplsql/cli/util/SystemOutCapturer.java renamed to src/test/java/org/utplsql/cli/util/SystemCapturer.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,38 @@
99

1010
/** All credit to Manasjyoti Sharma: https://stackoverflow.com/a/30665299
1111
*/
12-
public class SystemOutCapturer {
12+
public abstract class SystemCapturer {
13+
1314
private ByteArrayOutputStream baos;
1415
private PrintStream previous;
1516
private boolean capturing;
1617

18+
protected abstract PrintStream getOriginalStream();
19+
20+
protected abstract void setSystemStream( PrintStream stream );
21+
1722
public void start() {
1823
if (capturing) {
1924
return;
2025
}
2126

2227
capturing = true;
23-
previous = System.out;
28+
previous = getOriginalStream();
2429
baos = new ByteArrayOutputStream();
2530

2631
OutputStream outputStreamCombiner =
2732
new OutputStreamCombiner(Arrays.asList(previous, baos));
2833
PrintStream custom = new PrintStream(outputStreamCombiner);
2934

30-
System.setOut(custom);
35+
setSystemStream(custom);
3136
}
3237

3338
public String stop() {
3439
if (!capturing) {
3540
return "";
3641
}
3742

38-
System.setOut(previous);
43+
setSystemStream(previous);
3944

4045
String capturedValue = baos.toString();
4146

@@ -71,4 +76,30 @@ public void close() throws IOException {
7176
}
7277
}
7378
}
79+
80+
public static class SystemOutCapturer extends SystemCapturer {
81+
82+
@Override
83+
protected PrintStream getOriginalStream() {
84+
return System.out;
85+
}
86+
87+
@Override
88+
protected void setSystemStream(PrintStream stream) {
89+
System.setOut(stream);
90+
}
91+
}
92+
93+
public static class SystemErrCapturer extends SystemCapturer {
94+
95+
@Override
96+
protected PrintStream getOriginalStream() {
97+
return System.err;
98+
}
99+
100+
@Override
101+
protected void setSystemStream(PrintStream stream) {
102+
System.setErr(stream);
103+
}
104+
}
74105
}

0 commit comments

Comments
 (0)