2121# For now we only use a config with a single scheme. We should add support for
2222# handling multiple schemes.
2323MOCK_REMOTE = {
24- ' repo1' : [
24+ " repo1" : [
2525 # This is a series of changes to repo1. (File, NewContents)
26- (' A.txt' , 'A' ),
27- (' B.txt' , 'B' ),
28- (' A.txt' , 'a' ),
26+ (" A.txt" , "A" ),
27+ (" B.txt" , "B" ),
28+ (" A.txt" , "a" ),
2929 ],
30- ' repo2' : [
30+ " repo2" : [
3131 # This is a series of changes to repo2. (File, NewContents)
32- (' X.txt' , 'X' ),
33- (' Y.txt' , 'Y' ),
34- (' X.txt' , 'z' ),
32+ (" X.txt" , "X" ),
33+ (" Y.txt" , "Y" ),
34+ (" X.txt" , "z" ),
3535 ],
3636}
3737
3838MOCK_CONFIG = {
3939 # This is here just b/c we expect it. We should consider consolidating
4040 # clone-patterns into a dictionary where we map protocols (i.e. ['ssh,
4141 # 'https'] to patterns). Then we can define this issue.
42- ' ssh-clone-pattern' : ' DO_NOT_USE' ,
42+ " ssh-clone-pattern" : " DO_NOT_USE" ,
4343 # We reset this value with our remote path when we process
44- ' https-clone-pattern' : '' ,
45- ' repos' : {
46- ' repo1' : {
47- ' remote' : {'id' : ' repo1' },
44+ " https-clone-pattern" : "" ,
45+ " repos" : {
46+ " repo1" : {
47+ " remote" : {"id" : " repo1" },
4848 },
49- ' repo2' : {
50- ' remote' : {'id' : ' repo2' },
49+ " repo2" : {
50+ " remote" : {"id" : " repo2" },
5151 },
5252 },
53- ' default-branch-scheme' : ' main' ,
54- ' branch-schemes' : {
55- ' main' : {
56- ' aliases' : [' main' ],
57- ' repos' : {
58- ' repo1' : ' main' ,
59- ' repo2' : ' main' ,
60- }
53+ " default-branch-scheme" : " main" ,
54+ " branch-schemes" : {
55+ " main" : {
56+ " aliases" : [" main" ],
57+ " repos" : {
58+ " repo1" : " main" ,
59+ " repo2" : " main" ,
60+ },
6161 }
62- }
62+ },
6363}
6464
6565MOCK_ADDITIONAL_SCHEME = {
66- ' branch-schemes' : {
67- ' extra' : {
68- ' aliases' : [' extra' ],
69- ' repos' : {
66+ " branch-schemes" : {
67+ " extra" : {
68+ " aliases" : [" extra" ],
69+ " repos" : {
7070 # Spell this differently just to make it distinguishable in
7171 # test output, even though we only have one branch.
7272 # TODO: Support multiple test branches in the repo instead.
73- ' repo1' : ' refs/heads/main' ,
74- ' repo2' : ' refs/heads/main' ,
75- }
73+ " repo1" : " refs/heads/main" ,
74+ " repo2" : " refs/heads/main" ,
75+ },
7676 }
7777 }
7878}
@@ -85,18 +85,21 @@ def __init__(self, command, returncode, output):
8585 self .output = output
8686
8787 def __str__ (self ):
88- return f"Command returned a non-zero exit status { self .returncode } :\n " \
89- f"Command: { ' ' .join (self .command )} \n " \
90- f"Output: { self .output .decode ('utf-8' )} "
88+ return (
89+ f"Command returned a non-zero exit status { self .returncode } :\n "
90+ f"Command: { ' ' .join (self .command )} \n "
91+ f"Output: { self .output .decode ('utf-8' )} "
92+ )
9193
9294
9395def call_quietly (* args , ** kwargs ):
94- kwargs [' stderr' ] = subprocess .STDOUT
96+ kwargs [" stderr" ] = subprocess .STDOUT
9597 try :
9698 return subprocess .check_output (* args , ** kwargs )
9799 except subprocess .CalledProcessError as e :
98- raise CallQuietlyException (command = e .cmd , returncode = e .returncode ,
99- output = e .stdout ) from e
100+ raise CallQuietlyException (
101+ command = e .cmd , returncode = e .returncode , output = e .stdout
102+ ) from e
100103
101104
102105def create_dir (d ):
@@ -105,75 +108,82 @@ def create_dir(d):
105108
106109
107110def teardown_mock_remote (base_dir ):
108- call_quietly (['rm' , ' -rf' , base_dir ])
111+ call_quietly (["rm" , " -rf" , base_dir ])
109112
110113
111114def get_config_path (base_dir ):
112- return os .path .join (base_dir , ' test-config.json' )
115+ return os .path .join (base_dir , " test-config.json" )
113116
114117
115118def get_additional_config_path (base_dir ):
116- return os .path .join (base_dir , ' test-additional-config.json' )
119+ return os .path .join (base_dir , " test-additional-config.json" )
117120
118121
119122def setup_mock_remote (base_dir , base_config ):
120123 create_dir (base_dir )
121124
122125 # We use local as a workspace for creating commits.
123- LOCAL_PATH = os .path .join (base_dir , ' local' )
126+ LOCAL_PATH = os .path .join (base_dir , " local" )
124127 # We use remote as a directory that simulates our remote unchecked out
125128 # repo.
126- REMOTE_PATH = os .path .join (base_dir , ' remote' )
129+ REMOTE_PATH = os .path .join (base_dir , " remote" )
127130
128131 create_dir (REMOTE_PATH )
129132 create_dir (LOCAL_PATH )
130133
131- for ( k , v ) in MOCK_REMOTE .items ():
134+ for k , v in MOCK_REMOTE .items ():
132135 local_repo_path = os .path .join (LOCAL_PATH , k )
133136 remote_repo_path = os .path .join (REMOTE_PATH , k )
134137 create_dir (remote_repo_path )
135138 create_dir (local_repo_path )
136- call_quietly (['git' , 'init' , '--bare' , remote_repo_path ])
137- call_quietly (['git' , 'symbolic-ref' , 'HEAD' , 'refs/heads/main' ],
138- cwd = remote_repo_path )
139- call_quietly (['git' , 'clone' , '-l' , remote_repo_path , local_repo_path ])
140- call_quietly (['git' , 'config' , '--local' , 'user.name' , 'swift_test' ],
141- cwd = local_repo_path )
142- call_quietly (['git' , 'config' , '--local' , 'user.email' , 'no-reply@swift.org' ],
143- cwd = local_repo_path )
144- call_quietly (['git' , 'config' , '--local' , 'commit.gpgsign' , 'false' ],
145- cwd = local_repo_path )
146- call_quietly (['git' , 'symbolic-ref' , 'HEAD' , 'refs/heads/main' ],
147- cwd = local_repo_path )
148- for (i , (filename , contents )) in enumerate (v ):
139+ call_quietly (["git" , "init" , "--bare" , remote_repo_path ])
140+ call_quietly (
141+ ["git" , "symbolic-ref" , "HEAD" , "refs/heads/main" ], cwd = remote_repo_path
142+ )
143+ call_quietly (["git" , "clone" , "-l" , remote_repo_path , local_repo_path ])
144+ call_quietly (
145+ ["git" , "config" , "--local" , "user.name" , "swift_test" ], cwd = local_repo_path
146+ )
147+ call_quietly (
148+ ["git" , "config" , "--local" , "user.email" , "no-reply@swift.org" ],
149+ cwd = local_repo_path ,
150+ )
151+ call_quietly (
152+ ["git" , "config" , "--local" , "commit.gpgsign" , "false" ], cwd = local_repo_path
153+ )
154+ call_quietly (
155+ ["git" , "symbolic-ref" , "HEAD" , "refs/heads/main" ], cwd = local_repo_path
156+ )
157+ for i , (filename , contents ) in enumerate (v ):
149158 filename_path = os .path .join (local_repo_path , filename )
150- with open (filename_path , 'w' ) as f :
159+ with open (filename_path , "w" ) as f :
151160 f .write (contents )
152- call_quietly (['git' , 'add' , filename ], cwd = local_repo_path )
153- call_quietly (['git' , 'commit' , '-m' , 'Commit %d' % i ],
154- cwd = local_repo_path )
155- call_quietly (['git' , 'push' , 'origin' , 'main' ],
156- cwd = local_repo_path )
161+ call_quietly (["git" , "add" , filename ], cwd = local_repo_path )
162+ call_quietly (["git" , "commit" , "-m" , "Commit %d" % i ], cwd = local_repo_path )
163+ call_quietly (["git" , "push" , "origin" , "main" ], cwd = local_repo_path )
157164
158- https_clone_pattern = os .path .join (' file://%s' % REMOTE_PATH , '%s' )
159- base_config [' https-clone-pattern' ] = https_clone_pattern
165+ https_clone_pattern = os .path .join (" file://%s" % REMOTE_PATH , "%s" )
166+ base_config [" https-clone-pattern" ] = https_clone_pattern
160167
161- with open (get_config_path (base_dir ), 'w' ) as f :
168+ with open (get_config_path (base_dir ), "w" ) as f :
162169 json .dump (base_config , f )
163170
164- with open (get_additional_config_path (base_dir ), 'w' ) as f :
171+ with open (get_additional_config_path (base_dir ), "w" ) as f :
165172 json .dump (MOCK_ADDITIONAL_SCHEME , f )
166173
167174 return (LOCAL_PATH , REMOTE_PATH )
168175
169176
170- BASEDIR_ENV_VAR = ' UPDATECHECKOUT_TEST_WORKSPACE_DIR'
177+ BASEDIR_ENV_VAR = " UPDATECHECKOUT_TEST_WORKSPACE_DIR"
171178CURRENT_FILE_DIR = os .path .dirname (os .path .abspath (__file__ ))
172- UPDATE_CHECKOUT_EXECUTABLE = 'update-checkout.cmd' if os .name == 'nt' else 'update-checkout'
173- UPDATE_CHECKOUT_PATH = os .path .abspath (os .path .join (CURRENT_FILE_DIR ,
174- os .path .pardir ,
175- os .path .pardir ,
176- UPDATE_CHECKOUT_EXECUTABLE ))
179+ UPDATE_CHECKOUT_EXECUTABLE = (
180+ "update-checkout.cmd" if os .name == "nt" else "update-checkout"
181+ )
182+ UPDATE_CHECKOUT_PATH = os .path .abspath (
183+ os .path .join (
184+ CURRENT_FILE_DIR , os .path .pardir , os .path .pardir , UPDATE_CHECKOUT_EXECUTABLE
185+ )
186+ )
177187
178188
179189class SchemeMockTestCase (unittest .TestCase ):
@@ -184,16 +194,19 @@ def __init__(self, *args, **kwargs):
184194 self .config = MOCK_CONFIG .copy ()
185195 self .workspace = os .getenv (BASEDIR_ENV_VAR )
186196 if self .workspace is None :
187- raise RuntimeError ('Misconfigured test suite! Environment '
188- 'variable %s must be set!' % BASEDIR_ENV_VAR )
197+ raise RuntimeError (
198+ "Misconfigured test suite! Environment "
199+ "variable %s must be set!" % BASEDIR_ENV_VAR
200+ )
189201 self .config_path = get_config_path (self .workspace )
190202 self .additional_config_path = get_additional_config_path (self .workspace )
191203 self .update_checkout_path = UPDATE_CHECKOUT_PATH
192204 if not os .access (self .update_checkout_path , os .X_OK ):
193- raise RuntimeError ('Error! Could not find executable '
194- 'update-checkout at path: %s'
195- % self .update_checkout_path )
196- self .source_root = os .path .join (self .workspace , 'source_root' )
205+ raise RuntimeError (
206+ "Error! Could not find executable "
207+ "update-checkout at path: %s" % self .update_checkout_path
208+ )
209+ self .source_root = os .path .join (self .workspace , "source_root" )
197210
198211 def setUp (self ):
199212 create_dir (self .source_root )
@@ -205,7 +218,7 @@ def tearDown(self):
205218 teardown_mock_remote (self .workspace )
206219
207220 def call (self , * args , ** kwargs ):
208- kwargs [' cwd' ] = self .source_root
221+ kwargs [" cwd" ] = self .source_root
209222 return call_quietly (* args , ** kwargs )
210223
211224 def get_all_repos (self ):
0 commit comments