From 512d5205c6d4d94bf49a5eda82a41310c057141b Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 12:22:24 -0800 Subject: [PATCH 1/6] Return without error when there are no valid python files --- tests/test_torchfix.py | 34 ++++++++++++++++++++++++++++------ torchfix/__main__.py | 16 ++++++++++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index 7b79fb7..882a49e 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -1,16 +1,18 @@ +import logging +import subprocess from pathlib import Path + +import libcst.codemod as codemod from torchfix.torchfix import ( - TorchChecker, - TorchCodemod, - TorchCodemodConfig, DISABLED_BY_DEFAULT, expand_error_codes, - GET_ALL_VISITORS, GET_ALL_ERROR_CODES, + GET_ALL_VISITORS, process_error_code_str, + TorchChecker, + TorchCodemod, + TorchCodemodConfig, ) -import logging -import libcst.codemod as codemod FIXTURES_PATH = Path(__file__).absolute().parent / "fixtures" LOGGER = logging.getLogger(__name__) @@ -103,3 +105,23 @@ def test_errorcodes_distinct(): def test_parse_error_code_str(case, expected): assert process_error_code_str(case) == expected + + +def test_no_python_files(tmp_path): + # Create a temporary directory with no Python files + non_python_file = tmp_path / "not_a_python_file.txt" + non_python_file.write_text("This is not a Python file") + + # Run torchfix on the temporary directory + # TODO: Fix this. This will not run the test on current build + result = subprocess.run( + ["torchfix", str(tmp_path)], + capture_output=True, + text=True, + ) + + # Check that the script exits successfully + assert result.returncode == 0 + + # Check that the correct message is printed + assert "No Python files with torch imports found." in result.stderr diff --git a/torchfix/__main__.py b/torchfix/__main__.py index eb17658..781b8d1 100644 --- a/torchfix/__main__.py +++ b/torchfix/__main__.py @@ -1,20 +1,22 @@ import argparse -import libcst.codemod as codemod import contextlib import ctypes -import sys import io +import sys + +import libcst.codemod as codemod + +from .common import CYAN, ENDC from .torchfix import ( - TorchCodemod, - TorchCodemodConfig, __version__ as TorchFixVersion, DISABLED_BY_DEFAULT, GET_ALL_ERROR_CODES, process_error_code_str, + TorchCodemod, + TorchCodemodConfig, ) -from .common import CYAN, ENDC # Should get rid of this code eventually. @@ -83,7 +85,6 @@ def _parse_args() -> argparse.Namespace: def main() -> None: args = _parse_args() - files = codemod.gather_files(args.path) # Filter out files that don't have "torch" string in them. @@ -97,6 +98,9 @@ def main() -> None: torch_files.append(file) break + if not torch_files: + print("No Python files with torch imports found.", file=sys.stderr) + return config = TorchCodemodConfig() config.select = list(process_error_code_str(args.select)) command_instance = TorchCodemod(codemod.CodemodContext(), config) From 07e09dc4756f443114967447e998e8bd853aedbc Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 12:31:13 -0800 Subject: [PATCH 2/6] Run against current script --- tests/test_torchfix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index 882a49e..dc60e35 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -115,7 +115,7 @@ def test_no_python_files(tmp_path): # Run torchfix on the temporary directory # TODO: Fix this. This will not run the test on current build result = subprocess.run( - ["torchfix", str(tmp_path)], + ["python3", "torchfix/__main__.py", str(tmp_path)], capture_output=True, text=True, ) From 54908dda6f01be174191e44ae1b04faf37a8a645 Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 12:40:16 -0800 Subject: [PATCH 3/6] remove older comment --- tests/test_torchfix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index dc60e35..c3cb49f 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -113,7 +113,7 @@ def test_no_python_files(tmp_path): non_python_file.write_text("This is not a Python file") # Run torchfix on the temporary directory - # TODO: Fix this. This will not run the test on current build + # Seems wrong to call python3 directly here. result = subprocess.run( ["python3", "torchfix/__main__.py", str(tmp_path)], capture_output=True, From d5717ccf5529118e42756e90c5777a2487cd6cf1 Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 12:42:10 -0800 Subject: [PATCH 4/6] Run with -m modifier --- tests/test_torchfix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index c3cb49f..083c144 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -115,7 +115,7 @@ def test_no_python_files(tmp_path): # Run torchfix on the temporary directory # Seems wrong to call python3 directly here. result = subprocess.run( - ["python3", "torchfix/__main__.py", str(tmp_path)], + ["python3", "-m", "torchfix", str(tmp_path)], capture_output=True, text=True, ) From 45d515bd9c44d18bacd0f903a3a0594c780388e1 Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 12:47:08 -0800 Subject: [PATCH 5/6] remove old comment --- tests/test_torchfix.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index 083c144..46f1e71 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -113,13 +113,11 @@ def test_no_python_files(tmp_path): non_python_file.write_text("This is not a Python file") # Run torchfix on the temporary directory - # Seems wrong to call python3 directly here. result = subprocess.run( ["python3", "-m", "torchfix", str(tmp_path)], capture_output=True, text=True, ) - # Check that the script exits successfully assert result.returncode == 0 From 1b523c048f45dde17f65ab5e3ec6fffb0f9a426e Mon Sep 17 00:00:00 2001 From: zackl-meta Date: Mon, 11 Nov 2024 17:08:02 -0800 Subject: [PATCH 6/6] Remove print line --- tests/test_torchfix.py | 3 --- torchfix/__main__.py | 1 - 2 files changed, 4 deletions(-) diff --git a/tests/test_torchfix.py b/tests/test_torchfix.py index 46f1e71..a31bc1b 100644 --- a/tests/test_torchfix.py +++ b/tests/test_torchfix.py @@ -120,6 +120,3 @@ def test_no_python_files(tmp_path): ) # Check that the script exits successfully assert result.returncode == 0 - - # Check that the correct message is printed - assert "No Python files with torch imports found." in result.stderr diff --git a/torchfix/__main__.py b/torchfix/__main__.py index 781b8d1..4964e01 100644 --- a/torchfix/__main__.py +++ b/torchfix/__main__.py @@ -99,7 +99,6 @@ def main() -> None: break if not torch_files: - print("No Python files with torch imports found.", file=sys.stderr) return config = TorchCodemodConfig() config.select = list(process_error_code_str(args.select))