Skip to content

Commit

Permalink
Fix: Handle no valid python files in the directory. (#83)
Browse files Browse the repository at this point in the history
If you run torchfix script on a directory without Python files, it will
terminate with an error. This modifies the script to just do nothing in
such cases, without terminating with an error.


### Testing:
Added test "test_no_python_files"

### Without fix the new test fails:
   assert 1 == 0
...... raise Exception("Must have at least one job to
process!")\nException: Must have at least one job to
process!\n').returncode ......
  • Loading branch information
zleman1593 authored Nov 12, 2024
1 parent 87289c1 commit 6bfffd9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
29 changes: 23 additions & 6 deletions tests/test_torchfix.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -103,3 +105,18 @@ 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
result = subprocess.run(
["python3", "-m", "torchfix", str(tmp_path)],
capture_output=True,
text=True,
)
# Check that the script exits successfully
assert result.returncode == 0
15 changes: 9 additions & 6 deletions torchfix/__main__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -97,6 +98,8 @@ def main() -> None:
torch_files.append(file)
break

if not torch_files:
return
config = TorchCodemodConfig()
config.select = list(process_error_code_str(args.select))
command_instance = TorchCodemod(codemod.CodemodContext(), config)
Expand Down

0 comments on commit 6bfffd9

Please sign in to comment.