-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
260 additions
and
2 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,130 @@ | ||
name: Tests | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
pytest: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
shell: bash -el {0} # setup-miniconda requires bash | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: | | ||
pip3 install toml | ||
echo "python_version=$(python3 scripts/get_python_version.py)" >> "$GITHUB_OUTPUT" | ||
pip3 install --user uv | ||
id: get-python-version | ||
- uses: conda-incubator/setup-miniconda@v3 | ||
with: | ||
miniforge-version: latest | ||
activate-environment: test | ||
python-version: ${{ steps.get-python-version.outputs.python_version }} | ||
- name: Cache Conda environment | ||
id: cache-conda | ||
uses: actions/cache@v4 | ||
env: | ||
cache-name: cache-conda | ||
with: | ||
path: ~/miniconda3/envs/test | ||
key: ${{ runner.os }}-conda-${{ env.cache-name }}-${{ hashFiles('deps/x86_64-unknown-linux-gnu/requirements_dev.txt') }} | ||
# restore-keys: | | ||
# ${{ runner.os }}-conda-${{ env.cache-name }}- | ||
# ${{ runner.os }}-conda- | ||
# ${{ runner.os }}- | ||
- if: steps.cache-conda.outputs.cache-hit == 'true' | ||
run: echo 'conda cache hit!' | ||
- name: Install dependencies | ||
if: steps.cache-conda.outputs.cache-hit != 'true' | ||
run: | | ||
# python -m pip install --upgrade pip | ||
uv pip install -r deps/x86_64-unknown-linux-gnu/requirements_dev.txt | ||
uv pip install -e . | ||
bash scripts/install_binaries.sh | ||
- name: Run pytest | ||
run: | | ||
set +e # Do not exit shell on pytest failure | ||
out=$(pytest 2> stderr.txt) | ||
exit_code=$? | ||
err=$(<stderr.txt) | ||
# Display the raw output in the step | ||
echo "${out}" | ||
echo "${err}" | ||
# Display the Markdown output in the job summary | ||
echo "\`\`\`python" >> $GITHUB_STEP_SUMMARY | ||
echo "${out}" >> $GITHUB_STEP_SUMMARY | ||
echo "${err}" >> $GITHUB_STEP_SUMMARY | ||
if [[ $exit_code -eq 5 ]] | ||
then | ||
echo | ||
echo 'WARNING: No tests were run and it is considered as success' >> $GITHUB_STEP_SUMMARY | ||
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
exit 0 | ||
else | ||
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
# Exit with the exit-code returned by pytest | ||
exit ${exit_code} | ||
fi | ||
doctest: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
shell: bash -el {0} # setup-miniconda requires bash | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: | | ||
pip3 install toml | ||
echo "python_version=$(python3 scripts/get_python_version.py)" >> "$GITHUB_OUTPUT" | ||
pip3 install --user uv | ||
id: get-python-version | ||
- uses: conda-incubator/setup-miniconda@v3 | ||
with: | ||
miniforge-version: latest | ||
activate-environment: test | ||
python-version: ${{ steps.get-python-version.outputs.python_version }} | ||
- name: Cache Conda environment | ||
id: cache-conda | ||
uses: actions/cache@v4 | ||
env: | ||
cache-name: cache-conda | ||
with: | ||
# npm cache files are stored in `~/.npm` on Linux/macOS | ||
path: ~/miniconda3/envs/test | ||
key: ${{ runner.os }}-conda-${{ env.cache-name }}-${{ hashFiles('deps/x86_64-unknown-linux-gnu/requirements_dev.txt') }} | ||
# restore-keys: | | ||
# ${{ runner.os }}-conda-${{ env.cache-name }}- | ||
# ${{ runner.os }}-conda- | ||
# ${{ runner.os }}- | ||
- if: steps.cache-conda.outputs.cache-hit == 'true' | ||
run: echo 'conda cache hit!' | ||
- name: Install dependencies | ||
if: steps.cache-conda.outputs.cache-hit != 'true' | ||
run: | | ||
# python -m pip install --upgrade pip | ||
uv pip install -r deps/x86_64-unknown-linux-gnu/requirements_dev.txt | ||
uv pip install -e . | ||
bash scripts/install_binaries.sh | ||
- name: Run doctest | ||
run: | | ||
set +e # Do not exit shell on pytest failure | ||
out=$(python scripts/run_doctest.py 2> stderr.txt) | ||
exit_code=$? | ||
err=$(<stderr.txt) | ||
# Display the raw output in the step | ||
echo "${out}" | ||
echo "${err}" | ||
# Display the Markdown output in the job summary | ||
echo "\`\`\`python" >> $GITHUB_STEP_SUMMARY | ||
echo "${out}" >> $GITHUB_STEP_SUMMARY | ||
echo "${err}" >> $GITHUB_STEP_SUMMARY | ||
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
# Exit with the exit-code returned by pytest | ||
exit ${exit_code} |
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 |
---|---|---|
@@ -1,7 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
from foo import bar | ||
from ....utils.a.b.c import relative1 | ||
|
||
|
||
def foo(): | ||
def lazy_import(): | ||
from ... import relative_three_dots | ||
|
||
|
||
def foo1(): | ||
from python_import import foo | ||
|
||
def foo2(): | ||
from python_import import foo | ||
|
||
def foo3(): | ||
from python_import import foo | ||
|
||
def invalid1(): | ||
from python_import import foo as bar |
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 |
---|---|---|
@@ -1,2 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
def test_relative_import(): | ||
import logging | ||
from pathlib import Path | ||
|
||
import tree_sitter_python as tspython | ||
from tree_sitter import Language, Parser | ||
|
||
from python_import.utils import get_all_imports_in_file_as_absolute_with_word | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SCRIPT_DIR = Path(__file__).parent | ||
PY_LANGUAGE = Language(tspython.language()) | ||
parser = Parser(PY_LANGUAGE) | ||
|
||
|
||
def test_multiple_counts(): | ||
imports = get_all_imports_in_file_as_absolute_with_word( | ||
SCRIPT_DIR / "sample_projects/project1", | ||
SCRIPT_DIR / "sample_projects/project1/src/myproject1/a/b/c/d.py", | ||
parser, | ||
"foo", | ||
) | ||
|
||
# not "from python_import import foo as bar" | ||
# not from foo import bar | ||
assert imports == { | ||
"from python_import import foo": 3, | ||
} | ||
|
||
|
||
def test_multiple_statements(): | ||
imports = get_all_imports_in_file_as_absolute_with_word( | ||
SCRIPT_DIR / "sample_projects/project1", | ||
SCRIPT_DIR / "sample_projects/project1/src/myproject1/a/b/c/d.py", | ||
parser, | ||
"bar", | ||
) | ||
|
||
assert imports == { | ||
"from foo import bar": 1, | ||
"from python_import import foo as bar": 1, | ||
} | ||
|
||
|
||
def test_relative1(): | ||
imports = get_all_imports_in_file_as_absolute_with_word( | ||
SCRIPT_DIR / "sample_projects/project1", | ||
SCRIPT_DIR / "sample_projects/project1/src/myproject1/a/b/c/d.py", | ||
parser, | ||
"relative1", | ||
) | ||
|
||
assert imports == { | ||
"from myproject1.utils.a.b.c import relative1": 1, | ||
} | ||
|
||
|
||
def test_relative_three_dots(): | ||
# from ... import relative_three_dots1 | ||
imports = get_all_imports_in_file_as_absolute_with_word( | ||
SCRIPT_DIR / "sample_projects/project1", | ||
SCRIPT_DIR / "sample_projects/project1/src/myproject1/a/b/c/d.py", | ||
parser, | ||
"relative_three_dots", | ||
) | ||
|
||
assert imports == { | ||
"from myproject1.a import relative_three_dots": 1, | ||
} |