diff --git a/__tests__/pytest-junit.test.ts b/__tests__/pytest-junit.test.ts index ee5cfc3..b1f06df 100644 --- a/__tests__/pytest-junit.test.ts +++ b/__tests__/pytest-junit.test.ts @@ -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') }) }) diff --git a/dist/index.js b/dist/index.js index a84f2fa..a6fbec0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; diff --git a/src/parsers/pytest-junit/pytest-junit-parser.ts b/src/parsers/pytest-junit/pytest-junit-parser.ts index cad0c0d..41e9932 100644 --- a/src/parsers/pytest-junit/pytest-junit-parser.ts +++ b/src/parsers/pytest-junit/pytest-junit-parser.ts @@ -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)) + ) + } }