Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable examples tests for NewlineAtEndOfFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopsss authored and romani committed Jan 8, 2024
1 parent 93beeae commit 6c24054
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/xpath/xpathquerygenerator/InputXpathQueryGeneratorTextBlockCrlf.java eol=crlf
src/test/resources/com/puppycrawl/tools/checkstyle/grammar/javadoc/InputCrAsNewlineMultiline.javadoc -text
src/test/resources/com/puppycrawl/tools/checkstyle/grammar/javadoc/InputDosLineEndingAsNewlineMultiline.javadoc eol=crlf
src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example3.java eol=crlf
4 changes: 4 additions & 0 deletions config/checkstyle-examples-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@
<suppress checks="FileLength"
files="visibilitymodifier[\\/]Example\d+.java"/>

<!-- No new line by design to show violation -->
<suppress checks="NewlineAtEndOfFile"
files="newlineatendoffile[\\/]Example\d+.*"/>

</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package com.puppycrawl.tools.checkstyle.checks;

import org.junit.jupiter.api.Disabled;
import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF;
import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_WRONG_ENDING;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
public class NewlineAtEndOfFileCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -33,28 +35,51 @@ protected String getPackageLocation() {

@Test
public void testExample1() throws Exception {
final String[] expected = {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}

@Test
public void testExample2() throws Exception {
final String[] expected = {

"1: " + getCheckMessage(MSG_KEY_NO_NEWLINE_EOF),
};

verifyWithInlineConfigParser(getPath("Example2.txt"), expected);
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
}

@Test
public void testExample3() throws Exception {
final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_WRONG_ENDING),
};

verifyWithInlineConfigParser(getPath("Example3.java"), expected);
}

@Test
public void testExample4() throws Exception {
final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_NO_NEWLINE_EOF),
};

verifyWithInlineConfigParser(getPath("Example4.py"), expected);
}

@Test
public void testExample5() throws Exception {
final String[] expected = {
"1: " + getCheckMessage(MSG_KEY_NO_NEWLINE_EOF),
};

verifyWithInlineConfigParser(getPath("Example3.txt"), expected);
verifyWithInlineConfigParser(getPath("Example5.java"), expected);
}

@Test
public void testExample6() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verifyWithInlineConfigParser(getPath("Example6.txt"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile"/>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.newlineatendoffile;
// xdoc section -- start
public class Example1 { // ⤶
// ⤶
} // ⤶ // ok, file ends with a new line.
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile"/>
</module>
*/
// violation 5 lines above 'File does not end with a newline.'
package com.puppycrawl.tools.checkstyle.checks.newlineatendoffile;
// xdoc section -- start
public class Example2 { // ⤶
// ⤶
} // no ⤶ below it is violation
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
</module>
*/
// violation 7 lines above 'ending for file is LF(\\n), but CRLF(\\r\\n) is'
package com.puppycrawl.tools.checkstyle.checks.newlineatendoffile;
// xdoc section -- start
public class Example3 { // ⤶
} // ␍⤶ it is violation as line ending is different from expected
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile">
<property name="fileExtensions" value="java, xml, py"/>
</module>
</module>
*/
// violation 7 lines above 'File does not end with a newline.'
// xdoc section -- start
print("Hello world!")
# no ⤶ below it is violation
// xdoc section -- end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile"/>
</module>
*/
// violation 5 lines above 'File does not end with a newline.'
package com.puppycrawl.tools.checkstyle.checks.newlineatendoffile;
// xdoc section -- start
public class Example5 { // ⤶
// ⤶
} // no ⤶ below it is violation
// xdoc section -- end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*xml
<module name="Checker">
<module name="NewlineAtEndOfFile">
<property name="fileExtensions" value="java, xml, py"/>
</module>
</module>
*/
// xdoc section -- start
Sample txt file. // ok, this file is not specified in the config.
// xdoc section -- end
59 changes: 28 additions & 31 deletions src/xdocs/checks/misc/newlineatendoffile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ StaticMethodCandidateCheck.name = Static Method Candidate
&lt;module name=&quot;NewlineAtEndOfFile&quot;/&gt;
&lt;/module&gt;
</source>
<p id="Example1-code">Example:</p>
<p id="Example1-code">Correct Example:</p>
<source>
// File ending with a new line
public class Test {&#x2936;
&#x2936;
}&#x2936; // ok
Note: The comment // ok is a virtual, not actually present in the file
public class Example1 { // &#x2936;
// &#x2936;
} // &#x2936; // ok, file ends with a new line.
</source>

// File ending without a new line
public class Test1 {&#x2936;
&#x2936;
} // violation, the file does not end with a new line
<p id="Example2-code">Violation Example:</p>
<source>
public class Example2 { // &#x2936;
// &#x2936;
} // no &#x2936; below it is violation
</source>

<p id="Example2-config">
<p id="Example3-config">
To configure the check to always use Unix-style line separators:
</p>
<source>
Expand All @@ -121,21 +121,13 @@ public class Test1 {&#x2936;
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example2-code">Example:</p>
<p id="Example3-code">Example:</p>
<source>
// File ending with a new line
public class Test {&#x2936;
&#x2936;
}&#x2936; // ok
Note: The comment // ok is a virtual, not actually present in the file

// File ending without a new line
public class Test1 {&#x2936;
&#x2936;
}&#x240d;&#x2936; // violation, expected line ending for file is LF(\n), but CRLF(\r\n) is detected
public class Example3 { // &#x2936;
} // &#x240d;&#x2936; it is violation as line ending is different from expected
</source>

<p id="Example3-config">
<p id="Example4-config">
To configure the check to work only on Java, XML and Python files:
</p>
<source>
Expand All @@ -145,17 +137,22 @@ public class Test1 {&#x2936;
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example3-code">Example:</p>
<p id="Example4-code">Python file <code>Example4.py</code>:</p>
<source>
// Any java file
public class Test {&#x2936;
} // violation, file should end with a new line.
print(&quot;Hello world!&quot;)
# no &#x2936; below it is violation
</source>

// Any py file
print(&quot;Hello World&quot;) // violation, file should end with a new line.
<p id="Example5-code">Java file <code>Example5.java</code>:</p>
<source>
public class Example5 { // &#x2936;
// &#x2936;
} // no &#x2936; below it is violation
</source>

// Any txt file
This is a sample text file. // ok, this file is not specified in the config.
<p id="Example6-code">Text file <code>Example6.txt</code>:</p>
<source>
Sample txt file. // ok, this file is not specified in the config.
</source>
</subsection>

Expand Down
43 changes: 32 additions & 11 deletions src/xdocs/checks/misc/newlineatendoffile.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,64 @@ StaticMethodCandidateCheck.name = Static Method Candidate
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">Example:</p>
<p id="Example1-code">Correct Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example1.java"/>
<param name="type" value="code"/>
</macro>

<p id="Example2-config">
<p id="Example2-code">Violation Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example2.java"/>
<param name="type" value="code"/>
</macro>

<p id="Example3-config">
To configure the check to always use Unix-style line separators:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example3.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example2-code">Example:</p>
<p id="Example3-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example3.java"/>
<param name="type" value="code"/>
</macro>

<p id="Example3-config">
<p id="Example4-config">
To configure the check to work only on Java, XML and Python files:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example3.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example4.py"/>
<param name="type" value="config"/>
</macro>
<p id="Example3-code">Example:</p>
<p id="Example4-code">Python file <code>Example4.py</code>:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example4.py"/>
<param name="type" value="code"/>
</macro>

<p id="Example5-code">Java file <code>Example5.java</code>:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example5.java"/>
<param name="type" value="code"/>
</macro>

<p id="Example6-code">Text file <code>Example6.txt</code>:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example3.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/Example6.txt"/>
<param name="type" value="code"/>
</macro>
</subsection>
Expand Down

0 comments on commit 6c24054

Please sign in to comment.