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: Handle no valid python files in the directory. #83

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
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
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