Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update:goptest absolutly path output #61

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
luoliwoshang marked this conversation as resolved.
Show resolved Hide resolved
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 {
luoliwoshang marked this conversation as resolved.
Show resolved Hide resolved
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');
}
Loading