def main(options, args): hn = HeaderNormalizer() candidates = hn.find_files_containing_headers(args.searchdir) matches = [] # Copy the list because you are removing bad candidates and if # you modify the list while it is driving the loop you'll get # unexpected results due to python's internal indexer. for file in candidates[:]: matches = hn.find_header_in_file(args.header_filename, file) if( len(matches) > 1): print("Found %d matches in %s" % (len(matches),file)) elif( len(matches) == 1): print("Found %d match in %s" % (len(matches),file)) else: candidates.remove(file) for m in matches: print( "\tLine %d Start %d End %d\t\t Data %s" %(m.line, m.col, m.col+m.length, m.string)) print() if options.do_rename: for file in candidates: print( "Renaming to use %s in %s" %(args.header_filename, file)) hn.rename_headers_in_file(args.header_filename, file)
def test_rename_headers_in_file_extra_spaces(self): header = "aaaaaaaaaa.h" file = join("scenarios","fakeproject","test.c") fileCopy = join("scenarios","fakeproject","testCopyCopy.c") copyfile(file, fileCopy) hn = HeaderNormalizer() matches = hn.find_header_in_file(header,fileCopy) self.assertEquals(len(matches),1) self.assertEquals(matches[0].line, 6) self.assertEquals(matches[0].col + matches[0].length, 29) self.assertEquals(matches[0].col, 1) hn.rename_headers_in_file(header, fileCopy) matches = hn.find_header_in_file(header, fileCopy) self.assertEquals(len(matches),1) self.assertEquals(matches[0].line, 6) self.assertEquals(matches[0].col + matches[0].length, 24) self.assertEquals(matches[0].col, 1) # check that the include line doesn't have any extra cruft tfd = open(fileCopy, "rb") tfd.seek(matches[0].end) data = tfd.read(5) self.assertTrue(data != b"aa.h\"") tfd.close() remove(fileCopy)
def test_find_header_in_file_include_multiple(self): hn = HeaderNormalizer() header ="header1.h" file = join("scenarios","fakeproject","test.c") matches = hn.find_header_in_file(header,file) self.assertEquals(len(matches),2) self.assertEquals(matches[0].string, b"#include \"header1.H\"") self.assertEquals(matches[0].start, 26) self.assertEquals(matches[0].line, 2) self.assertEquals(matches[1].string, b"#include \"header1.h\"") self.assertEquals(matches[1].start, 98) self.assertEquals(matches[1].line, 5)
def test_finds_header_files(self): hn = HeaderNormalizer() candidate_files = hn.find_files_containing_headers("scenarios") rootdir = join("scenarios","fakeproject") self.assertTrue(join(rootdir, "rootInclude.h") in candidate_files) self.assertTrue(join(rootdir, "rootInclude.hpp") in candidate_files) self.assertTrue(join(rootdir, "test.c") in candidate_files) self.assertTrue(join(rootdir, "Fake.txt") not in candidate_files) rootdir = join(rootdir, "capsheads") self.assertTrue(join(rootdir, "rootInclude.H") in candidate_files) self.assertTrue(join(rootdir, "rootInclude.HPP") in candidate_files) self.assertTrue(join(rootdir, "test.CPP") in candidate_files) self.assertTrue(join(rootdir, "dummy.txt") not in candidate_files)
def test_rename_headers_in_file(self): header = "header1.h" file = join("scenarios","fakeproject","test.c") fileCopy = join("scenarios","fakeproject","testCopy.c") copyfile(file, fileCopy) hn = HeaderNormalizer() hn.rename_headers_in_file(header, fileCopy) matches = hn.find_header_in_file(header, fileCopy) remove(fileCopy) self.assertEquals(len(matches),2) self.assertEquals(matches[0].string, b"#include \"header1.h\"") self.assertEquals(matches[0].start, 26) self.assertEquals(matches[0].line, 2) self.assertEquals(matches[1].string, b"#include \"header1.h\"") self.assertEquals(matches[1].start, 98) self.assertEquals(matches[1].line, 5)
def test_find_header_in_file_included_once(self): hn = HeaderNormalizer() file1 = join("scenarios","fakeproject","source","test.c") file2 = join("scenarios","fakeproject","test.c") header = "rootInclude.h" matches = hn.find_header_in_file(header,file1) self.assertEquals(len(matches), 1) self.assertEquals(matches[0].start, 28) self.assertEquals(matches[0].end, 52) self.assertEquals(matches[0].length, 24) self.assertEquals(matches[0].string, b"#include \"rootInclude.H\"") self.assertEquals(matches[0].line, 2) matches = hn.find_header_in_file(header,file2) self.assertEquals(len(matches), 1) self.assertEquals(matches[0].start, 0) self.assertEquals(matches[0].end, 24) self.assertEquals(matches[0].length, 24) self.assertEquals(matches[0].string, b"#include \"rootInclude.h\"") self.assertEquals(matches[0].line, 1)