Skip to content
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

Fix tests for Windows EOL check #1236

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -99,30 +101,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<Violation> 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
)
)
);
Expand Down Expand Up @@ -450,23 +441,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<Violation> 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"
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks strange for me, that file has 10 lines of code and we are expecting NewlineAtEndOfFileCheck at line 1

),
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
@EnabledOnOs(OS.WINDOWS)
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<Violation> 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,
Copy link
Contributor

Choose a reason for hiding this comment

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

please add reason for assertThat

Matchers.contains(
new ViolationMatcher(message, file, "3", name)
Copy link
Contributor

Choose a reason for hiding this comment

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

A can not match test name and validation in it:
test is about windowEndsOfLine
validation is about HeaderCheck.

)
);
}

/**
Expand Down Expand Up @@ -830,24 +849,8 @@ private void validate(final String file, final boolean result,
private Collection<Violation> 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<Violation> results =
new CheckstyleValidator(env).validate(
env.files(file)
Expand All @@ -866,6 +869,47 @@ private Collection<Violation> 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.
*
Expand Down Expand Up @@ -952,7 +996,5 @@ private boolean lineMatches(final Violation item) {
return this.line.isEmpty()
|| !this.line.isEmpty() && item.lines().equals(this.line);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* License-3.
*
* License-2.
*/
package foo;
/**
* Simple class.
* @since 1.0
*/
public class LicenseViolations { }
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
* Hello.
*
* World.
*/
package foo;
/**
* Simple class.
* @since 1.0
*/
public class WindowsEol { }

/*
* Hello.
*/
package foo;
/**
* Simple class.
* @since 1.0
*/
public class WindowsEol { }

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* Welcome.
*
* Friend.
* Hello.
* World.
*/
package foo;
/**
Expand Down