-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Run Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
|
||
- name: Install Poetry | ||
run: | | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
- name: Configure Poetry | ||
run: | | ||
poetry config virtualenvs.in-project true | ||
- name: Install dependencies | ||
run: | | ||
poetry install -vvv | ||
- name: Run tests | ||
run: poetry run python -m unittest discover |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import os | ||
import json | ||
from typing import Dict, List | ||
from typing import Dict | ||
import click | ||
|
||
|
||
|
Empty file.
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,110 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import patch, mock_open, call | ||
from click.testing import CliRunner | ||
from replace_text.replace_text import replace_text | ||
|
||
|
||
class TestReplaceText(unittest.TestCase): | ||
def assert_path_any_call(self, mock_obj, expected_path, mode, encoding): | ||
normalized_expected_path = os.path.normpath(expected_path) | ||
for mock_call in mock_obj.call_args_list: | ||
args, kwargs = mock_call | ||
if len(args) >= 1: | ||
normalized_actual_path = os.path.normpath(args[0]) | ||
if ( | ||
normalized_actual_path == normalized_expected_path | ||
and args[1] == mode | ||
and kwargs.get("encoding") == encoding | ||
): | ||
return | ||
raise AssertionError( | ||
f"Expected call not found: open('{normalized_expected_path}', '{mode}', encoding='{encoding}')" | ||
) | ||
|
||
@patch("builtins.open", new_callable=mock_open, read_data="key1 content key2") | ||
@patch("os.walk") | ||
@patch("json.load") | ||
def test_replace_text_keys_to_values_single_dict( | ||
self, mock_json_load, mock_os_walk, mock_file | ||
): | ||
mock_json_load.return_value = { | ||
"dictionaries": { | ||
"example1": {"key1": "value1", "key2": "value2", "key3": "value3"} | ||
}, | ||
"ignore_extensions": [".png", ".jpg"], | ||
} | ||
mock_os_walk.return_value = [ | ||
("/mocked/path", ("subdir",), ("file1.txt", "file2.jpg")) | ||
] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke( | ||
replace_text, | ||
["--direction", "1", "--folder", "/mocked/path", "--dict-name", "example1"], | ||
) | ||
|
||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "r", "utf-8") | ||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "w", "utf-8") | ||
mock_file().write.assert_called_with("value1 content value2") | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
@patch("builtins.open", new_callable=mock_open, read_data="value1 content value2") | ||
@patch("os.walk") | ||
@patch("json.load") | ||
def test_replace_text_values_to_keys_multiple_dicts( | ||
self, mock_json_load, mock_os_walk, mock_file | ||
): | ||
mock_json_load.return_value = { | ||
"dictionaries": { | ||
"example1": {"key1": "value1", "key2": "value2", "key3": "value3"}, | ||
"example2": {"hello": "world", "foo": "bar", "python": "rocks"}, | ||
}, | ||
"ignore_extensions": [".png", ".jpg"], | ||
} | ||
mock_os_walk.return_value = [ | ||
("/mocked/path", ("subdir",), ("file1.txt", "file2.jpg")) | ||
] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke( | ||
replace_text, | ||
["--direction", "2", "--folder", "/mocked/path", "--dict-name", "example1"], | ||
) | ||
|
||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "r", "utf-8") | ||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "w", "utf-8") | ||
mock_file().write.assert_called_with("key1 content key2") | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
@patch("builtins.open", new_callable=mock_open, read_data="hello content foo") | ||
@patch("os.walk") | ||
@patch("json.load") | ||
def test_replace_text_with_dict_name_flag( | ||
self, mock_json_load, mock_os_walk, mock_file | ||
): | ||
mock_json_load.return_value = { | ||
"dictionaries": { | ||
"example1": {"key1": "value1", "key2": "value2", "key3": "value3"}, | ||
"example2": {"hello": "world", "foo": "bar", "python": "rocks"}, | ||
}, | ||
"ignore_extensions": [".png", ".jpg"], | ||
} | ||
mock_os_walk.return_value = [ | ||
("/mocked/path", ("subdir",), ("file1.txt", "file2.jpg")) | ||
] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke( | ||
replace_text, | ||
["--direction", "1", "--folder", "/mocked/path", "--dict-name", "example2"], | ||
) | ||
|
||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "r", "utf-8") | ||
self.assert_path_any_call(mock_file, "/mocked/path/file1.txt", "w", "utf-8") | ||
mock_file().write.assert_called_with("world content bar") | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |