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