Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1 | # Copyright 2015 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from zuul import change_matcher as cm |
| 16 | from zuul import model |
| 17 | |
| 18 | from tests.base import BaseTestCase |
| 19 | |
| 20 | |
| 21 | class BaseTestMatcher(BaseTestCase): |
| 22 | |
| 23 | project = 'project' |
| 24 | |
| 25 | def setUp(self): |
| 26 | super(BaseTestMatcher, self).setUp() |
| 27 | self.change = model.Change(self.project) |
| 28 | |
| 29 | |
| 30 | class TestAbstractChangeMatcher(BaseTestMatcher): |
| 31 | |
| 32 | def test_str(self): |
| 33 | matcher = cm.ProjectMatcher(self.project) |
| 34 | self.assertEqual(str(matcher), '{ProjectMatcher:project}') |
| 35 | |
| 36 | def test_repr(self): |
| 37 | matcher = cm.ProjectMatcher(self.project) |
| 38 | self.assertEqual(repr(matcher), '<ProjectMatcher project>') |
| 39 | |
| 40 | |
| 41 | class TestProjectMatcher(BaseTestMatcher): |
| 42 | |
| 43 | def test_matches_returns_true(self): |
| 44 | matcher = cm.ProjectMatcher(self.project) |
| 45 | self.assertTrue(matcher.matches(self.change)) |
| 46 | |
| 47 | def test_matches_returns_false(self): |
| 48 | matcher = cm.ProjectMatcher('not_project') |
| 49 | self.assertFalse(matcher.matches(self.change)) |
| 50 | |
| 51 | |
| 52 | class TestBranchMatcher(BaseTestMatcher): |
| 53 | |
| 54 | def setUp(self): |
| 55 | super(TestBranchMatcher, self).setUp() |
| 56 | self.matcher = cm.BranchMatcher('foo') |
| 57 | |
| 58 | def test_matches_returns_true_on_matching_branch(self): |
| 59 | self.change.branch = 'foo' |
| 60 | self.assertTrue(self.matcher.matches(self.change)) |
| 61 | |
| 62 | def test_matches_returns_true_on_matching_ref(self): |
| 63 | self.change.branch = 'bar' |
| 64 | self.change.ref = 'foo' |
| 65 | self.assertTrue(self.matcher.matches(self.change)) |
| 66 | |
| 67 | def test_matches_returns_false_for_no_match(self): |
| 68 | self.change.branch = 'bar' |
| 69 | self.change.ref = 'baz' |
| 70 | self.assertFalse(self.matcher.matches(self.change)) |
| 71 | |
| 72 | def test_matches_returns_false_for_missing_attrs(self): |
| 73 | delattr(self.change, 'branch') |
| 74 | # ref is by default not an attribute |
| 75 | self.assertFalse(self.matcher.matches(self.change)) |
| 76 | |
| 77 | |
| 78 | class TestFileMatcher(BaseTestMatcher): |
| 79 | |
| 80 | def setUp(self): |
| 81 | super(TestFileMatcher, self).setUp() |
| 82 | self.matcher = cm.FileMatcher('filename') |
| 83 | |
| 84 | def test_matches_returns_true(self): |
| 85 | self.change.files = ['filename'] |
| 86 | self.assertTrue(self.matcher.matches(self.change)) |
| 87 | |
| 88 | def test_matches_returns_false_when_no_files(self): |
| 89 | self.assertFalse(self.matcher.matches(self.change)) |
| 90 | |
| 91 | def test_matches_returns_false_when_files_attr_missing(self): |
| 92 | delattr(self.change, 'files') |
| 93 | self.assertFalse(self.matcher.matches(self.change)) |
| 94 | |
| 95 | |
| 96 | class TestAbstractMatcherCollection(BaseTestMatcher): |
| 97 | |
| 98 | def test_str(self): |
| 99 | matcher = cm.MatchAll([cm.FileMatcher('foo')]) |
| 100 | self.assertEqual(str(matcher), '{MatchAll:{FileMatcher:foo}}') |
| 101 | |
| 102 | def test_repr(self): |
| 103 | matcher = cm.MatchAll([]) |
| 104 | self.assertEqual(repr(matcher), '<MatchAll>') |
| 105 | |
| 106 | |
| 107 | class TestMatchAllFiles(BaseTestMatcher): |
| 108 | |
| 109 | def setUp(self): |
| 110 | super(TestMatchAllFiles, self).setUp() |
| 111 | self.matcher = cm.MatchAllFiles([cm.FileMatcher('^docs/.*$')]) |
| 112 | |
| 113 | def _test_matches(self, expected, files=None): |
| 114 | if files is not None: |
| 115 | self.change.files = files |
| 116 | self.assertEqual(expected, self.matcher.matches(self.change)) |
| 117 | |
| 118 | def test_matches_returns_false_when_files_attr_missing(self): |
| 119 | delattr(self.change, 'files') |
| 120 | self._test_matches(False) |
| 121 | |
| 122 | def test_matches_returns_false_when_no_files(self): |
| 123 | self._test_matches(False) |
| 124 | |
| 125 | def test_matches_returns_false_when_not_all_files_match(self): |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 126 | self._test_matches(False, files=['/COMMIT_MSG', 'docs/foo', 'foo/bar']) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 127 | |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 128 | def test_matches_returns_false_when_commit_message_matches(self): |
| 129 | self._test_matches(False, files=['/COMMIT_MSG']) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 130 | |
| 131 | def test_matches_returns_true_when_all_files_match(self): |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 132 | self._test_matches(True, files=['/COMMIT_MSG', 'docs/foo']) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | class TestMatchAll(BaseTestMatcher): |
| 136 | |
| 137 | def test_matches_returns_true(self): |
| 138 | matcher = cm.MatchAll([cm.ProjectMatcher(self.project)]) |
| 139 | self.assertTrue(matcher.matches(self.change)) |
| 140 | |
| 141 | def test_matches_returns_false_for_missing_matcher(self): |
| 142 | matcher = cm.MatchAll([cm.ProjectMatcher('not_project')]) |
| 143 | self.assertFalse(matcher.matches(self.change)) |
| 144 | |
| 145 | |
| 146 | class TestMatchAny(BaseTestMatcher): |
| 147 | |
| 148 | def test_matches_returns_true(self): |
| 149 | matcher = cm.MatchAny([cm.ProjectMatcher(self.project)]) |
| 150 | self.assertTrue(matcher.matches(self.change)) |
| 151 | |
| 152 | def test_matches_returns_false(self): |
| 153 | matcher = cm.MatchAny([cm.ProjectMatcher('not_project')]) |
| 154 | self.assertFalse(matcher.matches(self.change)) |