From e332f9ffa43e18cc76b25914fff183e37838c794 Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Tue, 29 Mar 2022 05:09:27 -0700 Subject: [PATCH] test_build for implicitron Summary: To ensure that tests outside implicitron/ don't use implicitron, split the test for recursive includes in to two. License header checking is not needed here any more. Reviewed By: shapovalov Differential Revision: D35077830 fbshipit-source-id: 2ebe7436a6dcc5d21a116434f6ddd08705dfab34 --- tests/implicitron/test_build.py | 40 +++++++++++++++++++++++++++++++++ tests/test_build.py | 38 ++----------------------------- 2 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 tests/implicitron/test_build.py diff --git a/tests/implicitron/test_build.py b/tests/implicitron/test_build.py new file mode 100644 index 00000000..f5bb23b4 --- /dev/null +++ b/tests/implicitron/test_build.py @@ -0,0 +1,40 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +import importlib +import os +import sys +import unittest +import unittest.mock + +if os.environ.get("FB_TEST", False): + from common_testing import get_pytorch3d_dir +else: + from tests.common_testing import get_pytorch3d_dir + + +# This file groups together tests which look at the code without running it. +class TestBuild(unittest.TestCase): + def test_no_import_cycles(self): + # Check each module of pytorch3d imports cleanly, + # which may fail if there are import cycles. + + with unittest.mock.patch.dict(sys.modules): + for module in list(sys.modules): + # If any of pytorch3d is already imported, + # the test would be pointless. + if module.startswith("pytorch3d"): + sys.modules.pop(module, None) + + root_dir = get_pytorch3d_dir() / "pytorch3d" + for module_file in root_dir.glob("**/*.py"): + if module_file.stem in ("__init__", "plotly_vis"): + continue + relative_module = str(module_file.relative_to(root_dir))[:-3] + module = "pytorch3d." + relative_module.replace("/", ".") + with self.subTest(name=module): + with unittest.mock.patch.dict(sys.modules): + importlib.import_module(module) diff --git a/tests/test_build.py b/tests/test_build.py index aa2e1675..a466549f 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -35,42 +35,6 @@ class TestBuild(unittest.TestCase): for k, v in counter.items(): self.assertEqual(v, 1, f"Too many files with stem {k}.") - @unittest.skipIf(in_re_worker, "In RE worker") - def test_copyright(self): - root_dir = get_pytorch3d_dir() - - extensions = ("py", "cu", "cuh", "cpp", "h", "hpp", "sh") - - expect = "Copyright (c) Meta Platforms, Inc. and affiliates.\n" - - files_missing_copyright_header = [] - - for extension in extensions: - for path in root_dir.glob(f"**/*.{extension}"): - excluded_files = ( - "pytorch3d/transforms/external/kornia_angle_axis_to_rotation_matrix.py", - "pytorch3d/csrc/pulsar/include/fastermath.h", - ) - if in_conda_build: - excluded_files += ( - "run_test.py", - "run_test.sh", - "conda_test_runner.sh", - "conda_test_env_vars.sh", - ) - - if str(path).endswith(excluded_files): - continue - with open(path) as f: - firstline = f.readline() - if firstline.startswith(("# -*-", "#!", "/*")): - firstline = f.readline() - if not firstline.endswith(expect): - files_missing_copyright_header.append(str(path)) - - if len(files_missing_copyright_header) != 0: - self.fail("\n".join(files_missing_copyright_header)) - @unittest.skipIf(in_re_worker, "In RE worker") def test_valid_ipynbs(self): # Check that the ipython notebooks are valid json @@ -129,6 +93,8 @@ class TestBuild(unittest.TestCase): for module_file in root_dir.glob("**/*.py"): if module_file.stem in ("__init__", "plotly_vis"): continue + if "implicitron" in str(module_file): + continue relative_module = str(module_file.relative_to(root_dir))[:-3] module = "pytorch3d." + relative_module.replace("/", ".") with self.subTest(name=module):