feat: consider the workdir in pytest annotations

This commit is contained in:
Martin Fillafer 2023-03-14 11:30:29 +01:00
parent 7442569c25
commit 0d3dd791da
3 changed files with 40 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import * as path from 'path'
import {PytestJunitParser} from '../src/parsers/pytest-junit/pytest-junit-parser'
import {ParseOptions} from '../src/test-parser'
import {normalizeFilePath} from '../src/utils/path-utils'
import {getAnnotations} from '../src/report/get-annotations'
describe('pytest-junit tests', () => {
it('test with one successful test', async () => {
@ -29,7 +30,8 @@ describe('pytest-junit tests', () => {
const opts: ParseOptions = {
parseErrors: true,
trackedFiles: []
workDir: 'mnt/extra-addons',
trackedFiles: ['mnt/extra-addons/product_changes/tests/first_test.py']
}
const parser = new PytestJunitParser(opts)
@ -40,5 +42,9 @@ describe('pytest-junit tests', () => {
line: 6,
message: 'assert False'
})
const annotations = getAnnotations([result], 1)
expect(annotations.length).toBe(1)
expect(annotations[0].path).toBe('product_changes/tests/first_test.py')
})
})

15
dist/index.js generated vendored
View file

@ -1415,6 +1415,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.PytestJunitParser = void 0;
const xml2js_1 = __nccwpck_require__(6189);
const test_results_1 = __nccwpck_require__(2768);
const path_utils_1 = __nccwpck_require__(4070);
class PytestJunitParser {
constructor(options) {
this.options = options;
@ -1493,13 +1494,25 @@ class PytestJunitParser {
const line = Number.parseInt(pos);
if (path && Number.isFinite(line)) {
return {
path,
path: this.getRelativePath(path),
line,
message: lines[1]
};
}
return undefined;
}
getRelativePath(path) {
path = (0, path_utils_1.normalizeFilePath)(path);
const workDir = this.getWorkDir(path);
if (workDir !== undefined && path.startsWith(workDir)) {
path = path.substring(workDir.length + 1);
}
return path;
}
getWorkDir(path) {
var _a, _b;
return ((_b = (_a = this.options.workDir) !== null && _a !== void 0 ? _a : this.assumedWorkDir) !== null && _b !== void 0 ? _b : (this.assumedWorkDir = (0, path_utils_1.getBasePath)(path, this.options.trackedFiles)));
}
}
exports.PytestJunitParser = PytestJunitParser;

View file

@ -11,6 +11,7 @@ import {
TestCaseResult,
TestCaseError
} from '../../test-results'
import {getBasePath, normalizeFilePath} from '../../utils/path-utils'
export class PytestJunitParser implements TestParser {
assumedWorkDir: string | undefined
@ -102,7 +103,7 @@ export class PytestJunitParser implements TestParser {
if (path && Number.isFinite(line)) {
return {
path,
path: this.getRelativePath(path),
line,
message: lines[1]
}
@ -110,4 +111,21 @@ export class PytestJunitParser implements TestParser {
return undefined
}
private getRelativePath(path: string): string {
path = normalizeFilePath(path)
const workDir = this.getWorkDir(path)
if (workDir !== undefined && path.startsWith(workDir)) {
path = path.substring(workDir.length + 1)
}
return path
}
private getWorkDir(path: string): string | undefined {
return (
this.options.workDir ??
this.assumedWorkDir ??
(this.assumedWorkDir = getBasePath(path, this.options.trackedFiles))
)
}
}