BasPH commented on a change in pull request #7346: [AIRFLOW-6720] Detect 
missing tests for providers package
URL: https://github.com/apache/airflow/pull/7346#discussion_r373959313
 
 

 ##########
 File path: tests/test_project_structure.py
 ##########
 @@ -35,3 +70,50 @@ def assert_file_not_contains(self, filename, pattern):
         with open(filename, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, 
access=mmap.ACCESS_READ) as content:
             if content.find(bytes(pattern, 'utf-8')) != -1:
                 self.fail(f"File {filename} contians illegal pattern - 
{pattern}")
+
+    def test_providers_modules_should_have_tests(self):
+        # TODO: Should we extend this test to cover other directories?
+        expected_test_files = 
glob.glob(f"{ROOT_FOLDER}/airflow/providers/**/*.py", recursive=True)
+        # Make path relative
+        expected_test_files = (os.path.relpath(f, ROOT_FOLDER) for f in 
expected_test_files)
+        # Exclude example_dags
+        expected_test_files = (f for f in expected_test_files if 
"/example_dags/" not in f)
+        # Exclude __init__.py
+        expected_test_files = (f for f in expected_test_files if not 
f.endswith("__init__.py"))
+        # Change airflow/ to tests/
+        expected_test_files = (
+            f'tests/{f.partition("/")[2]}'
+            for f in expected_test_files if not f.endswith("__init__.py")
+        )
+        # Add test_ prefix to filename
+        expected_test_files = (
+            f'{f.rpartition("/")[0]}/test_{f.rpartition("/")[2]}'
+            for f in expected_test_files if not f.endswith("__init__.py")
+        )
+
+        current_test_files = 
glob.glob(f"{ROOT_FOLDER}/tests/providers/**/*.py", recursive=True)
+        # Make path relative
+        current_test_files = (os.path.relpath(f, ROOT_FOLDER) for f in 
current_test_files)
+        # Exclude __init__.py
+        current_test_files = (f for f in current_test_files if not 
f.endswith("__init__.py"))
+
+        expected_test_files = set(expected_test_files)
+        current_test_files = set(current_test_files)
+
+        missing_tests_files = expected_test_files - 
expected_test_files.intersection(current_test_files)
+        self.assertEqual(missing_tests_files, MISSING_TEST_FILES)
+
+    def test_keep_missing_test_files_update(self):
+        new_test_files = []
+        for test_file in MISSING_TEST_FILES:
+            if os.path.isfile(f"{ROOT_FOLDER}/{test_file}"):
+                new_test_files.append(test_file)
+        new_files_text = '\n'.join(new_test_files)
+        if new_test_files:
+            self.fail(
+                "It looks like you added a missing test files:\n"
+                f"{new_files_text}"
+                "\n"
+                "Thank you very much.\n"
+                "Can you remove it from the list of missing tests, please?"
+            )
 
 Review comment:
   `new_files_text` is only needed inside the if-clause, we can initialise it 
after `if new_test_files:`.
   
   Small nit, but can we reword it as "You've added a test file currently 
listed as missing:"

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to