diff --git a/.github/workflows/build-test-publish.yaml b/.github/workflows/build-test-publish.yaml index caf1378..7fb479e 100644 --- a/.github/workflows/build-test-publish.yaml +++ b/.github/workflows/build-test-publish.yaml @@ -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 @@ -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 @@ -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 @@ -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: @@ -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 @@ -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: diff --git a/pyproject.toml b/pyproject.toml index 92bbca4..82a8794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [project] name = "pyprojen" description = "Minimal, spiritual fork of projen. No JSII. Python focused." -authors = [{ 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" } diff --git a/src/pyprojen/cleanup.py b/src/pyprojen/cleanup.py index 3e9aee3..82dc6af 100644 --- a/src/pyprojen/cleanup.py +++ b/src/pyprojen/cleanup.py @@ -16,7 +16,7 @@ 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]): @@ -24,7 +24,7 @@ def remove_files(files: List[str]): 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]: @@ -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 [] diff --git a/src/pyprojen/util/semver.py b/src/pyprojen/util/semver.py index 737728d..eea2ae1 100644 --- a/src/pyprojen/util/semver.py +++ b/src/pyprojen/util/semver.py @@ -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. diff --git a/src/pyprojen/util/synth.py b/src/pyprojen/util/synth.py index 97cd138..385efa2 100644 --- a/src/pyprojen/util/synth.py +++ b/src/pyprojen/util/synth.py @@ -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" diff --git a/tests/conftest.py b/tests/conftest.py index fd33401..b50c843 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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", ] diff --git a/tests/fixtures/example_fixture.py b/tests/fixtures/example_fixture.py deleted file mode 100644 index db837b5..0000000 --- a/tests/fixtures/example_fixture.py +++ /dev/null @@ -1,14 +0,0 @@ -"""Example pytest fixture.""" - -from uuid import uuid4 - -import pytest - -from tests.consts import PROJECT_DIR - - -@pytest.fixture(scope="session") -def test_session_id() -> str: - """Demonstrate how pytest fixtures are used.""" - test_session_id = str(PROJECT_DIR.name) + str(uuid4())[:6] - return test_session_id diff --git a/tests/fixtures/test_project.py b/tests/fixtures/test_project.py new file mode 100644 index 0000000..aa17e78 --- /dev/null +++ b/tests/fixtures/test_project.py @@ -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() + 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) diff --git a/tests/unit_tests/test__example.py b/tests/unit_tests/test__example.py deleted file mode 100644 index 9fe4aab..0000000 --- a/tests/unit_tests/test__example.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Example test file in place so that a freshly created project can immediately pass tests.""" - - -def test__example(): - """An example test to demonstrate how to write unit tests.""" - assert True diff --git a/tests/unit_tests/test__textfile.py b/tests/unit_tests/test__textfile.py new file mode 100644 index 0000000..9b4d599 --- /dev/null +++ b/tests/unit_tests/test__textfile.py @@ -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) + + 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"]) diff --git a/version.txt b/version.txt index f9cece5..7df503e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v0.0.3 +v0.0.4