gaoyunhaii commented on code in PR #21736:
URL: https://github.com/apache/flink/pull/21736#discussion_r1090556245


##########
flink-test-utils-parent/flink-migration-test-utils/src/main/java/org/apache/flink/test/migration/MigrationTestsSnapshotGenerator.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.flink.test.migration;
+
+import org.apache.flink.FlinkVersion;
+import org.apache.flink.test.util.MigrationTest;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * This class aims to generate the snapshots for all the state migration 
tests. A migration tests
+ * should implement {@link MigrationTest} interface and its name should match 
{@code
+ * *(Test|ITCase)(.java|.scala)}. For scala tests, we also require the class 
name is the same with
+ * the containing filename.
+ */
+public class MigrationTestsSnapshotGenerator {
+
+    private static final Logger LOG =
+            LoggerFactory.getLogger(MigrationTestsSnapshotGenerator.class);
+
+    private static final String[] DEFAULT_PATH_PREFIXES =
+            new String[] {"src/test/java", "src/test/scala"};
+
+    private static final Pattern VERSION_PATTERN = Pattern.compile("" + 
"v?([0-9]+)[._]([0-9]+)");
+
+    private static final String CLASS_NAME_GROUP = "className";
+    private static final Pattern CLASS_NAME_PATTERN =
+            Pattern.compile("(?<" + CLASS_NAME_GROUP + 
">[a-zA-Z0-9]*(Test|ITCase))(.java|.scala)");
+
+    public static void main(String[] args) {
+        try {
+            if (args.length < 2) {
+                throw new IllegalArgumentException(
+                        "Usage: java MigrationTestsSnapshotGenerator [project 
root path] [version]");
+            }
+
+            File rootDirectory = new File(args[0]);
+            if (!rootDirectory.exists() || !rootDirectory.isDirectory()) {
+                throw new FileNotFoundException(
+                        rootDirectory + " does not exist or is not a 
directory.");
+            }
+
+            String versionName = args[1];
+            Matcher versionMatcher = VERSION_PATTERN.matcher(versionName);
+            if (!versionMatcher.matches()) {
+                throw new IllegalArgumentException(
+                        "Version "
+                                + versionName
+                                + "could not be parsed, "
+                                + "please specify the version with format like 
1.17, 1_17, v1_17, v1.17");
+            }
+            String normalizedVersionName =
+                    "v" + versionMatcher.group(1) + "_" + 
versionMatcher.group(2);
+            FlinkVersion version = FlinkVersion.valueOf(normalizedVersionName);
+
+            LOG.info("Start generating for module {} and version {}", 
rootDirectory, version);
+            SnapshotGeneratorExecutor executor = new 
SnapshotGeneratorExecutor();
+            List<Class<?>> migrationTests = 
findMigrationTests(rootDirectory.getAbsolutePath());
+            for (Class<?> migrationTestClass : migrationTests) {
+                LOG.info("Start generating for {}", 
migrationTestClass.getName());
+                executor.executeGenerate(migrationTestClass, version);
+                LOG.info("Finish generating for {}", 
migrationTestClass.getName());
+            }
+
+            // Avoids leaking threads blocks the process.
+            System.exit(0);
+        } catch (Throwable e) {
+            LOG.error("Failed to generate snapshots for the state migration 
tests", e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static List<Class<?>> findMigrationTests(String rootDirectory) 
throws Exception {
+        List<Class<?>> migrationTests = new ArrayList<>();
+        for (String prefix : DEFAULT_PATH_PREFIXES) {
+            recursivelyFindMigrationTests(rootDirectory, prefix, "", 
migrationTests);
+        }
+
+        return migrationTests;
+    }
+
+    private static void recursivelyFindMigrationTests(

Review Comment:
   It tries to avoid re-recreating the List object for each directory. 



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

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

Reply via email to