import check_cfc
import os
import platform
import unittest
class TestCheckCFC(unittest.TestCase):
def test_flip_dash_g(self):
self.assertIn('-g', check_cfc.flip_dash_g(['clang', '-c']))
self.assertNotIn('-g', check_cfc.flip_dash_g(['clang', '-c', '-g']))
self.assertNotIn(
'-g', check_cfc.flip_dash_g(['clang', '-g', '-c', '-g']))
def test_remove_dir_from_path(self):
bin_path = r'/usr/bin'
space_path = r'/home/user/space in path'
superstring_path = r'/usr/bin/local'
self.assertNotIn(
bin_path, check_cfc.remove_dir_from_path(bin_path, bin_path))
path_var = os.pathsep.join(
[superstring_path, bin_path, space_path, bin_path])
stripped_path_var = check_cfc.remove_dir_from_path(path_var, bin_path)
self.assertIn(superstring_path, stripped_path_var)
self.assertNotIn(bin_path, stripped_path_var.split(os.pathsep))
self.assertIn(space_path, stripped_path_var)
self.assertNotIn(r'/usr//bin',
check_cfc.remove_dir_from_path(r'/usr//bin', bin_path))
if platform == 'Windows':
self.assertNotIn(
bin_path, check_cfc.remove_dir_from_path(path_var, r'/USR/BIN'))
else:
self.assertIn(
bin_path, check_cfc.remove_dir_from_path(path_var, r'/USR/BIN'))
def test_is_output_specified(self):
self.assertTrue(
check_cfc.is_output_specified(['clang', '-o', 'test.o']))
self.assertTrue(check_cfc.is_output_specified(['clang', '-otest.o']))
self.assertFalse(
check_cfc.is_output_specified(['clang', '-gline-tables-only']))
self.assertFalse(check_cfc.is_output_specified(['clang', 'test.c']))
def test_get_output_file(self):
self.assertEqual(
check_cfc.get_output_file(['clang', '-o', 'test.o']), 'test.o')
self.assertEqual(
check_cfc.get_output_file(['clang', '-otest.o']), 'test.o')
self.assertIsNone(
check_cfc.get_output_file(['clang', '-gline-tables-only']))
self.assertIsNone(
check_cfc.get_output_file(['clang', '-c', 'test.cpp', 'test2.cpp']))
self.assertIsNone(check_cfc.get_output_file(['clang', '-c', 'test.c']))
def test_derive_output_file(self):
self.assertEqual(
check_cfc.derive_output_file(['clang', '-c', 'test.c']), 'test.o')
self.assertEqual(
check_cfc.derive_output_file(['clang', '-c', 'test.cpp']), 'test.o')
self.assertIsNone(check_cfc.derive_output_file(['clang', '--version']))
def test_is_normal_compile(self):
self.assertTrue(check_cfc.is_normal_compile(
['clang', '-c', 'test.cpp', '-o', 'test2.o']))
self.assertTrue(
check_cfc.is_normal_compile(['clang', '-c', 'test.cpp']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '-flto']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '-emit-llvm']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-E', 'test.cpp', '-o', 'test.ii']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-S', 'test.cpp', '-o', 'test.s']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.s', '-o', 'test.o']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.ii', '-o', 'test.o']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '--version']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', 'test.cpp', '--help']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', '-M', 'test.cpp']))
self.assertFalse(
check_cfc.is_normal_compile(['clang', '-c', '-MM', 'test.cpp']))
self.assertTrue(
check_cfc.is_normal_compile(['clang', '-c', '-MD', 'test.cpp']))
self.assertTrue(
check_cfc.is_normal_compile(['clang', '-c', '-MMD', 'test.cpp']))
def test_replace_output_file(self):
self.assertEqual(check_cfc.replace_output_file(
['clang', '-o', 'test.o'], 'testg.o'), ['clang', '-o', 'testg.o'])
self.assertEqual(check_cfc.replace_output_file(
['clang', '-otest.o'], 'testg.o'), ['clang', '-otestg.o'])
with self.assertRaises(Exception):
check_cfc.replace_output_file(['clang'], 'testg.o')
def test_add_output_file(self):
self.assertEqual(check_cfc.add_output_file(
['clang'], 'testg.o'), ['clang', '-o', 'testg.o'])
def test_set_output_file(self):
self.assertEqual(
check_cfc.set_output_file(['clang'], 'test.o'), ['clang', '-o', 'test.o'])
self.assertEqual(check_cfc.set_output_file(
['clang', '-o', 'test.o'], 'testb.o'), ['clang', '-o', 'testb.o'])
def test_get_input_file(self):
self.assertIsNone(check_cfc.get_input_file(['clang']))
self.assertEqual(
check_cfc.get_input_file(['clang', 'test.c']), 'test.c')
self.assertEqual(
check_cfc.get_input_file(['clang', 'test.cpp']), 'test.cpp')
self.assertIsNone(
check_cfc.get_input_file(['clang', 'test.c', 'test2.cpp']))
self.assertIsNone(
check_cfc.get_input_file(['clang', 'test.c', 'test2.c']))
self.assertIsNone(check_cfc.get_input_file(['clang', 'test.i']))
self.assertIsNone(check_cfc.get_input_file(['clang', 'test.ii']))
self.assertEqual(
check_cfc.get_input_file(['clang', '"test.c"']), '"test.c"')
self.assertEqual(
check_cfc.get_input_file(['clang', "'test.c'"]), "'test.c'")
self.assertEqual(
check_cfc.get_input_file(['clang', "\"'test.c'\""]), "\"'test.c'\"")
def test_set_input_file(self):
self.assertEqual(check_cfc.set_input_file(
['clang', 'test.c'], 'test.s'), ['clang', 'test.s'])
if __name__ == '__main__':
unittest.main()