Skip to content

Commit

Permalink
#1319 no license checking anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 18, 2025
1 parent ac78aa3 commit e75a0f2
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
package com.qulice.checkstyle;

import com.jcabi.log.Logger;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
Expand All @@ -41,18 +40,10 @@
import com.qulice.spi.ResourceValidator;
import com.qulice.spi.Violation;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.cactoos.text.IoCheckedText;
import org.cactoos.text.Replaced;
import org.cactoos.text.TextOf;
import org.cactoos.text.Trimmed;
import org.xml.sax.InputSource;

/**
Expand Down Expand Up @@ -169,7 +160,6 @@ private Configuration configuration() {
}
final Properties props = new Properties();
props.setProperty("cache.file", cache.getPath());
props.setProperty("header", this.header());
final InputSource src = new InputSource(
this.getClass().getResourceAsStream("checks.xml")
);
Expand All @@ -185,101 +175,4 @@ private Configuration configuration() {
}
return config;
}

/**
* Create header content, from file.
* @return The content of header
* @see #configuration()
*/
@SuppressWarnings("PMD.InefficientEmptyStringCheck")
private String header() {
final String name = this.env.param("license", "LICENSE.txt");
final URL url = this.toUrl(name);
final String content;
try {
content = new IoCheckedText(
new Replaced(
new Trimmed(
new TextOf(
url.openStream()
)
),
"[\\r\\n]+$",
""
)
).asString();
} catch (final IOException ex) {
throw new IllegalStateException("Failed to read license", ex);
}
final StringBuilder builder = new StringBuilder(100);
final String eol = System.lineSeparator();
builder.append("/*").append(eol);
for (final String line : CheckstyleValidator.splitPreserve(content, eol)) {
builder.append(" *");
if (!line.trim().isEmpty()) {
builder.append(' ').append(line.trim());
}
builder.append(eol);
}
builder.append(" */");
final String license = builder.toString();
Logger.debug(this, "LICENSE found: %s", url);
Logger.debug(
this,
"LICENSE full text after parsing:\n%s",
license
);
return license;
}

/**
* Convert file name to URL.
* @param name The name of file
* @return The URL
* @see #header()
*/
private URL toUrl(final String name) {
final URL url;
if (name.startsWith("file:")) {
try {
url = Paths.get(name.substring(5)).toUri().toURL();
} catch (final MalformedURLException ex) {
throw new IllegalStateException("Invalid URL", ex);
}
} else {
url = this.env.classloader().getResource(name);
if (url == null) {
throw new IllegalStateException(
String.format(
"'%s' resource is not found in classpath",
name
)
);
}
}
return url;
}

/**
* Divide string using separators to the parts adding empty lines for
* two consistently separators.
* @param content String line
* @param separators Separators string
* @return List of line parts
*/
private static List<String> splitPreserve(final String content, final String separators) {
final List<String> tokens = new LinkedList<>();
final int len = content.length();
int ind = 0;
int start = 0;
while (ind < len) {
if (separators.indexOf(content.charAt(ind)) >= 0) {
tokens.add(content.substring(start, ind));
start = ind + 1;
}
++ind;
}
tokens.add(content.substring(start, ind));
return tokens;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<property name="id" value="StartLineSymbolLimits"/>
<property name="message" value="Line cannot start with this symbol, move it to the previous line"/>
</module>
<!-- Checks for Header -->
<module name="Header">
<property name="header" value="${header}"/>
<property name="fileExtensions" value="java"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="/com/qulice/checkstyle/suppressions.xml"/>
<property name="optional" value="false"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,6 @@ public void setRule() {
this.rule = new License();
}

/**
* CheckstyleValidator can catch checkstyle violations.
* @throws Exception If something wrong happens inside
*/
@Test
void catchesCheckstyleViolationsInLicense() throws Exception {
final Environment.Mock mock = new Environment.Mock();
final File license = this.rule.savePackageInfo(
new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY)
).withLines("License-1.", "", "License-2.")
.withEol("\n")
.file();
final String content =
// @checkstyle StringLiteralsConcatenation (4 lines)
"/" + "*\n * License-3.\n *\n * License-2.\n */\n"
+ "package foo;\n"
+ "public class Foo { }\n";
final String name = "Foo.java";
final Environment env = mock.withParam(
CheckstyleValidatorTest.LICENSE_PROP,
this.toUrl(license)
).withFile(String.format("src/main/java/foo/%s", name), content);
final Collection<Violation> results =
new CheckstyleValidator(env)
.validate(env.files(name));
MatcherAssert.assertThat(
"Header validation is expected",
results,
Matchers.hasItem(
new ViolationMatcher(
"Line does not match expected header line of", name
)
)
);
}

/**
* CheckstyleValidator can accept instance method references.
* @throws Exception In case of error
Expand Down Expand Up @@ -453,51 +417,6 @@ void allowsOnlyProperlyOrderedAtClauses() throws Exception {
);
}

/**
* CheckstyleValidator will fail if Windows EOL-s are used.
* @throws Exception If something wrong happens inside
*/
@Test
void passesWindowsEndsOfLineWithoutException() throws Exception {
final String file = "WindowsEol.java";
final Collection<Violation> results = this.runValidation(file, false);
MatcherAssert.assertThat(
"violation should be reported correctly",
results,
Matchers.contains(
new ViolationMatcher(
"Line does not match expected header line of ' */'.",
file,
"3",
"HeaderCheck"
)
)
);
}

/**
* Fail validation with Windows-style formatting of the license and
* Linux-style formatting of the sources.
* @throws Exception If something wrong happens inside
*/
@Test
void testWindowsEndsOfLineWithLinuxSources() throws Exception {
final String file = "WindowsEolLinux.java";
final Collection<Violation> results = this.runValidation(file, false);
MatcherAssert.assertThat(
"violation should be reported correctly",
results,
Matchers.contains(
new ViolationMatcher(
"Line does not match expected header line of ' * Hello.'.",
file,
"2",
"HeaderCheck"
)
)
);
}

/**
* CheckstyleValidator can allow proper indentation in complex annotations.
* @throws Exception If something wrong happens inside
Expand Down

0 comments on commit e75a0f2

Please sign in to comment.