def test_repo_diff_commits(self): """Ensure expected commits objects are returned in diff.""" repo = RepoFactory(self.repo_store) c1_oid = repo.add_commit('foo', 'foobar.txt') c2_oid = repo.add_commit('bar', 'foobar.txt', parents=[c1_oid]) path = '/repo/{}/compare/{}..{}'.format(self.repo_path, c1_oid, c2_oid) resp = self.app.get(path) self.assertIn(c1_oid.hex, resp.json['commits'][0]['sha1']) self.assertIn(c2_oid.hex, resp.json['commits'][1]['sha1'])
def test_repo_compare_commits(self): """Ensure expected changes exist in diff patch.""" repo = RepoFactory(self.repo_store) c1_oid = repo.add_commit('foo', 'foobar.txt') c2_oid = repo.add_commit('bar', 'foobar.txt', parents=[c1_oid]) path = '/repo/{}/compare/{}..{}'.format(self.repo_path, c1_oid, c2_oid) resp = self.app.get(path) self.assertIn('-foo', resp.body) self.assertIn('+bar', resp.body)
def test_repo_diff_non_unicode_commits(self): """Ensure non utf-8 chars are handled but stripped from diff.""" factory = RepoFactory(self.repo_store) message = 'not particularly sensible latin-1: \xe9\xe9\xe9.' oid = factory.add_commit(message, 'foo.py') oid2 = factory.add_commit('a sensible commit message', 'foo.py', [oid]) resp = self.app.get('/repo/{}/compare/{}..{}'.format( self.repo_path, oid, oid2)) self.assertEqual(resp.json['commits'][0]['message'], message.decode('utf-8', 'replace'))
def test_repo_detect_merges_missing_source(self): """A non-existent source commit is ignored.""" factory = RepoFactory(self.repo_store) # A---B a = factory.add_commit('a\n', 'file') b = factory.add_commit('b\n', 'file', parents=[a]) resp = self.app.post_json('/repo/{}/detect-merges/{}'.format( self.repo_path, b), {'sources': [factory.nonexistent_oid()]}) self.assertEqual(200, resp.status_code) self.assertEqual({}, resp.json)
def test_repo_get_unicode_log(self): factory = RepoFactory(self.repo_store) message = u'나는 김치 사랑'.encode('utf-8') message2 = u'(╯°□°)╯︵ ┻━┻'.encode('utf-8') oid = factory.add_commit(message, '자장면/짜장면.py') oid2 = factory.add_commit(message2, '엄마야!.js', [oid]) resp = self.app.get('/repo/{}/log/{}'.format(self.repo_path, oid2)) self.assertEqual(message2.decode('utf-8', 'replace'), resp.json[0]['message']) self.assertEqual(message.decode('utf-8', 'replace'), resp.json[1]['message'])
def test_cross_repo_diff_invalid_commit(self): """Cross repo diff with an invalid commit returns HTTP 404.""" factory = RepoFactory(self.repo_store) c1 = factory.add_commit('foo', 'foobar.txt') repo2_name = uuid.uuid4().hex RepoFactory( os.path.join(self.repo_root, repo2_name), clone_from=factory) c2 = factory.add_commit('bar', 'foobar.txt', parents=[c1]) resp = self.app.get('/repo/{}:{}/diff/{}:{}'.format( self.repo_path, repo2_name, c2, 'invalid'), expect_errors=True) self.assertEqual(404, resp.status_code)
def test_repo_common_ancestor_diff(self): """Ensure expected changes exist in diff patch.""" repo = RepoFactory(self.repo_store) c1 = repo.add_commit('foo', 'foobar.txt') c2_right = repo.add_commit('bar', 'foobar.txt', parents=[c1]) c3_right = repo.add_commit('baz', 'foobar.txt', parents=[c2_right]) c2_left = repo.add_commit('qux', 'foobar.txt', parents=[c1]) c3_left = repo.add_commit('corge', 'foobar.txt', parents=[c2_left]) resp = self.app.get('/repo/{}/compare/{}...{}'.format( self.repo_path, c3_left, c3_right)) self.assertIn('-foo', resp.json_body['patch']) self.assertIn('+baz', resp.json_body['patch']) self.assertNotIn('+corge', resp.json_body['patch'])
def test_repo_diff_unicode_commits(self): """Ensure expected utf-8 commits objects are returned in diff.""" factory = RepoFactory(self.repo_store) message = u'屋漏偏逢连夜雨'.encode('utf-8') message2 = u'说曹操,曹操到'.encode('utf-8') oid = factory.add_commit(message, 'foo.py') oid2 = factory.add_commit(message2, 'bar.py', [oid]) resp = self.app.get('/repo/{}/compare/{}..{}'.format( self.repo_path, oid, oid2)) self.assertEqual(resp.json['commits'][0]['message'], message.decode('utf-8')) self.assertEqual(resp.json['commits'][1]['message'], message2.decode('utf-8'))
def test_repo_detect_merges_unmerged(self): """An unmerged commit is not returned.""" factory = RepoFactory(self.repo_store) # A---B # \ # C a = factory.add_commit('a\n', 'file') b = factory.add_commit('b\n', 'file', parents=[a]) c = factory.add_commit('c\n', 'file', parents=[a]) resp = self.app.post_json('/repo/{}/detect-merges/{}'.format( self.repo_path, b), {'sources': [c.hex]}) self.assertEqual(200, resp.status_code) self.assertEqual({}, resp.json)
def test_cross_repo_merge_diff(self): """Merge diff can be requested across 2 repositories.""" factory = RepoFactory(self.repo_store) c1 = factory.add_commit('foo', 'foobar.txt') repo2_name = uuid.uuid4().hex factory2 = RepoFactory( os.path.join(self.repo_root, repo2_name), clone_from=factory) c2 = factory.add_commit('bar', 'foobar.txt', parents=[c1]) c3 = factory2.add_commit('baz', 'foobar.txt', parents=[c1]) resp = self.app.get('/repo/{}:{}/compare-merge/{}:{}'.format( self.repo_path, repo2_name, c2, c3)) self.assertIn('-bar', resp.json['patch'])
def test_repo_detect_merges_pulled(self): """Commits that were pulled (fast-forward) are their own merge points.""" factory = RepoFactory(self.repo_store) # A---B---C a = factory.add_commit('a\n', 'file') b = factory.add_commit('b\n', 'file', parents=[a]) c = factory.add_commit('c\n', 'file', parents=[b]) # The start commit would never be the source of a merge proposal, # but include it anyway to test boundary conditions. resp = self.app.post_json('/repo/{}/detect-merges/{}'.format( self.repo_path, c), {'sources': [a.hex, b.hex, c.hex]}) self.assertEqual(200, resp.status_code) self.assertEqual({a.hex: a.hex, b.hex: b.hex, c.hex: c.hex}, resp.json)
def test_repo_diff_merge_with_conflicts(self): """Ensure that compare-merge returns conflicts information.""" repo = RepoFactory(self.repo_store) c1 = repo.add_commit('foo\n', 'blah.txt') c2_left = repo.add_commit('foo\nbar\n', 'blah.txt', parents=[c1]) c2_right = repo.add_commit('foo\nbaz\n', 'blah.txt', parents=[c1]) resp = self.app.get('/repo/{}/compare-merge/{}:{}'.format( self.repo_path, c2_left, c2_right)) self.assertIn(dedent("""\ +<<<<<<< blah.txt bar +======= +baz +>>>>>>> blah.txt """), resp.json_body['patch']) self.assertEqual(['blah.txt'], resp.json_body['conflicts'])
def test_repo_alternates_objects_shared(self): """Ensure objects are shared from alternate repo.""" factory = RepoFactory(self.repo_path) commit_oid = factory.add_commit("foo", "foobar.txt") new_repo_path = os.path.join(self.repo_store, uuid.uuid4().hex) repo_path_with_alt = store.init_repo(new_repo_path, alternate_repo_paths=[factory.repo.path]) repo_with_alt = open_repo(repo_path_with_alt) self.assertEqual(commit_oid.hex, repo_with_alt.get(commit_oid).hex)
def test_repo_get_non_unicode_log(self): """Ensure that non-unicode data is discarded.""" factory = RepoFactory(self.repo_store) message = '\xe9\xe9\xe9' # latin-1 oid = factory.add_commit(message, 'foo.py') resp = self.app.get('/repo/{}/log/{}'.format(self.repo_path, oid)) self.assertEqual(message.decode('utf-8', 'replace'), resp.json[0]['message'])
def test_repo_diff_merge_empty(self): """Ensure that diffing two identical commits returns an empty string as the patch, not None.""" repo = RepoFactory(self.repo_store) c1 = repo.add_commit('foo\n', 'blah.txt') resp = self.app.get('/repo/{}/compare-merge/{}:{}'.format( self.repo_path, c1, c1)) self.assertEqual('', resp.json_body['patch'])
def test_repo_get_unicode_ref(self): factory = RepoFactory(self.repo_store) commit_oid = factory.add_commit('foo', 'foobar.txt') tag_name = u'☃'.encode('utf-8') tag_message = u'☃'.encode('utf-8') factory.add_tag(tag_name, tag_message, commit_oid) tag = 'refs/tags/{}'.format(tag_name) resp = self.get_ref(tag) self.assertTrue(tag.decode('utf-8') in resp)
def test_repo_repack_verify_commits_to_pack(self): """Ensure commits in different packs exist in merged pack.""" factory = RepoFactory(self.repo_store) oid = factory.add_commit('foo', 'foobar.txt') with chdir(factory.pack_dir): subprocess.call(['git', 'gc', '-q']) # pack first commit oid2 = factory.add_commit('bar', 'foobar.txt', [oid]) p = subprocess.Popen(['git', 'pack-objects', '-q', 'pack2'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.communicate(input=oid2.hex) self.assertEqual(2, len(factory.packs)) # ensure 2 packs exist self.app.post_json('/repo/{}/repack'.format(self.repo_path), {'prune': True, 'single': True}) self.assertEqual(1, len(factory.packs)) repacked_pack = os.path.join(factory.pack_dir, factory.packs[0]) out = subprocess.check_output(['git', 'verify-pack', repacked_pack, '-v']) self.assertIn(oid.hex, out) self.assertIn(oid2.hex, out)
def test_repo_diff_merge(self): """Ensure expected changes exist in diff patch.""" repo = RepoFactory(self.repo_store) c1 = repo.add_commit('foo\nbar\nbaz\n', 'blah.txt') c2_right = repo.add_commit('quux\nbar\nbaz\n', 'blah.txt', parents=[c1]) c3_right = repo.add_commit('quux\nbar\nbaz\n', 'blah.txt', parents=[c2_right]) c2_left = repo.add_commit('foo\nbar\nbar\n', 'blah.txt', parents=[c1]) c3_left = repo.add_commit('foo\nbar\nbar\n', 'blah.txt', parents=[c2_left]) resp = self.app.get('/repo/{}/compare-merge/{}:{}'.format( self.repo_path, c3_right, c3_left)) self.assertIn(' quux', resp.json_body['patch']) self.assertIn('-baz', resp.json_body['patch']) self.assertIn('+bar', resp.json_body['patch']) self.assertNotIn('foo', resp.json_body['patch']) self.assertEqual([], resp.json_body['conflicts'])
def test_repo_get_commit(self): factory = RepoFactory(self.repo_store) message = 'Computers make me angry.' commit_oid = factory.add_commit(message, 'foobar.txt') resp = self.app.get('/repo/{}/commits/{}'.format( self.repo_path, commit_oid.hex)) commit_resp = resp.json self.assertEqual(commit_oid.hex, commit_resp['sha1']) self.assertEqual(message, commit_resp['message'])
def test_ignore_non_unicode_refs(self): """Ensure non-unicode refs are dropped from ref collection.""" factory = RepoFactory(self.repo_store) commit_oid = factory.add_commit('foo', 'foobar.txt') tag = '\xe9\xe9\xe9' # latin-1 tag_message = 'tag message' factory.add_tag(tag, tag_message, commit_oid) resp = self.app.get('/repo/{}/refs'.format(self.repo_path)) refs = resp.json self.assertEqual(1, len(refs.keys()))
def test_allow_unicode_refs(self): """Ensure unicode refs are included in ref collection.""" factory = RepoFactory(self.repo_store) commit_oid = factory.add_commit('foo', 'foobar.txt') tag = u'おいしいイカ'.encode('utf-8') tag_message = u'かわいい タコ'.encode('utf-8') factory.add_tag(tag, tag_message, commit_oid) resp = self.app.get('/repo/{}/refs'.format(self.repo_path)) refs = resp.json self.assertEqual(2, len(refs.keys()))
def test_repo_repack_verify_pack(self): """Ensure commit exists in pack.""" factory = RepoFactory(self.repo_store) oid = factory.add_commit('foo', 'foobar.txt') resp = self.app.post_json('/repo/{}/repack'.format(self.repo_path), {'prune': True, 'single': True}) for filename in factory.packs: pack = os.path.join(factory.pack_dir, filename) out = subprocess.check_output(['git', 'verify-pack', pack, '-v']) self.assertEqual(200, resp.status_code) self.assertIn(oid.hex, out)
def test_repo_get_log_signatures(self): """Ensure signatures are correct.""" factory = RepoFactory(self.repo_store) committer = factory.makeSignature(u'村上 春樹'.encode('utf-8'), u'tsukuru@猫の町.co.jp'.encode('utf-8'), encoding='utf-8') author = factory.makeSignature( u'Владимир Владимирович Набоков'.encode('utf-8'), u'Набоко@zembla.ru'.encode('utf-8'), encoding='utf-8') oid = factory.add_commit('Obfuscate colophon.', 'path.foo', author=author, committer=committer) resp = self.app.get('/repo/{}/log/{}'.format(self.repo_path, oid)) self.assertEqual(author.name, resp.json[0]['author']['name'])
def test_repo_detect_merges_merged(self): """Commits that were merged have sensible merge points.""" factory = RepoFactory(self.repo_store) # A---C---D---G---H # \ / / # B---E---F---I a = factory.add_commit('a\n', 'file') b = factory.add_commit('b\n', 'file', parents=[a]) c = factory.add_commit('c\n', 'file', parents=[a, b]) d = factory.add_commit('d\n', 'file', parents=[c]) e = factory.add_commit('e\n', 'file', parents=[b]) f = factory.add_commit('f\n', 'file', parents=[e]) g = factory.add_commit('g\n', 'file', parents=[d, f]) h = factory.add_commit('h\n', 'file', parents=[g]) i = factory.add_commit('i\n', 'file', parents=[f]) resp = self.app.post_json('/repo/{}/detect-merges/{}'.format( self.repo_path, h), {'sources': [b.hex, e.hex, i.hex]}) self.assertEqual(200, resp.status_code) self.assertEqual({b.hex: c.hex, e.hex: g.hex}, resp.json)