Skip to content

Commit

Permalink
chore: add some basic tests for Python (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Sep 21, 2024
1 parent 1cb5b5c commit 3725163
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 10 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/python-tests.yml
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"test:wasm": "cd wasm-bindings && wasm-pack test --node",
"test:watch": "nodemon --ext rs --exec \"cargo test --exclude grit-wasm-bindings\"",
"lint:clippy": "cargo clippy -- -D warnings"
"lint:clippy": "cargo clippy -- -D warnings",
"test:python": "cd python/gritql && pytest"
}
}
2 changes: 1 addition & 1 deletion python/gritql/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def find_install() -> Path:
_debug(f"'grit' found in PATH at {grit_path}")
return Path(grit_path)

platform = "apple-darwin" if sys.platform == "darwin" else "linux"
platform = "apple-darwin" if sys.platform == "darwin" else "unknown-linux-gnu"

dir_name = _cache_dir() / "grit"
install_dir = dir_name / ".install"
Expand Down
2 changes: 2 additions & 0 deletions python/gritql/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
2 changes: 0 additions & 2 deletions python/gritql/requirements.txt

This file was deleted.

55 changes: 55 additions & 0 deletions python/gritql/tests/test_installer.py
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()
26 changes: 26 additions & 0 deletions python/gritql/tests/test_run.py
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'])
13 changes: 7 additions & 6 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 3725163

Please sign in to comment.