diff --git a/plugin_test.py b/plugin_test.py index 53056cc..11cea53 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os from collections import Counter import pytest @@ -10,14 +9,11 @@ pytest_plugins = "pytester" -# result.stderr.no_fnmatch_line() was added to testdir on pytest 5.3.0 -# https://docs.pytest.org/en/stable/changelog.html#pytest-5-3-0-2019-11-19 -def no_fnmatch_line(result: pytest.RunResult, pattern: str): - result.stderr.no_fnmatch_line(pattern + "*") - - -def test_annotation_succeed_no_output(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_succeed_no_output( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -26,14 +22,16 @@ def test_success(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() + result.stderr.no_fnmatch_line("::error file=test_annotation_succeed_no_output.py*") - no_fnmatch_line(result, "::error file=test_annotation_succeed_no_output.py") - -def test_annotation_pytest_error(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_pytest_error( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -46,8 +44,8 @@ def test_error(): assert fixture() == 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.re_match_lines( [ @@ -56,8 +54,8 @@ def test_error(): ) -def test_annotation_fail(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_fail(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -66,8 +64,8 @@ def test_fail(): assert 0 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_annotation_fail.py,line=5::test_fail*assert 0*", @@ -75,8 +73,11 @@ def test_fail(): ) -def test_annotation_exception(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_exception( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -86,8 +87,8 @@ def test_fail(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_annotation_exception.py,line=5::test_fail*oops*", @@ -95,8 +96,8 @@ def test_fail(): ) -def test_annotation_warning(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_warning(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import warnings import pytest @@ -107,8 +108,8 @@ def test_warning(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::warning file=test_annotation_warning.py,line=6::beware", @@ -116,8 +117,11 @@ def test_warning(): ) -def test_annotation_exclude_warnings(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_exclude_warnings( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import warnings import pytest @@ -128,13 +132,16 @@ def test_warning(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess("--exclude-warning-annotations") + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess("--exclude-warning-annotations") assert not result.stderr.lines -def test_annotation_warning_cwd(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_warning_cwd( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import warnings import pytest @@ -145,11 +152,11 @@ def test_warning(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - testdir.monkeypatch.setenv("GITHUB_WORKSPACE", os.path.dirname(str(testdir.tmpdir))) - testdir.mkdir("foo") - testdir.makefile(".ini", pytest="[pytest]\ntestpaths=..") - result = testdir.runpytest_subprocess("--rootdir=foo") + monkeypatch.setenv("GITHUB_ACTIONS", "true") + monkeypatch.setenv("GITHUB_WORKSPACE", str(pytester.path.parent)) + pytester.mkdir("foo") + pytester.makefile(".ini", pytest="[pytest]\ntestpaths=..") + result = pytester.runpytest_subprocess("--rootdir=foo") result.stderr.fnmatch_lines( [ "::warning file=test_annotation_warning_cwd0/test_annotation_warning_cwd.py,line=6::beware", @@ -157,15 +164,18 @@ def test_warning(): ) -def test_annotation_third_party_exception(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_third_party_exception( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( my_module=""" def fn(): raise Exception('oops') """ ) - testdir.makepyfile( + pytester.makepyfile( """ import pytest from my_module import fn @@ -175,8 +185,8 @@ def test_fail(): fn() """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*", @@ -184,8 +194,11 @@ def test_fail(): ) -def test_annotation_third_party_warning(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_third_party_warning( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( my_module=""" import warnings @@ -194,7 +207,7 @@ def fn(): """ ) - testdir.makepyfile( + pytester.makepyfile( """ import pytest from my_module import fn @@ -204,8 +217,8 @@ def test_warning(): fn() """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( # ["::warning file=test_annotation_third_party_warning.py,line=6::beware",] [ @@ -214,8 +227,11 @@ def test_warning(): ) -def test_annotation_warning_runpath(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_warning_runpath( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import warnings import pytest @@ -226,9 +242,9 @@ def test_warning(): assert 1 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - testdir.monkeypatch.setenv("PYTEST_RUN_PATH", "some_path") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + monkeypatch.setenv("PYTEST_RUN_PATH", "some_path") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::warning file=some_path/test_annotation_warning_runpath.py,line=6::beware", @@ -236,8 +252,11 @@ def test_warning(): ) -def test_annotation_fail_disabled_outside_workflow(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_fail_disabled_outside_workflow( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -246,15 +265,18 @@ def test_fail(): assert 0 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "") - result = testdir.runpytest_subprocess() - no_fnmatch_line( - result, "::error file=test_annotation_fail_disabled_outside_workflow.py*" + monkeypatch.setenv("GITHUB_ACTIONS", "") + result = pytester.runpytest_subprocess() + result.stderr.no_fnmatch_line( + "::error file=test_annotation_fail_disabled_outside_workflow.py*" ) -def test_annotation_fail_cwd(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_fail_cwd( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -263,11 +285,11 @@ def test_fail(): assert 0 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - testdir.monkeypatch.setenv("GITHUB_WORKSPACE", os.path.dirname(str(testdir.tmpdir))) - testdir.mkdir("foo") - testdir.makefile(".ini", pytest="[pytest]\ntestpaths=..") - result = testdir.runpytest_subprocess("--rootdir=foo") + monkeypatch.setenv("GITHUB_ACTIONS", "true") + monkeypatch.setenv("GITHUB_WORKSPACE", str(pytester.path.parent)) + pytester.mkdir("foo") + pytester.makefile(".ini", pytest="[pytest]\ntestpaths=..") + result = pytester.runpytest_subprocess("--rootdir=foo") result.stderr.fnmatch_lines( [ "::error file=test_annotation_fail_cwd0/test_annotation_fail_cwd.py,line=5::test_fail*assert 0*", @@ -275,8 +297,11 @@ def test_fail(): ) -def test_annotation_fail_runpath(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_fail_runpath( + pytester: pytest.Pytester, + monkeypatch: pytest.MonkeyPatch, +): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -285,9 +310,9 @@ def test_fail(): assert 0 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - testdir.monkeypatch.setenv("PYTEST_RUN_PATH", "some_path") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + monkeypatch.setenv("PYTEST_RUN_PATH", "some_path") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=some_path/test_annotation_fail_runpath.py,line=5::test_fail*assert 0*", @@ -295,8 +320,8 @@ def test_fail(): ) -def test_annotation_long(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_long(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -317,18 +342,18 @@ def test_fail(): assert f(x) == 3 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_annotation_long.py,line=17::test_fail*assert 8 == 3*where 8 = f(8)*", ] ) - no_fnmatch_line(result, "::*assert x += 1*") + result.stderr.no_fnmatch_line("::*assert x += 1*") -def test_class_method(testdir: pytest.Testdir): - testdir.makepyfile( +def test_class_method(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -339,18 +364,18 @@ def test_method(self): assert x == 2 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_class_method.py,line=7::TestClass.test_method*assert 1 == 2*", ] ) - no_fnmatch_line(result, "::*x = 1*") + result.stderr.no_fnmatch_line("::*x = 1*") -def test_annotation_param(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_param(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -365,8 +390,8 @@ def test_param(a, b): assert a == b """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ "::error file=test_annotation_param.py,line=11::test_param?other?1*assert 2 == 3*", @@ -378,8 +403,8 @@ def test_param(a, b): version.parse("9.0.0") > PYTEST_VERSION, reason="subtests are only supported in pytest 9+", ) -def test_annotation_subtest(testdir: pytest.Testdir): - testdir.makepyfile( +def test_annotation_subtest(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest pytest_plugins = 'pytest_github_actions_annotate_failures' @@ -390,8 +415,10 @@ def test(subtests): assert i % 2 == 0 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess() + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess() + + assert len(result.stderr.lines) == 2 result.stderr.fnmatch_lines( [ "::error file=test_annotation_subtest.py,line=7::test *custom message* *i=1*assert (1 %25 2) == 0*", @@ -400,8 +427,8 @@ def test(subtests): ) -def test_with_xdist(testdir: pytest.Testdir): - testdir.makepyfile( +def test_with_xdist(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch): + pytester.makepyfile( """ import pytest import warnings @@ -414,8 +441,8 @@ def test_fails(n): assert n == 100 """ ) - testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") - result = testdir.runpytest_subprocess("-n", "10", "-s") + monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = pytester.runpytest_subprocess("-n", "10", "-s") lines = Counter( line for line in result.errlines if line.startswith(("::error ", "::warning ")) )