0

I have the following conftest.py for a pytest plugin for hy, adapted from the official hy conftest, but AssertionErros are not diffed for files beginning with test-, only test_, even when using -vv on the command line.

import os
from pathlib import Path

import pytest

import hy

NATIVE_TESTS = Path.cwd().joinpath("tests", "native_tests")

# https://github.com/hylang/hy/issues/2029
os.environ.pop("HYSTARTUP", None)


def pytest_collect_file(parent, path):
    if (
        path.ext in (".hy", ".py")
        and (
            path.basename.startswith("test_")
            or path.basename.startswith("test-")
            or path.basename.endswith("_test")
            or path.basename.endswith("-test")
        )
        # Mimics https://github.com/hylang/hy/blob/master/conftest.py#L38:
        and (
            (not NATIVE_TESTS.exists())
            or (
                NATIVE_TESTS.exists() and (str(NATIVE_TESTS) in (path.dirname + os.sep))
            )
        )
        and path.basename != "__init__.hy"
    ):
        path = Path(path)
        path.touch(exist_ok=True)
        return pytest.Module.from_parent(parent, path=path)

I have the following __init__.py as well, but it doesn't seem to be working:

import pytest

pytest.register_assert_rewrite("pytest_hy")
ShadowRylander
  • 361
  • 2
  • 8

1 Answers1

1

Mangling applies to names in Hy code, not file names. So if you have a file named foo_bar.hy, you can import it as (import foo-bar), but calling your file foo-bar.hy is not really supported in any fashion by Hy's import system, let alone by pytest. In particular, (import foo-bar) will look for foo_bar.hy, as just mentioned, not foo-bar.hy.

Kodiologist
  • 2,984
  • 18
  • 33
  • But the conftest picks up these files and runs them anyway; its just the AssertionError diffs that aren't working. – ShadowRylander Jul 31 '23 at 00:26
  • @ShadowRylander I'm surprised it gets that close to working, but it's still not intentionally supported. If you want to make this work, you may need to edit the guts of both pytest and Hy. I'd recommend just using underscores in your file names, though. – Kodiologist Jul 31 '23 at 11:18
  • Will do, but did you not write the original conftest? I adapted it from yours and [this plugin here](https://github.com/arjaz/pytest-hylang). – ShadowRylander Jul 31 '23 at 12:21
  • @ShadowRylander I did, yeah: https://github.com/hylang/hy/commits/master/conftest.py I never tried to support file names with hyphens, though. In Hy's `setup.cfg` you can see how I set `python_functions` to compensate for mangling of function names, as opposed to file names. – Kodiologist Jul 31 '23 at 16:01
  • You might find Hyrule's `conftest.py` enlightening. It's a little simpler. – Kodiologist Jul 31 '23 at 16:03
  • I'll check it out; thanks for all the help! – ShadowRylander Jul 31 '23 at 18:20