Skip to content

Commit

Permalink
Fix test fixtures to auto-teardown (#5)
Browse files Browse the repository at this point in the history
Use the context manager in the fixture, rather than the test, to
ensure it is automatically torn down when the test is over.
  • Loading branch information
KyleFromNVIDIA authored Jan 19, 2024
1 parent 903b95f commit b3f8c8d
Showing 1 changed file with 67 additions and 70 deletions.
137 changes: 67 additions & 70 deletions test/rapids_pre_commit_hooks/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,158 +127,155 @@ 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
linter.add_warning((0, 5), "say good bye instead").add_replacement(
(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
"""
)

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
"""
)

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
Expand Down

0 comments on commit b3f8c8d

Please sign in to comment.