def testCodeFixExtractSuccessAllChanges(self):
        bugData = self.getCorrectBugDataFileMiddle()
        expectedFixedCode = """int main(void)
{
    int a, b;
    b = 2;
    a = 3;
    a = 0;
    if (a != 0)
    {
"""
        fileDiff = self.getFileDiff(bugData.getStartLine() - 1, [],
                                    ['    int a;'])
        fileDiff += "\n" + self.getFileDiff(
            bugData.getStartLine(), ['    int a, b;', '    b = 2;'], [])
        fileDiff += "\n" + self.getFileDiff(bugData.getStartLine() + 2,
                                            ['    if (a != 0)'],
                                            ['    if (a == 0)'])

        extractor = CodeExtractor(bugData)
        extractor.loadCodeFromFile()
        extractor.extractBugCode()
        extractor.loadDiff(fileDiff)
        extractor.extractFixCode()
        fixedCode = extractor.getFixCodeFragment()
        usedDiffs = extractor.getUsedDiffs()

        self.assertEqual(expectedFixedCode, fixedCode)
        self.assertEqual(3, len(usedDiffs))
        self.assertEqual('7d7', usedDiffs[0])
        self.assertEqual('7a7', usedDiffs[1])
        self.assertEqual('10c10', usedDiffs[2])
    def testExtractCodeWithEmptyDiffBetweenTwoCommits(self):
        gp = gitprovider.GitProvider(config.getRepoDir())
        commits = gp.getAllVersions('trainDbScriptTest')
        commit1 = commits[-2]
        commit2 = commits[-2]
        file1 = gp.getFileContents('bugcode2.cpp', commit1)
        file2 = gp.getFileContents('bugcode2.cpp', commit2)
        diff = LinuxDiffer().diff(file1, file2)
        usedDiffs = []
        bugData = self.getBugData()
        extractor = CodeExtractor(bugData)
        extractor.loadCodeFromText(file1, '\r\n', '\n')
        extractor.extractBugCode()
        extractor.loadDiff(diff)
        with self.assertRaises(ValueError):
            extractor.extractFixCode()
        bugCode = extractor.getBugCodeFragment()
        fixCode = extractor.getFixCodeFragment()
        usedDiffs = extractor.getUsedDiffs()
        expectedOutputFix = ''
        expectedOutputBug = """int main(void)
{
    int a;
    a = 3;
    a = 0;
    if (a == 0)
    {
"""
        self.assertEqual(expectedOutputBug, bugCode)
        self.assertEqual(expectedOutputFix, fixCode)
        self.assertEqual(0, len(usedDiffs))
    def testCodeFixExtractFailureNoDiff(self):
        bugData = self.getCorrectBugDataFileMiddle()
        bugCode = self.getBugCodeFileMiddle()
        expectedFixedCode = ''
        fileDiff = self.getFileDiff(bugData.getEndLine() + 20, [],
                                    ['    a = 3;'])

        extractor = CodeExtractor(bugData)
        extractor.loadCodeFromFile()
        extractor.extractBugCode()
        extractor.loadDiff(fileDiff)
        with self.assertRaises(ValueError):
            extractor.extractFixCode()
        fixedCode = extractor.getFixCodeFragment()
        usedDiffs = extractor.getUsedDiffs()

        self.assertEqual(expectedFixedCode, fixedCode)
        self.assertEqual(0, len(usedDiffs))
    def testCodeFixExtractSuccessRemove(self):
        bugData = self.getCorrectBugDataFileMiddle()
        expectedFixedCode = """int main(void)
{
    int a;
    a = 0;
    if (a == 0)
    {
"""
        fileDiff = self.getFileDiff(bugData.getStartLine(), [], ['    a = 3;'])
        extractor = CodeExtractor(bugData)
        extractor.loadCodeFromFile()
        extractor.extractBugCode()
        extractor.loadDiff(fileDiff)
        extractor.extractFixCode()
        fixedCode = extractor.getFixCodeFragment()
        usedDiffs = extractor.getUsedDiffs()

        self.assertEqual(expectedFixedCode, fixedCode)
        self.assertEqual(1, len(usedDiffs))
        self.assertEqual('8d8', usedDiffs[0])
    def testExtractCodeWithDiffBetweenTwoCommitsMultiDiffInFragment(self):
        gp = gitprovider.GitProvider(config.getRepoDir())
        commits = gp.getAllVersions('trainDbScriptTest')
        commit1 = commits[-6]
        commit2 = commits[-7]
        file1 = gp.getFileContents('bugcode3.cpp', commit1)
        file2 = gp.getFileContents('bugcode3.cpp', commit2)
        diff = LinuxDiffer().diff(file1, file2)
        usedDiffs = []
        bugData = self.getBugData3()
        extractor = CodeExtractor(bugData)
        extractor.loadCodeFromText(file1, '\r\n', '\n')
        extractor.extractBugCode()
        extractor.loadDiff(diff)
        extractor.extractFixCode()
        bugCode = extractor.getBugCodeFragment()
        fixCode = extractor.getFixCodeFragment()
        usedDiffs = extractor.getUsedDiffs()
        expectedOutputFix = """{;
    int a;
    a = 0;
    a = 2;
    if (a != 0)
    {;
        int b = 1 / a;
"""
        expectedOutputBug = """{
    int a;
    a = 0;
    a = 2;
    if (a != 0)
    {
        int b = 1 / a;
"""
        self.assertEqual(expectedOutputBug, bugCode)
        self.assertEqual(expectedOutputFix, fixCode)
        self.assertEqual(2, len(usedDiffs))
Beispiel #6
0
    def extractCode(self, id):
        bugData = self.ccdb.getNotResolvedBugData(id)
        #TODO: Possible improvement for bugData
        if bugData is None:
            #TODO: Implement custom errors
            return None

        fileRelativePath = self.convertFilePathToRepoRelativePath(
            bugData.getFile())
        try:
            fullCodeWithBug = self.vcs.getFileContents(
                fileRelativePath, self.commits[self.currentCommitIndex + 1])
            fullCodeWithoutBug = self.vcs.getFileContents(
                fileRelativePath, self.commits[self.currentCommitIndex])
        except KeyError as extractError:
            return None

        diff = POSIXDiffer().diff(fullCodeWithBug, fullCodeWithoutBug)

        extractor = CodeExtractor(bugData)
        try:
            extractor.extractAll(fullCodeWithBug, diff)
        except ValueError as extractError:
            return None

        bugCodeFragment = extractor.getBugCodeFragment()
        fixCodeFragment = extractor.getFixCodeFragment()

        usedDiffs = extractor.getUsedDiffs()
        #Easy version - ignore bug if none or more than one diff used to fix
        #TODO: Possible improvement here
        if len(usedDiffs) != 1:
            return None
        return entities.FixData(bugCodeFragment, fixCodeFragment,
                                bugData.getChecker(), bugData.getMessage(),
                                bugData.getLine() - bugData.getStartLine())