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 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(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)