You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2020/09/04 15:11:12 UTC

[GitHub] [lucene-solr] cpoerschke commented on a change in pull request #1743: Gradual naming convention enforcement.

cpoerschke commented on a change in pull request #1743:
URL: https://github.com/apache/lucene-solr/pull/1743#discussion_r483679806



##########
File path: lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
##########
@@ -613,6 +613,7 @@ public static TestRuleIgnoreAfterMaxFailures replaceMaxFailureRule(TestRuleIgnor
     RuleChain r = RuleChain.outerRule(new TestRuleIgnoreTestSuites())
       .around(ignoreAfterMaxFailures)
       .around(suiteFailureMarker = new TestRuleMarkFailure())
+      .around(new VerifyTestClassNamingConvention())

Review comment:
       question: would this convention automatically and always apply to all classes derived from `LuceneTestCase` including any non-`org.apache` name spaces or would it be possible to opt-out (without an exclusion list) somehow for custom code that might perhaps have chosen a different convention?

##########
File path: lucene/test-framework/src/java/org/apache/lucene/util/VerifyTestClassNamingConvention.java
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.
+ */
+package org.apache.lucene.util;
+
+import com.carrotsearch.randomizedtesting.RandomizedContext;
+import org.junit.Assume;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UncheckedIOException;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * Enforce test naming convention.
+ */
+public class VerifyTestClassNamingConvention extends AbstractBeforeAfterRule {
+  public static final Pattern ALLOWED_CONVENTION = Pattern.compile("(.+?)\\.Test[^.]+");
+
+  private static Set<String> exceptions;
+  static {
+    try {
+      exceptions = new HashSet<>();
+      try (BufferedReader is =
+             new BufferedReader(
+                 new InputStreamReader(
+                   VerifyTestClassNamingConvention.class.getResourceAsStream("test-naming-exceptions.txt"),
+                   StandardCharsets.UTF_8))) {
+        is.lines().forEach(exceptions::add);
+      }
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+  }
+
+  @Override
+  protected void before() throws Exception {
+    if (TestRuleIgnoreTestSuites.isRunningNested()) {
+      // Ignore nested test suites that test the test framework itself.
+      return;
+    }
+
+    String suiteName = RandomizedContext.current().getTargetClass().getName();
+
+    // You can use this helper method to dump all suite names to a file.
+    // Run gradle with one worker so that it doesn't try to append to the same
+    // file from multiple processes:
+    //
+    // gradlew  test --max-workers 1 -Dtests.useSecurityManager=false
+    //
+    // dumpSuiteNamesOnly(suiteName);
+
+    if (!ALLOWED_CONVENTION.matcher(suiteName).matches()) {
+      // if this class exists on the exception list, leave it.

Review comment:
       It's possible (though rare) that both `TestFooBar.java` and `FooBarTest.java` classes co-exist. I wonder if the `ALLOW_CONVENTION` and `test-naming-exceptions.txt` logic might be mutually exclusive i.e. when something is on the exclusion list then its opposite is not valid i.e. the excluded test may be renamed (and removed from the exclusion list) but until that is done the conventional naming is discouraged to avoid confusion between the two variants?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org