Skip to content

Commit

Permalink
Make pytest-snapshot closer to kotest (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Apr 23, 2024
2 parents 584f692 + 337ed7c commit f1325e8
Show file tree
Hide file tree
Showing 30 changed files with 1,290 additions and 524 deletions.
34 changes: 20 additions & 14 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,35 @@ jobs:
with:
python-version-file: "python/selfie-lib/pyproject.toml"
cache: "poetry"
- run: poetry install
- name: selfie-lib - poetry install
run: poetry install
working-directory: python/selfie-lib
- run: poetry run pytest -vv
- name: selfie-lib - pytest
run: poetry run pytest -vv
working-directory: python/selfie-lib
- run: poetry run pyright
- name: selfie-lib - pyright
run: poetry run pyright
working-directory: python/selfie-lib
- run: poetry run ruff format --check
- name: selfie-lib - ruff
run: poetry run ruff format --check
working-directory: python/selfie-lib
- run: poetry install
- name: pytest-selfie - poetry install
run: poetry install
working-directory: python/pytest-selfie
- run: poetry run pytest -vv
- name: pytest-selfie - pyright
run: poetry run pyright
working-directory: python/pytest-selfie
- run: poetry run tox -e py
- name: pytest-selfie - ruff
run: poetry run ruff format --check
working-directory: python/pytest-selfie
- run: poetry run pyright
working-directory: python/pytest-selfie
- run: poetry run ruff format --check
working-directory: python/pytest-selfie
- run: poetry install
- name: example-pytest-selfie - poetry install
run: poetry install
working-directory: python/example-pytest-selfie
# - run: poetry run pytest -vv
# working-directory: python/example-pytest-selfie
- run: poetry run pyright
- name: example-pytest-selfie - pyright
run: poetry run pyright
working-directory: python/example-pytest-selfie
- run: poetry run ruff format --check
- name: example-pytest-selfie - ruff
run: poetry run ruff format --check
working-directory: python/example-pytest-selfie
12 changes: 6 additions & 6 deletions python/example-pytest-selfie/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions python/example-pytest-selfie/tests/Simple_test.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from selfie_lib.Selfie import expect_selfie


def test_comment_removal(): # selfieonce
expect_selfie("no op").to_be("no op")
13 changes: 13 additions & 0 deletions python/example-pytest-selfie/tests/simple_inline_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from selfie_lib.Selfie import expect_selfie


# def test_read_pass():
# expect_selfie("A").to_be("A")


# def test_read_fail():
# expect_selfie("A").to_be("B")


def test_write():
expect_selfie("B").to_be_TODO()
9 changes: 9 additions & 0 deletions python/example-pytest-selfie/tests/simple_ondisk_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from selfie_lib.Selfie import expect_selfie


def test_write():
expect_selfie("A").to_match_disk()


def test_read():
expect_selfie("B").to_match_disk_TODO()
12 changes: 6 additions & 6 deletions python/pytest-selfie/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions python/pytest-selfie/pytest_selfie/SelfieSettingsAPI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from pathlib import Path
import re
from typing import Optional
from selfie_lib import Mode
import pytest


class SelfieSettingsAPI:
"""API for configuring the selfie plugin, you can set its values like this https://docs.pytest.org/en/7.1.x/reference/customize.html#configuration-file-formats"""

def __init__(self, config: pytest.Config):
self.root_dir = config.rootpath

@property
def allow_multiple_equivalent_writes_to_one_location(self) -> bool:
"""Allow multiple equivalent writes to one location."""
return True

@property
def snapshot_folder_name(self) -> Optional[str]:
"""Defaults to None, which means that snapshots are stored right next to the test that created them."""
return None

@property
def root_folder(self) -> Path:
"""Returns the root folder for storing snapshots. Set by https://docs.pytest.org/en/7.1.x/reference/customize.html#finding-the-rootdir"""
return self.root_dir

def calc_mode(self) -> Mode:
override = os.getenv("selfie") or os.getenv("SELFIE")
if override:
# Convert the mode to lowercase and match it with the Mode enum
try:
return Mode[override.lower()]
except KeyError:
raise ValueError(f"No such mode: {override}")

ci = os.getenv("ci") or os.getenv("CI")
if ci and ci.lower() == "true":
return Mode.readonly
else:
return Mode.interactive


class SelfieSettingsSmuggleError(SelfieSettingsAPI):
def __init__(self, error: BaseException):
self.error = error
1 change: 1 addition & 0 deletions python/pytest-selfie/pytest_selfie/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .SelfieSettingsAPI import SelfieSettingsAPI as SelfieSettingsAPI
Loading

0 comments on commit f1325e8

Please sign in to comment.