Skip to content

Commit

Permalink
Fix/#28 (#30)
Browse files Browse the repository at this point in the history
* fix(files): always return path object with get_input_file_from_dir

* chore: update testing command on pre-commit config

* chore: lint
  • Loading branch information
Saigesp authored Nov 8, 2023
1 parent 269bdb8 commit 5532330
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repos:
hooks:
- id: local-tests
name: local-tests
entry: python -m unittest discover -b
entry: python -m unittest discover -b tests/
always_run: true
pass_filenames: false
language: system
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DEEPL_AUTH_KEY=your-key-here
The tests will run automatically when you commit your changes. However, you can run them before with the command:

```shell
python -m unittest discover -b
python -m unittest discover -b tests/
```

### Commit your update
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Normally, multilingual projects base their translations on JSON files with diffe
json_translate deepl /path/to/input-file.json FR

# Input file: Output file:
# { {
# { {
# "welcome": "Welcome!", "welcome": "Bienvenue!",
# "questions": { "questions": {
# "sure": "Are you sure?" "sure": "Êtes-vous sûr?"
Expand Down Expand Up @@ -132,7 +132,6 @@ INDENTATION_DEFAULT=2
ENCODING=utf-8
```


### Example file
Translate the example file `/tests/data/en_US.json` to spanish:
```shell
Expand Down
16 changes: 9 additions & 7 deletions json_translate/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ def get_input_file_from_dir(input_dir: str) -> os.PathLike:
:param input_dir: selected folder to search for files
:return: file to translate
"""
if Path(input_dir).is_dir():
json_files = find_files(input_dir)
return get_input_file(json_files, input_dir)
input_dir_norm = os.path.normpath(input_dir)

if not input_dir.endswith(".json"):
if Path(input_dir_norm).is_dir():
json_files = find_files(input_dir_norm)
return get_input_file(json_files, input_dir_norm)

if not input_dir_norm.endswith(".json"):
print( # noqa: T201
"You must select a json file or a folder containing json files"
)
exit(1)

if not Path(input_dir).is_file():
print("File not found") # noqa: T201
if not Path(input_dir_norm).is_file():
print("File not found:", input_dir_norm) # noqa: T201
exit(1)

return os.path.normpath(input_dir)
return Path(input_dir_norm)


def get_output_file(
Expand Down
1 change: 0 additions & 1 deletion json_translate/translators/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
class AWSTranslator(BaseTranslator):
"""AWS translator class."""


def __init__(self, *args, **kwargs):
"""Initialize AWS translator instance.
Expand Down
23 changes: 23 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
# -*- coding: utf-8 -*-
import unittest
from pathlib import Path
from unittest.mock import patch, MagicMock
from json_translate import files


class FilesTest(unittest.TestCase):
"""Tests for files module."""

@patch("files.get_input_file")
def test_get_input_file_from_dir_calls_get_input_file_if_input_is_dir(self, get_input_file_mock: MagicMock):
"""It calls get_input_file when input is a folder."""
files.get_input_file_from_dir("tests/data")
get_input_file_mock.assert_called_once_with(['en_US.json', 'fr.json'], 'tests/data')

def test_get_input_file_from_dir_exits_if_input_file_is_not_json(self):
"""It exists when input file is not json."""
with self.assertRaises(SystemExit):
files.get_input_file_from_dir("tests/test_files.py")

def test_get_input_file_from_dir_exits_if_input_file_not_exists(self):
"""It exists when input file doesn't exit."""
with self.assertRaises(SystemExit):
files.get_input_file_from_dir("tests/lorem.json")

def test_get_input_file_from_dir_returns_path(self):
"""It returns a Path instance."""
result = files.get_input_file_from_dir("tests/data/en_US.json")
self.assertTrue(isinstance(result, Path))

def test_get_input_dir_from_file_removes_redundat_separators(self):
"""It normalize a pathname.
Expand Down

0 comments on commit 5532330

Please sign in to comment.