-
Notifications
You must be signed in to change notification settings - Fork 5
chore: Introduce FileHandler #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 +1,9 @@ | ||
| from twyn.base.exceptions import TwynError | ||
|
|
||
|
|
||
| class PathIsNotFileError(TwynError): | ||
| message = "Specified dependencies path is not a file" | ||
|
|
||
|
|
||
| class PathNotFoundError(TwynError, FileNotFoundError): | ||
| message = "Specified dependencies file path does not exist" | ||
|
|
||
|
|
||
| class NoMatchingParserError(TwynError): | ||
| message = "Could not assign a dependency file parser. Please specify it with --dependency-file" | ||
|
|
||
|
|
||
| class MultipleParsersError(TwynError): | ||
| message = ( | ||
| "Can't auto detect dependencies file to parse. More than one format was found." | ||
| ) | ||
| message = "Can't auto detect dependencies file to parse. More than one format was found." |
This file contains hidden or 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,9 @@ | ||
| from twyn.base.exceptions import TwynError | ||
|
|
||
|
|
||
| class PathIsNotFileError(TwynError): | ||
| message = "Specified dependencies path is not a file" | ||
|
|
||
|
|
||
| class PathNotFoundError(TwynError, FileNotFoundError): | ||
| message = "Specified dependencies file path does not exist" |
This file contains hidden or 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,42 @@ | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Protocol | ||
|
|
||
| from twyn.base.exceptions import TwynError | ||
| from twyn.file_handler.exceptions import PathIsNotFileError, PathNotFoundError | ||
|
|
||
| logger = logging.getLogger() | ||
|
|
||
|
|
||
| class BaseFileHandler(Protocol): | ||
| def __init__(self, file_path: str) -> None: ... | ||
| def read(self) -> str: ... | ||
| def file_exists(self) -> bool: ... | ||
|
|
||
|
|
||
| class FileHandlerPathlib(BaseFileHandler): | ||
| def __init__(self, file_path: str) -> None: | ||
| self.file_path = Path(os.path.abspath(os.path.join(os.getcwd(), file_path))) | ||
|
|
||
| def read(self) -> str: | ||
| self._raise_for_file_exists() | ||
|
|
||
| content = self.file_path.read_text() | ||
| logger.debug("Successfully read content from local dependencies file") | ||
|
|
||
| return content | ||
|
|
||
| def file_exists(self) -> bool: | ||
| try: | ||
| self._raise_for_file_exists() | ||
| except TwynError: | ||
| return False | ||
| return True | ||
|
|
||
| def _raise_for_file_exists(self) -> None: | ||
| if not self.file_path.exists(): | ||
| raise PathNotFoundError | ||
|
|
||
| if not self.file_path.is_file(): | ||
| raise PathIsNotFileError | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from twyn.file_handler.exceptions import PathIsNotFileError, PathNotFoundError | ||
| from twyn.file_handler.file_handler import FileHandlerPathlib | ||
|
|
||
|
|
||
| class TestFileHandlerPathlib: | ||
| def test_file_exists(self, pyproject_toml_file: str): | ||
| parser = FileHandlerPathlib(pyproject_toml_file) | ||
| assert parser.file_exists() is True | ||
|
|
||
| def test_read_file_success(self, pyproject_toml_file: str): | ||
| parser = FileHandlerPathlib(pyproject_toml_file) | ||
| read = parser.read() | ||
| assert len(read) > 1 | ||
| assert isinstance(read, str) | ||
|
|
||
| def test_read_file_does_not_exist( | ||
| self, | ||
| ): | ||
| parser = FileHandlerPathlib("") | ||
| with pytest.raises(PathIsNotFileError): | ||
| parser.read() | ||
|
|
||
| @patch("pathlib.Path.exists") | ||
| @patch("pathlib.Path.is_file") | ||
| @pytest.mark.parametrize( | ||
| "file_exists, is_file, exception", | ||
| [[False, False, PathNotFoundError], [True, False, PathIsNotFileError]], | ||
| ) | ||
| def test_raise_for_valid_file(self, mock_is_file, mock_exists, file_exists, is_file, exception): | ||
| mock_exists.return_value = file_exists | ||
| mock_is_file.return_value = is_file | ||
|
|
||
| parser = FileHandlerPathlib("fake.txt") | ||
| assert parser.file_exists() is False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.