-
Notifications
You must be signed in to change notification settings - Fork 112
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
k3p7i3
wants to merge
3
commits into
yegor256:master
Choose a base branch
from
k3p7i3:i-1127
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
) | ||
) | ||
); | ||
|
@@ -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" | ||
), | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A can not match test name and validation in it: |
||
) | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -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) | ||
|
@@ -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. | ||
* | ||
|
@@ -952,7 +996,5 @@ private boolean lineMatches(final Violation item) { | |
return this.line.isEmpty() | ||
|| !this.line.isEmpty() && item.lines().equals(this.line); | ||
} | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
qulice-checkstyle/src/test/resources/com/qulice/checkstyle/LicenseViolations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
22 changes: 10 additions & 12 deletions
22
qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } | ||
|
5 changes: 2 additions & 3 deletions
5
qulice-checkstyle/src/test/resources/com/qulice/checkstyle/WindowsEolLinux.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
/* | ||
* Welcome. | ||
* | ||
* Friend. | ||
* Hello. | ||
* World. | ||
*/ | ||
package foo; | ||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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