Skip to content

Commit

Permalink
Add Ruff to CI (#21739)
Browse files Browse the repository at this point in the history
Add Ruff to (lint > action.yml) CI
#21738
  • Loading branch information
anthonykim1 authored Aug 4, 2023
1 parent c490339 commit 3fed49f
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .github/actions/lint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ runs:
python -m black . --check
working-directory: pythonFiles
shell: bash

- name: Run Ruff
run: |
python -m pip install -U ruff
python -m ruff check .
working-directory: pythonFiles
shell: bash
8 changes: 4 additions & 4 deletions pythonFiles/installed_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def parse_requirements(line: str) -> Optional[Requirement]:
return req
elif req.marker.evaluate():
return req
except:
except Exception:
return None


Expand All @@ -51,7 +51,7 @@ def process_requirements(req_file: pathlib.Path) -> List[Dict[str, Union[str, in
try:
# Check if package is installed
metadata(req.name)
except:
except Exception:
diagnostics.append(
{
"line": n,
Expand Down Expand Up @@ -79,7 +79,7 @@ def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]
try:
raw_text = req_file.read_text(encoding="utf-8")
pyproject = tomli.loads(raw_text)
except:
except Exception:
return diagnostics

lines = raw_text.splitlines()
Expand All @@ -91,7 +91,7 @@ def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]
try:
# Check if package is installed
metadata(req.name)
except:
except Exception:
diagnostics.append(
{
"line": n,
Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/normalizeSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def normalize_lines(selection):

# Insert a newline between each top-level statement, and append a newline to the selection.
source = "\n".join(statements) + "\n"
except:
except Exception:
# If there's a problem when parsing statements,
# append a blank line to end the block and send it as-is.
source = selection + "\n\n"
Expand Down
31 changes: 31 additions & 0 deletions pythonFiles/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ ignore = [
'tests/testing_tools/adapter/pytest/test_cli.py',
'tests/testing_tools/adapter/pytest/test_discovery.py',
]

[tool.ruff]
line-length = 140
ignore = ["E402"]
exclude = [
# Ignore testing_tools files same as Pyright way
'get-pip.py',
'install_debugpy.py',
'tensorboard_launcher.py',
'testlauncher.py',
'visualstudio_py_testlauncher.py',
'testing_tools/unittest_discovery.py',
'testing_tools/adapter/util.py',
'testing_tools/adapter/pytest/_discovery.py',
'testing_tools/adapter/pytest/_pytest_item.py',
'tests/debug_adapter/test_install_debugpy.py',
'tests/testing_tools/adapter/.data',
'tests/testing_tools/adapter/test___main__.py',
'tests/testing_tools/adapter/test_discovery.py',
'tests/testing_tools/adapter/test_functional.py',
'tests/testing_tools/adapter/test_report.py',
'tests/testing_tools/adapter/test_util.py',
'tests/testing_tools/adapter/pytest/test_cli.py',
'tests/testing_tools/adapter/pytest/test_discovery.py',
'pythonFiles/testing_tools/*',
'pythonFiles/testing_tools/adapter/pytest/__init__.py',
'pythonFiles/tests/pytestadapter/expected_execution_test_output.py',
'pythonFiles/tests/unittestadapter/.data/discovery_error/file_one.py',
'pythonFiles/tests/unittestadapter/test_utils.py',

]
3 changes: 1 addition & 2 deletions pythonFiles/shell_exec.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
import sys
import subprocess
import sys

# This is a simple solution to waiting for completion of commands sent to terminal.
# 1. Intercept commands send to a terminal
Expand Down
4 changes: 2 additions & 2 deletions pythonFiles/testing_tools/adapter/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

from __future__ import absolute_import

from ._cli import add_subparser as add_cli_subparser
from ._discovery import discover
from ._cli import add_subparser as add_cli_subparser # noqa: F401
from ._discovery import discover # noqa: F401
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
TEST_ADD_FUNCTION = "unittest_folder/test_add.py::TestAddFunction::"
SUCCESS = "success"
FAILURE = "failure"
TEST_SUBTRACT_FUNCTION_NEGATIVE_NUMBERS_ERROR = "self = <test_subtract.TestSubtractFunction testMethod=test_subtract_negative_numbers>\n\n def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers\n self,\n ):\n result = subtract(-2, -3)\n> self.assertEqual(result, 100000)\nE AssertionError: 1 != 100000\n\nunittest_folder/test_subtract.py:25: AssertionError"

TEST_SUBTRACT_FUNCTION_NEGATIVE_NUMBERS_ERROR = "self = <test_subtract.TestSubtractFunction testMethod=test_subtract_negative_numbers>\n\n def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers\n self,\n ):\n result = subtract(-2, -3)\n> self.assertEqual(result, 100000)\nE AssertionError: 1 != 100000\n\nunittest_folder/test_subtract.py:25: AssertionError" # noqa: E501


# This is the expected output for the unittest_folder execute tests
# └── unittest_folder
Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/tests/pytestadapter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import threading
import uuid
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple

TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
from typing_extensions import TypedDict
Expand Down
5 changes: 3 additions & 2 deletions pythonFiles/tests/pytestadapter/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil

import pytest

from tests.pytestadapter import expected_execution_test_output

from .helpers import TEST_DATA_PATH, runner
Expand Down Expand Up @@ -161,7 +162,7 @@ def test_pytest_execution(test_ids, expected_const):
Keyword arguments:
test_ids -- an array of test_ids to run.
expected_const -- a dictionary of the expected output from running pytest discovery on the files.
"""
""" # noqa: E501
args = test_ids
actual = runner(args)
assert actual
Expand All @@ -179,6 +180,6 @@ def test_pytest_execution(test_ids, expected_const):
or actual_result_dict[key]["outcome"] == "error"
):
actual_result_dict[key]["message"] = "ERROR MESSAGE"
if actual_result_dict[key]["traceback"] != None:
if actual_result_dict[key]["traceback"] is not None:
actual_result_dict[key]["traceback"] = "TRACEBACK"
assert actual_result_dict == expected_const
3 changes: 1 addition & 2 deletions pythonFiles/tests/test_create_microvenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys

import create_microvenv
import pytest


def test_create_microvenv():
Expand All @@ -26,4 +25,4 @@ def run_process(args, error_message):
create_microvenv.run_process = run_process

create_microvenv.main()
assert run_process_called == True
assert run_process_called is True
5 changes: 3 additions & 2 deletions pythonFiles/tests/test_create_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import os
import sys

import create_venv
import pytest

import create_venv


@pytest.mark.skipif(
sys.platform == "win32", reason="Windows does not have micro venv fallback."
Expand Down Expand Up @@ -35,7 +36,7 @@ def run_process(args, error_message):
create_venv.main(["--name", ".test_venv"])

# run_process is called when the venv does not exist
assert run_process_called == True
assert run_process_called is True


@pytest.mark.skipif(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import unittest

import something_else # type: ignore
import something_else # type: ignore # noqa: F401


class DiscoveryErrorOne(unittest.TestCase):
Expand Down
4 changes: 3 additions & 1 deletion pythonFiles/tests/unittestadapter/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest

import pytest

from unittestadapter.utils import (
TestNode,
TestNodeTypeEnum,
Expand Down Expand Up @@ -284,7 +285,8 @@ def test_build_decorated_tree() -> None:


def test_build_empty_tree() -> None:
"""The build_test_tree function should return None if there are no discovered test suites, and an empty list of errors if there are none in the discovered data."""
"""The build_test_tree function should return None if there are no discovered test suites,
and an empty list of errors if there are none in the discovered data."""

start_dir = os.fsdecode(TEST_DATA_PATH)
pattern = "does_not_exist*"
Expand Down
7 changes: 4 additions & 3 deletions pythonFiles/unittestadapter/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
sys.path.append(os.fspath(script_dir))
sys.path.insert(0, os.fspath(script_dir / "lib" / "python"))

from testing_tools import process_json_util, socket_manager
from typing_extensions import NotRequired, TypeAlias, TypedDict

from testing_tools import process_json_util, socket_manager
from unittestadapter.utils import parse_unittest_args

DEFAULT_PORT = "45454"
Expand Down Expand Up @@ -194,12 +195,12 @@ def run_tests(
# Discover tests at path with the file name as a pattern (if any).
loader = unittest.TestLoader()

args = {
args = { # noqa: F841
"start_dir": start_dir,
"pattern": pattern,
"top_level_dir": top_level_dir,
}
suite = loader.discover(start_dir, pattern, top_level_dir)
suite = loader.discover(start_dir, pattern, top_level_dir) # noqa: F841

# Run tests.
runner = unittest.TextTestRunner(resultclass=UnittestTestResult)
Expand Down
7 changes: 4 additions & 3 deletions pythonFiles/unittestadapter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def get_source_line(obj) -> str:
"""Get the line number of a test case start line."""
try:
sourcelines, lineno = inspect.getsourcelines(obj)
except:
except Exception:
try:
# tornado-specific, see https://github.com/microsoft/vscode-python/issues/17285.
sourcelines, lineno = inspect.getsourcelines(obj.orig_method)
except:
except Exception:
return "*"

# Return the line number of the first line of the test case definition.
Expand Down Expand Up @@ -226,7 +226,8 @@ def parse_unittest_args(args: List[str]) -> Tuple[str, str, Union[str, None]]:
The returned tuple contains the following items
- start_directory: The directory where to start discovery, defaults to .
- pattern: The pattern to match test files, defaults to test*.py
- top_level_directory: The top-level directory of the project, defaults to None, and unittest will use start_directory behind the scenes.
- top_level_directory: The top-level directory of the project, defaults to None,
and unittest will use start_directory behind the scenes.
"""

arg_parser = argparse.ArgumentParser()
Expand Down
9 changes: 4 additions & 5 deletions pythonFiles/vscode_datascience_helpers/tests/logParser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from io import TextIOWrapper
import sys
import argparse
import os
from io import TextIOWrapper

os.system("color")
from pathlib import Path
import re
from pathlib import Path

parser = argparse.ArgumentParser(description="Parse a test log into its parts")
parser.add_argument("testlog", type=str, nargs=1, help="Log to parse")
Expand Down Expand Up @@ -63,14 +62,14 @@ def splitByPid(testlog):
pid = int(match.group(1))

# See if we've created a log for this pid or not
if not pid in pids:
if pid not in pids:
pids.add(pid)
logFile = "{}_{}.log".format(baseFile, pid)
print("Writing to new log: " + logFile)
logs[pid] = Path(logFile).open(mode="w")

# Add this line to the log
if pid != None:
if pid is not None:
logs[pid].write(line)
# Close all of the open logs
for key in logs:
Expand Down

0 comments on commit 3fed49f

Please sign in to comment.