-
Notifications
You must be signed in to change notification settings - Fork 0
Add poetry-sphinx-build-language pre-commit hook #1
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
Open
niall-byrne
wants to merge
12
commits into
expand_translation_scripts
Choose a base branch
from
add-poetry-sphinx-build-language-hook-5336113803166927788
base: expand_translation_scripts
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6c8b529
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 7617300
feat(PRE-COMMIT): add poetry-sphinx-build-language hook and fix CI
google-labs-jules[bot] 804f457
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 6a01666
feat(PRE-COMMIT): add poetry-sphinx-build-language hook with custom t…
google-labs-jules[bot] a1828a1
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 9c09b0d
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 3b85d65
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] fd8f496
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] c7ea176
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] f5db868
feat(PRE-COMMIT): add poetry-sphinx-build-language hook and refactor …
google-labs-jules[bot] ffd7345
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 077d528
feat(PRE-COMMIT): add poetry-sphinx-build-language hook
google-labs-jules[bot] 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| """CICD-Tools pre-commit scripts.""" | ||
|
|
||
| from .resources import with_cicd_resources | ||
| from .sphinx import sphinx_build_language | ||
|
|
||
| __all__ = ("with_cicd_resources", ) | ||
| __all__ = ( | ||
| "sphinx_build_language", | ||
| "with_cicd_resources", | ||
| ) |
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 @@ | ||
| """CLI related tools and types.""" |
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 @@ | ||
| """Tests for CLI related tools and types.""" |
26 changes: 26 additions & 0 deletions
26
src/cicd_tools_pre_commit/cli/tests/test_existing_directory.py
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,26 @@ | ||
| import argparse | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from cicd_tools_pre_commit.cli.types import existing_directory | ||
|
|
||
|
|
||
| class TestExistingDirectory(unittest.TestCase): | ||
|
|
||
| @patch("os.path.isdir") | ||
| def test_existing_directory__valid_dir__returns_path( | ||
| self, | ||
| mock_isdir, | ||
| ): | ||
| mock_isdir.return_value = True | ||
| path = "/valid/dir" | ||
| self.assertEqual(existing_directory(path), path) | ||
|
|
||
| @patch("os.path.isdir") | ||
| def test_existing_directory__invalid_dir__raises_error( | ||
| self, | ||
| mock_isdir, | ||
| ): | ||
| mock_isdir.return_value = False | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| existing_directory("/invalid/dir") |
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,29 @@ | ||
| import argparse | ||
| import unittest | ||
|
|
||
| from cicd_tools_pre_commit.cli.types import language_code | ||
|
|
||
|
|
||
| class TestLanguageCode(unittest.TestCase): | ||
|
|
||
| def test_language_code__valid_short__returns_code(self): | ||
| self.assertEqual(language_code("en"), "en") | ||
|
|
||
| def test_language_code__valid_long__returns_code(self): | ||
| self.assertEqual(language_code("zh_CN"), "zh_CN") | ||
|
|
||
| def test_language_code__invalid_format__raises_error(self): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| language_code("ENG") | ||
|
|
||
| def test_language_code__uppercase_short__raises_error(self): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| language_code("EN") | ||
|
|
||
| def test_language_code__hyphenated__raises_error(self): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| language_code("en-US") | ||
|
|
||
| def test_language_code__empty__raises_error(self): | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| language_code("") |
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,38 @@ | ||
| import argparse | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from cicd_tools_pre_commit.cli.types import valid_path | ||
|
|
||
|
|
||
| class TestValidPath(unittest.TestCase): | ||
|
|
||
| @patch("os.path.exists") | ||
| @patch("os.path.abspath") | ||
| @patch("os.path.dirname") | ||
| def test_valid_path__parent_exists__returns_path( | ||
| self, | ||
| mock_dirname, | ||
| mock_abspath, | ||
| mock_exists, | ||
| ): | ||
| mock_abspath.return_value = "/valid/parent/file" | ||
| mock_dirname.return_value = "/valid/parent" | ||
| mock_exists.return_value = True | ||
| path = "file" | ||
| self.assertEqual(valid_path(path), path) | ||
|
|
||
| @patch("os.path.exists") | ||
| @patch("os.path.abspath") | ||
| @patch("os.path.dirname") | ||
| def test_valid_path__parent_missing__raises_error( | ||
| self, | ||
| mock_dirname, | ||
| mock_abspath, | ||
| mock_exists, | ||
| ): | ||
| mock_abspath.return_value = "/invalid/parent/file" | ||
| mock_dirname.return_value = "/invalid/parent" | ||
| mock_exists.return_value = False | ||
| with self.assertRaises(argparse.ArgumentTypeError): | ||
| valid_path("file") |
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,11 @@ | ||
| """CLI validation types.""" | ||
|
|
||
| from .existing_directory import existing_directory | ||
| from .language_code import language_code | ||
| from .valid_path import valid_path | ||
|
|
||
| __all__ = ( | ||
| "existing_directory", | ||
| "language_code", | ||
| "valid_path", | ||
| ) |
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,13 @@ | ||
| """existing_directory argparse type.""" | ||
|
|
||
| import argparse | ||
| import os | ||
|
|
||
|
|
||
| def existing_directory(path: str) -> str: | ||
| """Check if the provided path is an existing directory.""" | ||
| if not os.path.isdir(path): | ||
| raise argparse.ArgumentTypeError( | ||
| f"The directory '{path}' does not exist.", | ||
| ) | ||
| return path |
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,16 @@ | ||
| """language_code argparse type.""" | ||
|
|
||
| import argparse | ||
| import re | ||
|
|
||
| LANGUAGE_CODE_REGEX = re.compile(r"^[a-z]{2}(_[A-Z]{2})?$") | ||
|
|
||
|
|
||
| def language_code(code: str) -> str: | ||
| """Check if the language code matches the allowed pattern.""" | ||
| if not LANGUAGE_CODE_REGEX.match(code): | ||
| raise argparse.ArgumentTypeError( | ||
| f"Language code '{code}' is invalid. " | ||
| "Expected format like 'en' or 'zh_CN'.", | ||
| ) | ||
| return code |
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,14 @@ | ||
| """valid_path argparse type.""" | ||
|
|
||
| import argparse | ||
| import os | ||
|
|
||
|
|
||
| def valid_path(path: str) -> str: | ||
| """Check if the provided path's parent directory exists.""" | ||
| parent = os.path.dirname(os.path.abspath(path)) | ||
| if not os.path.exists(parent): | ||
| raise argparse.ArgumentTypeError( | ||
| f"The parent directory of '{path}' does not exist.", | ||
| ) | ||
| return path |
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,53 @@ | ||
| """Sphinx related pre-commit scripts.""" | ||
|
|
||
| import argparse | ||
| import os | ||
|
|
||
| from .cli.types import existing_directory, language_code, valid_path | ||
| from .system import call | ||
|
|
||
|
|
||
| def sphinx_build_language() -> None: | ||
| """Build sphinx documentation for a specific language.""" | ||
| parser = argparse.ArgumentParser( | ||
| description="Build sphinx documentation for a specific language.", | ||
| ) | ||
| parser.add_argument( | ||
| "-l", | ||
| "--language", | ||
| required=True, | ||
| type=language_code, | ||
| help="The target language (e.g. en)", | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--source", | ||
| required=True, | ||
| type=existing_directory, | ||
| help="The source folder", | ||
| ) | ||
| parser.add_argument( | ||
| "-b", | ||
| "--build", | ||
| required=True, | ||
| type=valid_path, | ||
| help="The build folder", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| target_build_folder = os.path.join(args.build, args.language) | ||
| command = [ | ||
| "poetry", | ||
| "run", | ||
| "sphinx-build", | ||
| "-Ea", | ||
| "-b", | ||
| "html", | ||
| "-D", | ||
| f"language={args.language}", | ||
| args.source, | ||
| target_build_folder, | ||
| ] | ||
|
|
||
| call(command) |
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,102 @@ | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from cicd_tools_pre_commit.sphinx import sphinx_build_language | ||
|
|
||
|
|
||
| class TestSphinx(unittest.TestCase): | ||
niall-byrne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @patch( | ||
| "sys.argv", | ||
| ["sphinx_build_language", "-l", "en", "-t", "source", "-b", "build"], | ||
| ) | ||
| @patch("cicd_tools_pre_commit.sphinx.call") | ||
| @patch("cicd_tools_pre_commit.sphinx.existing_directory") | ||
| @patch("cicd_tools_pre_commit.sphinx.language_code") | ||
| @patch("cicd_tools_pre_commit.sphinx.valid_path") | ||
| def test_sphinx_build_language__valid_args__calls_sphinx_build( | ||
| self, | ||
| mock_valid_path, | ||
| mock_language_code, | ||
| mock_existing_directory, | ||
| mock_call, | ||
| ): | ||
| mock_existing_directory.side_effect = lambda x: x | ||
| mock_language_code.side_effect = lambda x: x | ||
| mock_valid_path.side_effect = lambda x: x | ||
|
|
||
| sphinx_build_language() | ||
|
|
||
| mock_call.assert_called_once_with( | ||
| [ | ||
| "poetry", | ||
| "run", | ||
| "sphinx-build", | ||
| "-Ea", | ||
| "-b", | ||
| "html", | ||
| "-D", | ||
| "language=en", | ||
| "source", | ||
| "build/en", | ||
| ], | ||
| ) | ||
|
|
||
| @patch( | ||
| "sys.argv", | ||
| [ | ||
| "sphinx_build_language", | ||
| "-l", | ||
| "invalid", | ||
| "-t", | ||
| "source", | ||
| "-b", | ||
| "build", | ||
| ], | ||
| ) | ||
| @patch("cicd_tools_pre_commit.sphinx.call") | ||
| @patch("argparse.ArgumentParser.error") | ||
| @patch("cicd_tools_pre_commit.sphinx.existing_directory") | ||
| @patch("cicd_tools_pre_commit.sphinx.valid_path") | ||
| def test_sphinx_build_language__invalid_language__raises_system_exit( | ||
| self, | ||
| mock_valid_path, | ||
| mock_existing_directory, | ||
| mock_error, | ||
| mock_call, | ||
| ): | ||
| mock_error.side_effect = SystemExit(2) | ||
| mock_existing_directory.side_effect = lambda x: x | ||
| mock_valid_path.side_effect = lambda x: x | ||
|
|
||
| with self.assertRaises(SystemExit): | ||
| sphinx_build_language() | ||
|
|
||
| mock_call.assert_not_called() | ||
|
|
||
| @patch( | ||
| "sys.argv", | ||
| ["sphinx_build_language", "-l", "en", "-t", "missing", "-b", "build"], | ||
| ) | ||
| @patch("cicd_tools_pre_commit.sphinx.call") | ||
| @patch("argparse.ArgumentParser.error") | ||
| @patch("cicd_tools_pre_commit.sphinx.existing_directory") | ||
| @patch("cicd_tools_pre_commit.sphinx.language_code") | ||
| @patch("cicd_tools_pre_commit.sphinx.valid_path") | ||
| def test_sphinx_build_language__missing_source__raises_system_exit( | ||
| self, | ||
| mock_valid_path, | ||
| mock_language_code, | ||
| mock_existing_directory, | ||
| mock_error, | ||
| mock_call, | ||
| ): | ||
| mock_error.side_effect = SystemExit(2) | ||
| mock_existing_directory.side_effect = SystemExit(2) | ||
| mock_language_code.side_effect = lambda x: x | ||
| mock_valid_path.side_effect = lambda x: x | ||
|
|
||
| with self.assertRaises(SystemExit): | ||
| sphinx_build_language() | ||
|
|
||
| mock_call.assert_not_called() | ||
Oops, something went wrong.
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.