Skip to content

Commit a753211

Browse files
committed
Fix for a bunch of tests.
1 parent 00cd06f commit a753211

File tree

6 files changed

+75
-51
lines changed

6 files changed

+75
-51
lines changed

news/2 Fixes/17280.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix for unable to import when running unittest.

news/2 Fixes/17285.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve detecting lines when using testing wrappers.

news/2 Fixes/17309.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Missing location info for `async def` functions.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
import inspect
3+
import sys
4+
import traceback
5+
6+
start_dir = sys.argv[1]
7+
pattern = sys.argv[2]
8+
9+
def get_sourceline(obj):
10+
try:
11+
s, n = inspect.getsourcelines(obj)
12+
except:
13+
try:
14+
# this handles `tornado` case we need a better
15+
# way to get to the wrapped function.
16+
# This is a temporary solution
17+
s, n = inspect.getsourcelines(obj.orig_method)
18+
except:
19+
return '*'
20+
21+
for i, v in enumerate(s):
22+
if v.strip().startswith(('def', 'async def')):
23+
return str(n+i)
24+
return '*'
25+
26+
def generate_test_cases(suite):
27+
for test in suite:
28+
if isinstance(test, unittest.TestCase):
29+
yield test
30+
else:
31+
for test_case in generate_test_cases(test):
32+
yield test_case
33+
try:
34+
loader = unittest.TestLoader()
35+
suite = loader.discover(start_dir, pattern=pattern)
36+
37+
38+
print("start") # Don't remove this line
39+
loader_errors = []
40+
for s in generate_test_cases(suite):
41+
tm = getattr(s, s._testMethodName)
42+
testId = s.id()
43+
if testId.startswith("unittest.loader._FailedTest"):
44+
loader_errors.append(s._exception)
45+
else:
46+
print(testId.replace(".", ":") + ":" + get_sourceline(tm))
47+
except:
48+
print("=== exception start ===")
49+
traceback.print_exc()
50+
print("=== exception end ===")
51+
52+
53+
for error in loader_errors:
54+
try:
55+
print("=== exception start ===")
56+
print(error.msg)
57+
print("=== exception end ===")
58+
except:
59+
pass

src/client/common/process/internal/scripts/testing_tools.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const SCRIPTS_DIR = path.join(_SCRIPTS_DIR, 'testing_tools');
1111

1212
export function runAdapter(adapterArgs: string[]): string[] {
1313
const script = path.join(SCRIPTS_DIR, 'run_adapter.py');
14-
// Note that we for now we do not run this "isolated". The
15-
// script relies on some magic that conflicts with the
16-
// isolated script.
1714
return [script, ...adapterArgs];
1815
}
16+
17+
export function unittestDiscovery(args: string[]): string[] {
18+
const script = path.join(SCRIPTS_DIR, 'unittest_discovery.py');
19+
return [script, ...args];
20+
}

src/client/testing/testController/unittest/unittestController.ts

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
TestData,
2121
} from '../common/types';
2222
import { unittestGetTestFolders, unittestGetTestPattern } from './arguments';
23-
import { execCode } from '../../../common/process/internal/python';
2423
import {
2524
createErrorTestItem,
2625
createWorkspaceRootTestItem,
@@ -31,6 +30,7 @@ import {
3130
import { traceError } from '../../../common/logger';
3231
import { sendTelemetryEvent } from '../../../telemetry';
3332
import { EventName } from '../../../telemetry/constants';
33+
import { unittestDiscovery } from '../../../common/process/internal/scripts/testing_tools';
3434

3535
@injectable()
3636
export class UnittestController implements ITestFrameworkController {
@@ -111,51 +111,16 @@ export class UnittestController implements ITestFrameworkController {
111111

112112
const startDir = unittestGetTestFolders(options.args)[0];
113113
const pattern = unittestGetTestPattern(options.args);
114-
const discoveryScript = `
115-
import unittest
116-
import inspect
117-
118-
def get_sourceline(obj):
119-
s, n = inspect.getsourcelines(obj)
120-
for i, v in enumerate(s):
121-
if v.strip().startswith('def'):
122-
return str(n+i)
123-
return '*'
124-
125-
def generate_test_cases(suite):
126-
for test in suite:
127-
if isinstance(test, unittest.TestCase):
128-
yield test
129-
else:
130-
for test_case in generate_test_cases(test):
131-
yield test_case
132-
133-
loader = unittest.TestLoader()
134-
suite = loader.discover("${startDir}", pattern="${pattern}")
135-
136-
print("start") # Don't remove this line
137-
loader_errors = []
138-
for s in generate_test_cases(suite):
139-
tm = getattr(s, s._testMethodName)
140-
testId = s.id()
141-
if testId.startswith("unittest.loader._FailedTest"):
142-
loader_errors.append(s._exception)
143-
else:
144-
print(testId.replace(".", ":") + ":" + get_sourceline(tm))
145-
146-
for error in loader_errors:
147-
try:
148-
print("=== exception start ===")
149-
print(error.msg)
150-
print("=== exception end ===")
151-
except:
152-
pass
153-
`;
114+
let testDir = startDir;
115+
if (path.isAbsolute(startDir)) {
116+
const relative = path.relative(options.cwd, startDir);
117+
testDir = relative.length > 0 ? relative : '.';
118+
}
154119

155120
const runOptions: Options = {
156121
// unittest needs to load modules in the workspace
157122
// isolating it breaks unittest discovery
158-
args: execCode(discoveryScript),
123+
args: unittestDiscovery([startDir, pattern]),
159124
cwd: options.cwd,
160125
workspaceFolder: options.workspaceFolder,
161126
token: options.token,
@@ -168,12 +133,7 @@ for error in loader_errors:
168133
let rawTestData: RawDiscoveredTests | undefined;
169134
try {
170135
const content = await this.discoveryRunner.run(UNITTEST_PROVIDER, runOptions);
171-
rawTestData = await testDiscoveryParser(
172-
options.cwd,
173-
path.isAbsolute(startDir) ? path.relative(options.cwd, startDir) : startDir,
174-
getTestIds(content),
175-
options.token,
176-
);
136+
rawTestData = await testDiscoveryParser(options.cwd, testDir, getTestIds(content), options.token);
177137
this.testData.set(workspace.uri.fsPath, rawTestData);
178138

179139
const exceptions = getTestDiscoveryExceptions(content);

0 commit comments

Comments
 (0)