Skip to content

Commit

Permalink
Only require test classes to have a name ending in Tests
Browse files Browse the repository at this point in the history
Previously, the check would apply to interfaces and annotations as
well. This commit tightens up the check so that it only applies to
classes.

Closes gh-364
  • Loading branch information
wilkinsona committed Jul 24, 2024
1 parent 7037333 commit 2c8dbd9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,23 +18,39 @@

import java.io.File;

import com.puppycrawl.tools.checkstyle.JavaParser;
import com.puppycrawl.tools.checkstyle.JavaParser.Options;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
* Checks that test filenames end {@literal Tests.java} and not {@literal Test.java}.
* Checks that test class filenames end {@literal Tests.java} and not {@literal Test.java}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class SpringTestFileNameCheck extends AbstractFileSetCheck {

@Override
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
String path = file.getPath().replace('\\', '/');
if (path.contains("src/test/java") && file.getName().endsWith("Test.java")) {
log(1, "testfilename.wrongName");
visitCompilationUnit(JavaParser.parseFileText(fileText, Options.WITHOUT_COMMENTS));
}
}


private void visitCompilationUnit(DetailAST ast) {
DetailAST child = ast.getFirstChild();
while (child != null) {
if (child.getType() == TokenTypes.CLASS_DEF) {
log(1, "testfilename.wrongName");
return;
}
child = child.getNextSibling();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -123,8 +123,10 @@ public static Collection<Parameter> paramaters() throws IOException {
.map(Parameter::new)
.collect(Collectors.toCollection(ArrayList::new));
parameters.add(new Parameter(new File(SOURCES_DIR, "nopackageinfo/NoPackageInfo.java")));
parameters.add(new Parameter(new File(SOURCES_DIR, "src/test/java/NamedTest.java")));
parameters.add(new Parameter(new File(SOURCES_DIR, "src/test/java/NamedTests.java")));
Arrays.stream(new File(SOURCES_DIR, "src/test/java").listFiles(SpringChecksTests::sourceFile))
.sorted()
.map(Parameter::new)
.forEach(parameters::add);
return parameters;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This is an annotation with a legal name. Only test classes must
* have a name that ends with {@code Tests}.
*
* @author Andy Wilkinson
*/
public @interface AnnotationEndingInTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This is an interface with a legal name. Only test classes must
* have a name that ends with {@code Tests}.
*
* @author Andy Wilkinson
*/
public interface InterfaceEndingInTest {

}

0 comments on commit 2c8dbd9

Please sign in to comment.