Skip to content

Commit

Permalink
Add unit test for precli-init command (#671)
Browse files Browse the repository at this point in the history
This change tests some of the output of precli-init, specifically around
the -o argument to ensure the file doesn't already exist or can't be
written to.

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored Oct 30, 2024
1 parent 3176cad commit 9630541
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
23 changes: 13 additions & 10 deletions precli/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ def setup_arg_parser() -> Namespace:
"--output",
dest="output",
action="store",
type=argparse.FileType("w", encoding="utf-8"),
default=".precli.toml",
help="output the config to given file",
)
args = parser.parse_args()

return parser.parse_args()
if args.output:
path = pathlib.Path(args.output.name)
if path.exists():
overwrite = input(
f"The file '{path}' already exists. Overwrite? (y/N): "
)
if overwrite.lower() != "y":
print("Operation cancelled.")
sys.exit(1)

return args


def get_config() -> dict:
Expand Down Expand Up @@ -69,15 +81,6 @@ def main():
try:
path = pathlib.Path(args.output)

# Check if the file already exists and prompt for overwrite
if path.exists():
overwrite = input(
f"The file '{args.output}' already exists. Overwrite? (y/N): "
)
if overwrite.lower() != "y":
print("Operation cancelled.")
return 1

# Check if the target file is pyproject.toml and prepare the structure
if path.name == "pyproject.toml":
if path.exists():
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/cli/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Secure Sauce LLC
# SPDX-License-Identifier: BUSL-1.1
import json
import os
import tempfile
from unittest import mock

import pytest

from precli.cli import init


class TestInit:
@classmethod
def setup_class(cls):
cls.current_dir = os.getcwd()

@classmethod
def teardown_class(cls):
os.chdir(cls.current_dir)

def test_main_invalid_output(self, monkeypatch):
monkeypatch.setattr("sys.argv", ["precli-init", "-o", "../does/not/exists"])
with pytest.raises(SystemExit) as excinfo:
init.main()
assert excinfo.value.code == 2

@mock.patch("builtins.input", lambda _: "no")
def test_main_output_already_exists(self, monkeypatch):
monkeypatch.setattr("sys.argv", ["precli-init", "-o", "output.txt"])
temp_dir = tempfile.mkdtemp()
os.chdir(temp_dir)
with open("output.txt", "w") as fd:
fd.write("This file already exists. Do not overwrite.")

with pytest.raises(SystemExit) as excinfo:
init.main()
assert excinfo.value.code == 1
6 changes: 0 additions & 6 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
class TestMain:
@classmethod
def setup_class(cls):
cls.base_path = os.path.join(
"tests",
"unit",
"cli",
"examples",
)
cls.current_dir = os.getcwd()

@classmethod
Expand Down

0 comments on commit 9630541

Please sign in to comment.