-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add some basic tests for Python (#517)
- Loading branch information
Showing
8 changed files
with
132 additions
and
10 deletions.
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,39 @@ | ||
name: Python Tests | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
paths: | ||
- "python/**" | ||
pull_request: | ||
branches: [main] | ||
paths: | ||
- "python/**" | ||
|
||
jobs: | ||
test: | ||
runs-on: "nscloud-ubuntu-22.04-amd64-4x16" | ||
strategy: | ||
matrix: | ||
python-version: ["3.10"] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install uv | ||
run: | | ||
curl -LsSf https://astral.sh/uv/install.sh | sh | ||
- name: Install dependencies | ||
working-directory: python | ||
run: | | ||
uv venv | ||
uv pip install '.[dev]' | ||
- name: Run tests | ||
working-directory: python | ||
run: | | ||
uv run pytest |
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
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
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,2 @@ | ||
[pytest] | ||
pythonpath = . |
This file was deleted.
Oops, something went wrong.
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,55 @@ | ||
import pytest | ||
from pathlib import Path | ||
from unittest.mock import patch, MagicMock | ||
from gritql.installer import find_install, CLIError | ||
|
||
def test_find_install_existing_grit(): | ||
with patch('shutil.which', return_value='/usr/local/bin/grit'): | ||
assert find_install() == Path('/usr/local/bin/grit') | ||
|
||
def test_find_install_download_grit_macos(): | ||
with patch('shutil.which', return_value=None), \ | ||
patch('sys.platform', 'darwin'), \ | ||
patch('platform.machine', return_value='arm64'), \ | ||
patch('httpx.Client') as mock_client, \ | ||
patch('tarfile.open'), \ | ||
patch('os.chmod'): | ||
|
||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.iter_bytes.return_value = [b'mock_data'] | ||
mock_client.return_value.__enter__.return_value.get.return_value = mock_response | ||
|
||
result = find_install() | ||
assert isinstance(result, Path) | ||
assert result.name == 'marzano' | ||
|
||
# Test the URL that is called | ||
expected_url = "https://github.com/getgrit/gritql/releases/latest/download/marzano-aarch64-apple-darwin.tar.gz" | ||
mock_client.return_value.__enter__.return_value.get.assert_called_once_with(expected_url, follow_redirects=True) | ||
|
||
def test_find_install_download_grit_linux(): | ||
with patch('shutil.which', return_value=None), \ | ||
patch('sys.platform', 'linux'), \ | ||
patch('platform.machine', return_value='x86_64'), \ | ||
patch('httpx.Client') as mock_client, \ | ||
patch('tarfile.open'), \ | ||
patch('os.chmod'): | ||
|
||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.iter_bytes.return_value = [b'mock_data'] | ||
mock_client.return_value.__enter__.return_value.get.return_value = mock_response | ||
|
||
result = find_install() | ||
assert isinstance(result, Path) | ||
assert result.name == 'marzano' | ||
|
||
# Test the URL that is called | ||
expected_url = "https://github.com/getgrit/gritql/releases/latest/download/marzano-x86_64-unknown-linux-gnu.tar.gz" | ||
mock_client.return_value.__enter__.return_value.get.assert_called_once_with(expected_url, follow_redirects=True) | ||
|
||
def test_find_install_windows(): | ||
with patch('sys.platform', 'win32'): | ||
with pytest.raises(CLIError, match="Windows is not supported yet"): | ||
find_install() |
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,26 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from gritql.run import run_cli, apply_pattern | ||
|
||
def test_run_cli(): | ||
with patch('gritql.run.find_install', return_value='/path/to/grit'), \ | ||
patch('subprocess.run') as mock_run: | ||
|
||
mock_run.return_value.returncode = 0 | ||
|
||
assert run_cli(['test', 'args']) == 0 | ||
mock_run.assert_called_once_with(['/path/to/grit', 'test', 'args']) | ||
|
||
def test_apply_pattern(): | ||
with patch('gritql.run.run_cli') as mock_run_cli: | ||
mock_run_cli.return_value = 0 | ||
|
||
assert apply_pattern('test_pattern', ['arg1', 'arg2']) == 0 | ||
mock_run_cli.assert_called_once_with(['apply', 'test_pattern', 'arg1', 'arg2']) | ||
|
||
def test_apply_pattern_with_grit_dir(): | ||
with patch('gritql.run.run_cli') as mock_run_cli: | ||
mock_run_cli.return_value = 0 | ||
|
||
assert apply_pattern('test_pattern', ['arg1'], grit_dir='/path/to/grit') == 0 | ||
mock_run_cli.assert_called_once_with(['apply', 'test_pattern', 'arg1', '--grit-dir', '/path/to/grit']) |
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 |
---|---|---|
|
@@ -6,20 +6,21 @@ classifiers = [ | |
"Programming Language :: Python :: Implementation :: CPython", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
] | ||
version = "0.1.4" | ||
version = "0.1.5" | ||
dependencies = ["typer>=0.9.0", "httpx>=0.18.2"] | ||
|
||
[project.optional-dependencies] | ||
dev = ["pytest>=7.0"] | ||
|
||
|
||
[tool.poetry] | ||
name = "gritql" | ||
version = "0.1.4" | ||
version = "0.1.5" | ||
description = "Python bindings for GritQL" | ||
authors = ["Grit <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.8" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |