Ejemplo n.º 1
0
class BuggyFileListTest(unittest.TestCase):
    def setUp(self):
        self.filelist = BuggyFileList()
        self.mock_buggy_file_factory = MockBuggyFileFactory()
        self.filelist._file_factory = self.mock_buggy_file_factory

    def test_buggyfile_list_creates_and_stores_buggy_file(self):
        filename = 'myfile'
        mockbug = MockBug()
        self.filelist.add_buggy_file(mockbug, filename)
        self.assertEquals(1, len(self.filelist.get_files()))
        actual = self.mock_buggy_file_factory.names_requested
        self.assertEquals(['myfile'], actual)

    def test_buggyfile_list_adds_bug_to_buggy_file(self):
        filename = 'myfile'
        mockbug = MockBug()
        self.filelist.add_buggy_file(mockbug, filename)
        self.assertEquals(1, len(self.filelist._filenames[filename].bugs))
        self.assertEquals(mockbug, self.filelist._filenames[filename].bugs[0])

    def test_buggyfile_reuses_buggyfiles_it_has_already_created(self):
        filename = 'myfile'
        mockbug1 = MockBug()
        mockbug2 = MockBug()
        self.filelist.add_buggy_file(mockbug1, filename)
        self.filelist.add_buggy_file(mockbug2, filename)
        self.assertEquals(1, len(self.filelist.get_files()))
        self.assertEquals(2, len(self.filelist._filenames[filename].bugs))
Ejemplo n.º 2
0
    def test_sorting_buggy_files_by_bugginess(self):
        buggy_file_list = BuggyFileList()
        buggy_file_factory = MockBuggyFileFactory()
        buggy_file_list._file_factory = buggy_file_factory

        #create a few buggy files with different scores
        buggy_file_factory.next_score = 1
        buggy_file_list.add_buggy_file(self.bug1, 'file0')
        buggy_file_factory.next_score = 3
        buggy_file_list.add_buggy_file(self.bug1, 'file1')
        buggy_file_list.add_buggy_file(self.bug2, 'file2')
        buggy_file_factory.next_score = 2
        buggy_file_list.add_buggy_file(self.bug2, 'file3')
        self.fp._buggy_file_list = buggy_file_list

        results = self.fp._get_buggy_files_sorted_by_bugginess()

        self.assertEquals('file1', results[0].filename)
        self.assertEquals('file2', results[1].filename)
        self.assertEquals('file3', results[2].filename)
        self.assertEquals('file0', results[3].filename)
Ejemplo n.º 3
0
class FlyPaper(object):
    def __init__(self, bugid_file, repodir, startdate, showbugs,
                 output_format):
        self._bugid_file = bugid_file
        self._startdate = startdate
        self._showbugs = showbugs
        self._repodir = repodir
        self._output_format = output_format
        self._buglist = BugList()
        self._buggy_file_list = BuggyFileList()
        self._changesets = ChangesetList()

    def show_bug_catchers(self):
        #populate list of bugs
        self._buglist.read_bug_list(self._bugid_file)

        #populate list of changesets
        self._repo = RepoFactory.get_repo(self._repodir)
        self._repo.get_full_changesetlist(self._startdate, self._changesets)

        #match bugs with the changesets that fix them
        self._match_bugs_with_changesets()

        #forget changesets which do not fix a bug
        self._changesets.remove_changesets_which_do_not_fix_a_bug()

        #populate list of files which were modified when fixing bugs
        self._build_buggy_file_list()

        #sort files by bugginess and output results
        results = self._get_buggy_files_sorted_by_bugginess()
        print self._get_output(results)

    def _match_bugs_with_changesets(self):
        for changeset in self._changesets.get_changesets():
            for bugid in self._buglist.bugs.keys():
                if changeset.description_contains(bugid):
                    bug = self._buglist.bugs[bugid]
                    bug.add_fixing_changeset(changeset)
                    changeset.add_bug_fixed(bug)

    def _build_buggy_file_list(self):
        for changeset in self._changesets.get_changesets():
            for filename in changeset.modified_files:
                for bug in changeset.bugs_fixed:
                    self._buggy_file_list.add_buggy_file(bug, filename)

    def _get_buggy_files_sorted_by_bugginess(self):
        #create dict mapping scores to buggy files
        score_map = {}
        for buggyfile in self._buggy_file_list.get_files():
            score = buggyfile.get_score(self._startdate)
            if score not in score_map:
                score_map[score] = []
            score_map[score].append(buggyfile)

        #create list sorted by score
        sorted_buggy_files = []
        all_scores = score_map.keys()
        all_scores.sort()
        all_scores.reverse()
        for score in all_scores:
            buggyfiles = score_map[score]
            buggyfiles.sort(cmp=lambda x, y: cmp(x.filename, y.filename))
            sorted_buggy_files.extend(buggyfiles)
        return sorted_buggy_files

    def _get_output(self, buggyfiles):
        if self._output_format == 'json':
            return self._get_output_json(buggyfiles)
        return self._get_output_plain_text(buggyfiles)

    def _get_output_plain_text(self, buggyfiles):
        output = ""
        for buggyfile in buggyfiles:
            score_str = "%.03f" % buggyfile.get_score(self._startdate)
            output += score_str + " " + buggyfile.filename
            if self._showbugs:
                buglist = [x.__str__() for x in buggyfile.get_bugs()]
                output += " "
                output += ",".join(buglist)
            output += "\n"
        return output

    def _get_output_json(self, buggyfiles):
        out_obj = {}
        files = []
        for buggyfile in buggyfiles:
            onefile = {}
            onefile['filename'] = buggyfile.filename
            onefile['score'] = buggyfile.get_score(self._startdate)
            if self._showbugs:
                onefile['bugs'] = [x.__str__() for x in buggyfile.get_bugs()]
            files.append(onefile)
        out_obj['files'] = files
        return json.dumps(out_obj, sort_keys=True, indent=2)