summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/code_analysis/test_mach_commands.py
blob: 774688c62f277ad941c54cd6c7214d0e88c08c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import unittest
from unittest import mock

import mozpack.path as mozpath
from mach.registrar import Registrar
from mozunit import main

from mozbuild.base import MozbuildObject


class TestStaticAnalysis(unittest.TestCase):
    def setUp(self):
        self.remove_cats = []
        for cat in ("build", "post-build", "misc", "testing", "devenv"):
            if cat in Registrar.categories:
                continue
            Registrar.register_category(cat, cat, cat)
            self.remove_cats.append(cat)

    def tearDown(self):
        for cat in self.remove_cats:
            del Registrar.categories[cat]
            del Registrar.commands_by_category[cat]

    def test_bug_1615884(self):
        # TODO: cleaner test
        # we're testing the `_is_ignored_path` but in an ideal
        # world we should test the clang_analysis mach command
        # since that small function is an internal detail.
        # But there is zero test infra for that mach command
        from mozbuild.code_analysis.mach_commands import _is_ignored_path

        config = MozbuildObject.from_environment()
        context = mock.MagicMock()
        context.cwd = config.topsrcdir

        command_context = mock.MagicMock()
        command_context.topsrcdir = os.path.join("/root", "dir")
        path = os.path.join("/root", "dir", "path1")

        ignored_dirs_re = r"path1|path2/here|path3\there"
        self.assertTrue(
            _is_ignored_path(command_context, ignored_dirs_re, path) is not None
        )

        # simulating a win32 env
        win32_path = "\\root\\dir\\path1"
        command_context.topsrcdir = "\\root\\dir"
        old_sep = os.sep
        os.sep = "\\"
        try:
            self.assertTrue(
                _is_ignored_path(command_context, ignored_dirs_re, win32_path)
                is not None
            )
        finally:
            os.sep = old_sep

        self.assertTrue(
            _is_ignored_path(command_context, ignored_dirs_re, "path2") is None
        )

    def test_get_files(self):
        from mozbuild.code_analysis.mach_commands import get_abspath_files

        config = MozbuildObject.from_environment()
        context = mock.MagicMock()
        context.cwd = config.topsrcdir

        command_context = mock.MagicMock()
        command_context.topsrcdir = mozpath.join("/root", "dir")
        source = get_abspath_files(
            command_context, ["file1", mozpath.join("directory", "file2")]
        )

        self.assertTrue(
            source
            == [
                mozpath.join(command_context.topsrcdir, "file1"),
                mozpath.join(command_context.topsrcdir, "directory", "file2"),
            ]
        )


if __name__ == "__main__":
    main()