test_build.py 2.67 KB
Newer Older
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
1
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
2
import json
3
import os
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
4
5
6
7
import unittest
from collections import Counter
from pathlib import Path

8

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
9
# This file groups together tests which look at the code without running it.
10
11
# When running the tests inside conda's build, the code is not available.
in_conda_build = os.environ.get("CONDA_BUILD_STATE", "") == "TEST"
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
12
13
14


class TestBuild(unittest.TestCase):
15
    @unittest.skipIf(in_conda_build, "In conda build")
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    def test_name_clash(self):
        # For setup.py, all translation units need distinct names, so we
        # cannot have foo.cu and foo.cpp, even in different directories.
        test_dir = Path(__file__).resolve().parent
        source_dir = test_dir.parent / "pytorch3d"

        stems = []
        for extension in [".cu", ".cpp"]:
            files = source_dir.glob(f"**/*{extension}")
            stems.extend(f.stem for f in files)

        counter = Counter(stems)
        for k, v in counter.items():
            self.assertEqual(v, 1, f"Too many files with stem {k}.")

31
    @unittest.skipIf(in_conda_build, "In conda build")
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
32
33
34
35
36
37
38
39
40
41
42
    def test_copyright(self):
        test_dir = Path(__file__).resolve().parent
        root_dir = test_dir.parent

        extensions = ("py", "cu", "cuh", "cpp", "h", "hpp", "sh")

        expect = (
            "Copyright (c) Facebook, Inc. and its affiliates."
            + " All rights reserved.\n"
        )

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
43
44
        files_missing_copyright_header = []

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
45
        for extension in extensions:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
46
47
            for path in root_dir.glob(f"**/*.{extension}"):
                if str(path).endswith(
Christoph Lassner's avatar
Christoph Lassner committed
48
49
50
                    "pytorch3d/transforms/external/kornia_angle_axis_to_rotation_matrix.py"
                ):
                    continue
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
51
                if str(path).endswith("pytorch3d/csrc/pulsar/include/fastermath.h"):
Christoph Lassner's avatar
Christoph Lassner committed
52
                    continue
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
53
                with open(path) as f:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
54
55
56
                    firstline = f.readline()
                    if firstline.startswith(("# -*-", "#!")):
                        firstline = f.readline()
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                    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_conda_build, "In conda build")
    def test_valid_ipynbs(self):
        # Check that the ipython notebooks are valid json
        test_dir = Path(__file__).resolve().parent
        tutorials_dir = test_dir.parent / "docs" / "tutorials"
        tutorials = sorted(tutorials_dir.glob("*.ipynb"))

        for tutorial in tutorials:
            with open(tutorial) as f:
                json.load(f)