1#!/usr/bin/env python2.7 2 3"""Test internal functions within check_cfc.py.""" 4 5import check_cfc 6import os 7import platform 8import unittest 9 10 11class TestCheckCFC(unittest.TestCase): 12 13 def test_flip_dash_g(self): 14 self.assertIn('-g', check_cfc.flip_dash_g(['clang', '-c'])) 15 self.assertNotIn('-g', check_cfc.flip_dash_g(['clang', '-c', '-g'])) 16 self.assertNotIn( 17 '-g', check_cfc.flip_dash_g(['clang', '-g', '-c', '-g'])) 18 19 def test_remove_dir_from_path(self): 20 bin_path = r'/usr/bin' 21 space_path = r'/home/user/space in path' 22 superstring_path = r'/usr/bin/local' 23 24 # Test removing last thing in path 25 self.assertNotIn( 26 bin_path, check_cfc.remove_dir_from_path(bin_path, bin_path)) 27 28 # Test removing one entry and leaving others 29 # Also tests removing repeated path 30 path_var = os.pathsep.join( 31 [superstring_path, bin_path, space_path, bin_path]) 32 stripped_path_var = check_cfc.remove_dir_from_path(path_var, bin_path) 33 self.assertIn(superstring_path, stripped_path_var) 34 self.assertNotIn(bin_path, stripped_path_var.split(os.pathsep)) 35 self.assertIn(space_path, stripped_path_var) 36 37 # Test removing non-canonical path 38 self.assertNotIn(r'/usr//bin', 39 check_cfc.remove_dir_from_path(r'/usr//bin', bin_path)) 40 41 if platform == 'Windows': 42 # Windows is case insensitive so should remove a different case 43 # path 44 self.assertNotIn( 45 bin_path, check_cfc.remove_dir_from_path(path_var, r'/USR/BIN')) 46 else: 47 # Case sensitive so will not remove different case path 48 self.assertIn( 49 bin_path, check_cfc.remove_dir_from_path(path_var, r'/USR/BIN')) 50 51 def test_is_output_specified(self): 52 self.assertTrue( 53 check_cfc.is_output_specified(['clang', '-o', 'test.o'])) 54 self.assertTrue(check_cfc.is_output_specified(['clang', '-otest.o'])) 55 self.assertFalse( 56 check_cfc.is_output_specified(['clang', '-gline-tables-only'])) 57 # Not specified for implied output file name 58 self.assertFalse(check_cfc.is_output_specified(['clang', 'test.c'])) 59 60 def test_get_output_file(self): 61 self.assertEqual( 62 check_cfc.get_output_file(['clang', '-o', 'test.o']), 'test.o') 63 self.assertEqual( 64 check_cfc.get_output_file(['clang', '-otest.o']), 'test.o') 65 self.assertIsNone( 66 check_cfc.get_output_file(['clang', '-gline-tables-only'])) 67 # Can't get output file if more than one input file 68 self.assertIsNone( 69 check_cfc.get_output_file(['clang', '-c', 'test.cpp', 'test2.cpp'])) 70 # No output file specified 71 self.assertIsNone(check_cfc.get_output_file(['clang', '-c', 'test.c'])) 72 73 def test_derive_output_file(self): 74 # Test getting implicit output file 75 self.assertEqual( 76 check_cfc.derive_output_file(['clang', '-c', 'test.c']), 'test.o') 77 self.assertEqual( 78 check_cfc.derive_output_file(['clang', '-c', 'test.cpp']), 'test.o') 79 self.assertIsNone(check_cfc.derive_output_file(['clang', '--version'])) 80 81 def test_is_normal_compile(self): 82 self.assertTrue(check_cfc.is_normal_compile( 83 ['clang', '-c', 'test.cpp', '-o', 'test2.o'])) 84 self.assertTrue( 85 check_cfc.is_normal_compile(['clang', '-c', 'test.cpp'])) 86 # Outputting bitcode is not a normal compile 87 self.assertFalse( 88 check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '-flto'])) 89 self.assertFalse( 90 check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '-emit-llvm'])) 91 # Outputting preprocessed output or assembly is not a normal compile 92 self.assertFalse( 93 check_cfc.is_normal_compile(['clang', '-E', 'test.cpp', '-o', 'test.ii'])) 94 self.assertFalse( 95 check_cfc.is_normal_compile(['clang', '-S', 'test.cpp', '-o', 'test.s'])) 96 # Input of preprocessed or assembly is not a "normal compile" 97 self.assertFalse( 98 check_cfc.is_normal_compile(['clang', '-c', 'test.s', '-o', 'test.o'])) 99 self.assertFalse( 100 check_cfc.is_normal_compile(['clang', '-c', 'test.ii', '-o', 'test.o'])) 101 # Specifying --version and -c is not a normal compile 102 self.assertFalse( 103 check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '--version'])) 104 self.assertFalse( 105 check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '--help'])) 106 107 def test_replace_output_file(self): 108 self.assertEqual(check_cfc.replace_output_file( 109 ['clang', '-o', 'test.o'], 'testg.o'), ['clang', '-o', 'testg.o']) 110 self.assertEqual(check_cfc.replace_output_file( 111 ['clang', '-otest.o'], 'testg.o'), ['clang', '-otestg.o']) 112 with self.assertRaises(Exception): 113 check_cfc.replace_output_file(['clang'], 'testg.o') 114 115 def test_add_output_file(self): 116 self.assertEqual(check_cfc.add_output_file( 117 ['clang'], 'testg.o'), ['clang', '-o', 'testg.o']) 118 119 def test_set_output_file(self): 120 # Test output not specified 121 self.assertEqual( 122 check_cfc.set_output_file(['clang'], 'test.o'), ['clang', '-o', 'test.o']) 123 # Test output is specified 124 self.assertEqual(check_cfc.set_output_file( 125 ['clang', '-o', 'test.o'], 'testb.o'), ['clang', '-o', 'testb.o']) 126 127 def test_get_input_file(self): 128 # No input file 129 self.assertIsNone(check_cfc.get_input_file(['clang'])) 130 # Input C file 131 self.assertEqual( 132 check_cfc.get_input_file(['clang', 'test.c']), 'test.c') 133 # Input C++ file 134 self.assertEqual( 135 check_cfc.get_input_file(['clang', 'test.cpp']), 'test.cpp') 136 # Multiple input files 137 self.assertIsNone( 138 check_cfc.get_input_file(['clang', 'test.c', 'test2.cpp'])) 139 self.assertIsNone( 140 check_cfc.get_input_file(['clang', 'test.c', 'test2.c'])) 141 # Don't handle preprocessed files 142 self.assertIsNone(check_cfc.get_input_file(['clang', 'test.i'])) 143 self.assertIsNone(check_cfc.get_input_file(['clang', 'test.ii'])) 144 # Test identifying input file with quotes 145 self.assertEqual( 146 check_cfc.get_input_file(['clang', '"test.c"']), '"test.c"') 147 self.assertEqual( 148 check_cfc.get_input_file(['clang', "'test.c'"]), "'test.c'") 149 # Test multiple quotes 150 self.assertEqual( 151 check_cfc.get_input_file(['clang', "\"'test.c'\""]), "\"'test.c'\"") 152 153 def test_set_input_file(self): 154 self.assertEqual(check_cfc.set_input_file( 155 ['clang', 'test.c'], 'test.s'), ['clang', 'test.s']) 156 157if __name__ == '__main__': 158 unittest.main() 159