Skip to content

Commit

Permalink
Properly mock sys.argv (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA authored Jan 22, 2024
1 parent ad67587 commit 5ff1143
Showing 1 changed file with 18 additions and 26 deletions.
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

0 comments on commit 5ff1143

Please sign in to comment.