Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
exclude_paths:
- "src/test/groovy/*.groovy"
- "eo-runtime/src/test/java/integration/PhiUnphiIT.java"
- "eo-maven-plugin/src/test/java/org/eolang/maven/LintMojoTest.java"
57 changes: 40 additions & 17 deletions eo-maven-plugin/src/main/java/org/eolang/maven/LintMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -89,10 +90,10 @@ public final class LintMojo extends SafeMojo {
void exec() throws IOException {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withShaken();
final ConcurrentHashMap<Severity, Integer> counts = new ConcurrentHashMap<>();
counts.putIfAbsent(Severity.CRITICAL, 0);
counts.putIfAbsent(Severity.ERROR, 0);
counts.putIfAbsent(Severity.WARNING, 0);
final ConcurrentHashMap<Severity, List<Defect>> counts = new ConcurrentHashMap<>();
counts.putIfAbsent(Severity.CRITICAL, new ListOf<>());
counts.putIfAbsent(Severity.ERROR, new ListOf<>());
counts.putIfAbsent(Severity.WARNING, new ListOf<>());
Comment thread
h1alexbel marked this conversation as resolved.
Outdated
final int passed = new Threaded<>(
tojos,
tojo -> this.lintOne(tojo, counts)
Expand All @@ -111,17 +112,26 @@ void exec() throws IOException {
"Linted %d out of %d XMIR program(s) that needed this (out of %d total programs) in %[ms]s: %s",
passed, tojos.size(), tojos.size(), System.currentTimeMillis() - start, sum
);
if (counts.get(Severity.ERROR) > 0 || counts.get(Severity.CRITICAL) > 0) {
Logger.info(
this,
"Read more about lints: https://www.objectionary.com/lints/%s",
counts.values().stream()
.flatMap(List::stream)
.collect(Collectors.toList()).get(0).version()
);
if (!counts.get(Severity.WARNING).isEmpty() && this.failOnWarning) {
throw new IllegalStateException(
String.format(
"In %d XMIR files, we found %s (must stop here)",
"In %d XMIR files, we found %s (use -Deo.failOnWarning=false to ignore)",
tojos.size(), sum
)
);
} else if (counts.get(Severity.WARNING) > 0 && this.failOnWarning) {
} else if (
!counts.get(Severity.ERROR).isEmpty() || !counts.get(Severity.CRITICAL).isEmpty()
) {
throw new IllegalStateException(
String.format(
"In %d XMIR files, we found %s (use -Deo.failOnWarning=false to ignore)",
"In %d XMIR files, we found %s (must stop here)",
tojos.size(), sum
)
);
Expand All @@ -136,7 +146,7 @@ void exec() throws IOException {
* @throws Exception If failed to lint
*/
private int lintOne(final ForeignTojo tojo,
final ConcurrentHashMap<Severity, Integer> counts) throws Exception {
final ConcurrentHashMap<Severity, List<Defect>> counts) throws Exception {
final Path source = tojo.shaken();
final XML xmir = new XMLDocument(source);
final String name = xmir.xpath("/program/@name").get(0);
Expand All @@ -160,7 +170,7 @@ private int lintOne(final ForeignTojo tojo,
* @return Amount of seen XMIR files
* @throws IOException If failed to lint
*/
private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IOException {
private int lintAll(final ConcurrentHashMap<Severity, List<Defect>> counts) throws IOException {
final Map<String, Path> paths = new HashMap<>();
for (final ForeignTojo tojo : this.scopedTojos().withShaken()) {
paths.put(tojo.identifier(), tojo.shaken());
Expand All @@ -174,7 +184,13 @@ private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IO
}
final Collection<Defect> defects = new Programs(pkg).defects();
for (final Defect defect : defects) {
counts.compute(defect.severity(), (sev, before) -> before + 1);
counts.compute(
defect.severity(),
(sev, before) -> {
before.add(defect);
return before;
}
);
LintMojo.embed(
pkg.get(defect.program()),
new ListOf<>(defect)
Expand Down Expand Up @@ -235,17 +251,18 @@ private static String plural(final int count, final String name) {
* @param counts Counts of errors, warnings, and critical
* @return Summary text
*/
private static String summary(final ConcurrentHashMap<Severity, Integer> counts) {
private static String summary(
final ConcurrentHashMap<Severity, List<Defect>> counts) {
final List<String> parts = new ArrayList<>(0);
final int criticals = counts.get(Severity.CRITICAL);
final int criticals = counts.get(Severity.CRITICAL).size();
if (criticals > 0) {
parts.add(LintMojo.plural(criticals, "critical error"));
}
final int errors = counts.get(Severity.ERROR);
final int errors = counts.get(Severity.ERROR).size();
if (errors > 0) {
parts.add(LintMojo.plural(errors, "error"));
}
final int warnings = counts.get(Severity.WARNING);
final int warnings = counts.get(Severity.WARNING).size();
if (warnings > 0) {
parts.add(LintMojo.plural(warnings, "warning"));
}
Expand All @@ -272,15 +289,21 @@ private static String summary(final ConcurrentHashMap<Severity, Integer> counts)
* @return XML after linting
*/
private static XML lint(final XML xmir,
final ConcurrentHashMap<Severity, Integer> counts) {
final ConcurrentHashMap<Severity, List<Defect>> counts) {
final Directives dirs = new Directives();
final Collection<Defect> defects = new Program(xmir).defects();
if (!defects.isEmpty()) {
dirs.xpath("/program").addIf("errors").strict(1);
LintMojo.embed(xmir, defects);
}
for (final Defect defect : defects) {
counts.compute(defect.severity(), (sev, before) -> before + 1);
counts.compute(
defect.severity(),
(sev, before) -> {
before.add(defect);
return before;
}
);
LintMojo.logOne(defect);
}
final Node node = xmir.inner();
Expand Down
26 changes: 26 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/LintMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ void lintsAgainAfterModification(@Mktmp final Path temp, @RandomProgram final St
);
}

@Test
void printsLintsUrlWithVersion(@Mktmp final Path temp, @RandomProgram final String program)
throws IOException {
new Farea(temp).together(
f -> {
f.clean();
f.files().file("src/main/eo/foo.eo").write(program.getBytes());
f.build()
.plugins()
.appendItself()
.execution()
.goals("register", "parse", "shake", "lint")
.configuration()
.set("failOnWarning", "false");
f.exec("process-classes");
MatcherAssert.assertThat(
"Lints URL was not printed, but it should",
f.log().content(),
Matchers.matchesPattern(
"(?s).*\\[INFO] Read more about lints: https://www\\.objectionary\\.com/lints/\\d+\\.\\d+\\.\\d+.*"
)
);
}
);
}

@Test
void doesNotFailWithNoErrorsAndWarnings(@Mktmp final Path temp) {
Assertions.assertDoesNotThrow(
Expand Down