Skip to content

Commit 6351796

Browse files
authored
Fix the output option (#676)
The output option was broken in such that argparse would create the file if it didn't exist, then the code would additionally check if the file exist, which it would. This change removes the overwrite checking code and simply changes argparse to error if the file already exists. It will be up to the user to specify a different file or delete the existing. Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
1 parent c20db74 commit 6351796

4 files changed

Lines changed: 10 additions & 26 deletions

File tree

precli/cli/init.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,12 @@ def setup_arg_parser() -> Namespace:
2525
"--output",
2626
dest="output",
2727
action="store",
28-
type=argparse.FileType("w", encoding="utf-8"),
28+
type=argparse.FileType("x", encoding="utf-8"),
2929
default=".precli.toml",
3030
help="output the config to given file",
3131
)
3232
args = parser.parse_args()
3333

34-
if args.output:
35-
path = pathlib.Path(args.output.name)
36-
if path.exists():
37-
overwrite = input(
38-
f"The file '{path}' already exists. Overwrite? (y/N): "
39-
)
40-
if overwrite.lower() != "y":
41-
print("Operation cancelled.")
42-
sys.exit(1)
43-
4434
return args
4535

4636

precli/cli/main.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def setup_arg_parser():
116116
"--output",
117117
dest="output",
118118
action="store",
119-
type=argparse.FileType("w", encoding="utf-8"),
119+
type=argparse.FileType("x", encoding="utf-8"),
120120
default=sys.stdout,
121121
help="output the results to a file",
122122
)
@@ -159,16 +159,6 @@ def setup_arg_parser():
159159
f"argument -c/--config: can't load '{args.config.name}': {err}"
160160
)
161161

162-
if args.output:
163-
path = pathlib.Path(args.output.name)
164-
if path.exists():
165-
overwrite = input(
166-
f"The file '{path}' already exists. Overwrite? (y/N): "
167-
)
168-
if overwrite.lower() != "y":
169-
print("Operation cancelled.")
170-
sys.exit(1)
171-
172162
if not args.targets:
173163
parser.print_usage()
174164
sys.exit(2)

tests/unit/cli/test_init.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_main_invalid_output(self, monkeypatch):
2626
assert excinfo.value.code == 2
2727

2828
@mock.patch("builtins.input", lambda _: "no")
29-
def test_main_output_already_exists(self, monkeypatch):
29+
def test_main_output_already_exists(self, monkeypatch, capsys):
3030
monkeypatch.setattr("sys.argv", ["precli-init", "-o", "output.txt"])
3131
temp_dir = tempfile.mkdtemp()
3232
os.chdir(temp_dir)
@@ -35,4 +35,6 @@ def test_main_output_already_exists(self, monkeypatch):
3535

3636
with pytest.raises(SystemExit) as excinfo:
3737
init.main()
38-
assert excinfo.value.code == 1
38+
assert excinfo.value.code == 2
39+
captured = capsys.readouterr()
40+
assert "[Errno 17] File exists" in captured.err

tests/unit/cli/test_main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_main_invalid_output(self, monkeypatch):
6565
assert excinfo.value.code == 2
6666

6767
@mock.patch("builtins.input", lambda _: "no")
68-
def test_main_output_already_exists(self, monkeypatch):
68+
def test_main_output_already_exists(self, monkeypatch, capsys):
6969
monkeypatch.setattr("sys.argv", ["precli", "-o", "output.txt"])
7070
temp_dir = tempfile.mkdtemp()
7171
os.chdir(temp_dir)
@@ -74,7 +74,9 @@ def test_main_output_already_exists(self, monkeypatch):
7474

7575
with pytest.raises(SystemExit) as excinfo:
7676
main.main()
77-
assert excinfo.value.code == 1
77+
assert excinfo.value.code == 2
78+
captured = capsys.readouterr()
79+
assert "[Errno 17] File exists" in captured.err
7880

7981
def test_main_more_than_one_renderer(self, monkeypatch, capsys):
8082
monkeypatch.setattr("sys.argv", ["precli", "--json", "--markdown"])

0 commit comments

Comments
 (0)