Skip to content

Commit

Permalink
fix output logs on integration test failure (#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbastien authored Feb 21, 2025
1 parent acf8084 commit 8b57d89
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface Api {
*/
export async function activate(context: vscode.ExtensionContext): Promise<Api> {
try {
const outputChannel = new SwiftOutputChannel("Swift", !process.env["VSCODE_TEST"]);
const outputChannel = new SwiftOutputChannel("Swift");
outputChannel.log("Activating Swift for Visual Studio Code...");

checkAndWarnAboutWindowsSymlinks(outputChannel);
Expand Down
2 changes: 1 addition & 1 deletion src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export class LanguageClientManager implements vscode.Disposable {
documentSelector: LanguageClientManager.documentSelector,
revealOutputChannelOn: RevealOutputChannelOn.Never,
workspaceFolder: workspaceFolder,
outputChannel: new SwiftOutputChannel("SourceKit Language Server", false),
outputChannel: new SwiftOutputChannel("SourceKit Language Server"),
middleware: {
provideCodeLenses: async (document, token, next) => {
const result = await next(document, token);
Expand Down
2 changes: 1 addition & 1 deletion src/toolchain/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export class SwiftToolchain {
const plistKey = type === "XCTest" ? "XCTEST_VERSION" : "SWIFT_TESTING_VERSION";
const version = infoPlist.DefaultProperties[plistKey];
if (!version) {
new SwiftOutputChannel("swift", true).appendLine(
new SwiftOutputChannel("swift").appendLine(
`Warning: ${platformManifest} is missing the ${plistKey} key.`
);
return undefined;
Expand Down
12 changes: 0 additions & 12 deletions src/ui/SwiftOutputChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,21 @@ export class SwiftOutputChannel implements vscode.OutputChannel {
*/
constructor(
public name: string,
private logToConsole: boolean = true,
logStoreLinesSize: number = 250_000 // default to capturing 250k log lines
) {
this.name = name;
this.logToConsole = process.env["CI"] !== "1" && logToConsole;
this.channel = vscode.window.createOutputChannel(name, "Swift");
this.logStore = new RollingLog(logStoreLinesSize);
}

append(value: string): void {
this.channel.append(value);
this.logStore.append(value);

if (this.logToConsole) {
// eslint-disable-next-line no-console
console.log(value);
}
}

appendLine(value: string): void {
this.channel.appendLine(value);
this.logStore.appendLine(value);

if (this.logToConsole) {
// eslint-disable-next-line no-console
console.log(value);
}
}

replace(value: string): void {
Expand Down
2 changes: 1 addition & 1 deletion test/integration-tests/ui/SwiftOutputChannel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ suite("SwiftOutputChannel", function () {
const channels: SwiftOutputChannel[] = [];
setup(function () {
const channelName = `SwiftOutputChannel Tests ${this.currentTest?.id ?? "<unknown test>"}`;
channel = new SwiftOutputChannel(channelName, false, 3);
channel = new SwiftOutputChannel(channelName, 3);
channels.push(channel);
});

Expand Down
40 changes: 19 additions & 21 deletions test/integration-tests/utilities/testutilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,8 @@ const extensionBootstrapper = (() => {
let activator: (() => Promise<Api>) | undefined = undefined;
let activatedAPI: Api | undefined = undefined;
let lastTestName: string | undefined = undefined;
let lastTestLogs: string[] = [];
const testTitle = (currentTest: Mocha.Test) => currentTest.titlePath().join(" → ");

mocha.afterEach(function () {
if (this.currentTest && this.currentTest.isFailed()) {
console.log(`Captured logs during ${testTitle(this.currentTest)}:`);
if (lastTestLogs.length === 0) {
console.log("No logs captured.");
}
for (const log of lastTestLogs) {
console.log(log);
}
}
});

mocha.beforeEach(function () {
if (this.currentTest && activatedAPI && process.env["VSCODE_TEST"]) {
activatedAPI.outputChannel.clear();
activatedAPI.outputChannel.appendLine(`Starting test: ${testTitle(this.currentTest)}`);
}
});

function testRunnerSetup(
before: Mocha.HookFunction,
setup:
Expand Down Expand Up @@ -119,6 +99,25 @@ const extensionBootstrapper = (() => {
}
});

mocha.beforeEach(function () {
if (this.currentTest && activatedAPI) {
activatedAPI.outputChannel.clear();
activatedAPI.outputChannel.appendLine(
`Starting test: ${testTitle(this.currentTest)}`
);
}
});

mocha.afterEach(function () {
if (this.currentTest && activatedAPI && this.currentTest.isFailed()) {
console.log(`Captured logs during ${testTitle(this.currentTest)}:`);
for (const log of activatedAPI.outputChannel.logs) {
console.log(log);
}
console.log("======== END OF LOGS ========\n\n");
}
});

after(async function () {
try {
// First run the users supplied teardown, then await the autoTeardown if it exists.
Expand Down Expand Up @@ -196,7 +195,6 @@ const extensionBootstrapper = (() => {
if (!activatedAPI) {
throw new Error("Extension is not activated. Call activateExtension() first.");
}
lastTestLogs = activatedAPI.outputChannel.logs;

// Wait for up to 10 seconds for all tasks to complete before deactivating.
// Long running tasks should be avoided in tests, but this is a safety net.
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/debugger/lldb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ suite("debugger.lldb Tests", () => {
});
mockContext = mockObject<WorkspaceContext>({
toolchain: instance(mockToolchain),
outputChannel: new SwiftOutputChannel("mockChannel", false),
outputChannel: new SwiftOutputChannel("mockChannel"),
});
});

Expand Down

0 comments on commit 8b57d89

Please sign in to comment.