Skip to content

Commit 3a512fb

Browse files
committed
Add tests for output file
1 parent 9f30859 commit 3a512fb

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

src/isal/igzip.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,10 @@ def _argument_parser():
432432
dest="compress",
433433
const=False,
434434
help="Decompress the file instead of compressing.")
435-
parser.add_argument("-c", "--stdout", action="store_true",
435+
output_group = parser.add_mutually_exclusive_group()
436+
output_group.add_argument("-c", "--stdout", action="store_true",
436437
help="write on standard output")
437-
parser.add_argument("-o", "--output", help="Write to this output file")
438+
output_group.add_argument("-o", "--output", help="Write to this output file")
438439
parser.add_argument("-f", "--force", action="store_true",
439440
help="Overwrite output without prompting")
440441
# -b flag not taken by either gzip or igzip. Hidden attribute. Above 32K
@@ -451,42 +452,38 @@ def main():
451452

452453
compresslevel = args.compresslevel or _COMPRESS_LEVEL_TRADEOFF
453454

454-
# Determine input file
455-
if args.compress and args.file is None:
456-
in_file = sys.stdin.buffer
457-
elif args.compress and args.file is not None:
458-
in_file = io.open(args.file, mode="rb")
459-
elif not args.compress and args.file is None:
460-
in_file = IGzipFile(mode="rb", fileobj=sys.stdin.buffer)
461-
elif not args.compress and args.file is not None:
462-
base, extension = os.path.splitext(args.file)
463-
if extension != ".gz" and not args.stdout:
464-
sys.exit(f"filename doesn't end in .gz: {args.file!r}. "
465-
f"Cannot determine output filename.")
466-
in_file = open(args.file, "rb")
467-
468-
# Determine output file
469-
if args.output is not None:
470-
if os.path.exists(args.output) and not args.force:
471-
response = input(f"{args.output} already exists; "
472-
f"do you wish to overwrite (y/n)?")
473-
if response not in {"y", "Y"}:
474-
sys.exit("not overwritten")
475-
if args.compress:
476-
out_file = IGzipFile(args.output,
477-
mode="wb", compresslevel=compresslevel)
455+
if args.file is None or args.stdout:
456+
out_fileobj = sys.stdout.buffer
457+
else:
458+
if args.output is None:
459+
if args.compress:
460+
out_filepath = args.file + ".gz"
461+
else:
462+
out_filepath, extension = os.path.splitext(args.file)
463+
if extension != ".gz" and not args.stdout:
464+
sys.exit(f"filename doesn't end in .gz: {args.file!r}. "
465+
f"Cannot determine output filename.")
478466
else:
479-
out_file = io.open(args.output, "wb")
480-
elif args.compress and (args.file is None or args.stdout):
481-
out_file = IGzipFile(mode="wb", compresslevel=compresslevel,
482-
fileobj=sys.stdout.buffer)
483-
elif args.compress and args.file is not None:
484-
out_file = open(args.file + ".gz", mode="wb",
485-
compresslevel=compresslevel)
486-
elif not args.compress and (args.file is None or args.stdout):
487-
out_file = sys.stdout.buffer
488-
elif not args.compress and args.file is not None:
489-
out_file = io.open(base, "wb")
467+
out_filepath = args.output
468+
if os.path.exists(out_filepath) and not args.force:
469+
yes_or_no = input(f"{out_filepath} already exists; "
470+
f"do you wish to overwrite (y/n)?")
471+
if yes_or_no not in {"y", "Y", "yes"}:
472+
sys.exit("not overwritten")
473+
out_fileobj = io.open(out_filepath, mode="wb")
474+
475+
if args.file is None:
476+
in_fileobj = sys.stdin.buffer
477+
else:
478+
in_fileobj = io.open(args.file, mode="rb")
479+
480+
if args.compress:
481+
in_file = in_fileobj
482+
out_file = IGzipFile(mode="wb", fileobj=out_fileobj,
483+
compresslevel=compresslevel)
484+
else:
485+
in_file = IGzipFile(mode="rb", fileobj=in_fileobj)
486+
out_file = out_fileobj
490487

491488
global READ_BUFFER_SIZE
492489
READ_BUFFER_SIZE = args.buffer_size

tests/test_igzip.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ def test_compress_infile_stdout(capsysbinary, tmp_path):
150150
assert err == b''
151151

152152

153+
def test_decompress_infile_outfile(tmp_path, capsysbinary):
154+
test_gz = tmp_path / "test.gz"
155+
test_gz.write_bytes(gzip.compress(DATA))
156+
out_file = tmp_path / "out"
157+
sys.argv = ['', '-d', '-o', str(out_file), str(test_gz)]
158+
igzip.main()
159+
out, err = capsysbinary.readouterr()
160+
assert out_file.read_bytes() == DATA
161+
assert err == b''
162+
assert out == b''
163+
164+
165+
def test_compress_infile_out_file(tmp_path, capsysbinary):
166+
test = tmp_path / "test"
167+
test.write_bytes(DATA)
168+
out_file = tmp_path / "compressed.gz"
169+
sys.argv = ['', '-o', str(out_file), str(test)]
170+
igzip.main()
171+
out, err = capsysbinary.readouterr()
172+
assert gzip.decompress(out_file.read_bytes()) == DATA
173+
assert err == b''
174+
assert out == b''
175+
176+
153177
def test_decompress():
154178
assert igzip.decompress(COMPRESSED_DATA) == DATA
155179

0 commit comments

Comments
 (0)