blob: 3d5345f5a9342b1da35c11a942f614099f0b9007 [file] [log] [blame]
Maru Newby3fe5f852015-01-13 04:22:14 +00001# 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
15from zuul import change_matcher as cm
16from zuul import model
17
18from tests.base import BaseTestCase
19
20
21class 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
30class 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
41class 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
52class 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):
David Shrewsburyf6dc1762017-10-02 13:34:37 -040063 delattr(self.change, 'branch')
Maru Newby3fe5f852015-01-13 04:22:14 +000064 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
Maru Newby3fe5f852015-01-13 04:22:14 +000072
73class TestFileMatcher(BaseTestMatcher):
74
75 def setUp(self):
76 super(TestFileMatcher, self).setUp()
77 self.matcher = cm.FileMatcher('filename')
78
79 def test_matches_returns_true(self):
80 self.change.files = ['filename']
81 self.assertTrue(self.matcher.matches(self.change))
82
83 def test_matches_returns_false_when_no_files(self):
84 self.assertFalse(self.matcher.matches(self.change))
85
86 def test_matches_returns_false_when_files_attr_missing(self):
87 delattr(self.change, 'files')
88 self.assertFalse(self.matcher.matches(self.change))
89
90
91class TestAbstractMatcherCollection(BaseTestMatcher):
92
93 def test_str(self):
94 matcher = cm.MatchAll([cm.FileMatcher('foo')])
95 self.assertEqual(str(matcher), '{MatchAll:{FileMatcher:foo}}')
96
97 def test_repr(self):
98 matcher = cm.MatchAll([])
99 self.assertEqual(repr(matcher), '<MatchAll>')
100
101
102class TestMatchAllFiles(BaseTestMatcher):
103
104 def setUp(self):
105 super(TestMatchAllFiles, self).setUp()
106 self.matcher = cm.MatchAllFiles([cm.FileMatcher('^docs/.*$')])
107
108 def _test_matches(self, expected, files=None):
109 if files is not None:
110 self.change.files = files
111 self.assertEqual(expected, self.matcher.matches(self.change))
112
113 def test_matches_returns_false_when_files_attr_missing(self):
114 delattr(self.change, 'files')
115 self._test_matches(False)
116
117 def test_matches_returns_false_when_no_files(self):
118 self._test_matches(False)
119
120 def test_matches_returns_false_when_not_all_files_match(self):
Alexander Evseevdbe6fab2015-11-19 12:46:34 +0300121 self._test_matches(False, files=['/COMMIT_MSG', 'docs/foo', 'foo/bar'])
Maru Newby3fe5f852015-01-13 04:22:14 +0000122
Jan Hruban570d01c2016-03-10 21:51:32 +0100123 def test_matches_returns_true_when_single_file_does_not_match(self):
124 self._test_matches(True, files=['docs/foo'])
125
Alexander Evseevdbe6fab2015-11-19 12:46:34 +0300126 def test_matches_returns_false_when_commit_message_matches(self):
127 self._test_matches(False, files=['/COMMIT_MSG'])
Maru Newby3fe5f852015-01-13 04:22:14 +0000128
129 def test_matches_returns_true_when_all_files_match(self):
Alexander Evseevdbe6fab2015-11-19 12:46:34 +0300130 self._test_matches(True, files=['/COMMIT_MSG', 'docs/foo'])
Maru Newby3fe5f852015-01-13 04:22:14 +0000131
Jan Hruban570d01c2016-03-10 21:51:32 +0100132 def test_matches_returns_true_when_single_file_matches(self):
133 self._test_matches(True, files=['docs/foo'])
134
Maru Newby3fe5f852015-01-13 04:22:14 +0000135
136class TestMatchAll(BaseTestMatcher):
137
138 def test_matches_returns_true(self):
139 matcher = cm.MatchAll([cm.ProjectMatcher(self.project)])
140 self.assertTrue(matcher.matches(self.change))
141
142 def test_matches_returns_false_for_missing_matcher(self):
143 matcher = cm.MatchAll([cm.ProjectMatcher('not_project')])
144 self.assertFalse(matcher.matches(self.change))
145
146
147class TestMatchAny(BaseTestMatcher):
148
149 def test_matches_returns_true(self):
150 matcher = cm.MatchAny([cm.ProjectMatcher(self.project)])
151 self.assertTrue(matcher.matches(self.change))
152
153 def test_matches_returns_false(self):
154 matcher = cm.MatchAny([cm.ProjectMatcher('not_project')])
155 self.assertFalse(matcher.matches(self.change))