20
20
#
21
21
22
22
import os
23
+ import subprocess
23
24
24
- import attr
25
25
import pytest
26
26
27
- from commoncode .command import execute2
28
27
from commoncode .fileutils import as_posixpath
29
28
from commoncode .fileutils import resource_iter
30
29
from commoncode .testcase import FileDrivenTesting
40
39
"""
41
40
42
41
43
- @attr .s
44
- class Result :
45
- exit_code = attr .ib ()
46
- output = attr .ib ()
47
-
48
-
49
42
def run_extract (options , expected_rc = None , cwd = None ):
50
43
"""
51
44
Run extractcode as a plain subprocess. Return rc, stdout, stderr.
52
45
"""
53
46
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 :
57
57
opts = ' ' .join (options )
58
- error = '''
59
- Failure to run: extractcode %( opts)s
58
+ error = f '''
59
+ Failure to run: extractcode { opts } :
60
60
stdout:
61
- %( stdout)s
61
+ { result . stdout }
62
62
63
63
stderr:
64
- %( stderr)s
65
- ''' % locals ()
66
- assert rc == expected_rc , error
64
+ { result . stderr }
65
+ '''
66
+ assert result . returncode == expected_rc , error
67
67
68
- return Result ( exit_code = rc , output = stdout + stderr )
68
+ return result
69
69
70
70
71
71
def test_extractcode_command_can_take_an_empty_directory ():
72
72
test_dir = test_env .get_temp_dir ()
73
73
result = run_extract ([test_dir ], expected_rc = 0 )
74
74
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
77
77
78
78
79
79
def test_extractcode_command_does_extract_verbose ():
80
80
test_dir = test_env .get_test_loc ('cli/extract' , copy = True )
81
81
result = run_extract (['--verbose' , test_dir ], expected_rc = 1 )
82
82
83
83
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
95
91
96
92
97
93
def test_extractcode_command_always_shows_something_if_not_using_a_tty_verbose_or_not ():
98
94
test_dir = test_env .get_test_loc ('cli/extract/some.tar.gz' , copy = True )
99
95
100
96
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
102
100
103
101
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
105
104
106
105
107
106
def test_extractcode_command_works_with_relative_paths ():
@@ -129,9 +128,9 @@ def test_extractcode_command_works_with_relative_paths():
129
128
test_tgt_dir = join (project_root , test_src_file ) + extractcode .EXTRACT_SUFFIX
130
129
result = run_extract ([test_src_file ], expected_rc = 0 , cwd = project_root )
131
130
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
135
134
136
135
expected = ['/c/a/a.txt' , '/c/b/a.txt' , '/c/c/a.txt' ]
137
136
file_result = [
@@ -161,11 +160,11 @@ def test_extractcode_command_works_with_relative_paths_verbose():
161
160
shutil .copy (test_file , test_src_dir )
162
161
test_src_file = join (test_src_dir , 'basic.zip' )
163
162
164
- result = run_extract (['--verbose' , test_src_file ] , expected_rc = 2 )
163
+ result = run_extract (['--verbose' , test_src_file ] , expected_rc = 0 )
165
164
166
165
# extract the path from the second line of the output
167
166
# check that the path is relative and not absolute
168
- lines = result .output .splitlines (False )
167
+ lines = result .stderr .splitlines (False )
169
168
line = lines [1 ]
170
169
line_path = line .split (':' , 1 )[- 1 ].strip ()
171
170
if on_windows :
@@ -182,24 +181,24 @@ def test_usage_and_help_return_a_correct_script_name_on_all_platforms():
182
181
183
182
result = run_extract (options , expected_rc = 0 )
184
183
185
- assert 'Usage: extractcode [OPTIONS]' in result .output
184
+ assert 'Usage: extractcode [OPTIONS]' in result .stdout
186
185
# this was showing up on Windows
187
- assert 'extractcode-script.py' not in result .output
186
+ assert 'extractcode-script.py' not in result .stderr
188
187
189
188
result = run_extract ([])
190
- assert 'Usage: extractcode [OPTIONS]' in result .output
189
+ assert 'Usage: extractcode [OPTIONS]' in result .stderr
191
190
# this was showing up on Windows
192
- assert 'extractcode-script.py' not in result .output
191
+ assert 'extractcode-script.py' not in result .stderr
193
192
194
193
result = run_extract (['-xyz' ] , expected_rc = 2 )
195
194
# this was showing up on Windows
196
- assert 'extractcode-script.py' not in result .output
195
+ assert 'extractcode-script.py' not in result .stderr
197
196
198
197
199
198
def test_extractcode_command_can_extract_archive_with_unicode_names_verbose ():
200
199
test_dir = test_env .get_test_loc ('cli/unicodearch' , copy = True )
201
200
result = run_extract (['--verbose' , test_dir ] , expected_rc = 0 )
202
- assert 'Sanders' in result .output
201
+ assert 'Sanders' in result .stdout
203
202
204
203
file_result = [
205
204
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():
272
271
test_dir = test_env .get_test_loc ('cli/extract_nuget' , copy = True )
273
272
result = run_extract (['--verbose' , test_dir ])
274
273
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