-
Notifications
You must be signed in to change notification settings - Fork 3.8k
CASSANDRA-17684 Bundle CQL.html as Python package resource #4584
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
briangoins
wants to merge
6
commits into
apache:trunk
Choose a base branch
from
briangoins:CASSANDRA-17684/trunk
base: trunk
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
6 commits
Select commit
Hold shift + click to select a range
3600d78
CASSANDRA-17684 bundling CQL.html as Python package resource
briangoins b954e0c
test python 3.8 compatibility code for get_docspath
briangoins 4576fa9
remove redundant package directive
briangoins 92131e1
mkdir on `copy-cql-docs-to-pylib` task
briangoins 01d604e
fix context manager pattern
briangoins 37e5ab7
explanatory comment for exception
briangoins 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
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,24 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Bundled resources for cqlshlib. | ||
|
|
||
| This package contains static resources (like CQL documentation) that are | ||
| bundled with cqlshlib for distribution as a Python package. These resources | ||
| are used as fallbacks when the documentation cannot be found in the standard | ||
| installation paths. | ||
| """ |
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,196 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import tempfile | ||
| from unittest.mock import patch | ||
|
|
||
| from .basecase import BaseTestCase | ||
| from cqlshlib.cqlshmain import get_docspath, _get_docs_from_package_resource, Shell | ||
|
|
||
|
|
||
| class TestGetDocspath(BaseTestCase): | ||
| """ | ||
| Tests for the get_docspath() function. | ||
|
|
||
| Verifies that CQL documentation paths are resolved according to the | ||
| function's priority logic. | ||
| """ | ||
|
|
||
| def test_local_dev_path(self): | ||
| """Local doc/cql3/CQL.html takes precedence over all other paths.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| docs_dir = os.path.join(tmpdir, 'doc', 'cql3') | ||
| os.makedirs(docs_dir) | ||
| docs_file = os.path.join(docs_dir, 'CQL.html') | ||
| with open(docs_file, 'w') as f: | ||
| f.write('<html></html>') | ||
|
|
||
| result = get_docspath(tmpdir) | ||
|
|
||
| self.assertTrue(result.startswith('file://')) | ||
| self.assertIn('doc/cql3/CQL.html', result) | ||
| self.assertEqual(result, 'file://' + os.path.abspath(docs_file)) | ||
|
|
||
| def test_linux_package_path(self): | ||
| """Linux package path when local path doesn't exist.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with patch('os.path.exists') as mock_exists: | ||
| def exists_side_effect(path): | ||
| if path == os.path.join(tmpdir, 'doc', 'cql3', 'CQL.html'): | ||
| return False | ||
| if path == '/usr/share/doc/cassandra/CQL.html': | ||
| return True | ||
| return False | ||
|
|
||
| mock_exists.side_effect = exists_side_effect | ||
|
|
||
| result = get_docspath(tmpdir) | ||
|
|
||
| self.assertEqual(result, 'file:///usr/share/doc/cassandra/CQL.html') | ||
|
|
||
| def test_macos_path(self): | ||
| """macOS path when local and Linux paths don't exist.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with patch('os.path.exists') as mock_exists: | ||
| def exists_side_effect(path): | ||
| if path == os.path.join(tmpdir, 'doc', 'cql3', 'CQL.html'): | ||
| return False | ||
| if path == '/usr/share/doc/cassandra/CQL.html': | ||
| return False | ||
| if path == '/usr/local/share/doc/cassandra/CQL.html': | ||
| return True | ||
| return False | ||
|
|
||
| mock_exists.side_effect = exists_side_effect | ||
|
|
||
| result = get_docspath(tmpdir) | ||
|
|
||
| self.assertEqual(result, 'file:///usr/local/share/doc/cassandra/CQL.html') | ||
|
|
||
| def test_package_resource(self): | ||
| """Package resource when filesystem paths don't exist.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with patch('os.path.exists', return_value=False): | ||
| with patch('cqlshlib.cqlshmain._get_docs_from_package_resource') as mock_resource: | ||
| mock_resource.return_value = 'file:///some/resource/path/CQL.html' | ||
|
|
||
| result = get_docspath(tmpdir) | ||
|
|
||
| self.assertEqual(result, 'file:///some/resource/path/CQL.html') | ||
| mock_resource.assert_called_once() | ||
|
|
||
| def test_online_url_fallback(self): | ||
| """Online documentation URL when all local paths fail.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with patch('os.path.exists', return_value=False): | ||
| with patch('cqlshlib.cqlshmain._get_docs_from_package_resource', return_value=None): | ||
| result = get_docspath(tmpdir) | ||
|
|
||
| self.assertEqual(result, Shell.DEFAULT_CQLDOCS_URL) | ||
|
|
||
|
|
||
| class TestGetDocsFromPackageResource(BaseTestCase): | ||
| """Tests for the _get_docs_from_package_resource() function.""" | ||
|
|
||
| def test_returns_none_on_import_error(self): | ||
| """Should return None if importlib.resources is not available.""" | ||
| with patch.dict('sys.modules', {'importlib.resources': None}): | ||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 9)): | ||
| with patch('builtins.__import__', side_effect=ImportError): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_returns_none_when_resource_not_found(self): | ||
| """Should return None if the resource file doesn't exist on filesystem.""" | ||
| from unittest.mock import MagicMock | ||
|
|
||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 9)): | ||
| with patch('importlib.resources.files') as mock_files: | ||
| mock_files.return_value.joinpath.return_value = '/wrong/path/CQL.html' | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_returns_file_url_when_resource_exists(self): | ||
| """Should return file:// URL when resource exists on filesystem.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| resource_file = os.path.join(tmpdir, 'CQL.html') | ||
| with open(resource_file, 'w') as f: | ||
| f.write('<html></html>') | ||
|
|
||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 9)): | ||
| with patch('importlib.resources.files') as mock_files: | ||
| mock_files.return_value.joinpath.return_value = resource_file | ||
| result = _get_docs_from_package_resource() | ||
| self.assertEqual(result, 'file://' + os.path.realpath(resource_file)) | ||
|
|
||
| def test_exception_handling(self): | ||
| """Should handle exceptions gracefully and return None.""" | ||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 9)): | ||
| with patch('importlib.resources.files', side_effect=Exception("Test error")): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_python38_returns_none_on_import_error(self): | ||
| """Should return None if importlib.util is not available on Python 3.8.""" | ||
| with patch.dict('sys.modules', {'importlib.util': None}): | ||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 8)): | ||
| with patch('builtins.__import__', side_effect=ImportError): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_python38_returns_none_when_spec_not_found(self): | ||
| """Should return None if package spec is not found on Python 3.8.""" | ||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 8)): | ||
| with patch('importlib.util.find_spec', return_value=None): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_python38_returns_none_when_resource_not_found(self): | ||
| """Should return None if the resource file doesn't exist on Python 3.8.""" | ||
| from unittest.mock import MagicMock | ||
|
|
||
| mock_spec = MagicMock() | ||
| mock_spec.origin = '/wrong/package/__init__.py' | ||
|
|
||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 8)): | ||
| with patch('importlib.util.find_spec', return_value=mock_spec): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_python38_returns_file_url_when_resource_exists(self): | ||
| """Should return file:// URL when resource exists on Python 3.8.""" | ||
| from unittest.mock import MagicMock | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| resource_file = os.path.join(tmpdir, 'CQL.html') | ||
| with open(resource_file, 'w') as f: | ||
| f.write('<html></html>') | ||
|
|
||
| mock_spec = MagicMock() | ||
| mock_spec.origin = os.path.join(tmpdir, '__init__.py') | ||
|
|
||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 8)): | ||
| with patch('importlib.util.find_spec', return_value=mock_spec): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertEqual(result, 'file://' + os.path.realpath(resource_file)) | ||
|
|
||
| def test_python38_exception_handling(self): | ||
| """Should handle exceptions gracefully and return None on Python 3.8.""" | ||
| with patch('cqlshlib.cqlshmain.sys.version_info', (3, 8)): | ||
| with patch('importlib.util.find_spec', side_effect=Exception("Test error")): | ||
| result = _get_docs_from_package_resource() | ||
| self.assertIsNone(result) |
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 |
|---|---|---|
|
|
@@ -30,6 +30,9 @@ def get_extensions(): | |
| setup( | ||
| name="cassandra-pylib", | ||
| description="Cassandra Python Libraries", | ||
| packages=["cqlshlib"], | ||
| packages=["cqlshlib", "cqlshlib.resources"], | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bschoening this will be one package installed, |
||
| package_data={ | ||
| "cqlshlib.resources": ["CQL.html", "CQL.css"], | ||
| }, | ||
| ext_modules=get_extensions(), | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'except' clause does nothing but pass and there is no explanatory comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added explanatory comment: briangoins@37e5ab7