Exemplo n.º 1
0
 def test_init(self):
     pi = indexer.RepoIndexer('p1', 'file:///tmp/p1')
     pi.set_branch('master')
     self.assertEqual(pi.ref_id, 'file:///tmp/p1:p1:master')
     self.assertTrue(os.path.isdir(indexer.conf['git_store']))
     seen_refs = pickle.load(open(self.seen_refs, 'rb'))
     self.assertTrue(len(seen_refs), 1)
     self.assertIn('file:///tmp/p1:p1:master', seen_refs)
Exemplo n.º 2
0
 def test_init_with_meta_ref(self):
     pi = indexer.RepoIndexer('p1', 'file:///tmp/p1', meta_ref='Fedora')
     pi.set_branch('master')
     self.assertEqual(pi.ref_id, 'file:///tmp/p1:p1:master')
     self.assertEqual(pi.meta_ref, 'meta_ref: Fedora')
     self.assertTrue(os.path.isdir(indexer.conf['git_store']))
     seen_refs = pickle.load(open(self.seen_refs, 'rb'))
     # The meta-ref is not added to seen refs store
     self.assertTrue(len(seen_refs), 1)
     self.assertIn('file:///tmp/p1:p1:master', seen_refs)
Exemplo n.º 3
0
    def test_index_tags(self):
        pi = indexer.RepoIndexer('p1', 'file:///tmp/p1',
                                 con=self.con)
        with mock.patch.object(indexer, 'run') as run:
            run.return_value = "123\trefs/tags/t1\n124\trefs/tags/t2\n"
            pi.get_refs()
            pi.get_tags()
            self.assertListEqual(
                pi.tags, [['123', 'refs/tags/t1'], ['124', 'refs/tags/t2']])

        # This is the initial commits list of a repository we
        # are going to index
        repo_commits = [
            {
                'sha': '123',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:master', ],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
            {
                'sha': '124',
                'author_date': 1410456006,
                'committer_date': 1410456006,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:master', ],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
        ]
        pi.commits = [rc['sha'] for rc in repo_commits]
        pi.set_branch('master')
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        to_create, _ = pi.compute_to_create_to_update()
        to_create = [
            c for c in repo_commits if c['sha'] in to_create]
        indexer.process_commits_desc_output = lambda buf, ref_id: to_create
        pi.index()
        # Start indexation of tags
        pi.index_tags()
        # Do it a second time
        pi.index_tags()

        tags = pi.t.get_tags(['file:///tmp/p1:p1'])

        t1 = [t['_source'] for t in tags if t['_source']['sha'] == '123'][0]
        self.assertEqual(t1['date'], 1410456005)
        self.assertEqual(t1['name'], 't1')

        t2 = [t['_source'] for t in tags if t['_source']['sha'] == '124'][0]
        self.assertEqual(t2['date'], 1410456006)
        self.assertEqual(t2['name'], 't2')
Exemplo n.º 4
0
    def test_index(self):
        pi = indexer.RepoIndexer('p1', 'file:///tmp/p1',
                                 con=self.con)

        # This is the initial commits list of a repository we
        # are going to index
        repo_commits = [
            {
                'sha': '3597334f2cb10772950c97ddf2f6cc17b184',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:master', ],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
        ]
        pi.commits = [rc['sha'] for rc in repo_commits]
        pi.set_branch('master')
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi, repo_commits)
        pi.index()
        # Check
        self.assertDictEqual(
            self.cmts.get_commits_by_id(pi.commits)['docs'][0]['_source'],
            repo_commits[0])
        self.assertEqual(
            len(self.cmts.get_commits_by_id(pi.commits)['docs']), 1)

        # The repo evolves with an additional commit
        additional_cmt = {
            'sha': '3597334f2cb10772950c97ddf2f6cc17b185',
            'author_date': 1410456006,
            'committer_date': 1410456006,
            'author_name': 'Nakata Daisuke',
            'committer_name': 'Nakata Daisuke',
            'author_email': '*****@*****.**',
            'committer_email': '*****@*****.**',
            'repos': [
                'file:///tmp/p1:p1:master', ],
            'line_modifieds': 15,
            'commit_msg': 'Second commit',
        }
        repo_commits.append(additional_cmt)
        pi.commits = [rc['sha'] for rc in repo_commits]
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi, repo_commits)
        pi.index()
        # Check
        cmts = set([c['_source']['sha'] for c in
                    self.cmts.get_commits_by_id(pi.commits)['docs']])
        self.assertEqual(len(cmts), 2)
        cmts.difference_update(set([c['sha'] for c in repo_commits]))
        self.assertEqual(len(cmts), 0)

        # The repo history has been rewritten
        repo_commits.pop()
        pi.commits = [rc['sha'] for rc in repo_commits]
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi, repo_commits)
        pi.index()
        # Check
        self.assertDictEqual(
            self.cmts.get_commits_by_id(pi.commits)['docs'][0]['_source'],
            repo_commits[0])
        self.assertEqual(
            len(self.cmts.get_commits_by_id(pi.commits)['docs']), 1)

        # Index p2 a fork of p1
        pi2 = indexer.RepoIndexer('p2', 'file:///tmp/p2',
                                  con=self.con)

        repo2_commits = [
            {
                'sha': '3597334f2cb10772950c97ddf2f6cc17b184',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p2:p2:master', ],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
        ]
        pi2.commits = [rc['sha'] for rc in repo2_commits]
        pi2.set_branch('master')
        # Start the indexation
        pi2.get_current_commit_indexed()
        pi2.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi2, repo2_commits)
        pi2.index()
        # Check the commits has been marked belonging to both repos
        cmt = self.cmts.get_commit(repo2_commits[0]['sha'])
        self.assertIn('file:///tmp/p2:p2:master', cmt['repos'])
        self.assertIn('file:///tmp/p1:p1:master', cmt['repos'])

        # Add another commit with metadata extracted
        cmt = {
            'sha': '3597334f2cb10772950c97ddf2f6cc17b200',
            'author_date': 1410456005,
            'committer_date': 1410456005,
            'author_name': 'Nakata Daisuke',
            'committer_name': 'Nakata Daisuke',
            'author_email': '*****@*****.**',
            'committer_email': '*****@*****.**',
            'repos': [
                'file:///tmp/p2:p2:master', ],
            'line_modifieds': 10,
            'commit_msg': 'Add init method',
            'close-bug': '123',
            'related-to-story': '124',
        }
        repo2_commits.append(cmt)
        pi2.commits = [rc['sha'] for rc in repo2_commits]
        # Start the indexation
        pi2.get_current_commit_indexed()
        pi2.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi2, repo2_commits)
        pi2.index()
        # Check the commits has been marked belonging to both repos
        cmt = self.cmts.get_commit(repo2_commits[1]['sha'])
        self.assertIn('close-bug', cmt)
        self.assertEqual(cmt['close-bug'], '123')
        self.assertIn('related-to-story', cmt)
        self.assertEqual(cmt['related-to-story'], '124')
Exemplo n.º 5
0
 def test_init(self):
     pi = indexer.RepoIndexer('p1', 'file:///tmp/p1')
     pi.set_branch('master')
     self.assertEqual(pi.ref_id, 'file:///tmp/p1:p1:master')
     self.assertTrue(os.path.isdir(indexer.conf['git_store']))
Exemplo n.º 6
0
    def test_cleaner(self):
        pi = indexer.RepoIndexer('p1', 'file:///tmp/p1',
                                 con=self.con)
        # This is the initial commits list of a repository we
        # are going to index
        repo_commits1 = [
            {
                'sha': '3597334f2cb10772950c97ddf2f6cc17b184',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:master', ],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
        ]
        pi.commits = [rc['sha'] for rc in repo_commits1]
        pi.set_branch('master')
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi, repo_commits1)
        pi.index()
        repo_commits2 = [
            {
                'sha': '3597334f2cb10772950c97ddf2f6cc17b185',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:devel', 'meta_ref: Fedora'],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
            {
                'sha': '3597334f2cb10772950c97ddf2f6cc17b186',
                'author_date': 1410456005,
                'committer_date': 1410456005,
                'author_name': 'Nakata Daisuke',
                'committer_name': 'Nakata Daisuke',
                'author_email': '*****@*****.**',
                'committer_email': '*****@*****.**',
                'repos': [
                    'file:///tmp/p1:p1:devel',
                    'file:///tmp/p1:p1:master',
                    'meta_ref: Fedora'],
                'line_modifieds': 10,
                'commit_msg': 'Add init method',
            },
        ]
        pi.commits = [rc['sha'] for rc in repo_commits2]
        pi.set_branch('devel')
        # Start the indexation
        pi.get_current_commit_indexed()
        pi.compute_to_index_to_delete()
        self.init_fake_process_commits_desc_output(pi, repo_commits2)
        pi.index()

        shas = ['3597334f2cb10772950c97ddf2f6cc17b184',
                '3597334f2cb10772950c97ddf2f6cc17b185',
                '3597334f2cb10772950c97ddf2f6cc17b186']

        pi.tags = [['3597334f2cb10772950c97ddf2f6cc17b184', 'refs/tags/t1'],
                   ['3597334f2cb10772950c97ddf2f6cc17b185', 'refs/tags/t2']]
        pi.index_tags()
        self.assertEqual(len(pi.t.get_tags(['file:///tmp/p1:p1'])), 2)

        # Check 3 commits are indexed
        self.assertEqual(
            len([c for c in self.cmts.get_commits_by_id(shas)['docs']
                 if c['found']]), 3)

        # Now create the RefsCleaner instance
        # '3597334f2cb10772950c97ddf2f6cc17b185' will be removed
        # '3597334f2cb10772950c97ddf2f6cc17b186' will be updated
        # as the devel branch is no longer referenced
        with patch.object(index.YAMLBackend, 'load_db'):
            with patch.object(projects.Projects, 'get_projects') as gp:
                projects_index = projects.Projects('/tmp/fakepath')
                gp.return_value = {
                    'p1': {
                        'refs': [
                            {'branch': 'master',
                             'shortrid': 'file:///tmp/p1:p1',
                             'fullrid': 'file:///tmp/p1:p1:master',
                             'uri': 'file:///tmp/p1'}
                         ]
                    }
                }
                rc = indexer.RefsCleaner(projects_index, con=self.con)
                refs_to_clean = rc.find_refs_to_clean()
                rc.clean(refs_to_clean)
        # Two commits must be in the db (two was from the master branch)
        cmts = self.cmts.get_commits_by_id(shas)['docs']
        self.assertEqual(len([c for c in cmts if c['found']]), 2)
        # Verify that remaining commits belong to ref
        # 'file:///tmp/p1:p1:master' only
        for cmt in cmts:
            if not cmt['found']:
                continue
            self.assertIn(
                'file:///tmp/p1:p1:master', cmt['_source']['repos'])
            self.assertNotIn(
                'file:///tmp/p1:p1:devel', cmt['_source']['repos'])

        # Here make sure tags are still reference as the base_id still exists
        self.assertEqual(len(pi.t.get_tags(['file:///tmp/p1:p1'])), 2)
        # Reinstance a RefsCleaner with no repos
        with patch.object(index.YAMLBackend, 'load_db'):
            with patch.object(projects.Projects, 'get_projects') as gp:
                projects_index = projects.Projects('/tmp/fakepath')
                gp.return_value = {
                    'p1': {
                        'refs': []
                        }
                    }
                rc = indexer.RefsCleaner(projects_index, con=self.con)
                refs_to_clean = rc.find_refs_to_clean()
                rc.clean(refs_to_clean)
        # Make sure tags have been deleted
        self.assertEqual(len(pi.t.get_tags(['file:///tmp/p1:p1'])), 0)