def fake_update(): # this simulates the effect of calling update() on a repo, # mainly that `export` and `log` now works. fake_vcs.log.side_effect = log_results fake_vcs.export.side_effect = None fake_vcs.export.return_value = SAMPLE_DIFF_BYTES fake_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(fake_vcs, id)
def test_file_whitelist(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) option = ProjectOption(project=project, name='build.file-whitelist', value='foo.txt') mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_get_vcs.return_value = mock_vcs db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert not Build.query.first() option.value = 'ci/*' db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_identify_revision.assert_called_once_with(repo, revision.sha) assert Build.query.first()
def test_invalid_config(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) project2 = self.create_project(repository=repo) self.create_plan(project) self.create_plan(project2) mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_vcs.read_file.side_effect = ('{{invalid yaml}}', yaml.safe_dump({ 'build.file-blacklist': ['ci/not-real'], })) mock_get_vcs.return_value = mock_vcs revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert len(list(Build.query)) == 2
def test_file_blacklist(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_vcs.read_file.side_effect = None mock_vcs.read_file.return_value = yaml.safe_dump({ 'build.file-blacklist': ['ci/*'], }) mock_get_vcs.return_value = mock_vcs revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert not Build.query.first() mock_vcs.read_file.return_value = yaml.safe_dump({ 'build.file-blacklist': ['ci/not-real'], }) revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_identify_revision.assert_called_once_with(repo, revision.sha) assert Build.query.first()
def test_correct(self): for (url, expected_name) in [ ('example.com:test.git', 'test.git'), ('example.com:test', 'test'), ('[email protected]:test', 'test'), ('example.com:prefix/test', 'test'), ('example.com:test-with-hyphen', 'test-with-hyphen'), ('example.com:some-prefix/test-with-hyphen', 'test-with-hyphen'), ]: assert Vcs.get_repository_name(url) == expected_name
def test_file_whitelist(self, mock_identify_revision, mock_get_vcs): repo = self.create_repo() revision = self.create_revision(repository=repo) project = self.create_project(repository=repo) self.create_plan(project) option = ProjectOption(project=project, name='build.file-whitelist', value='foo.txt') mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None mock_identify_revision.return_value = revision mock_get_vcs.return_value = mock_vcs db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_vcs.export.assert_called_once_with(revision.sha) assert not Build.query.first() option.value = 'ci/*' db.session.add(option) db.session.flush() revision_created_handler(revision_sha=revision.sha, repository_id=repo.id) mock_identify_revision.assert_called_once_with(repo, revision.sha) assert Build.query.first()
def test_get_changed_files_updates_vcs(self): repo = self.create_repo() sha = uuid4().hex revision = self.create_revision(repository=repo, sha=sha) # No updated needed. with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha, ), {}), ('export', (sha, ), {}), ]) # Successful update with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() # Raise first time, work second time. mock_vcs.export.side_effect = (UnknownRevision("", 1), SAMPLE_DIFF) mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha, ), {}), ('export', (sha, ), {}), ('update', (), {}), ('get_changed_files', (sha, ), {}), ('export', (sha, ), {}), ]) # Unsuccessful update with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() mock_vcs.exists.return_value = True # Revision is always unknown. mock_vcs.export.side_effect = UnknownRevision("", 1) mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files( mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) with self.assertRaises(UnknownRevision): ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha, ), {}), ('export', (sha, ), {}), ('update', (), {}), ('get_changed_files', (sha, ), {}), ('export', (sha, ), {}), ])
class SelectivelyApplyDiffTest(TestCase): PATCH_TEMPLATE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1,1 +1 @@ -FOO +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ BAD_PATCH_TEMPLATE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -0,0 +1 @@ -FOO +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_SOURCE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO \ No newline at end of file +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_TARGET = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO +blah \ No newline at end of file diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_BOTH = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO \ No newline at end of file +blah \ No newline at end of file diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ def setUp(self): self.vcs = Vcs(None, None) def test_simple(self): path = "a.txt" patch = self.PATCH_TEMPLATE.format(path=path) content = self.vcs._selectively_apply_diff(path, "FOO\n", diff=patch) assert content == "blah\n" def test_nested(self): path = "testing/a/b/c/a.txt" patch = self.PATCH_TEMPLATE.format(path=path) content = self.vcs._selectively_apply_diff(path, "FOO\n", diff=patch) assert content == "blah\n" def test_untouched(self): path = "a.txt" patch = self.PATCH_TEMPLATE.format(path="b.txt") content = self.vcs._selectively_apply_diff(path, "FOO\n", diff=patch) assert content == "FOO\n" def test_invalid_diff(self): path = "a.txt" patch = self.BAD_PATCH_TEMPLATE.format(path=path) with pytest.raises(InvalidDiffError): self.vcs._selectively_apply_diff(path, "FOO\n", diff=patch) def test_no_newline_source(self): path = "a.txt" patch = self.PATCH_TEMPLATE_NO_NEWLINE_SOURCE.format(path=path) content = self.vcs._selectively_apply_diff(path, "FOO", diff=patch) assert content == "blah\n" def test_no_newline_target(self): path = "a.txt" patch = self.PATCH_TEMPLATE_NO_NEWLINE_TARGET.format(path=path) content = self.vcs._selectively_apply_diff(path, "FOO\n", diff=patch) assert content == "blah" def test_no_newline_both(self): path = "a.txt" patch = self.PATCH_TEMPLATE_NO_NEWLINE_BOTH.format(path=path) content = self.vcs._selectively_apply_diff(path, "FOO", diff=patch) assert content == "blah"
def setUp(self): self.vcs = Vcs(None, None)
def test_get_changed_files_updates_vcs(self): repo = self.create_repo() sha = uuid4().hex revision = self.create_revision(repository=repo, sha=sha) # No updated needed. with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() mock_vcs.export.side_effect = None mock_vcs.export.return_value = SAMPLE_DIFF mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha,), {}), ('export', (sha,), {}), ]) # Successful update with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() # Raise first time, work second time. mock_vcs.export.side_effect = (UnknownRevision("", 1), SAMPLE_DIFF) mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha,), {}), ('export', (sha,), {}), ('update', (), {}), ('get_changed_files', (sha,), {}), ('export', (sha,), {}), ]) # Unsuccessful update with patch.object(repo, 'get_vcs') as get_vcs: mock_vcs = self.get_fake_vcs() mock_vcs.exists.return_value = True # Revision is always unknown. mock_vcs.export.side_effect = UnknownRevision("", 1) mock_vcs.get_changed_files.side_effect = lambda id: Vcs.get_changed_files(mock_vcs, id) mock_vcs.update.side_effect = None get_vcs.return_value = mock_vcs ct = CommitTrigger(revision) with self.assertRaises(UnknownRevision): ct.get_changed_files() self.assertEqual(list(mock_vcs.method_calls), [ ('exists', (), {}), ('get_changed_files', (sha,), {}), ('export', (sha,), {}), ('update', (), {}), ('get_changed_files', (sha,), {}), ('export', (sha,), {}), ])
class SelectivelyApplyDiffTest(TestCase): PATCH_TEMPLATE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1,1 +1 @@ -FOO +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ BAD_PATCH_TEMPLATE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -0,0 +1 @@ -FOO +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_SOURCE = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO \ No newline at end of file +blah diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_TARGET = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO +blah \ No newline at end of file diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ PATCH_TEMPLATE_NO_NEWLINE_BOTH = """diff --git a/{path} b/{path} index e69de29..d0c77a5 100644 --- a/{path} +++ b/{path} @@ -1 +1 @@ -FOO \ No newline at end of file +blah \ No newline at end of file diff --git a/FOO1 b/FOO1 index e69de29..d0c77a5 100644 --- a/FOO1 +++ b/FOO1 @@ -1,1 +1 @@ -blah +blah """ def setUp(self): self.vcs = Vcs(None, None) def test_simple(self): path = 'a.txt' patch = self.PATCH_TEMPLATE.format(path=path) content = self.vcs._selectively_apply_diff(path, 'FOO\n', diff=patch) assert content == 'blah\n' def test_nested(self): path = 'testing/a/b/c/a.txt' patch = self.PATCH_TEMPLATE.format(path=path) content = self.vcs._selectively_apply_diff(path, 'FOO\n', diff=patch) assert content == 'blah\n' def test_untouched(self): path = 'a.txt' patch = self.PATCH_TEMPLATE.format(path='b.txt') content = self.vcs._selectively_apply_diff(path, 'FOO\n', diff=patch) assert content == 'FOO\n' def test_invalid_diff(self): path = 'a.txt' patch = self.BAD_PATCH_TEMPLATE.format(path=path) with pytest.raises(InvalidDiffError): self.vcs._selectively_apply_diff(path, 'FOO\n', diff=patch) def test_no_newline_source(self): path = 'a.txt' patch = self.PATCH_TEMPLATE_NO_NEWLINE_SOURCE.format(path=path) content = self.vcs._selectively_apply_diff(path, 'FOO', diff=patch) assert content == 'blah\n' def test_no_newline_target(self): path = 'a.txt' patch = self.PATCH_TEMPLATE_NO_NEWLINE_TARGET.format(path=path) content = self.vcs._selectively_apply_diff(path, 'FOO\n', diff=patch) assert content == 'blah' def test_no_newline_both(self): path = 'a.txt' patch = self.PATCH_TEMPLATE_NO_NEWLINE_BOTH.format(path=path) content = self.vcs._selectively_apply_diff(path, 'FOO', diff=patch) assert content == 'blah'