Skip to content

Commit cc5910a

Browse files
committed
Test prompt for overwriting outputfile
1 parent 3a512fb commit cc5910a

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

tests/test_igzip.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_compress_infile_stdout(capsysbinary, tmp_path):
150150
assert err == b''
151151

152152

153-
def test_decompress_infile_outfile(tmp_path, capsysbinary):
153+
def test_decompress_infile_out_file(tmp_path, capsysbinary):
154154
test_gz = tmp_path / "test.gz"
155155
test_gz.write_bytes(gzip.compress(DATA))
156156
out_file = tmp_path / "out"
@@ -174,6 +174,67 @@ def test_compress_infile_out_file(tmp_path, capsysbinary):
174174
assert out == b''
175175

176176

177+
def test_compress_infile_out_file_force(tmp_path, capsysbinary):
178+
test = tmp_path / "test"
179+
test.write_bytes(DATA)
180+
out_file = tmp_path / "compressed.gz"
181+
out_file.touch()
182+
sys.argv = ['', '-f', '-o', str(out_file), str(test)]
183+
igzip.main()
184+
out, err = capsysbinary.readouterr()
185+
assert gzip.decompress(out_file.read_bytes()) == DATA
186+
assert err == b''
187+
assert out == b''
188+
189+
190+
def test_compress_infile_out_file_prompt(tmp_path, capsysbinary):
191+
test = tmp_path / "test"
192+
test.write_bytes(DATA)
193+
out_file = tmp_path / "compressed.gz"
194+
out_file.touch()
195+
sys.argv = ['', '-o', str(out_file), str(test)]
196+
with pytest.raises(EOFError):
197+
# EOFError because prompt cannot be answered non-interactively.
198+
igzip.main()
199+
out, err = capsysbinary.readouterr()
200+
assert b"compressed.gz already exists; do you wish to overwrite (y/n)?" \
201+
in out
202+
203+
204+
def test_compress_infile_out_file_inmplicit_name_prompt_refuse(
205+
tmp_path, capsysbinary):
206+
test = tmp_path / "test"
207+
test.write_bytes(DATA)
208+
out_file = tmp_path / "test.gz"
209+
out_file.touch()
210+
sys.argv = ['', str(test)]
211+
mock_stdin = io.BytesIO(b"n")
212+
sys.stdin = io.TextIOWrapper(mock_stdin)
213+
with pytest.raises(SystemExit) as error:
214+
igzip.main()
215+
error.match("not overwritten")
216+
out, err = capsysbinary.readouterr()
217+
assert b"test.gz already exists; do you wish to overwrite (y/n)?" \
218+
in out
219+
220+
221+
def test_compress_infile_out_file_inmplicit_name_prompt_accept(
222+
tmp_path, capsysbinary):
223+
test = tmp_path / "test"
224+
test.write_bytes(DATA)
225+
out_file = tmp_path / "test.gz"
226+
out_file.touch()
227+
sys.argv = ['', str(test)]
228+
mock_stdin = io.BytesIO(b"y")
229+
sys.stdin = io.TextIOWrapper(mock_stdin)
230+
igzip.main()
231+
out, err = capsysbinary.readouterr()
232+
assert gzip.decompress(out_file.read_bytes()) == DATA
233+
assert b"already exists; do you wish to overwrite" in out
234+
assert err == b""
235+
236+
237+
177238
def test_decompress():
178239
assert igzip.decompress(COMPRESSED_DATA) == DATA
179240

0 commit comments

Comments
 (0)