From 587a4bf2cf5819ed52de44aa0c179caa6b444e98 Mon Sep 17 00:00:00 2001 From: Polina Kopyrina Date: Mon, 18 Mar 2024 01:40:54 +0300 Subject: [PATCH 1/3] Fix tests for Windows EOL check --- .../checkstyle/CheckstyleValidatorTest.java | 133 ++++++++++++------ .../qulice/checkstyle/LicenseViolations.java | 11 ++ .../com/qulice/checkstyle/WindowsEol.java | 2 - .../qulice/checkstyle/WindowsEolLinux.java | 5 +- 4 files changed, 100 insertions(+), 51 deletions(-) create mode 100644 qulice-checkstyle/src/test/resources/com/qulice/checkstyle/LicenseViolations.java diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java index c6172fde7..03ed26711 100644 --- a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java @@ -99,30 +99,19 @@ public void setRule() { @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) - // @checkstyle RegexpSingleline (1 line) - "/" + "**\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 File license = this.createLicense( + mock, "\n", "License-1.", "", "License-2." + ); + final String file = "LicenseViolations.java"; + final Environment env = this.configureEnvironment(mock, license, file); final Collection results = new CheckstyleValidator(env) - .validate(env.files(name)); + .validate(env.files(file)); MatcherAssert.assertThat( results, Matchers.hasItem( new ViolationMatcher( - "Line does not match expected header line of", name + "Line does not match expected header line of", file ) ) ); @@ -450,23 +439,51 @@ void allowsOnlyProperlyOrderedAtClauses() throws Exception { * @throws Exception If something wrong happens inside */ @Test - @Disabled - void passesWindowsEndsOfLineWithoutException() throws Exception { - this.validate("WindowsEol.java", false, "LICENSE found:"); + void prohibitWindowsEndsOfLine() throws Exception { + final String file = "WindowsEol.java"; + final Collection results = this.runValidation(file, false); + final String message = "Lines in file should end with Unix-like end of line"; + final String name = "RegexpMultilineCheck"; + MatcherAssert.assertThat( + results, + Matchers.contains( + new ViolationMatcher( + "Expected line ending for file is LF(\\n), but CRLF(\\r\\n) is detected", + file, "1", "NewlineAtEndOfFileCheck" + ), + new ViolationMatcher( + message, file, "9", name + ), + new ViolationMatcher( + message, file, "10", name + ) + ) + ); } /** * Fail validation with Windows-style formatting of the license and * Linux-style formatting of the sources. * @throws Exception If something wrong happens inside - * @todo #61:30min This test and passesWindowsEndsOfLineWithoutException - * should be refactored to gather log4j logs and validate that they work - * correctly. (see changes done in #61) */ @Test - @Disabled void testWindowsEndsOfLineWithLinuxSources() throws Exception { - this.runValidation("WindowsEolLinux.java", false); + final String file = "WindowsEolLinux.java"; + final Environment.Mock mock = new Environment.Mock(); + final File license = this.createLicense(mock, "\r\n", "Hello.", "World."); + final Environment env = this.configureEnvironment(mock, license, file); + final Collection results = + new CheckstyleValidator(env).validate( + env.files(file) + ); + final String message = "Line does not match expected header line of ' *'"; + final String name = "HeaderCheck"; + MatcherAssert.assertThat( + results, + Matchers.contains( + new ViolationMatcher(message, file, "3", name) + ) + ); } /** @@ -830,24 +847,8 @@ private void validate(final String file, final boolean result, private Collection runValidation(final String file, final boolean passes) throws IOException { final Environment.Mock mock = new Environment.Mock(); - final File license = this.rule.savePackageInfo( - new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY) - ).withLines(CheckstyleValidatorTest.LICENSE) - .withEol("\n").file(); - final Environment env = mock.withParam( - CheckstyleValidatorTest.LICENSE_PROP, - this.toUrl(license) - ) - .withFile( - String.format("src/main/java/foo/%s", file), - new IoCheckedText( - new TextOf( - new ResourceOf( - new FormattedText("com/qulice/checkstyle/%s", file) - ) - ) - ).asString() - ); + final File license = this.createLicense(mock, "\n", CheckstyleValidatorTest.LICENSE); + final Environment env = this.configureEnvironment(mock, license, file); final Collection results = new CheckstyleValidator(env).validate( env.files(file) @@ -866,6 +867,48 @@ private Collection runValidation(final String file, return results; } + /** + * Returns environment with attached license and file with sources to process with validator. + * @param env Environment mock to configure. + * @param license File with licence content. + * @param file File with sources. + * @return Configured environment mock with license and file. + * @throws IOException In case of error. + */ + private Environment configureEnvironment(final Environment.Mock env, final File license, + final String file) throws IOException { + return env.withParam( + CheckstyleValidatorTest.LICENSE_PROP, + this.toUrl(license) + ) + .withFile( + String.format("%s/%s", CheckstyleValidatorTest.DIRECTORY, file), + new IoCheckedText( + new TextOf( + new ResourceOf( + new FormattedText("com/qulice/checkstyle/%s", file) + ) + ) + ).asString() + ); + } + + /** + * Creates a licence file in provided environment. + * @param env Environment where the licence file will be created. + * @param eol End of line symbol which will be used in license file. + * @param lines Text of license. + * @return File with license content attached to given environment. + * @throws IOException In case of error. + */ + private File createLicense(final Environment env, final String eol, final String... lines) + throws IOException { + return this.rule.savePackageInfo( + new File(env.basedir(), CheckstyleValidatorTest.DIRECTORY) + ).withLines(lines) + .withEol(eol).file(); + } + /** * Validation results matcher. * @@ -952,7 +995,5 @@ private boolean lineMatches(final Violation item) { return this.line.isEmpty() || !this.line.isEmpty() && item.lines().equals(this.line); } - } - } diff --git a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/LicenseViolations.java b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/LicenseViolations.java new file mode 100644 index 000000000..7caf329b3 --- /dev/null +++ b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/LicenseViolations.java @@ -0,0 +1,11 @@ +/** + * License-3. + * + * License-2. + */ +package foo; +/** + * Simple class. + * @since 1.0 + */ +public class LicenseViolations { } diff --git a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java index 6e3d76e9e..7b430c99a 100644 --- a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java +++ b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java @@ -1,7 +1,5 @@ /* * Hello. - * - * World. */ package foo; /** diff --git a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java index fe93ff1bb..ed8babc8d 100644 --- a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java +++ b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java @@ -1,7 +1,6 @@ /* - * Welcome. - * - * Friend. + * Hello. + * World. */ package foo; /** From 691ef104edb24d6d4d2799f53dfaf5bbb5019ff1 Mon Sep 17 00:00:00 2001 From: Polina Kopyrina Date: Mon, 18 Mar 2024 02:15:54 +0300 Subject: [PATCH 2/3] Set Windows EOL for test. --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitattributes b/.gitattributes index 34f942dac..ffcd0584e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,7 +14,9 @@ # These files are meant to check violations against use of Windows line endings NewLines.java -text newlines.txt -text +WindowsEol.java -text # Denote all files that are truly binary and should not be modified. *.png binary *.jpg binary + From d64bbdbe070e52760d16d1e1636922b56d117d43 Mon Sep 17 00:00:00 2001 From: Polina Kopyrina Date: Mon, 18 Mar 2024 18:47:55 +0300 Subject: [PATCH 3/3] Set Windows EOL for test. --- .../checkstyle/CheckstyleValidatorTest.java | 13 ++++++------ .../com/qulice/checkstyle/WindowsEol.java | 20 +++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java index 03ed26711..70ab58e59 100644 --- a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java @@ -50,6 +50,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; /** * Test case for {@link CheckstyleValidator} class. @@ -467,15 +469,15 @@ void prohibitWindowsEndsOfLine() throws Exception { * @throws Exception If something wrong happens inside */ @Test + @EnabledOnOs(OS.WINDOWS) void testWindowsEndsOfLineWithLinuxSources() throws Exception { final String file = "WindowsEolLinux.java"; final Environment.Mock mock = new Environment.Mock(); final File license = this.createLicense(mock, "\r\n", "Hello.", "World."); final Environment env = this.configureEnvironment(mock, license, file); - final Collection results = - new CheckstyleValidator(env).validate( - env.files(file) - ); + final Collection results = new CheckstyleValidator(env).validate( + env.files(file) + ); final String message = "Line does not match expected header line of ' *'"; final String name = "HeaderCheck"; MatcherAssert.assertThat( @@ -905,8 +907,7 @@ private File createLicense(final Environment env, final String eol, final String throws IOException { return this.rule.savePackageInfo( new File(env.basedir(), CheckstyleValidatorTest.DIRECTORY) - ).withLines(lines) - .withEol(eol).file(); + ).withLines(lines).withEol(eol).file(); } /** diff --git a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java index 7b430c99a..220e022a4 100644 --- a/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java +++ b/qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java @@ -1,10 +1,10 @@ -/* - * Hello. - */ -package foo; -/** - * Simple class. - * @since 1.0 - */ -public class WindowsEol { } - +/* + * Hello. + */ +package foo; +/** + * Simple class. + * @since 1.0 + */ +public class WindowsEol { } +