Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test fixtures to auto-teardown #5

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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