def checkFile(fpath): #print('checking %s' % fpath) disabledTests = [] txt = read_file(fpath) # First, look through for tests that are disabled in the way we expect. standardDisableCount = 0 for match in DISABLED_PAT.finditer(txt): disabledTests.append( DisabledUnitTest(fpath, txt, match.start(), match.end())) standardDisableCount += 1 improper = [] java = bool(fpath.endswith('.java')) # Now, scan through the file looking for stuff that's maybe disabled in the wrong way. # We do this so that over time, everyone who's working in the codebase converges on # the same best practices, instead of circumventing the system. if java: # In java, look for //@Test where we can't tie the associated method to a # standard comment about being disabled. for match in DISABLED_JAVA_PAT.finditer(txt): name = getNameOfNextTestMethod(fpath, txt, match.start()) if not disabledTestIsProperlyDocumented(name, disabledTests): tuple = (name, 1 + codescan.getLineNumForOffset(txt, match.start())) improper.append(tuple) nextInactiveBlock = codescan.getNextInactiveJavaBlock testnamePat = _JAVA_TESTNAME_PAT elif fpath.endswith('.cpp') or fpath.endswith('.h'): nextInactiveBlock = codescan.getNextInactiveCppBlock testnamePat = _CPP_TESTNAME_PAT else: nextInactiveBlock = _findNoInactiveBlocks testnamePat = None # In both java and C++, look for methods that have been completely commented # out. In C++, also check for methods that have been #ifdef'ed out.' i = 0 while testnamePat: range = nextInactiveBlock(txt, i) if not range: break block = txt[range[0]:range[1]] for match in testnamePat.finditer(block): name = _extractTestNameFromMatch(match, java) if not disabledTestIsProperlyDocumented(name, disabledTests): lineNum = 1 + codescan.getLineNumForOffset( txt, range[0] + match.start()) tuple = (name, lineNum) improper.append(tuple) i = range[1] return disabledTests, improper
def __init__(self, path, txt, start, end): self.when = '' self.which = '' self.where = '' self.by = '' self.ticket = '' self.owner = '' self.why = '' self.path = path self.revision = vcs.revno(path, True) self.scope = '' i = start j = end - 2 while txt[j] == '*': j-= 1 self.lineNum = codescan.getLineNumForOffset(txt, i) lines = [re.sub(r'^\s*\*', '', l).strip() for l in txt[i:j].split('\n') if l.strip()] lbl = None for l in lines: m = _LBL_PAT.match(l) if m: if lbl: DisabledUnitTest._assign(self, lbl, val) lbl = m.group(1) val = m.group(2) elif lbl: val += ' ' + l if lbl: DisabledUnitTest._assign(self, lbl, val) if self.ticket: m = TICKET_PAT.search(self.ticket) if m: self.ticket = m.group(1) if self.owner: self.owner = ', '.join([x.strip() for x in self.owner.replace(';',',').split(',')])
def checkFile(fpath): #print('checking %s' % fpath) disabledTests = [] txt = read_file(fpath) # First, look through for tests that are disabled in the way we expect. standardDisableCount = 0 for match in DISABLED_PAT.finditer(txt): disabledTests.append(DisabledUnitTest(fpath, txt, match.start(), match.end())) standardDisableCount += 1 improper = [] java = bool(fpath.endswith('.java')) # Now, scan through the file looking for stuff that's maybe disabled in the wrong way. # We do this so that over time, everyone who's working in the codebase converges on # the same best practices, instead of circumventing the system. if java: # In java, look for //@Test where we can't tie the associated method to a # standard comment about being disabled. for match in DISABLED_JAVA_PAT.finditer(txt): name = getNameOfNextTestMethod(fpath, txt, match.start()) if not disabledTestIsProperlyDocumented(name, disabledTests): tuple = (name, 1 + codescan.getLineNumForOffset(txt, match.start())) improper.append(tuple) nextInactiveBlock = codescan.getNextInactiveJavaBlock testnamePat = _JAVA_TESTNAME_PAT elif fpath.endswith('.cpp') or fpath.endswith('.h'): nextInactiveBlock = codescan.getNextInactiveCppBlock testnamePat = _CPP_TESTNAME_PAT else: nextInactiveBlock = _findNoInactiveBlocks testnamePat = None # In both java and C++, look for methods that have been completely commented # out. In C++, also check for methods that have been #ifdef'ed out.' i = 0 while testnamePat: range = nextInactiveBlock(txt, i) if not range: break block = txt[range[0]:range[1]] for match in testnamePat.finditer(block): name = _extractTestNameFromMatch(match, java) if not disabledTestIsProperlyDocumented(name, disabledTests): lineNum = 1 + codescan.getLineNumForOffset(txt, range[0] + match.start()) tuple = (name, lineNum) improper.append(tuple) i = range[1] return disabledTests, improper
def cvtMatches(matches, txt, path, idGrp, valGrp, locale): m2 = [] for m in matches: offset = m.start() linenum = codescan.getLineNumForOffset(txt, offset) strdef = StrDef(undoEscapes(m.group(idGrp)), undoEscapes(m.group(valGrp)), locale) strdef.addLoc(FileLocation(path, linenum)) m2.append(strdef) return m2
def visit(self, folder, item, relativePath): if JAVASCRIPT_PAT.match(item): path = os.path.join(folder, item) txt = _read(path) nextInactiveBlock = codescan.pickInactiveBlockFinder(path) #Remove all the inactive blocks from our analysis txt = codescan.getActiveBlocksOnly(txt, nextInactiveBlock) matches = [m for m in CONSOLE_PAT.finditer(txt)] comments = [c for c in COMMENT_PAT.finditer(txt)] lines = [] for m in matches: offset = m.start() linenum = codescan.getLineNumForOffset(txt, offset) comment = False for c in comments: if (linenum == codescan.getLineNumForOffset(txt, c.start())) and (offset > c.start()): comment = True if not comment: lines.append(linenum) if lines: self.badCount += 1 self.categorizedFiles[os.path.join(relativePath, item)] = lines
def __init__(self, path, txt, start, end): self.when = '' self.which = '' self.where = '' self.by = '' self.ticket = '' self.owner = '' self.why = '' self.path = path self.revision = vcs.revno(path, True) self.scope = '' i = start j = end - 2 while txt[j] == '*': j -= 1 self.lineNum = codescan.getLineNumForOffset(txt, i) lines = [ re.sub(r'^\s*\*', '', l).strip() for l in txt[i:j].split('\n') if l.strip() ] lbl = None for l in lines: m = _LBL_PAT.match(l) if m: if lbl: DisabledUnitTest._assign(self, lbl, val) lbl = m.group(1) val = m.group(2) elif lbl: val += ' ' + l if lbl: DisabledUnitTest._assign(self, lbl, val) if self.ticket: m = TICKET_PAT.search(self.ticket) if m: self.ticket = m.group(1) if self.owner: self.owner = ', '.join( [x.strip() for x in self.owner.replace(';', ',').split(',')])
def analyzeFile(fpath, stats): fpath = os.path.abspath(fpath) rel = stats.getRelativePath(fpath) #print('analyzing %s' % rel) txt = read_file(fpath) byteCount = len(txt) stats.addStat(fpath, 'byte count, impl + test', byteCount) lineCount = codescan.getLineNumForOffset(txt, byteCount) stats.addStat(fpath, 'line count, impl + test', lineCount) isTest = bool(_TEST_FILE_PAT.search(fpath)) codeType = 'impl' if isTest: codeType = 'test' stats.addStat(fpath, 'byte count, ' + codeType, byteCount) stats.addStat(fpath, 'line count, ' + codeType, lineCount) # See if we know how to do any further analysis on this file. pat = getClassPatForPath(fpath) if pat: if isTest: pat = getTestnamePatForPath(fpath) if pat: stats.addStat(fpath, 'unit test count', len(pat.findall(txt))) else: stats.addStat(fpath, 'class count', len(pat.findall(txt)))