Skip to content

Commit

Permalink
Add logging for failure to retrieve environment variables, testing re…
Browse files Browse the repository at this point in the history
…write (microsoft#22203)
  • Loading branch information
eleanorjboyd authored Oct 12, 2023
1 parent 65c8ac6 commit 9b5f58a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pythonFiles/unittestadapter/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# If I use from utils then there will be an import error in test_discovery.py.
from unittestadapter.utils import TestNode, build_test_tree, parse_unittest_args

DEFAULT_PORT = "45454"
DEFAULT_PORT = 45454


class PayloadDict(TypedDict):
Expand Down Expand Up @@ -121,12 +121,18 @@ def post_response(

start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])

# Perform test discovery.
testPort = int(os.environ.get("TEST_PORT", DEFAULT_PORT))
testUuid = os.environ.get("TEST_UUID")
# Post this discovery payload.
if testPort is DEFAULT_PORT:
print(
"Error[vscode-unittest]: TEST_PORT is not set.",
" TEST_UUID = ",
testUuid,
)
if testUuid is not None:
# Perform test discovery.
payload = discover_tests(start_dir, pattern, top_level_dir, testUuid)
# Post this discovery payload.
post_response(payload, testPort, testUuid)
# Post EOT token.
eot_payload: EOTPayloadDict = {"command_type": "discovery", "eot": True}
Expand Down
19 changes: 16 additions & 3 deletions pythonFiles/unittestadapter/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
from typing_extensions import Literal, NotRequired, TypeAlias, TypedDict
from unittestadapter.utils import parse_unittest_args

DEFAULT_PORT = "45454"

ErrorType = Union[
Tuple[Type[BaseException], BaseException, TracebackType], Tuple[None, None, None]
]
testPort = 0
testUuid = 0
START_DIR = ""
DEFAULT_PORT = 45454


class TestOutcomeEnum(str, enum.Enum):
Expand Down Expand Up @@ -269,7 +268,8 @@ def post_response(
run_test_ids_port_int = (
int(run_test_ids_port) if run_test_ids_port is not None else 0
)

if run_test_ids_port_int == 0:
print("Error[vscode-unittest]: RUN_TEST_IDS_PORT env var is not set.")
# get data from socket
test_ids_from_buffer = []
try:
Expand Down Expand Up @@ -303,6 +303,19 @@ def post_response(

testPort = int(os.environ.get("TEST_PORT", DEFAULT_PORT))
testUuid = os.environ.get("TEST_UUID")
if testPort is DEFAULT_PORT:
print(
"Error[vscode-unittest]: TEST_PORT is not set.",
" TEST_UUID = ",
testUuid,
)
if testUuid is None:
print(
"Error[vscode-unittest]: TEST_UUID is not set.",
" TEST_PORT = ",
testPort,
)
testUuid = "unknown"
if test_ids_from_buffer:
# Perform test execution.
payload = run_tests(
Expand Down
18 changes: 17 additions & 1 deletion pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from testing_tools import socket_manager
from typing_extensions import Literal, TypedDict

DEFAULT_PORT = 45454


class TestData(TypedDict):
"""A general class that all test objects inherit from."""
Expand Down Expand Up @@ -683,8 +685,22 @@ def send_post_request(
payload -- the payload data to be sent.
cls_encoder -- a custom encoder if needed.
"""
testPort = os.getenv("TEST_PORT", 45454)
testPort = os.getenv("TEST_PORT")
testUuid = os.getenv("TEST_UUID")
if testPort is None:
print(
"Error[vscode-pytest]: TEST_PORT is not set.",
" TEST_UUID = ",
testUuid,
)
testPort = DEFAULT_PORT
if testUuid is None:
print(
"Error[vscode-pytest]: TEST_UUID is not set.",
" TEST_PORT = ",
testPort,
)
testUuid = "unknown"
addr = ("localhost", int(testPort))
global __socket

Expand Down
2 changes: 2 additions & 0 deletions pythonFiles/vscode_pytest/run_pytest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
run_test_ids_port_int = (
int(run_test_ids_port) if run_test_ids_port is not None else 0
)
if run_test_ids_port_int == 0:
print("Error[vscode-pytest]: RUN_TEST_IDS_PORT env var is not set.")
test_ids_from_buffer = []
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
mutableEnv.PYTHONPATH = pythonPathCommand;
mutableEnv.TEST_UUID = uuid.toString();
mutableEnv.TEST_PORT = this.testServer.getPort().toString();

traceInfo(`All environment variables set for pytest discovery: ${JSON.stringify(mutableEnv)}`);
const spawnOptions: SpawnOptions = {
cwd,
throwOnStdErr: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
// add port with run test ids to env vars
const pytestRunTestIdsPort = await utils.startTestIdServer(testIds);
mutableEnv.RUN_TEST_IDS_PORT = pytestRunTestIdsPort.toString();
traceInfo(`All environment variables set for pytest execution: ${JSON.stringify(mutableEnv)}`);

const spawnOptions: SpawnOptions = {
cwd,
Expand Down

0 comments on commit 9b5f58a

Please sign in to comment.