-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for precli-init command (#671)
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
Showing
3 changed files
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters