Skip to content

Commit

Permalink
cmd_convert: Allow white space file names
Browse files Browse the repository at this point in the history
Removed file name checking.
Nonexisting/wrong files will be detected on OS level.

Signed-off-by: Adam Szczygieł <[email protected]>
  • Loading branch information
adsz-nordic committed Dec 13, 2023
1 parent 4eff3f5 commit fdec88b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
4 changes: 0 additions & 4 deletions suit_generator/cmd_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ def __init__(
self._validate()

def _validate(self):
if self._input_file.strip() == "":
raise ValueError(f"Invalid input file: {self._input_file}")
if self._output_file.strip() == "":
raise ValueError(f"Invalid output file: {self._output_file}")
if self._array_type.strip() == "":
raise ValueError(f"Invalid array_type: {self._array_type}")
if self._array_name.strip() == "":
Expand Down
44 changes: 30 additions & 14 deletions tests/test_cmd_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
@pytest.fixture
def mocker_existing_file(mocker):
getsize_data = mocker.Mock()
getsize_data.side_effect = lambda x: len(PRIVATE_KEY_FILE_NONEMPTY)
getsize_data.side_effect = lambda _: len(PRIVATE_KEY_FILE_NONEMPTY)
mocker.patch("os.path.getsize", getsize_data)

exists_data = mocker.Mock()
exists_data.side_effect = lambda x: True
exists_data.side_effect = lambda _: True
mocker.patch("os.path.exists", exists_data)


Expand All @@ -59,7 +59,7 @@ def default_converter(mocker_existing_file):
@pytest.fixture
def mocker_header_and_key(mocker):
getsize_data = mocker.Mock()
getsize_data.side_effect = lambda _: 1 # Just ensure it nonnegative
getsize_data.side_effect = lambda _: 1 # Just ensure it is nonnegative
mocker.patch("os.path.getsize", getsize_data)

exists_data = mocker.Mock()
Expand All @@ -76,7 +76,7 @@ def mocker_header_and_key(mocker):
@pytest.fixture
def mocker_key_and_footer(mocker):
getsize_data = mocker.Mock()
getsize_data.side_effect = lambda _: 1 # Just ensure it nonnegative
getsize_data.side_effect = lambda _: 1 # Just ensure it is nonnegative
mocker.patch("os.path.getsize", getsize_data)

exists_data = mocker.Mock()
Expand All @@ -93,7 +93,7 @@ def mocker_key_and_footer(mocker):
@pytest.fixture
def mocker_header_key_and_footer(mocker):
getsize_data = mocker.Mock()
getsize_data.side_effect = lambda _: 1 # Just ensure it nonnegative
getsize_data.side_effect = lambda _: 1 # Just ensure it is nonnegative
mocker.patch("os.path.getsize", getsize_data)

exists_data = mocker.Mock()
Expand Down Expand Up @@ -127,11 +127,11 @@ def valid_converter(mocker_existing_file):
@pytest.fixture
def mocker_empty_file(mocker):
exists_data = mocker.Mock()
exists_data.side_effect = lambda x: True
exists_data.side_effect = lambda _: True
mocker.patch("os.path.exists", exists_data)

size_data = mocker.Mock()
size_data.side_effect = lambda x: 0
size_data.side_effect = lambda _: 0
mocker.patch("os.path.getsize", size_data)

mocked_data = mocker.mock_open(read_data="")
Expand All @@ -153,11 +153,11 @@ def mocker_footer_file_nonempty(mocker):
@pytest.fixture
def mocker_private_key_file_nonempty(mocker):
getsize_data = mocker.Mock()
getsize_data.side_effect = lambda x: len(PRIVATE_KEY_FILE_NONEMPTY)
getsize_data.side_effect = lambda _: len(PRIVATE_KEY_FILE_NONEMPTY)
mocker.patch("os.path.getsize", getsize_data)

exists_data = mocker.Mock()
exists_data.side_effect = lambda x: True
exists_data.side_effect = lambda _: True
mocker.patch("os.path.exists", exists_data)

mocked_data = mocker.mock_open(read_data=PRIVATE_KEY_FILE_NONEMPTY)
Expand All @@ -168,16 +168,32 @@ def test_validate_invalid_input_file():
# GIVEN empty input file name
# WHEN converter is created
# THEN it raises an exception
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
KeyConverter(input_file="", output_file="some_output_file")


def test_validate_invalid_output_file():
# GIVEN empty output file name
def test_validate_whitespace_input_file(mocker_existing_file):
# GIVEN input file name consisting of white spaces
# WHEN converter is created
# THEN it does not raise an exception
KeyConverter(input_file=" ", output_file="some_output_file")


def test_validate_invalid_output_file(mocker_existing_file):
# GIVEN converter created with empty output file name
converter = KeyConverter(input_file="some_input_file", output_file="")
# WHEN file contents are prepared
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="")
with pytest.raises(FileNotFoundError):
converter.prepare_file_contents()


def test_validate_whitespace_output_file(mocker_private_key_file_nonempty):
# GIVEN converter created with output file name consisting of white spaces
converter = KeyConverter(input_file="some_input_file", output_file=" ")
# WHEN file contents are prepared
# THEN it does not raise exception
converter.prepare_file_contents()


def test_validate_array_type():
Expand Down

0 comments on commit fdec88b

Please sign in to comment.