Skip to content
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

Added first unit test for pyprojen #1

Merged
merged 11 commits into from
Sep 30, 2024
36 changes: 18 additions & 18 deletions .github/workflows/build-test-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
check-version-txt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Tag with the release version
Expand All @@ -32,11 +32,11 @@ jobs:
lint-format-and-static-code-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'
- name: Install pre-commit
run: |
pip install pre-commit
Expand All @@ -47,11 +47,11 @@ jobs:
build-wheel-and-sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'
- name: Install build CLI
run: |
pip install build
Expand All @@ -69,11 +69,11 @@ jobs:
- build-wheel-and-sdist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'
- name: Download wheel and sdist
uses: actions/download-artifact@v3
with:
Expand All @@ -82,7 +82,7 @@ jobs:
- name: Install test dependencies
run: |
pip install pytest pytest-cov ./dist/*.whl
- name: Lint, Format, and other static code quality checks
- name: Run tests
run: |
/bin/bash -x run.sh test:ci

Expand All @@ -96,11 +96,11 @@ jobs:
# if - this is a merge to main or push directly to the main branch
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: '3.10'
- name: Download wheel and sdist
uses: actions/download-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[project]
name = "pyprojen"
description = "Minimal, spiritual fork of projen. No JSII. Python focused."
authors = [{ name = "<your name>", email = "some-email@gmail.com" }]
authors = [{ name = "Eric Riddoch", email = "eric.riddoch@gmail.com" }]
readme = "README.md"
requires-python = ">=3.7"
license = { text = "MIT" }
Expand Down
6 changes: 3 additions & 3 deletions src/pyprojen/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def cleanup(dir: str, new_files: List[str], exclude: List[str]):
# Remove all files managed by pyprojen with legacy logic
remove_files(find_generated_files(dir, exclude))
except Exception as e:
logging.warn(f"warning: failed to clean up generated files: {str(e)}")
logging.warning(f"warning: failed to clean up generated files: {str(e)}")


def remove_files(files: List[str]):
for file in files:
try:
os.remove(file)
except Exception as e:
logging.warn(f"Failed to remove file {file}: {str(e)}")
logging.warning(f"Failed to remove file {file}: {str(e)}")


def find_orphaned_files(dir: str, old_files: List[str], new_files: List[str]) -> List[str]:
Expand All @@ -46,5 +46,5 @@ def get_files_from_manifest(dir: str) -> List[str]:
if "files" in manifest:
return manifest["files"]
except Exception as e:
logging.warn(f"warning: unable to get files to clean from file manifest: {str(e)}")
logging.warning(f"warning: unable to get files to clean from file manifest: {str(e)}")
return []
5 changes: 4 additions & 1 deletion src/pyprojen/util/semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def to_release_version(assembly_version: str, target: TargetName) -> str:


def to_bracket_notation(
semver_range: str, suffix: Optional[str] = None, semver: bool = True, target: TargetName = TargetName.JAVASCRIPT
semver_range: str,
suffix: Optional[str] = None,
semver: bool = True,
target: TargetName = TargetName.JAVASCRIPT,
) -> str:
"""
Convert a semver range to bracket notation.
Expand Down
2 changes: 2 additions & 0 deletions src/pyprojen/util/synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def synth_snapshot(project: "Project", options: SnapshotOptions = SnapshotOption
:param options: Options for creating the snapshot
:return: A dictionary representing the snapshot
"""
from pyprojen.json_file import JsonFile

if not project.outdir.startswith(tempfile.gettempdir()) and "project-temp-dir" not in project.outdir:
raise ValueError(
"Trying to capture a snapshot of a project outside of tmpdir, which implies this test might corrupt an existing project"
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@

# module import paths to python files containing fixtures
pytest_plugins = [
# e.g. "tests/fixtures/example_fixture.py" should be registered as:
"tests.fixtures.example_fixture",
"tests.fixtures.test_project",
]
14 changes: 0 additions & 14 deletions tests/fixtures/example_fixture.py

This file was deleted.

24 changes: 24 additions & 0 deletions tests/fixtures/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import shutil
import tempfile
from typing import (
Any,
Generator,
)

import pytest

from pyprojen.project import Project


@pytest.fixture(scope="function")
def test_project() -> Generator[Project, Any, None]:
"""Create a temporary project for testing."""
# Setup: Create a TestProject instance with a temporary directory
outdir = tempfile.mkdtemp()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Genius. So cool that this works.

project = Project(name="test-project", outdir=outdir)

# Yield the project for use in the test
yield project

# Teardown: Cleanup the temporary directory after the test
shutil.rmtree(outdir)
6 changes: 0 additions & 6 deletions tests/unit_tests/test__example.py

This file was deleted.

48 changes: 48 additions & 0 deletions tests/unit_tests/test__textfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pyprojen.project import Project
from pyprojen.textfile import TextFile
from pyprojen.util.synth import synth_snapshot

TEST_FILE_PATH = "hello/foo.txt"


def test__empty_file(test_project: Project):
"""Test that an empty file is created."""
# GIVEN: The project is already set up by the fixture

# WHEN: A TextFile is added to the project
TextFile(scope=test_project, file_path=TEST_FILE_PATH)

# THEN: Take a snapshot of the project and check the file content
output: dict = synth_snapshot(test_project)

assert output[TEST_FILE_PATH] == ""


def test__add_lines(test_project: Project):
"""Test that lines are added to the file."""
# GIVEN: The project is already set up by the fixture

# WHEN: A TextFile is added to the project with some lines
TextFile(scope=test_project, file_path=TEST_FILE_PATH, lines=["line 1", "line 2", "line 3"])

# THEN: Take a snapshot of the project and check the file content
output: dict = synth_snapshot(test_project)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The synth_snapshot function in action!


assert output[TEST_FILE_PATH] == "\n".join(["line 1", "line 2", "line 3"])


def test__add_lines_and_append(test_project: Project):
"""Test that lines are added to the file and appended."""
# GIVEN: The project is already set up by the fixture

# WHEN: A TextFile is added to the project with some lines
tf = TextFile(scope=test_project, file_path=TEST_FILE_PATH, lines=["line 1", "line 2"])

# AND: More lines are appended
tf.add_line("line 3")
tf.add_line("line 4")

# THEN: Take a snapshot of the project and check the file content
output: dict = synth_snapshot(test_project)

assert output[TEST_FILE_PATH] == "\n".join(["line 1", "line 2", "line 3", "line 4"])
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.3
v0.0.4
Loading