2020#
2121
2222import os
23+ import subprocess
2324
24- import attr
2525import pytest
2626
27- from commoncode .command import execute2
2827from commoncode .fileutils import as_posixpath
2928from commoncode .fileutils import resource_iter
3029from commoncode .testcase import FileDrivenTesting
4039"""
4140
4241
43- @attr .s
44- class Result :
45- exit_code = attr .ib ()
46- output = attr .ib ()
47-
48-
4942def run_extract (options , expected_rc = None , cwd = None ):
5043 """
5144 Run extractcode as a plain subprocess. Return rc, stdout, stderr.
5245 """
5346 cmd_loc = os .path .join (project_root , 'tmp' , 'bin' , 'extractcode' )
54- rc , stdout , stderr = execute2 (cmd_loc = cmd_loc , args = options , cwd = cwd )
55-
56- if expected_rc is not None and rc != expected_rc :
47+ args = [cmd_loc ] + options
48+ result = subprocess .run (args ,
49+ stderr = subprocess .PIPE ,
50+ stdout = subprocess .PIPE ,
51+ cwd = cwd ,
52+ universal_newlines = True ,
53+ # encoding='utf-8',
54+ )
55+
56+ if expected_rc is not None and result .returncode != expected_rc :
5757 opts = ' ' .join (options )
58- error = '''
59- Failure to run: extractcode %( opts)s
58+ error = f '''
59+ Failure to run: extractcode { opts } :
6060stdout:
61- %( stdout)s
61+ { result . stdout }
6262
6363stderr:
64- %( stderr)s
65- ''' % locals ()
66- assert rc == expected_rc , error
64+ { result . stderr }
65+ '''
66+ assert result . returncode == expected_rc , error
6767
68- return Result ( exit_code = rc , output = stdout + stderr )
68+ return result
6969
7070
7171def test_extractcode_command_can_take_an_empty_directory ():
7272 test_dir = test_env .get_temp_dir ()
7373 result = run_extract ([test_dir ], expected_rc = 0 )
7474
75- assert 'Extracting archives...' in result .output
76- assert 'Extracting done' in result .output
75+ assert 'Extracting archives...' in result .stderr
76+ assert 'Extracting done' in result .stderr
7777
7878
7979def test_extractcode_command_does_extract_verbose ():
8080 test_dir = test_env .get_test_loc ('cli/extract' , copy = True )
8181 result = run_extract (['--verbose' , test_dir ], expected_rc = 1 )
8282
8383 assert os .path .exists (os .path .join (test_dir , 'some.tar.gz-extract' ))
84- expected = [
85- 'Extracting archives...' ,
86- 'some.tar.gz' ,
87- 'broken.tar.gz' ,
88- 'tarred_gzipped.tgz' ,
89- 'ERROR extracting' ,
90- "broken.tar.gz: Unrecognized archive format" ,
91- 'Extracting done.' ,
92- ]
93- for e in expected :
94- assert e in result .output
84+ assert 'Extracting archives...' in result .stderr
85+ assert 'some.tar.gz' in result .stdout
86+ assert 'broken.tar.gz' in result .stderr
87+ assert 'tarred_gzipped.tgz' in result .stdout
88+ assert 'ERROR extracting' in result .stderr
89+ assert "broken.tar.gz: Unrecognized archive format" in result .stderr
90+ assert 'Extracting done.' in result .stderr
9591
9692
9793def test_extractcode_command_always_shows_something_if_not_using_a_tty_verbose_or_not ():
9894 test_dir = test_env .get_test_loc ('cli/extract/some.tar.gz' , copy = True )
9995
10096 result = run_extract (options = ['--verbose' , test_dir ], expected_rc = 0 )
101- assert all (x in result .output for x in ('Extracting archives...' , 'Extracting: some.tar.gz' , 'Extracting done.' ))
97+ assert 'Extracting archives...' in result .stderr
98+ assert 'Extracting: some.tar.gz' in result .stdout
99+ assert 'Extracting done.' in result .stderr
102100
103101 result = run_extract (options = [test_dir ], expected_rc = 0 )
104- assert all (x in result .output for x in ('Extracting archives...' , 'Extracting done.' ))
102+ assert 'Extracting archives...' in result .stderr
103+ assert 'Extracting done.' in result .stderr
105104
106105
107106def test_extractcode_command_works_with_relative_paths ():
@@ -129,9 +128,9 @@ def test_extractcode_command_works_with_relative_paths():
129128 test_tgt_dir = join (project_root , test_src_file ) + extractcode .EXTRACT_SUFFIX
130129 result = run_extract ([test_src_file ], expected_rc = 0 , cwd = project_root )
131130
132- assert 'Extracting done' in result .output
133- assert not 'WARNING' in result .output
134- assert not 'ERROR' in result .output
131+ assert 'Extracting done' in result .stderr
132+ assert not 'WARNING' in result .stderr
133+ assert not 'ERROR' in result .stderr
135134
136135 expected = ['/c/a/a.txt' , '/c/b/a.txt' , '/c/c/a.txt' ]
137136 file_result = [
@@ -161,11 +160,11 @@ def test_extractcode_command_works_with_relative_paths_verbose():
161160 shutil .copy (test_file , test_src_dir )
162161 test_src_file = join (test_src_dir , 'basic.zip' )
163162
164- result = run_extract (['--verbose' , test_src_file ] , expected_rc = 2 )
163+ result = run_extract (['--verbose' , test_src_file ] , expected_rc = 0 )
165164
166165 # extract the path from the second line of the output
167166 # check that the path is relative and not absolute
168- lines = result .output .splitlines (False )
167+ lines = result .stderr .splitlines (False )
169168 line = lines [1 ]
170169 line_path = line .split (':' , 1 )[- 1 ].strip ()
171170 if on_windows :
@@ -182,24 +181,24 @@ def test_usage_and_help_return_a_correct_script_name_on_all_platforms():
182181
183182 result = run_extract (options , expected_rc = 0 )
184183
185- assert 'Usage: extractcode [OPTIONS]' in result .output
184+ assert 'Usage: extractcode [OPTIONS]' in result .stdout
186185 # this was showing up on Windows
187- assert 'extractcode-script.py' not in result .output
186+ assert 'extractcode-script.py' not in result .stderr
188187
189188 result = run_extract ([])
190- assert 'Usage: extractcode [OPTIONS]' in result .output
189+ assert 'Usage: extractcode [OPTIONS]' in result .stderr
191190 # this was showing up on Windows
192- assert 'extractcode-script.py' not in result .output
191+ assert 'extractcode-script.py' not in result .stderr
193192
194193 result = run_extract (['-xyz' ] , expected_rc = 2 )
195194 # this was showing up on Windows
196- assert 'extractcode-script.py' not in result .output
195+ assert 'extractcode-script.py' not in result .stderr
197196
198197
199198def test_extractcode_command_can_extract_archive_with_unicode_names_verbose ():
200199 test_dir = test_env .get_test_loc ('cli/unicodearch' , copy = True )
201200 result = run_extract (['--verbose' , test_dir ] , expected_rc = 0 )
202- assert 'Sanders' in result .output
201+ assert 'Sanders' in result .stdout
203202
204203 file_result = [
205204 f for f in map (as_posixpath , resource_iter (test_dir , with_dirs = False ))
@@ -272,6 +271,7 @@ def test_extractcode_command_can_extract_nuget():
272271 test_dir = test_env .get_test_loc ('cli/extract_nuget' , copy = True )
273272 result = run_extract (['--verbose' , test_dir ])
274273
275- if result .exit_code != 0 :
276- print (result .output )
277- assert 'ERROR extracting' not in result .output
274+ if result .returncode != 0 :
275+ print (result .stdout )
276+ assert 'ERROR extracting' not in result .stdout
277+ assert 'ERROR extracting' not in result .stderr
0 commit comments