From 90796688b814982cd40f9b4236cf8c7df56bd786 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Thu, 18 Jan 2024 10:46:54 -0500 Subject: [PATCH] Fix test fixtures to auto-teardown Use the context manager in the fixture, rather than the test, to ensure it is automatically torn down when the test is over. --- test/rapids_pre_commit_hooks/test_lint.py | 137 +++++++++++----------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/test/rapids_pre_commit_hooks/test_lint.py b/test/rapids_pre_commit_hooks/test_lint.py index 0be3782..779c87a 100644 --- a/test/rapids_pre_commit_hooks/test_lint.py +++ b/test/rapids_pre_commit_hooks/test_lint.py @@ -127,20 +127,20 @@ def test_fix(self): class TestLintMain: @pytest.fixture - def tmpfile(self): - f = tempfile.NamedTemporaryFile("w+") - f.write("Hello world!") - f.flush() - f.seek(0) - return f + def hello_world_file(self): + with tempfile.NamedTemporaryFile("w+") as f: + f.write("Hello world!") + f.flush() + f.seek(0) + yield f @pytest.fixture - def tmpfile2(self): - f = tempfile.NamedTemporaryFile("w+") - f.write("Hello!") - f.flush() - f.seek(0) - return f + def hello_file(self): + with tempfile.NamedTemporaryFile("w+") as f: + f.write("Hello!") + f.flush() + f.seek(0) + yield f def the_check(self, linter, args): assert args.check_test @@ -148,56 +148,51 @@ def the_check(self, linter, args): (0, 5), "Good bye" ) if linter.content[5] != "!": - linter.add_warning((5, 5), "use punctuation").add_replacement( - (5, 5), "," - ) - - def test_no_warnings_no_fix(self, tmpfile, capsys): - with tmpfile: - with MockArgv("check-test", "--check-test", tmpfile.name): - with LintMain() as m: - m.argparser.add_argument("--check-test", action="store_true") - assert tmpfile.read() == "Hello world!" + linter.add_warning((5, 5), "use punctuation").add_replacement((5, 5), ",") + + def test_no_warnings_no_fix(self, hello_world_file, capsys): + with MockArgv("check-test", "--check-test", hello_world_file.name): + with LintMain() as m: + m.argparser.add_argument("--check-test", action="store_true") + assert hello_world_file.read() == "Hello world!" captured = capsys.readouterr() assert captured.out == "" - def test_no_warnings_fix(self, tmpfile, capsys): - with tmpfile: - with MockArgv("check-test", "--check-test", "--fix", tmpfile.name): - with LintMain() as m: - m.argparser.add_argument("--check-test", action="store_true") - assert tmpfile.read() == "Hello world!" + def test_no_warnings_fix(self, hello_world_file, capsys): + with MockArgv("check-test", "--check-test", "--fix", hello_world_file.name): + with LintMain() as m: + m.argparser.add_argument("--check-test", action="store_true") + assert hello_world_file.read() == "Hello world!" captured = capsys.readouterr() assert captured.out == "" - def test_warnings_no_fix(self, tmpfile, capsys): - with tmpfile: - with MockArgv("check-test", "--check-test", tmpfile.name), pytest.raises( - SystemExit, match=r"^1$" - ): - with LintMain() as m: - m.argparser.add_argument("--check-test", action="store_true") - m.add_check(self.the_check) - assert tmpfile.read() == "Hello world!" + def test_warnings_no_fix(self, hello_world_file, capsys): + with MockArgv( + "check-test", "--check-test", hello_world_file.name + ), pytest.raises(SystemExit, match=r"^1$"): + with LintMain() as m: + m.argparser.add_argument("--check-test", action="store_true") + m.add_check(self.the_check) + assert hello_world_file.read() == "Hello world!" captured = capsys.readouterr() assert ( captured.out - == f"""In file {tmpfile.name}:1: + == f"""In file {hello_world_file.name}:1: Hello world! ~~~~~ warning: say good bye instead -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ~~~~~Good bye note: suggested fix -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^ warning: use punctuation -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^, note: suggested fix @@ -205,34 +200,33 @@ def test_warnings_no_fix(self, tmpfile, capsys): """ ) - def test_warnings_fix(self, tmpfile, capsys): - with tmpfile: - with MockArgv( - "check-test", "--check-test", "--fix", tmpfile.name - ), pytest.raises(SystemExit, match=r"^1$"): - with LintMain() as m: - m.argparser.add_argument("--check-test", action="store_true") - m.add_check(self.the_check) - assert tmpfile.read() == "Good bye, world!" + def test_warnings_fix(self, hello_world_file, capsys): + with MockArgv( + "check-test", "--check-test", "--fix", hello_world_file.name + ), pytest.raises(SystemExit, match=r"^1$"): + with LintMain() as m: + m.argparser.add_argument("--check-test", action="store_true") + m.add_check(self.the_check) + assert hello_world_file.read() == "Good bye, world!" captured = capsys.readouterr() assert ( captured.out - == f"""In file {tmpfile.name}:1: + == f"""In file {hello_world_file.name}:1: Hello world! ~~~~~ warning: say good bye instead -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ~~~~~Good bye note: suggested fix applied -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^ warning: use punctuation -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^, note: suggested fix applied @@ -240,45 +234,48 @@ def test_warnings_fix(self, tmpfile, capsys): """ ) - def test_multiple_files(self, tmpfile, tmpfile2, capsys): - with tmpfile, tmpfile2: - with MockArgv( - "check-test", "--check-test", "--fix", tmpfile.name, tmpfile2.name - ), pytest.raises(SystemExit, match=r"^1$"): - with LintMain() as m: - m.argparser.add_argument("--check-test", action="store_true") - m.add_check(self.the_check) - assert tmpfile.read() == "Good bye, world!" - assert tmpfile2.read() == "Good bye!" + def test_multiple_files(self, hello_world_file, hello_file, capsys): + with MockArgv( + "check-test", + "--check-test", + "--fix", + hello_world_file.name, + hello_file.name, + ), pytest.raises(SystemExit, match=r"^1$"): + with LintMain() as m: + m.argparser.add_argument("--check-test", action="store_true") + m.add_check(self.the_check) + assert hello_world_file.read() == "Good bye, world!" + assert hello_file.read() == "Good bye!" captured = capsys.readouterr() assert ( captured.out - == f"""In file {tmpfile.name}:1: + == f"""In file {hello_world_file.name}:1: Hello world! ~~~~~ warning: say good bye instead -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ~~~~~Good bye note: suggested fix applied -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^ warning: use punctuation -In file {tmpfile.name}:1: +In file {hello_world_file.name}:1: Hello world! ^, note: suggested fix applied -In file {tmpfile2.name}:1: +In file {hello_file.name}:1: Hello! ~~~~~ warning: say good bye instead -In file {tmpfile2.name}:1: +In file {hello_file.name}:1: Hello! ~~~~~Good bye note: suggested fix applied