1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4from __future__ import absolute_import, unicode_literals, print_function
5import os
6import unittest
7from unittest import mock
8
9from mozunit import main
10from mach.registrar import Registrar
11from mozbuild.base import MozbuildObject
12import mozpack.path as mozpath
13
14
15class TestStaticAnalysis(unittest.TestCase):
16    def setUp(self):
17        self.remove_cats = []
18        for cat in ("build", "post-build", "misc", "testing", "devenv"):
19            if cat in Registrar.categories:
20                continue
21            Registrar.register_category(cat, cat, cat)
22            self.remove_cats.append(cat)
23
24    def tearDown(self):
25        for cat in self.remove_cats:
26            del Registrar.categories[cat]
27            del Registrar.commands_by_category[cat]
28
29    def test_bug_1615884(self):
30        # TODO: cleaner test
31        # we're testing the `_is_ignored_path` but in an ideal
32        # world we should test the clang_analysis mach command
33        # since that small function is an internal detail.
34        # But there is zero test infra for that mach command
35        from mozbuild.code_analysis.mach_commands import _is_ignored_path
36
37        config = MozbuildObject.from_environment()
38        context = mock.MagicMock()
39        context.cwd = config.topsrcdir
40
41        command_context = mock.MagicMock()
42        command_context.topsrcdir = os.path.join("/root", "dir")
43        path = os.path.join("/root", "dir", "path1")
44
45        ignored_dirs_re = r"path1|path2/here|path3\there"
46        self.assertTrue(
47            _is_ignored_path(command_context, ignored_dirs_re, path) is not None
48        )
49
50        # simulating a win32 env
51        win32_path = "\\root\\dir\\path1"
52        command_context.topsrcdir = "\\root\\dir"
53        old_sep = os.sep
54        os.sep = "\\"
55        try:
56            self.assertTrue(
57                _is_ignored_path(command_context, ignored_dirs_re, win32_path)
58                is not None
59            )
60        finally:
61            os.sep = old_sep
62
63        self.assertTrue(
64            _is_ignored_path(command_context, ignored_dirs_re, "path2") is None
65        )
66
67    def test_get_files(self):
68        from mozbuild.code_analysis.mach_commands import get_abspath_files
69
70        config = MozbuildObject.from_environment()
71        context = mock.MagicMock()
72        context.cwd = config.topsrcdir
73
74        command_context = mock.MagicMock()
75        command_context.topsrcdir = mozpath.join("/root", "dir")
76        source = get_abspath_files(
77            command_context, ["file1", mozpath.join("directory", "file2")]
78        )
79
80        self.assertTrue(
81            source
82            == [
83                mozpath.join(command_context.topsrcdir, "file1"),
84                mozpath.join(command_context.topsrcdir, "directory", "file2"),
85            ]
86        )
87
88
89if __name__ == "__main__":
90    main()
91