3 """Test internal functions within check_cfc.py."""
11 class TestCheckCFC(unittest
.TestCase
):
12 def test_flip_dash_g(self
):
13 self
.assertIn("-g", check_cfc
.flip_dash_g(["clang", "-c"]))
14 self
.assertNotIn("-g", check_cfc
.flip_dash_g(["clang", "-c", "-g"]))
15 self
.assertNotIn("-g", check_cfc
.flip_dash_g(["clang", "-g", "-c", "-g"]))
17 def test_remove_dir_from_path(self
):
18 bin_path
= r
"/usr/bin"
19 space_path
= r
"/home/user/space in path"
20 superstring_path
= r
"/usr/bin/local"
22 # Test removing last thing in path
23 self
.assertNotIn(bin_path
, check_cfc
.remove_dir_from_path(bin_path
, bin_path
))
25 # Test removing one entry and leaving others
26 # Also tests removing repeated path
27 path_var
= os
.pathsep
.join([superstring_path
, bin_path
, space_path
, bin_path
])
28 stripped_path_var
= check_cfc
.remove_dir_from_path(path_var
, bin_path
)
29 self
.assertIn(superstring_path
, stripped_path_var
)
30 self
.assertNotIn(bin_path
, stripped_path_var
.split(os
.pathsep
))
31 self
.assertIn(space_path
, stripped_path_var
)
33 # Test removing non-canonical path
35 r
"/usr//bin", check_cfc
.remove_dir_from_path(r
"/usr//bin", bin_path
)
38 if platform
== "Windows":
39 # Windows is case insensitive so should remove a different case
42 bin_path
, check_cfc
.remove_dir_from_path(path_var
, r
"/USR/BIN")
45 # Case sensitive so will not remove different case path
47 bin_path
, check_cfc
.remove_dir_from_path(path_var
, r
"/USR/BIN")
50 def test_is_output_specified(self
):
51 self
.assertTrue(check_cfc
.is_output_specified(["clang", "-o", "test.o"]))
52 self
.assertTrue(check_cfc
.is_output_specified(["clang", "-otest.o"]))
53 self
.assertFalse(check_cfc
.is_output_specified(["clang", "-gline-tables-only"]))
54 # Not specified for implied output file name
55 self
.assertFalse(check_cfc
.is_output_specified(["clang", "test.c"]))
57 def test_get_output_file(self
):
58 self
.assertEqual(check_cfc
.get_output_file(["clang", "-o", "test.o"]), "test.o")
59 self
.assertEqual(check_cfc
.get_output_file(["clang", "-otest.o"]), "test.o")
60 self
.assertIsNone(check_cfc
.get_output_file(["clang", "-gline-tables-only"]))
61 # Can't get output file if more than one input file
63 check_cfc
.get_output_file(["clang", "-c", "test.cpp", "test2.cpp"])
65 # No output file specified
66 self
.assertIsNone(check_cfc
.get_output_file(["clang", "-c", "test.c"]))
68 def test_derive_output_file(self
):
69 # Test getting implicit output file
71 check_cfc
.derive_output_file(["clang", "-c", "test.c"]), "test.o"
74 check_cfc
.derive_output_file(["clang", "-c", "test.cpp"]), "test.o"
76 self
.assertIsNone(check_cfc
.derive_output_file(["clang", "--version"]))
78 def test_is_normal_compile(self
):
80 check_cfc
.is_normal_compile(["clang", "-c", "test.cpp", "-o", "test2.o"])
82 self
.assertTrue(check_cfc
.is_normal_compile(["clang", "-c", "test.cpp"]))
83 # Outputting bitcode is not a normal compile
85 check_cfc
.is_normal_compile(["clang", "-c", "test.cpp", "-flto"])
88 check_cfc
.is_normal_compile(["clang", "-c", "test.cpp", "-emit-llvm"])
90 # Outputting preprocessed output or assembly is not a normal compile
92 check_cfc
.is_normal_compile(["clang", "-E", "test.cpp", "-o", "test.ii"])
95 check_cfc
.is_normal_compile(["clang", "-S", "test.cpp", "-o", "test.s"])
97 # Input of preprocessed or assembly is not a "normal compile"
99 check_cfc
.is_normal_compile(["clang", "-c", "test.s", "-o", "test.o"])
102 check_cfc
.is_normal_compile(["clang", "-c", "test.ii", "-o", "test.o"])
104 # Specifying --version and -c is not a normal compile
106 check_cfc
.is_normal_compile(["clang", "-c", "test.cpp", "--version"])
109 check_cfc
.is_normal_compile(["clang", "-c", "test.cpp", "--help"])
111 # Outputting dependency files is not a normal compile
112 self
.assertFalse(check_cfc
.is_normal_compile(["clang", "-c", "-M", "test.cpp"]))
114 check_cfc
.is_normal_compile(["clang", "-c", "-MM", "test.cpp"])
116 # Creating a dependency file as a side effect still outputs an object file
117 self
.assertTrue(check_cfc
.is_normal_compile(["clang", "-c", "-MD", "test.cpp"]))
119 check_cfc
.is_normal_compile(["clang", "-c", "-MMD", "test.cpp"])
122 def test_replace_output_file(self
):
124 check_cfc
.replace_output_file(["clang", "-o", "test.o"], "testg.o"),
125 ["clang", "-o", "testg.o"],
128 check_cfc
.replace_output_file(["clang", "-otest.o"], "testg.o"),
129 ["clang", "-otestg.o"],
131 with self
.assertRaises(Exception):
132 check_cfc
.replace_output_file(["clang"], "testg.o")
134 def test_add_output_file(self
):
136 check_cfc
.add_output_file(["clang"], "testg.o"), ["clang", "-o", "testg.o"]
139 def test_set_output_file(self
):
140 # Test output not specified
142 check_cfc
.set_output_file(["clang"], "test.o"), ["clang", "-o", "test.o"]
144 # Test output is specified
146 check_cfc
.set_output_file(["clang", "-o", "test.o"], "testb.o"),
147 ["clang", "-o", "testb.o"],
150 def test_get_input_file(self
):
152 self
.assertIsNone(check_cfc
.get_input_file(["clang"]))
154 self
.assertEqual(check_cfc
.get_input_file(["clang", "test.c"]), "test.c")
156 self
.assertEqual(check_cfc
.get_input_file(["clang", "test.cpp"]), "test.cpp")
157 # Multiple input files
158 self
.assertIsNone(check_cfc
.get_input_file(["clang", "test.c", "test2.cpp"]))
159 self
.assertIsNone(check_cfc
.get_input_file(["clang", "test.c", "test2.c"]))
160 # Don't handle preprocessed files
161 self
.assertIsNone(check_cfc
.get_input_file(["clang", "test.i"]))
162 self
.assertIsNone(check_cfc
.get_input_file(["clang", "test.ii"]))
163 # Test identifying input file with quotes
164 self
.assertEqual(check_cfc
.get_input_file(["clang", '"test.c"']), '"test.c"')
165 self
.assertEqual(check_cfc
.get_input_file(["clang", "'test.c'"]), "'test.c'")
166 # Test multiple quotes
168 check_cfc
.get_input_file(["clang", "\"'test.c'\""]), "\"'test.c'\""
171 def test_set_input_file(self
):
173 check_cfc
.set_input_file(["clang", "test.c"], "test.s"), ["clang", "test.s"]
177 if __name__
== "__main__":