blob: e8bc47b86c4430edd8dc474505f756ee98a7c1dc [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Classes and functions that implement command-line-like issue updates."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12import unittest
13
14from features import commands
15from framework import framework_constants
16from proto import tracker_pb2
17from services import service_manager
18from testing import fake
19from tracker import tracker_bizobj
20from tracker import tracker_constants
21
22
23class CommandsTest(unittest.TestCase):
24
25 def VerifyParseQuickEditCommmand(
26 self, cmd, exp_summary='sum', exp_status='New', exp_owner_id=111,
27 exp_cc_ids=None, exp_labels=None):
28
29 issue = tracker_pb2.Issue()
30 issue.project_name = 'proj'
31 issue.local_id = 1
32 issue.summary = 'sum'
33 issue.status = 'New'
34 issue.owner_id = 111
35 issue.cc_ids.extend([222, 333])
36 issue.labels.extend(['Type-Defect', 'Priority-Medium', 'Hot'])
37
38 if exp_cc_ids is None:
39 exp_cc_ids = [222, 333]
40 if exp_labels is None:
41 exp_labels = ['Type-Defect', 'Priority-Medium', 'Hot']
42
43 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
44 logged_in_user_id = 999
45 services = service_manager.Services(
46 config=fake.ConfigService(),
47 issue=fake.IssueService(),
48 user=fake.UserService())
49 services.user.TestAddUser('jrobbins', 333)
50 services.user.TestAddUser('jrobbins@jrobbins.org', 888)
51
52 cnxn = 'fake cnxn'
53 (summary, status, owner_id, cc_ids,
54 labels) = commands.ParseQuickEditCommand(
55 cnxn, cmd, issue, config, logged_in_user_id, services)
56 self.assertEqual(exp_summary, summary)
57 self.assertEqual(exp_status, status)
58 self.assertEqual(exp_owner_id, owner_id)
59 self.assertListEqual(exp_cc_ids, cc_ids)
60 self.assertListEqual(exp_labels, labels)
61
62 def testParseQuickEditCommmand_Empty(self):
63 self.VerifyParseQuickEditCommmand('') # Nothing should change.
64
65 def testParseQuickEditCommmand_BuiltInFields(self):
66 self.VerifyParseQuickEditCommmand(
67 'status=Fixed', exp_status='Fixed')
68 self.VerifyParseQuickEditCommmand( # Normalized capitalization.
69 'status=fixed', exp_status='Fixed')
70 self.VerifyParseQuickEditCommmand(
71 'status=limbo', exp_status='limbo')
72
73 self.VerifyParseQuickEditCommmand(
74 'owner=me', exp_owner_id=999)
75 self.VerifyParseQuickEditCommmand(
76 'owner=jrobbins@jrobbins.org', exp_owner_id=888)
77 self.VerifyParseQuickEditCommmand(
78 'owner=----', exp_owner_id=framework_constants.NO_USER_SPECIFIED)
79
80 self.VerifyParseQuickEditCommmand(
81 'summary=JustOneWord', exp_summary='JustOneWord')
82 self.VerifyParseQuickEditCommmand(
83 'summary="quoted sentence"', exp_summary='quoted sentence')
84 self.VerifyParseQuickEditCommmand(
85 "summary='quoted sentence'", exp_summary='quoted sentence')
86
87 self.VerifyParseQuickEditCommmand(
88 'cc=me', exp_cc_ids=[222, 333, 999])
89 self.VerifyParseQuickEditCommmand(
90 'cc=jrobbins@jrobbins.org', exp_cc_ids=[222, 333, 888])
91 self.VerifyParseQuickEditCommmand(
92 'cc=me,jrobbins@jrobbins.org',
93 exp_cc_ids=[222, 333, 999, 888])
94 self.VerifyParseQuickEditCommmand(
95 'cc=-jrobbins,jrobbins@jrobbins.org',
96 exp_cc_ids=[222, 888])
97
98 def testParseQuickEditCommmand_Labels(self):
99 self.VerifyParseQuickEditCommmand(
100 'Priority=Low', exp_labels=['Type-Defect', 'Hot', 'Priority-Low'])
101 self.VerifyParseQuickEditCommmand(
102 'priority=low', exp_labels=['Type-Defect', 'Hot', 'Priority-Low'])
103 self.VerifyParseQuickEditCommmand(
104 'priority-low', exp_labels=['Type-Defect', 'Hot', 'Priority-Low'])
105 self.VerifyParseQuickEditCommmand(
106 '-priority-low', exp_labels=['Type-Defect', 'Priority-Medium', 'Hot'])
107 self.VerifyParseQuickEditCommmand(
108 '-priority-medium', exp_labels=['Type-Defect', 'Hot'])
109
110 self.VerifyParseQuickEditCommmand(
111 'Cold', exp_labels=['Type-Defect', 'Priority-Medium', 'Hot', 'Cold'])
112 self.VerifyParseQuickEditCommmand(
113 '+Cold', exp_labels=['Type-Defect', 'Priority-Medium', 'Hot', 'Cold'])
114 self.VerifyParseQuickEditCommmand(
115 '-Hot Cold', exp_labels=['Type-Defect', 'Priority-Medium', 'Cold'])
116 self.VerifyParseQuickEditCommmand(
117 '-Hot', exp_labels=['Type-Defect', 'Priority-Medium'])
118
119 def testParseQuickEditCommmand_Multiple(self):
120 self.VerifyParseQuickEditCommmand(
121 'Priority=Low -hot owner:me cc:-jrobbins summary="other summary"',
122 exp_summary='other summary', exp_owner_id=999,
123 exp_cc_ids=[222], exp_labels=['Type-Defect', 'Priority-Low'])
124
125 def testBreakCommandIntoParts_Empty(self):
126 self.assertListEqual(
127 [],
128 commands._BreakCommandIntoParts(''))
129
130 def testBreakCommandIntoParts_Single(self):
131 self.assertListEqual(
132 [('summary', 'new summary')],
133 commands._BreakCommandIntoParts('summary="new summary"'))
134 self.assertListEqual(
135 [('summary', 'OneWordSummary')],
136 commands._BreakCommandIntoParts('summary=OneWordSummary'))
137 self.assertListEqual(
138 [('key', 'value')],
139 commands._BreakCommandIntoParts('key=value'))
140 self.assertListEqual(
141 [('key', 'value-with-dashes')],
142 commands._BreakCommandIntoParts('key=value-with-dashes'))
143 self.assertListEqual(
144 [('key', 'value')],
145 commands._BreakCommandIntoParts('key:value'))
146 self.assertListEqual(
147 [('key', 'value')],
148 commands._BreakCommandIntoParts(' key:value '))
149 self.assertListEqual(
150 [('key', 'value')],
151 commands._BreakCommandIntoParts('key:"value"'))
152 self.assertListEqual(
153 [('key', 'user@dom.com')],
154 commands._BreakCommandIntoParts('key:user@dom.com'))
155 self.assertListEqual(
156 [('key', 'a@dom.com,-b@dom.com')],
157 commands._BreakCommandIntoParts('key:a@dom.com,-b@dom.com'))
158 self.assertListEqual(
159 [(None, 'label')],
160 commands._BreakCommandIntoParts('label'))
161 self.assertListEqual(
162 [(None, '-label')],
163 commands._BreakCommandIntoParts('-label'))
164 self.assertListEqual(
165 [(None, '+label')],
166 commands._BreakCommandIntoParts('+label'))
167
168 def testBreakCommandIntoParts_Multiple(self):
169 self.assertListEqual(
170 [('summary', 'new summary'), (None, 'Hot'), (None, '-Cold'),
171 ('owner', 'me'), ('cc', '+a,-b')],
172 commands._BreakCommandIntoParts(
173 'summary="new summary" Hot -Cold owner:me cc:+a,-b'))
174
175
176class CommandSyntaxParsingTest(unittest.TestCase):
177
178 def setUp(self):
179 self.services = service_manager.Services(
180 project=fake.ProjectService(),
181 config=fake.ConfigService(),
182 user=fake.UserService())
183
184 self.services.project.TestAddProject('proj', owner_ids=[111])
185 self.services.user.TestAddUser('a@example.com', 222)
186
187 cnxn = 'fake connection'
188 config = self.services.config.GetProjectConfig(cnxn, 789)
189
190 for status in ['New', 'ReadyForReview']:
191 config.well_known_statuses.append(tracker_pb2.StatusDef(
192 status=status))
193
194 for label in ['Prioity-Low', 'Priority-High']:
195 config.well_known_labels.append(tracker_pb2.LabelDef(
196 label=label))
197
198 config.exclusive_label_prefixes.extend(
199 tracker_constants.DEFAULT_EXCL_LABEL_PREFIXES)
200
201 self.services.config.StoreConfig(cnxn, config)
202
203 def testStandardizeStatus(self):
204 config = self.services.config.GetProjectConfig('fake cnxn', 789)
205 self.assertEqual('New',
206 commands._StandardizeStatus('NEW', config))
207 self.assertEqual('New',
208 commands._StandardizeStatus('n$Ew ', config))
209 self.assertEqual(
210 'custom-label',
211 commands._StandardizeLabel('custom=label ', config))
212
213 def testStandardizeLabel(self):
214 config = self.services.config.GetProjectConfig('fake cnxn', 789)
215 self.assertEqual(
216 'Priority-High',
217 commands._StandardizeLabel('priority-high', config))
218 self.assertEqual(
219 'Priority-High',
220 commands._StandardizeLabel('PRIORITY=HIGH', config))
221
222 def testLookupMeOrUsername(self):
223 self.assertEqual(
224 123,
225 commands._LookupMeOrUsername('fake cnxn', 'me', self.services, 123))
226
227 self.assertEqual(
228 222,
229 commands._LookupMeOrUsername(
230 'fake cnxn', 'a@example.com', self.services, 0))