forked from objectionary/eo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolveMojoTest.java
More file actions
268 lines (253 loc) · 9.2 KB
/
Copy pathResolveMojoTest.java
File metadata and controls
268 lines (253 loc) · 9.2 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.maven;
import com.yegor256.Mktmp;
import com.yegor256.MktmpResolver;
import com.yegor256.WeAreOnline;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.cactoos.Func;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Test case for {@link ResolveMojo}.
*
* @since 0.1
*/
@ExtendWith(WeAreOnline.class)
@ExtendWith(MktmpResolver.class)
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
final class ResolveMojoTest {
/**
* The message that the JAR file must exist.
*/
private static final String JAR_MUST_EXIST = "The jar file must exist, but it doesn't";
/**
* The message that the JAR file must not exist.
*/
private static final String JAR_NOT_EXIST = "The jar file must not exist, but it doesn't";
@Test
void resolvesWithSingleDependency(@Mktmp final Path temp) throws IOException {
new FakeMaven(temp)
.withProgram(
String.format(
"%s\n\n%s",
"+rt jvm org.eolang:eo-runtime:0.7.0",
"[] > foo /int"
)
).execute(new FakeMaven.Resolve());
final Path path = temp
.resolve("target")
.resolve(ResolveMojo.DIR)
.resolve("org.eolang/eo-runtime/-/0.7.0");
MatcherAssert.assertThat(
"Dependency directory must exist, but it doesn't",
path.toFile(),
FileMatchers.anExistingDirectory()
);
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
path.resolve("eo-runtime-0.7.0.jar").toFile(),
FileMatchers.anExistingFile()
);
}
@Test
void resolvesWithoutAnyDependencies(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp).withProgram(
"[a b] > sum",
" plus. > @",
" a",
" b"
);
maven.foreignTojos().add("sum");
maven.execute(new FakeMaven.Resolve());
final Path path = temp
.resolve("target")
.resolve(ResolveMojo.DIR)
.resolve("org.eolang/eo-runtime/-/");
MatcherAssert.assertThat(
"The directory with runtime must exist, but doesn't",
path.toFile(),
FileMatchers.anExistingDirectory()
);
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
path,
new ContainsFiles("**/eo-runtime-*.jar")
);
}
@Test
void resolvesWithEoRuntimeDependency(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
maven.withHelloWorld().execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
maven.targetPath(),
new ContainsFiles("**/eo-runtime-*.jar")
);
}
@Test
void resolvesWithoutEoRuntimeDependency(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
maven.withHelloWorld()
.with("withRuntimeDependency", false)
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_NOT_EXIST,
maven.targetPath(),
Matchers.not(new ContainsFiles("**/eo-runtime-*.jar"))
);
}
@Test
void resolvesIfRuntimeDependencyComesFromTojos(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
maven.withHelloWorld()
.withProgram("+rt jvm org.eolang:eo-runtime:0.22.1", "", "[] > main")
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
maven.targetPath(),
new ContainsFiles("**/eo-runtime-0.22.1.jar")
);
}
@Test
void resolvesIfRuntimeDependencyComesFromTojosButParamIsFalse(@Mktmp final Path temp)
throws IOException {
final FakeMaven maven = new FakeMaven(temp);
maven.withHelloWorld()
.withProgram("+rt jvm org.eolang:eo-runtime:0.22.1", "", "[] > main")
.with("withRuntimeDependency", false)
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_NOT_EXIST,
maven.targetPath(),
Matchers.not(new ContainsFiles("**/eo-runtime-*.jar"))
);
}
@Test
void resolvesWithRuntimeDependencyFromPom(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
final Dependency runtime = new Dependency();
runtime.setGroupId("org.eolang");
runtime.setArtifactId("eo-runtime");
runtime.setVersion("0.7.0");
final MavenProject project = new MavenProject();
project.setDependencies(Collections.singletonList(runtime));
maven.withHelloWorld()
.with("project", project)
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
maven.targetPath(),
new ContainsFiles("**/eo-runtime-0.7.0.jar")
);
}
@Test
void resolvesWithoutTransitiveDependencies(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
maven.withHelloWorld()
.with("ignoreTransitive", false)
.with(
"transitiveStrategy",
(Func<Dependency, Iterable<Dependency>>) ignore -> Collections.emptyList()
)
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
maven.targetPath(),
new ContainsFiles("**/eo-runtime-*.jar")
);
}
@Test
void throwsExceptionWithTransitiveDependency(@Mktmp final Path temp) {
final FakeMaven maven = new FakeMaven(temp);
final Dependency dependency = new Dependency();
dependency.setScope("compiled");
dependency.setGroupId("org.eolang");
dependency.setArtifactId("eo-transitive");
dependency.setVersion("0.1.0");
Assertions.assertThrows(
IllegalStateException.class,
() -> maven
.withProgram(
"+rt jvm org.eolang:eo-foreign:0.22.1\n",
"[] > foo /int"
)
.with("ignoreTransitive", false)
.with(
"transitiveStrategy",
(Func<Dependency, Iterable<Dependency>>) ignore -> Collections.singleton(
dependency
)
)
.execute(new FakeMaven.Resolve()),
"Expected an IllegalStateException exception when transitive dependency"
);
}
/**
* Test conflicts.
*
* @param temp Temp folder
* @throws IOException In case of I/O issues.
*/
@Test
void resolvesWithConflictingDependencies(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp).withProgram(
String.format(
"%s\n\n%s",
"+rt jvm org.eolang:eo-runtime:0.22.1",
"[] > foo /int"
)
).withProgram(
String.format(
"%s\n\n%s",
"+rt jvm org.eolang:eo-runtime:0.22.0",
"[] > foo /int"
)
);
final Exception excpt = Assertions.assertThrows(
IllegalStateException.class,
() -> maven.execute(new FakeMaven.Resolve())
);
MatcherAssert.assertThat(
"Expected that conflicting dependencies were found, but they were not",
excpt.getCause().getCause().getMessage(),
Matchers.containsString(
"1 conflicting dependencies are found: {org.eolang:eo-runtime:jar:=[0.22.0, 0.22.1]}"
)
);
}
@Test
void resolvesWithConflictingDependenciesNoFail(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp)
.withProgram(
String.format(
"%s\n\n%s",
"+rt jvm org.eolang:eo-runtime:jar-with-dependencies:0.22.1",
"[] > foo /int"
)
).withProgram(
String.format(
"%s\n\n%s",
"+rt jvm org.eolang:eo-runtime:jar-with-dependencies:0.22.0",
"[] > foo /int"
)
);
maven.with("ignoreVersionConflicts", true)
.execute(new FakeMaven.Resolve());
MatcherAssert.assertThat(
ResolveMojoTest.JAR_MUST_EXIST,
maven.targetPath(),
new ContainsFiles("**/eo-runtime-*.jar")
);
}
}