Skip to content

File Path Substring Logic #1334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6,779 changes: 6,779 additions & 0 deletions effective-pom.xml

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions qulice-checkstyle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@
<path>${project.artifactId}</path>
</configuration>
</plugin>
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>checkstyle:src/test/resources/.*\.java</exclude>
<exclude>pmd:src/test/resources/.*\.java</exclude>
<exclude>checkstyle:.*/test/testdata/.*\.java</exclude>
<exclude>pmd:.*/test/testdata/.*\.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand All @@ -120,16 +132,6 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<configuration>
<excludes combine.children="append">
<exclude>checkstyle:/src/test/resources/com/qulice/checkstyle/.*</exclude>
<exclude>pmd:.*/src/test/resources/.*</exclude>
<exclude>dependencies:org.antlr</exclude>
<exclude>xml:/src/main/resources/com/qulice/checkstyle/checks.xml</exclude>
<exclude>xml:/src/test/resources/com/qulice/checkstyle/ChecksTest/.*</exclude>
<exclude>duplicatefinder:</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Listener of Checkstyle events.
Expand Down Expand Up @@ -69,9 +72,15 @@ public void fileFinished(final AuditEvent event) {

@Override
public void addError(final AuditEvent event) {
final String name = event.getFileName().substring(
this.env.basedir().toString().length()
);
final Path basePath = Paths.get(this.env.basedir().toURI());
final Path filePath = Paths.get(event.getFileName());
final String name;
if (filePath.isAbsolute()) {
name = basePath.relativize(filePath).toString();
} else {
name = filePath.toString();
}

if (!this.env.exclude("checkstyle", name)) {
this.all.add(event);
}
Expand All @@ -81,12 +90,18 @@ public void addError(final AuditEvent event) {
public void addException(final AuditEvent event,
final Throwable throwable) {
final String check = event.getSourceName();
final Path basePath = Paths.get(this.env.basedir().toURI());
final Path filePath = Paths.get(event.getFileName());
final String relativeFileName;
if (filePath.isAbsolute()) {
relativeFileName = basePath.relativize(filePath).toString();
} else {
relativeFileName = filePath.toString();
}
Logger.error(
this,
"%s[%d]: %s (%s), %[exception]s",
event.getFileName().substring(
this.env.basedir().toString().length()
),
relativeFileName,
event.getLine(),
event.getMessage(),
check.substring(check.lastIndexOf('.') + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.io.File;
import java.util.Collection;
import java.util.LinkedList;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -100,14 +102,13 @@ public Collection<Violation> validate(final Collection<File> files) {
*/
public List<File> getNonExcludedFiles(final Collection<File> files) {
final List<File> relevant = new LinkedList<>();
final Path basePath = Paths.get(this.env.basedir().toURI());
for (final File file : files) {
final String name = file.getPath().substring(
this.env.basedir().toString().length()
);
final String name = basePath.relativize(Paths.get(file.toURI())).toString();
if (this.env.exclude("checkstyle", name)) {
continue;
}
if (!name.matches("^.*\\.java$")) {
if (!name.toLowerCase(java.util.Locale.ROOT).endsWith(".java")) {
continue;
}
relevant.add(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -48,11 +50,9 @@ void testCheckstyleTruePositive(final String dir) throws Exception {
.addError(Mockito.any(AuditEvent.class));
this.check(dir, "/Invalid.java", listener);
final String[] violations = IOUtils.toString(
this.getClass().getResourceAsStream(
String.format("%s/violations.txt", dir)
),
Files.newInputStream(Paths.get("src/test/testdata/com/qulice/checkstyle", dir, "violations.txt")),
StandardCharsets.UTF_8
).split("\n");
).split("\n");
for (final String line : violations) {
final String[] sectors = line.split(":");
final Integer pos = Integer.valueOf(sectors[0]);
Expand Down Expand Up @@ -117,9 +117,7 @@ private void check(
) throws Exception {
final Checker checker = new Checker();
final InputSource src = new InputSource(
this.getClass().getResourceAsStream(
String.format("%s/config.xml", dir)
)
Files.newInputStream(Paths.get("src/test/testdata/com/qulice/checkstyle", dir, "config.xml"))
);
checker.setModuleClassLoader(
Thread.currentThread().getContextClassLoader()
Expand All @@ -134,9 +132,7 @@ private void check(
final List<File> files = new ArrayList<>(0);
files.add(
new File(
this.getClass().getResource(
String.format("%s%s", dir, name)
).getFile()
Paths.get("src/test/testdata/com/qulice/checkstyle", dir, name).toString()
)
);
checker.addListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.qulice.spi.Violation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import org.cactoos.io.ResourceOf;
import org.cactoos.list.ListOf;
Expand Down Expand Up @@ -784,13 +786,7 @@ private Collection<Violation> runValidation(final String file,
)
.withFile(
String.format("src/main/java/foo/%s", file),
new IoCheckedText(
new TextOf(
new ResourceOf(
new FormattedText("com/qulice/checkstyle/%s", file)
)
)
).asString()
new String(Files.readAllBytes(Paths.get("src/test/testdata/com/qulice/checkstyle/" + file)), java.nio.charset.StandardCharsets.UTF_8)
);
final Collection<Violation> results =
new CheckstyleValidator(env).validate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void success(final String[] lines, final String reason, final Matcher<String> ex
expected
);
}


@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<Arguments> params() {
return Stream.of(
Arguments.arguments(
Expand Down
15 changes: 15 additions & 0 deletions qulice-maven-plugin/src/it/dependency-not-matches-exclude/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -47,6 +59,9 @@
<exclude>findbugs:~com.qulice.entity.model.*</exclude>
<exclude>xml:/src/main/java/com/qulice/entity/hibernate.cfg.xml</exclude>
<exclude>checkstyle:/src/main/java/com/qulice/entity/hibernate.cfg.xml</exclude>
<exclude>checkstyle:/src/test/resources/.*\\.java</exclude>
<exclude>pmd:/src/test/resources/.*\\.java</exclude>
<exclude>findbugs:~com.qulice.checkstyle.ChecksTest.*</exclude>
</excludes>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
*/
package com.qulice.entity.model;


import javax.persistence.Entity;
import javax.persistence.Table;

/**
* Test model for dependency-not-matches-exclude integration test.
* @since 1.0
*/
@Entity
@Table(name = "TEST_TABLE")
public class TestModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.qulice.maven.DefaultMavenEnvironment;

/**
* Test case for {@link DefaultMavenEnvironment} class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
* Copyright (c) 2011-2025 Yegor Bugayenko
* SPDX-License-Identifier: MIT
*/
package com.qulice.maven;
Expand Down Expand Up @@ -117,7 +117,7 @@ void excludePathFromCheckstyleValidation() throws Exception {
);
env.setExcludes(
Collections.singletonList(
String.format("checkstyle:/%s/.*", subdir.getFileName())
String.format("checkstyle:%s/.*", subdir.getFileName())
)
);
final CheckstyleValidator validator = new CheckstyleValidator(env);
Expand Down
Loading