-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainTest.java
More file actions
305 lines (252 loc) · 10.4 KB
/
MainTest.java
File metadata and controls
305 lines (252 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package org.codejive.jpm;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;
/** Integration tests for the Main class, focusing on the new 'do' command and aliases. */
class MainTest {
@TempDir Path tempDir;
private String originalDir;
@BeforeEach
void setUp() {
originalDir = System.getProperty("user.dir");
System.setProperty("user.dir", tempDir.toString());
System.setProperty("picocli.ansi", "false");
}
@AfterEach
void tearDown() {
System.setProperty("user.dir", originalDir);
}
/**
* Helper method to capture stdout/stderr for tests that need to check command output. Only use
* this for tests that check jpm's own output, not for tests that execute system commands.
*/
private TestOutputCapture captureOutput() {
PrintStream originalOut = System.out;
PrintStream originalErr = System.err;
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
return new TestOutputCapture(originalOut, originalErr, outContent, errContent);
}
/** Helper class to manage output capture and restoration */
private static class TestOutputCapture implements AutoCloseable {
private final PrintStream originalOut;
private final PrintStream originalErr;
private final ByteArrayOutputStream outContent;
private final ByteArrayOutputStream errContent;
TestOutputCapture(
PrintStream originalOut,
PrintStream originalErr,
ByteArrayOutputStream outContent,
ByteArrayOutputStream errContent) {
this.originalOut = originalOut;
this.originalErr = originalErr;
this.outContent = outContent;
this.errContent = errContent;
}
String getOut() {
return outContent.toString();
}
String getErr() {
return errContent.toString();
}
@Override
public void close() {
System.setOut(originalOut);
System.setErr(originalErr);
}
}
@Test
void testDoCommandList() throws IOException {
// Create app.yml with actions
createAppYml();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "--list");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
assertThat(output).contains("Available actions:", "build", "test", "run", "hello");
}
}
@Test
void testDoCommandListShortFlag() throws IOException {
// Create app.yml with actions
createAppYml();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "-l");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
assertThat(output).contains("Available actions:");
}
}
@Test
void testDoCommandListNoActions() throws IOException {
// Create app.yml without actions
createAppYmlWithoutActions();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "--list");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
assertThat(output).contains("No actions defined in app.yml");
}
}
@Test
void testDoCommandListNoAppYml() {
// No app.yml file exists
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "--list");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
assertThat(output).contains("No actions defined in app.yml");
}
}
@Test
void testDoCommandMissingActionName() throws IOException {
createAppYml();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do");
assertThat(exitCode).isEqualTo(1);
String errorOutput = capture.getErr();
assertThat(errorOutput)
.contains("Action name is required", "Use --list to see available actions");
}
}
@Test
void testDoCommandNonexistentAction() throws IOException {
createAppYml();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "nonexistent");
assertThat(exitCode).isEqualTo(1);
String errorOutput = capture.getErr();
assertThat(errorOutput)
.contains(
"Action 'nonexistent' not found in app.yml. Use --list to see available actions.");
}
}
@Test
void testBuildAlias() throws IOException {
createAppYml();
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("build");
// Test that build alias works (delegates to 'do build')
assertThat(exitCode >= 0).isTrue();
}
@Test
void testTestAlias() throws IOException {
createAppYml();
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("test");
// Test that test alias works (delegates to 'do test')
assertThat(exitCode >= 0).isTrue();
}
@Test
void testRunAlias() throws IOException {
createAppYml();
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("run");
// Test that run alias works (delegates to 'do run')
assertThat(exitCode >= 0).isTrue();
}
@Test
void testAliasWithNonexistentAction() throws IOException {
// Create app.yml without 'build' action
createAppYmlWithoutBuildAction();
// Test the "do" command directly (which is what the alias redirects to)
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("do", "build");
// Should fail with exit code 1 when action is not found
assertThat(exitCode).isEqualTo(1);
String errorOutput = capture.getErr();
assertThat(errorOutput).contains("Action 'build' not found in app.yml");
}
}
@Test
void testDoWithOutput() throws IOException {
// Create app.yml with action that doesn't use {{deps}}
createAppYml();
CommandLine cmd = Main.getCommandLine();
try (TestOutputCapture capture = captureOutput()) {
int exitCode = cmd.execute("do", "hello");
assertThat(exitCode >= 0).isTrue(); // Should not be negative (internal error)
String output = capture.getOut();
assertThat(output).contains("Hello World");
}
}
@Test
void testMainWithNoArgs() {
try (TestOutputCapture capture = captureOutput()) {
// Test the default behavior using CommandLine
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute();
assertThat(exitCode >= 0).isTrue(); // Should not be negative (internal error)
assertThat(capture.getErr()).contains("Missing required subcommand");
assertThat(capture.getErr()).contains("Usage: jpm [-hvV] [COMMAND]");
assertThat(capture.getErr())
.contains("Simple command line tool for managing Maven artifacts");
}
}
@Test
void testDoAliasWithArgs() throws IOException {
createAppYml();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("run", "--foo", "bar");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
// The run action should execute and include the classpath in the output
assertThat(output).contains("running... .", "libs", "--foo bar");
}
}
private void createAppYml() throws IOException {
String yamlContent =
"dependencies:\n"
+ " fake:dummy: \"1.2.3\"\n"
+ "\n"
+ "actions:\n"
+ " build: \"echo building... .{/}libs{:}ext\"\n"
+ " test: \"echo testing... .{/}libs{:}ext\"\n"
+ " run: \"echo running... .{/}libs{:}ext\"\n"
+ " hello: \"echo Hello World\"\n";
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
}
private void createAppYmlWithoutActions() throws IOException {
String yamlContent = "dependencies:\n" + " fake:dummy: \"1.2.3\"\n";
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
}
private void createAppYmlWithoutBuildAction() throws IOException {
String yamlContent =
"dependencies:\n"
+ " fake:dummy: \"1.2.3\"\n"
+ "\n"
+ "actions:\n"
+ " test: \"java -cp {{deps}} TestRunner\"\n"
+ " hello: \"echo Hello World\"\n";
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
}
@Test
void testVerboseFlag() {
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("--help");
assertThat(exitCode).isEqualTo(0);
String output = capture.getOut();
assertThat(output).contains("-v, --verbose");
assertThat(output).contains("Enable verbose output for debugging");
}
}
}