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"): 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 StaticAnalysis 36 37 config = MozbuildObject.from_environment() 38 context = mock.MagicMock() 39 context.cwd = config.topsrcdir 40 41 cmd = StaticAnalysis(context) 42 cmd.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(cmd._is_ignored_path(ignored_dirs_re, path) is not None) 47 48 # simulating a win32 env 49 win32_path = "\\root\\dir\\path1" 50 cmd.topsrcdir = "\\root\\dir" 51 old_sep = os.sep 52 os.sep = "\\" 53 try: 54 self.assertTrue( 55 cmd._is_ignored_path(ignored_dirs_re, win32_path) is not None 56 ) 57 finally: 58 os.sep = old_sep 59 60 self.assertTrue(cmd._is_ignored_path(ignored_dirs_re, "path2") is None) 61 62 def test_get_files(self): 63 from mozbuild.code_analysis.mach_commands import StaticAnalysis 64 65 config = MozbuildObject.from_environment() 66 context = mock.MagicMock() 67 context.cwd = config.topsrcdir 68 69 cmd = StaticAnalysis(context) 70 cmd.topsrcdir = mozpath.join("/root", "dir") 71 source = cmd.get_abspath_files(["file1", mozpath.join("directory", "file2")]) 72 73 self.assertTrue( 74 source 75 == [ 76 mozpath.join(cmd.topsrcdir, "file1"), 77 mozpath.join(cmd.topsrcdir, "directory", "file2"), 78 ] 79 ) 80 81 82if __name__ == "__main__": 83 main() 84