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

support pytest-ruff plugin for testing #24698

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# This file has no test, it's just a random script.

if __name__ == "__main__":
print("Hello World!")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This test passes.
def test_function(): # test_marker--test_function
assert 1 == 1
91 changes: 91 additions & 0 deletions python_files/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,94 @@
],
"id_": TEST_DATA_PATH_STR,
}
# This is the expected output for the folder_with_script folder when run with ruff
# └── .data
# └── folder_with_script
# └── script_random.py
# └── ruff
# └── test_simple.py
# └── ruff
# └── test_function
ruff_test_expected_output = {
"name": ".data",
"path": TEST_DATA_PATH_STR,
"type_": "folder",
"children": [
{
"name": "folder_with_script",
"path": os.fspath(TEST_DATA_PATH / "folder_with_script"),
"type_": "folder",
"id_": os.fspath(TEST_DATA_PATH / "folder_with_script"),
"children": [
{
"name": "script_random.py",
"path": os.fspath(TEST_DATA_PATH / "folder_with_script" / "script_random.py"),
"type_": "file",
"id_": os.fspath(TEST_DATA_PATH / "folder_with_script" / "script_random.py"),
"children": [
{
"name": "ruff",
"path": os.fspath(
TEST_DATA_PATH / "folder_with_script" / "script_random.py"
),
"lineno": "",
"type_": "test",
"id_": get_absolute_test_id(
"folder_with_script/script_random.py::ruff",
TEST_DATA_PATH / "folder_with_script" / "script_random.py",
),
"runID": get_absolute_test_id(
"folder_with_script/script_random.py::ruff",
TEST_DATA_PATH / "folder_with_script" / "script_random.py",
),
}
],
},
{
"name": "test_simple.py",
"path": os.fspath(TEST_DATA_PATH / "folder_with_script" / "test_simple.py"),
"type_": "file",
"id_": os.fspath(TEST_DATA_PATH / "folder_with_script" / "test_simple.py"),
"children": [
{
"name": "ruff",
"path": os.fspath(
TEST_DATA_PATH / "folder_with_script" / "test_simple.py"
),
"lineno": "",
"type_": "test",
"id_": get_absolute_test_id(
"folder_with_script/test_simple.py::ruff",
TEST_DATA_PATH / "folder_with_script" / "test_simple.py",
),
"runID": get_absolute_test_id(
"folder_with_script/test_simple.py::ruff",
TEST_DATA_PATH / "folder_with_script" / "test_simple.py",
),
},
{
"name": "test_function",
"path": os.fspath(
TEST_DATA_PATH / "folder_with_script" / "test_simple.py"
),
"lineno": find_test_line_number(
"test_function",
TEST_DATA_PATH / "folder_with_script" / "test_simple.py",
),
"type_": "test",
"id_": get_absolute_test_id(
"folder_with_script/test_simple.py::test_function",
TEST_DATA_PATH / "folder_with_script" / "test_simple.py",
),
"runID": get_absolute_test_id(
"folder_with_script/test_simple.py::test_function",
TEST_DATA_PATH / "folder_with_script" / "test_simple.py",
),
},
],
},
],
}
],
"id_": TEST_DATA_PATH_STR,
}
27 changes: 27 additions & 0 deletions python_files/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,30 @@ def test_config_sub_folder():
if actual_item.get("tests") is not None:
tests: Any = actual_item.get("tests")
assert tests.get("name") == "config_sub_folder"


def test_ruff_plugin():
"""Here the session node will be a subfolder of the workspace root and the test are in another subfolder.

This tests checks to see if test node path are under the session node and if so the
session node is correctly updated to the common path.
"""
file_path = helpers.TEST_DATA_PATH / "folder_with_script"
actual = helpers.runner(
[os.fspath(file_path), "--collect-only", "--ruff"],
)

assert actual
actual_list: List[Dict[str, Any]] = actual
if actual_list is not None:
actual_item = actual_list.pop(0)
assert all(item in actual_item for item in ("status", "cwd", "error"))
assert (
actual_item.get("status") == "success"
), f"Status is not 'success', error is: {actual_item.get('error')}"
assert actual_item.get("cwd") == os.fspath(helpers.TEST_DATA_PATH)
assert is_same_tree(
actual_item.get("tests"),
expected_discovery_test_output.ruff_test_expected_output,
["id_", "lineno", "name", "runID"],
), f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_discovery_test_output.ruff_test_expected_output, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
9 changes: 1 addition & 8 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
import pathlib
import sys
import traceback
from typing import (
TYPE_CHECKING,
Any,
Dict,
Generator,
Literal,
TypedDict,
)
from typing import TYPE_CHECKING, Any, Dict, Generator, Literal, TypedDict

import pytest

Expand Down
16 changes: 10 additions & 6 deletions src/client/testing/testController/common/resultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ export class PythonResultResolver implements ITestResultResolver {
// search through freshly built array of testItem to find the failed test and update UI.
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri && indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
if (indiItem.uri) {
if (indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
}
runInstance.errored(indiItem, message);
}
}
Expand All @@ -210,8 +212,10 @@ export class PythonResultResolver implements ITestResultResolver {
// search through freshly built array of testItem to find the failed test and update UI.
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri && indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
if (indiItem.uri) {
if (indiItem.range) {
message.location = new Location(indiItem.uri, indiItem.range);
}
runInstance.failed(indiItem, message);
}
}
Expand All @@ -222,7 +226,7 @@ export class PythonResultResolver implements ITestResultResolver {
if (grabTestItem !== undefined) {
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri && indiItem.range) {
if (indiItem.uri) {
runInstance.passed(grabTestItem);
}
}
Expand All @@ -234,7 +238,7 @@ export class PythonResultResolver implements ITestResultResolver {
if (grabTestItem !== undefined) {
testCases.forEach((indiItem) => {
if (indiItem.id === grabVSid) {
if (indiItem.uri && indiItem.range) {
if (indiItem.uri) {
runInstance.skipped(grabTestItem);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/client/testing/testController/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ export function populateTestTree(
const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path));
testItem.tags = [RunTestTag, DebugTestTag];

const range = new Range(
new Position(Number(child.lineno) - 1, 0),
new Position(Number(child.lineno), 0),
);
let range: Range | undefined;
if (child.lineno) {
range = new Range(new Position(Number(child.lineno) - 1, 0), new Position(Number(child.lineno), 0));
}
testItem.canResolveChildren = false;
testItem.range = range;
testItem.tags = [RunTestTag, DebugTestTag];
Expand Down
Loading