-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
setup_unittest.py
224 lines (199 loc) · 9.95 KB
/
setup_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright (C) 2021-2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import logging
import os
import sys
from webkitcorepy import Editor, OutputCapture, testing, mocks as wkmocks
from webkitcorepy.mocks import Terminal as MockTerminal
from webkitscmpy import local, program, mocks
class TestSetup(testing.PathTestCase):
basepath = 'mock/repository'
def setUp(self):
super(TestSetup, self).setUp()
os.mkdir(os.path.join(self.path, '.git'))
os.mkdir(os.path.join(self.path, '.svn'))
def test_svn(self):
with OutputCapture(level=logging.INFO) as captured, mocks.local.Git(), mocks.local.Svn(self.path):
self.assertEqual(1, program.main(
args=('setup', '-v'),
path=self.path,
))
self.assertEqual(captured.stderr.getvalue(), 'No setup required for {}\n'.format(self.path))
def test_none(self):
with OutputCapture(level=logging.INFO) as captured, mocks.local.Git(), mocks.local.Svn():
self.assertEqual(1, program.main(
args=('setup', '-v'),
path=self.path,
))
self.assertEqual(captured.stderr.getvalue(), 'No setup required for ?\n')
def test_github(self):
with OutputCapture(level=logging.INFO) as captured, MockTerminal.input('y'), mocks.remote.GitHub() as remote:
self.assertEqual(0, program.main(
args=('-C', 'https://{}'.format(remote.remote), 'setup', '-v'),
path=self.path,
))
self.assertEqual(
captured.stdout.getvalue(),
"Create a private fork of 'WebKit/WebKit' named 'WebKit' belonging to 'username' ([Yes]/No): \n"
'Setup succeeded!\n',
)
self.assertEqual(captured.stderr.getvalue(), '')
self.assertEqual(
captured.root.log.getvalue(),
'''Saving GitHub credentials in system credential store...
GitHub credentials saved via Keyring!
Verifying user owned fork...
Created a private fork of 'WebKit' belonging to 'username'!
''',
)
def test_git(self):
self.maxDiff = None
with OutputCapture(level=logging.INFO) as captured, mocks.local.Git(self.path) as repo, \
mocks.local.Svn(), wkmocks.Environment(EMAIL_ADDRESS='', SVN_LOG_EDITOR='xed -w'):
self.assertEqual(0, program.main(
args=('setup', '--defaults', '-v'),
path=self.path,
))
config = repo.config()
self.assertEqual('xed -w', config.get('core.editor', ''))
self.assertEqual('^[-+@a-zA-Z_].*$', config.get('diff.objcpp.xfuncname', ''))
self.assertEqual('^[@a-zA-Z_].*$', config.get('diff.objcppheader.xfuncname', ''))
self.assertEqual('auto', config.get('color.status', ''))
self.assertEqual('auto', config.get('color.diff', ''))
self.assertEqual('auto', config.get('color.branch', ''))
self.assertEqual('true', config.get('pull.rebase', ''))
self.assertEqual(
captured.stdout.getvalue(),
'For detailed information about the options configured by this script, please see:\n'
'https://github.com/WebKit/WebKit/wiki/Git-Config#Configuration-Options\n\n\n'
'Setup succeeded!\n',
)
self.assertEqual(captured.stderr.getvalue(), '')
self.assertEqual(
captured.root.log.getvalue(),
'''Setting git user email for {repository}...
Skipped setting email to 'tapple@webkit.org', it's already set for this repository
Setting git user name for {repository}...
Skipped setting name to 'Tim Apple', it's already set for this repository
No project git config found, continuing
Setting better Objective-C diffing behavior for this repository...
Set better Objective-C diffing behavior for this repository!
Using a rebase merge strategy for this repository
Setting git editor for {repository}...
Setting contents of 'SVN_LOG_EDITOR' as editor
Set git editor to 'SVN_LOG_EDITOR' for this repository
'''.format(repository=self.path),
)
def test_github_checkout(self):
with OutputCapture(level=logging.INFO) as captured, mocks.remote.GitHub() as remote, \
MockTerminal.input('n', 'n', 'committer@webkit.org', 'n', 'Committer', 's', 'overwrite', 'y', 'disabled', '1', 'y'), \
mocks.local.Git(self.path, remote='https://{}.git'.format(remote.remote)) as repo, \
wkmocks.Environment(EMAIL_ADDRESS='', SVN_LOG_EDITOR=''):
self.assertEqual('https://github.example.com/WebKit/WebKit.git', local.Git(self.path).url())
self.assertEqual(0, program.main(
args=('setup', '-v', '-a'),
path=self.path,
))
config = repo.config()
self.assertNotIn('color.status', config)
self.assertEqual('Committer', config.get('user.name', ''))
self.assertEqual('committer@webkit.org', config.get('user.email', ''))
self.assertEqual('!f()', config.get('credential.https://github.example.com.helper', '').split()[0])
self.assertEqual('https://github.example.com/WebKit/WebKit.git', local.Git(self.path).url())
programs = ['default'] + [p.name for p in Editor.programs()]
self.assertEqual(
captured.stdout.getvalue(),
'''For detailed information about the options configured by this script, please see:
https://github.com/WebKit/WebKit/wiki/Git-Config#Configuration-Options
Would you like to open this URL in your browser? ([Yes]/No):
Set 'tapple@webkit.org' as the git user email for this repository ([Yes]/No):
Enter git user email for this repository:
Set 'Tim Apple' as the git user name for this repository ([Yes]/No):
Enter git user name for this repository:
Auto-color status, diff, and branch for this repository? ([Yes]/Skip):
Would you like to automatically rebase your branch when creating or
updating a pull request? (Yes/[No]/Later):
Would you like to create new branches to retain history when you overwrite
a pull request branch? ([when-user-owned]/disabled/always/never):
Pick a commit message editor for this repository:
{}
:
Create a private fork of 'WebKit/WebKit' named 'WebKit' belonging to 'username' ([Yes]/No):
Setup succeeded!
'''.format('\n '.join([
'{}) {}'.format(
count + 1, programs[count] if count else '[{}]'.format(programs[count]),
) for count in range(len(programs))])))
self.assertEqual(captured.stderr.getvalue(), '')
self.assertEqual(
captured.root.log.getvalue(),
'''Setting git user email for {repository}...
Set git user email to 'committer@webkit.org' for this repository
Setting git user name for {repository}...
Set git user name to 'Committer' for this repository
No project git config found, continuing
Setting better Objective-C diffing behavior for this repository...
Set better Objective-C diffing behavior for this repository!
Using a rebase merge strategy for this repository
Setting auto update on PR creation...
Disabled auto update on PR creation
Setting git editor for {repository}...
Using the default git editor for this repository
Saving GitHub credentials in system credential store...
GitHub credentials saved via Keyring!
Verifying user owned fork...
Created a private fork of 'WebKit' belonging to 'username'!
Adding forked remote as 'fork'...
Added remote 'fork'
Fetching 'fork'
'''.format(repository=self.path),
)
def test_commit_message(self):
with OutputCapture(level=logging.INFO), mocks.local.Git(self.path) as git, mocks.local.Svn():
self.assertEqual(0, program.main(
args=('setup', '--defaults', '-v'),
path=self.path,
hooks=os.path.join(os.path.abspath(os.path.dirname(__file__)), 'hooks')
))
pcm = os.path.join(self.path, '.git', 'hooks', 'prepare-commit-msg')
self.assertTrue(os.path.isfile(pcm))
os.rename(pcm, os.path.join(os.path.dirname(pcm), 'prepare_commit_msg.py'))
sys.path.insert(0, os.path.dirname(pcm))
try:
from prepare_commit_msg import main
self.assertEqual(
main(os.path.join(self.path, 'COMMIT_MESSAGE')),
0,
)
with open(os.path.join(self.path, 'COMMIT_MESSAGE'), 'r') as file:
self.assertEqual(
file.read(),
'''Generated commit message
# Please populate the above commit message. Lines starting
# with '#' will be ignored
# 'On branch main
# Your branch is up to date with 'origin/main'.
#
# nothing to commit, working tree clean
''')
finally:
sys.path.remove(os.path.dirname(pcm))