/
VerifyMojoTest.java
300 lines (285 loc) 路 11.3 KB
/
VerifyMojoTest.java
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StXSL;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Xsline;
import java.nio.file.Path;
import org.cactoos.io.ResourceOf;
import org.eolang.maven.log.CaptureLogs;
import org.eolang.maven.log.Logs;
import org.eolang.maven.util.HmBase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Test cases for {@link VerifyMojo}.
*
* @since 0.31.0
* @todo #2674:30min The messages "Warnings identified" from
* /org/eolang/parser/fail-on-warnings.xsl
* can have nullable line number. Need fix it, that it works as in
* /org/eolang/parser/warnings/mandatory-version-meta.xsl and
* /org/eolang/parser/warnings/mandatory-home-meta.xsl.
* After you need fix {@code createRegEx()}.
* @todo #2890:30min Fix this {@link VerifyMojoTest#detectsErrorsSuccessfully}
* flaky test and enable it. It failed in ci
* <a href="https://github.com/objectionary/eo/actions/runs/8041230784/job/21960239171?pr=2892">here</a>
* without providing the regex and message. Also may be it would be cleaner to fix
* error Assertion since now it is hard to get why it failed.
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
final class VerifyMojoTest {
@Test
void doesNotFailWithNoErrorsAndWarnings(@TempDir final Path temp) {
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp)
.withHelloWorld()
.execute(new FakeMaven.Verify()),
"Correct program should not have failed, but it does"
);
}
@Test
@Disabled
@CaptureLogs
void detectsErrorsSuccessfully(
@TempDir final Path temp,
final Logs out) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > main",
" QQ.io.stdout",
" \"Hello world\""
)
.execute(new FakeMaven.Verify()),
"Program with noname attributes should have failed or error, but it didn't"
);
final String message = this.getMessage(out, "Errors identified");
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Errors identified")),
"Errors message should have program name and error line number"
);
}
@Test
@CaptureLogs
void detectsCriticalErrorsSuccessfully(
@TempDir final Path temp,
final Logs out) throws Exception {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > main",
" \"Hello world\""
)
.execute(new FakeMaven.Verify()),
"Wrong program should have failed or error, but it didn't"
);
final String message = this.getMessage(out, "Critical error identified");
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Critical error identified")),
"Critical error message should have program name and error line number"
);
}
@Test
@CaptureLogs
void detectsWarningWithCorrespondingFlag(
@TempDir final Path temp,
final Logs out) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > main",
" # This is the default 64+ symbols comment in front of named abstract object.",
" [] > @",
" \"Hello world\" > @"
)
.with("failOnWarning", true)
.execute(new FakeMaven.Verify()),
"Program with sparse decorated object should have failed on warning, but it didn't"
);
final String message = this.getMessage(out, "Warnings identified");
Assertions.assertTrue(
message.matches(this.createRegEx(temp, "Warnings identified")),
"Warnings message should have program name and error line number"
);
}
@Test
void doesNotDetectWarningWithoutCorrespondingFlag(@TempDir final Path temp) {
Assertions.assertDoesNotThrow(
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > main",
" # This is the default 64+ symbols comment in front of named abstract object.",
" [] > @",
" \"Hello world\" > @"
)
.with("failOnWarning", false)
.execute(new FakeMaven.Verify()),
"Program with sparse decorated object should not have failed on warning without flag, but it does"
);
}
@Test
void failsOptimizationOnError(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f",
"+alias THIS-IS-WRONG org.eolang.io.stdout\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[args] > main",
" (stdout \"Hello!\").print > @"
)
.execute(new FakeMaven.Verify()),
"Error in the eo code because of invalid alias, should fail"
);
}
@Test
void failsOptimizationOnCritical(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(
"+package f\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[args] > main",
" seq > @",
" TRUE > x",
" FALSE > x"
).with("trackOptimizationSteps", true)
.execute(new FakeMaven.Verify())
);
}
@Test
void failsParsingOnError(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram("something > is wrong here")
.execute(new FakeMaven.Verify()),
"Program with invalid syntax should have failed, but it didn't"
);
}
@Test
void failsOnInvalidProgram(@TempDir final Path temp) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withProgram(AssembleMojoTest.INVALID_PROGRAM)
.execute(new FakeMaven.Verify()),
"Invalid program with wrong syntax should have failed to assemble, but it didn't"
);
}
@Test
void failsOnWarning(@TempDir final Path temp) throws Exception {
final FakeMaven maven = new FakeMaven(temp)
.withProgram(
"+architect yegor256@gmail.com",
"+tests",
"+package org.eolang.examples\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[] > main",
" # This is the default 64+ symbols comment in front of named abstract object.",
" [] > @",
" hello > test"
);
VerifyMojoTest.applyXsl(
"org/eolang/maven/set-warning-severity.xsl",
maven.execute(ParseMojo.class)
.result()
.get("target/1-parse/foo/x/main.xmir")
);
Assertions.assertThrows(
IllegalStateException.class,
() -> maven.with("failOnWarning", true)
.execute(VerifyMojo.class),
"Program with warning should fail"
);
}
/**
* Apply XSL transformation.
* @param xsl Path to XSL within classpath
* @param xml Path to XML to be tranformed
*/
private static void applyXsl(final String xsl, final Path xml) throws Exception {
final XML output = new Xsline(
new TrDefault<Shift>()
.with(
new StXSL(
new XSLDocument(
new ResourceOf(xsl).stream()
)))
).pass(new XMLDocument(xml));
new HmBase(xml.getParent()).save(output.toString(), xml.getParent().relativize(xml));
}
/**
* Parse the error message to program name and error line number for checking.
* @param logs Logs logs
* @param error String needed error message
*/
private String getMessage(final Logs logs, final String error) {
return String.valueOf(logs.captured().stream()
.filter(
log -> log.contains(error)
).findFirst()
);
}
/**
* Create regular expression for testing.
* @param path Path program
* @param error String needed error message
*/
private String createRegEx(final Path path, final String error) {
final String str = ".*".concat(error)
.concat(":\\s{3}(")
.concat(
path.resolve("foo/x/main.eo").toString()
.replace("\\", "\\\\")
);
final String res;
if (error.equals("Warnings identified")) {
res = str.concat(", \\d*: .*[\\s]*)+\\]");
} else {
res = str.concat(", \\d+: .*[\\s]*)+\\]");
}
return res;
}
}