Skip to content

Commit

Permalink
Merge pull request #61 from luoliwoshang/goptest-output-path
Browse files Browse the repository at this point in the history
update:goptest absolutly path output
  • Loading branch information
xushiwei authored May 8, 2024
2 parents 5434840 + 869e488 commit dc065c1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import { getCurrentPackage } from './goModules';
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
import { getNonVendorPackages } from './goPackages';
import { getBinPath, getCurrentGoPath, getTempFilePath, LineBuffer, resolvePath } from './util';
import { getModFolderPath } from './goModules';
import { parseEnvFile } from './utils/envUtils';
import {
getEnvPath,
expandFilePathInOutput,
getCurrentGoRoot,
getCurrentGoWorkspaceFromGOPATH
getCurrentGoWorkspaceFromGOPATH,
expandFilePathInErrorOutput
} from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';
import { GoExtensionContext } from './context';
Expand Down Expand Up @@ -312,7 +314,6 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {

outputChannel.appendLine(['Running tool:', goRuntimePath, ...outArgs].join(' '));
outputChannel.appendLine('');

let testResult = false;
try {
testResult = await new Promise<boolean>(async (resolve, reject) => {
Expand Down Expand Up @@ -344,10 +345,22 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
testResultLines.forEach((line) => outputChannel.appendLine(line));
}
});

const modPath =
testconfig.isMod && testconfig.isGop
? (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir
: testconfig.dir;
// go test emits build errors on stderr, which contain paths relative to the cwd
errBuf.onLine((line) => outputChannel.appendLine(expandFilePathInOutput(line, testconfig.dir)));
errBuf.onDone((last) => last && outputChannel.appendLine(expandFilePathInOutput(last, testconfig.dir)));
errBuf.onLine((line) => {
outputChannel.appendLine(
expandFilePathInErrorOutput(line, testconfig.dir, !!testconfig.isGop, modPath)
);
});
errBuf.onDone((last) => {
last &&
outputChannel.appendLine(
expandFilePathInErrorOutput(last, testconfig.dir, !!testconfig.isGop, modPath)
);
});

tp.stdout.on('data', (chunk) => outBuf.append(chunk.toString()));
tp.stderr.on('data', (chunk) => errBuf.append(chunk.toString()));
Expand Down
36 changes: 36 additions & 0 deletions src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,39 @@ export function expandFilePathInOutput(output: string, cwd: string): string {
}
return lines.join('\n');
}

/**
* This function accepts the error output of goptest or gotest and converts any relative file paths within it to absolute file paths.
* @param output
* @param cwd
* @param isGop
* @param modPath
* @returns
*/
export function expandFilePathInErrorOutput(output: string, cwd: string, isGop: boolean, modPath: string): string {
const lines = output.split('\n');
for (let i = 0; i < lines.length; i++) {
const matches = lines[i].match(/\s*(\S+\.(go|gop|gox)):(\d+):/);
if (matches && matches[1]) {
const file = matches[1];
const fileExt = path.extname(file);
let absoluteFilePath = '';
if (path.isAbsolute(file)) {
absoluteFilePath = file;
} else {
// When go+ test output error occurs, the path thrown by cl is relative to the project root directory
if (fileExt === '.go') {
let outputDir = cwd;
if (isGop && path.dirname(file) !== '.') {
// If it is the output of goptest and the file path is not relative to the current directory,
// update outputDir to modPath as the output directory is relative to the module path
outputDir = modPath;
}
absoluteFilePath = path.join(outputDir, file);
} else if (['.gop', '.gox'].includes(fileExt)) absoluteFilePath = path.join(modPath, file);
}
lines[i] = lines[i].replace(file, absoluteFilePath);
}
}
return lines.join('\n');
}

0 comments on commit dc065c1

Please sign in to comment.