Skip to content

Commit

Permalink
Merge pull request #3798 from objectionary/3797
Browse files Browse the repository at this point in the history
latest version of `lints`
  • Loading branch information
yegor256 authored Feb 6, 2025
2 parents 18fc0a7 + 7b45541 commit d70f640
Show file tree
Hide file tree
Showing 69 changed files with 972 additions and 1,507 deletions.
7 changes: 6 additions & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SOFTWARE.
<dependency>
<groupId>org.eolang</groupId>
<artifactId>lints</artifactId>
<version>0.0.26</version>
<version>0.0.38</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
Expand Down Expand Up @@ -144,6 +144,11 @@ SOFTWARE.
<artifactId>mojo-executor</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<!-- version from parent POM -->
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down
135 changes: 73 additions & 62 deletions eo-maven-plugin/src/main/java/org/eolang/maven/LintMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -143,7 +144,7 @@ private int lintOne(final ForeignTojo tojo,
final Path target = new Place(name).make(base, AssembleMojo.XMIR);
tojo.withLinted(
new FpDefault(
src -> this.counted(LintMojo.lint(xmir), counts).toString(),
src -> LintMojo.lint(xmir, counts).toString(),
this.cache.toPath().resolve(LintMojo.CACHE),
this.plugin.getVersion(),
new TojoHash(tojo),
Expand All @@ -161,8 +162,11 @@ private int lintOne(final ForeignTojo tojo,
*/
private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IOException {
final Map<String, Path> paths = new HashMap<>();
for (final ForeignTojo tojo : this.scopedTojos().withLinted()) {
paths.put(tojo.identifier(), tojo.linted());
for (final ForeignTojo tojo : this.scopedTojos().withShaken()) {
paths.put(tojo.identifier(), tojo.shaken());
}
for (final ForeignTojo tojo : this.compileTojos().withShaken()) {
paths.put(tojo.identifier(), tojo.shaken());
}
final Map<String, XML> pkg = new HashMap<>();
for (final Map.Entry<String, Path> ent : paths.entrySet()) {
Expand All @@ -175,48 +179,55 @@ private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IO
pkg.get(defect.program()),
new ListOf<>(defect)
);
LintMojo.logOne(defect);
}
return pkg.size();
}

/**
* Log errors of XMIR.
* @param xml XMIR.
* @param counts Counts of errors, warnings, and critical
* @return TRUE if it's good
* Log one defect.
* @param defect The defect to log
*/
private XML counted(final XML xml, final ConcurrentHashMap<Severity, Integer> counts) {
for (final XML error : xml.nodes("/program/errors/error")) {
final List<String> line = error.xpath("@line");
final StringBuilder message = new StringBuilder()
.append(Logger.format("%[file]s", xml.xpath("/program/@source").get(0)));
if (!line.isEmpty()) {
message.append(':').append(Integer.parseInt(line.get(0)));
}
message.append(' ')
.append(error.xpath("text()").get(0))
.append(" (")
.append(error.xpath("@check").get(0))
.append(' ')
.append(error.xpath("@severity").get(0))
.append(')');
final Severity severity = Severity.parsed(error.xpath("@severity").get(0));
counts.compute(severity, (sev, before) -> before + 1);
switch (severity) {
case WARNING:
Logger.warn(this, message.toString());
break;
case ERROR:
case CRITICAL:
Logger.error(this, message.toString());
break;
default:
throw new IllegalArgumentException(
String.format("Not yet supported severity: %s", severity)
);
}
private static void logOne(final Defect defect) {
final StringBuilder message = new StringBuilder()
.append(defect.program())
.append(':').append(defect.line())
.append(' ')
.append(defect.text())
.append(" (")
.append(defect.rule())
.append(')');
switch (defect.severity()) {
case WARNING:
Logger.warn(LintMojo.class, message.toString());
break;
case ERROR:
case CRITICAL:
Logger.error(LintMojo.class, message.toString());
break;
default:
throw new IllegalArgumentException(
String.format(
"Not yet supported severity: %s",
defect.severity()
)
);
}
return xml;
}

/**
* Text in plural or singular form.
* @param count Counts of errors, warnings, and critical
* @param name Name of them
* @return Summary text
*/
private static String plural(final int count, final String name) {
final StringBuilder txt = new StringBuilder();
txt.append(count).append(' ').append(name);
if (count > 1) {
txt.append('s');
}
return txt.toString();
}

/**
Expand All @@ -225,53 +236,53 @@ private XML counted(final XML xml, final ConcurrentHashMap<Severity, Integer> co
* @return Summary text
*/
private static String summary(final ConcurrentHashMap<Severity, Integer> counts) {
final StringBuilder sum = new StringBuilder(100);
final List<String> parts = new ArrayList<>(0);
final int criticals = counts.get(Severity.CRITICAL);
if (criticals > 0) {
sum.append(criticals).append(" critical error");
if (criticals > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(criticals, "critical error"));
}
final int errors = counts.get(Severity.ERROR);
if (errors > 0) {
if (sum.length() > 0) {
sum.append(", ");
}
sum.append(errors).append(" error");
if (errors > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(errors, "error"));
}
final int warnings = counts.get(Severity.WARNING);
if (warnings > 0) {
if (sum.length() > 0) {
sum.append(", ");
}
sum.append(warnings).append(" warning");
if (warnings > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(warnings, "warning"));
}
if (parts.isEmpty()) {
parts.add("no complaints");
}
if (sum.length() == 0) {
sum.append("no complaints");
final String sum;
if (parts.size() < 3) {
sum = String.join(" and ", parts);
} else {
sum = String.format(
"%s, and %s",
String.join(", ", parts.subList(0, parts.size() - 2)),
parts.get(parts.size() - 1)
);
}
return sum.toString();
return sum;
}

/**
* Find all possible linting defects and add them to the XMIR.
* @param xmir The XML before linting
* @param counts Counts of errors, warnings, and critical
* @return XML after linting
* @throws IOException If fails
*/
private static XML lint(final XML xmir) throws IOException {
private static XML lint(final XML xmir,
final ConcurrentHashMap<Severity, Integer> 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);
LintMojo.logOne(defect);
}
final Node node = xmir.inner();
new Xembler(dirs).applyQuietly(node);
return new XMLDocument(node);
Expand Down
12 changes: 12 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,18 @@ protected final ForeignTojos scopedTojos() {
return this.tojos;
}

/**
* Tojos to use, in "compile" scope only.
* @return Tojos to use
* @checkstyle AnonInnerLengthCheck (100 lines)
*/
protected final ForeignTojos compileTojos() {
return new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.foreign.toPath(), this.foreignFormat),
() -> "compile"
);
}

/**
* Make a measured train from another train.
* @param train The train
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ public Collection<ForeignTojo> withShaken() {
return this.select(row -> row.exists(Attribute.SHAKEN.getKey()));
}

/**
* Get the tojos that have corresponding linted XMIR.
* @return The tojos.
*/
public Collection<ForeignTojo> withLinted() {
return this.select(row -> row.exists(Attribute.LINTED.getKey()));
}

/**
* Get the tojos that doesn't have dependency.
* @return The tojos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import org.eolang.maven.log.CaptureLogs;
import org.eolang.maven.log.Logs;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -120,23 +118,6 @@ void assemblesNotFailWithFailOnError(@Mktmp final Path temp) throws IOException
);
}

@CaptureLogs
@Test
void assemblesSuccessfullyInOfflineMode(final Logs out,
@Mktmp final Path temp) throws IOException {
new FakeMaven(temp)
.withHelloWorld()
.with("offline", true)
.execute(AssembleMojo.class);
MatcherAssert.assertThat(
"While execution AssembleMojo log should have contained message about offline mode, but it didn't",
String.join("\n", out.captured()),
Matchers.containsString(
"No programs were pulled because eo.offline flag is set to TRUE"
)
);
}

@Test
void configuresChildParameters(@Mktmp final Path temp) throws IOException {
final Map<String, Path> res = new FakeMaven(temp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,13 @@ FakeMaven withPlacedBinary(final Path binary) {
FakeMaven withHelloWorld() throws IOException {
return this.withProgram(
"+alias stdout org.eolang.io.stdout",
"+home https://www.eolang.org",
"+package foo.x",
"+unlint object-has-data",
"+unlint broken-alias-second",
"+package f\n",
"+unlint incorrect-alias",
"+version 0.0.0",
"",
"# No comments.",
"[x] > main",
" (stdout \"Hello!\" x).print > @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void detectsWarningWithCorrespondingFlag(@Mktmp final Path temp) throws IOExcept
new XMLDocument(
maven.result().get("target/6-lint/foo/x/main.xmir")
).nodes("//errors/error[@severity='warning']"),
Matchers.hasSize(Matchers.equalTo(7))
Matchers.hasSize(Matchers.greaterThanOrEqualTo(2))
);
}

Expand Down
82 changes: 0 additions & 82 deletions eo-maven-plugin/src/test/java/org/eolang/maven/LogFormatTest.java

This file was deleted.

Loading

1 comment on commit d70f640

@0pdd
Copy link

@0pdd 0pdd commented on d70f640 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2896-621330ec disappeared from eo-maven-plugin/src/test/java/org/eolang/maven/log/CaptureLogs.java), that's why I closed #3181. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.