-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainIT.java
More file actions
161 lines (137 loc) · 5.64 KB
/
MainIT.java
File metadata and controls
161 lines (137 loc) · 5.64 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
package org.codejive.jpm;
import static org.assertj.core.api.Assertions.*;
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 MainIT {
@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 testDoAliasWithArgs() throws IOException {
createAppYmlWithRepositories();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode = cmd.execute("build", "--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("javac", "jfiglet", "--foo bar");
}
}
@Test
void testCopyCommandWithRepositoryOptions() throws IOException {
// Test copy command with --repo options
CommandLine cmd = Main.getCommandLine();
int exitCode =
cmd.execute(
"copy",
"--repo",
"central=https://repo1.maven.org/maven2",
"--repo",
"https://jcenter.bintray.com",
"com.google.guava:guava:31.1-jre");
// The command should execute successfully (even if dependency resolution might fail)
assertThat(exitCode >= 0).isTrue();
}
@Test
void testInstallCommandWithRepositoryOptions() throws IOException {
CommandLine cmd = Main.getCommandLine();
int exitCode =
cmd.execute(
"install",
"--repo",
"central=https://repo1.maven.org/maven2",
"com.google.guava:guava:31.1-jre");
// The command should execute successfully (even if dependency resolution might fail)
assertThat(exitCode >= 0).isTrue();
}
@Test
void testPathCommandWithRepositoryOptionsAndAppYml() throws IOException {
// Create app.yml with repositories
createAppYmlWithRepositories();
try (TestOutputCapture capture = captureOutput()) {
CommandLine cmd = Main.getCommandLine();
int exitCode =
cmd.execute(
"path",
"--repo",
"jcenter=https://jcenter.bintray.com",
"com.google.guava:guava:31.1-jre");
// The command should execute (even if dependency resolution might fail)
assertThat(exitCode >= 0).isTrue();
}
}
private void createAppYmlWithRepositories() throws IOException {
String yamlContent =
"dependencies:\n"
+ " com.github.lalyos:jfiglet: \"0.0.9\"\n"
+ "\n"
+ "repositories:\n"
+ " central: \"https://repo1.maven.org/maven2\"\n"
+ " custom: \"https://my.custom.repo/maven2\"\n"
+ "\n"
+ "actions:\n"
+ " build: \"echo javac -cp {{deps}} *.java\"\n"
+ " test: \"echo java -cp {{deps}} TestRunner\"\n";
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
}
}