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

Properly mock sys.argv #8

Merged
merged 1 commit into from
Jan 22, 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
44 changes: 18 additions & 26 deletions test/rapids_pre_commit_hooks/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import contextlib
import sys
import tempfile
from unittest.mock import patch

import pytest

from rapids_pre_commit_hooks.lint import Linter, LintMain, OverlappingReplacementsError


class MockArgv(contextlib.AbstractContextManager):
def __init__(self, *argv):
self.argv = argv

def __enter__(self):
self.old_argv = sys.argv
sys.argv = list(self.argv)

def __exit__(self, exc_type, exc_value, traceback):
sys.argv = self.old_argv


class TestLinter:
def test_lines(self):
linter = Linter(
Expand Down Expand Up @@ -151,7 +138,7 @@ def the_check(self, linter, args):
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 patch("sys.argv", ["check-test", "--check-test", hello_world_file.name]):
m = LintMain()
m.argparser.add_argument("--check-test", action="store_true")
with m.execute():
Expand All @@ -161,7 +148,9 @@ def test_no_warnings_no_fix(self, hello_world_file, capsys):
assert captured.out == ""

def test_no_warnings_fix(self, hello_world_file, capsys):
with MockArgv("check-test", "--check-test", "--fix", hello_world_file.name):
with patch(
"sys.argv", ["check-test", "--check-test", "--fix", hello_world_file.name]
):
m = LintMain()
m.argparser.add_argument("--check-test", action="store_true")
with m.execute():
Expand All @@ -171,8 +160,8 @@ def test_no_warnings_fix(self, hello_world_file, capsys):
assert captured.out == ""

def test_warnings_no_fix(self, hello_world_file, capsys):
with MockArgv(
"check-test", "--check-test", hello_world_file.name
with patch(
"sys.argv", ["check-test", "--check-test", hello_world_file.name]
), pytest.raises(SystemExit, match=r"^1$"):
m = LintMain()
m.argparser.add_argument("--check-test", action="store_true")
Expand Down Expand Up @@ -206,8 +195,8 @@ def test_warnings_no_fix(self, hello_world_file, capsys):
)

def test_warnings_fix(self, hello_world_file, capsys):
with MockArgv(
"check-test", "--check-test", "--fix", hello_world_file.name
with patch(
"sys.argv", ["check-test", "--check-test", "--fix", hello_world_file.name]
), pytest.raises(SystemExit, match=r"^1$"):
m = LintMain()
m.argparser.add_argument("--check-test", action="store_true")
Expand Down Expand Up @@ -241,12 +230,15 @@ def test_warnings_fix(self, hello_world_file, capsys):
)

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,
with patch(
"sys.argv",
[
"check-test",
"--check-test",
"--fix",
hello_world_file.name,
hello_file.name,
],
), pytest.raises(SystemExit, match=r"^1$"):
m = LintMain()
m.argparser.add_argument("--check-test", action="store_true")
Expand Down