Skip to content

Commit 540e8e5

Browse files
authored
Skip docCommentCompletion tests on linux (#1720)
These have a tendency to crash when accessing the active editor in a headless environment.
1 parent 83791c9 commit 540e8e5

File tree

3 files changed

+35
-86
lines changed

3 files changed

+35
-86
lines changed

src/editor/CommentCompletion.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
6060
document: vscode.TextDocument,
6161
position: vscode.Position
6262
) {
63-
if (!vscode.window.activeTextEditor) {
63+
const editor = vscode.window.visibleTextEditors.find(
64+
e => e.document.uri.toString() === document.uri.toString()
65+
);
66+
if (!editor || editor.document.isClosed) {
6467
return;
6568
}
6669
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
@@ -72,7 +75,7 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
7275
? [lineText, lineText, ""]
7376
: /^(\s*)\/\/\s(.+)/.exec(lineText);
7477
if (match) {
75-
await vscode.window.activeTextEditor.edit(
78+
await editor.edit(
7679
edit => {
7780
edit.replace(
7881
new vscode.Range(position.line, 0, position.line, match[0].length),
@@ -82,10 +85,7 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
8285
{ undoStopBefore: false, undoStopAfter: true }
8386
);
8487
const newPosition = new vscode.Position(position.line, match[1].length + 4);
85-
vscode.window.activeTextEditor.selection = new vscode.Selection(
86-
newPosition,
87-
newPosition
88-
);
88+
editor.selection = new vscode.Selection(newPosition, newPosition);
8989
}
9090
}
9191
}

test/integration-tests/editor/CommentCompletion.test.ts

Lines changed: 19 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,13 @@ import { CommentCompletionProviders } from "../../../src/editor/CommentCompletio
1818
import { Workbench } from "../../../src/utilities/commands";
1919

2020
suite("CommentCompletion Test Suite", () => {
21-
let document: vscode.TextDocument | undefined;
2221
let provider: CommentCompletionProviders;
2322

2423
setup(() => {
2524
provider = new CommentCompletionProviders();
2625
});
2726

28-
teardown(async () => {
29-
const editor = vscode.window.visibleTextEditors.find(
30-
editor => editor.document === document
31-
);
32-
33-
if (editor && document) {
34-
await vscode.window.showTextDocument(document, editor.viewColumn);
35-
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
36-
}
37-
38-
provider.dispose();
39-
});
27+
teardown(() => provider.dispose());
4028

4129
suite("Function Comment Completion", () => {
4230
test("Completion on line that isn't a comment", async () => {
@@ -404,7 +392,13 @@ suite("CommentCompletion Test Suite", () => {
404392
});
405393
});
406394

407-
suite("Document Comment Completion", () => {
395+
suite("Document Comment Completion", function () {
396+
setup(function () {
397+
if (process.platform === "linux") {
398+
// Skip as these tests access the active test editor which will sometimes crash on Linux.
399+
this.skip();
400+
}
401+
});
408402
test("Should not provide completions on first line", async () => {
409403
const { document, positions } = await openDocument(`1️⃣
410404
public func foo() {}`);
@@ -439,32 +433,28 @@ suite("CommentCompletion Test Suite", () => {
439433

440434
test("Should continue a documentation comment block on new line", async () => {
441435
const { document, positions } = await openDocument(`
442-
/// aaa
443-
1️⃣
444-
public func foo() {}`);
436+
/// aaa
437+
1️⃣
438+
public func foo() {}`);
445439

446440
const position = positions["1️⃣"];
447441
await provider.docCommentCompletion.provideCompletionItems(document, position);
448442

449-
const newLine = document.getText(
450-
new vscode.Range(position, position.translate(0, 1000))
451-
);
443+
const newLine = document.lineAt(position.line).text;
452444

453445
assert.strictEqual(newLine, "/// ", "New line should continue the comment block");
454446
});
455447

456448
test("Should continue a documentation comment when an existing comment line is split", async () => {
457449
const { document, positions } = await openDocument(`
458-
/// aaa
459-
1️⃣// bbb
460-
public func foo() {}`);
450+
/// aaa
451+
1️⃣// bbb
452+
public func foo() {}`);
461453

462454
const position = positions["1️⃣"];
463455
await provider.docCommentCompletion.provideCompletionItems(document, position);
464456

465-
const newLine = document.getText(
466-
new vscode.Range(position, position.translate(0, 1000))
467-
);
457+
const newLine = document.lineAt(position.line).text;
468458

469459
assert.strictEqual(newLine, "/// bbb", "New line should continue the comment block");
470460
});
@@ -484,7 +474,7 @@ suite("CommentCompletion Test Suite", () => {
484474
});
485475

486476
test("Should handle case when no active text editor", async () => {
487-
const { document, positions } = await openDocument(`
477+
const { document: doc, positions } = await openDocument(`
488478
/// aaa
489479
1️⃣
490480
public func foo() {}`);
@@ -495,10 +485,7 @@ suite("CommentCompletion Test Suite", () => {
495485
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
496486

497487
// This should not throw an error
498-
const items = await provider.docCommentCompletion.provideCompletionItems(
499-
document,
500-
position
501-
);
488+
const items = await provider.docCommentCompletion.provideCompletionItems(doc, position);
502489

503490
assert.equal(
504491
items,
@@ -507,50 +494,6 @@ suite("CommentCompletion Test Suite", () => {
507494
);
508495
});
509496

510-
test("Should specifically test continueExistingDocCommentBlock with no active editor", async () => {
511-
const { document, positions } = await openDocument(`
512-
/// aaa
513-
1️⃣
514-
public func foo() {}`);
515-
516-
const position = positions["1️⃣"];
517-
518-
// Close all editors to simulate no active text editor
519-
await vscode.commands.executeCommand(Workbench.ACTION_CLOSEALLEDITORS);
520-
521-
// Store original activeTextEditor property
522-
const originalActiveTextEditor = Object.getOwnPropertyDescriptor(
523-
vscode.window,
524-
"activeTextEditor"
525-
);
526-
527-
// Mock the activeTextEditor to be null for the specific method call
528-
Object.defineProperty(vscode.window, "activeTextEditor", {
529-
get: () => null,
530-
configurable: true,
531-
});
532-
533-
try {
534-
// Call the method directly to ensure the branch is covered
535-
await provider.docCommentCompletion["continueExistingDocCommentBlock"](
536-
document,
537-
position
538-
);
539-
540-
// If we get here, the method didn't throw an error, which is what we want
541-
assert.ok(true, "Method should not throw when there's no active editor");
542-
} finally {
543-
// Restore the original activeTextEditor property
544-
if (originalActiveTextEditor) {
545-
Object.defineProperty(
546-
vscode.window,
547-
"activeTextEditor",
548-
originalActiveTextEditor
549-
);
550-
}
551-
}
552-
});
553-
554497
test("Should handle when previous line has // but not ///", async () => {
555498
const { document, positions } = await openDocument(`
556499
// aaa
@@ -641,8 +584,7 @@ suite("CommentCompletion Test Suite", () => {
641584
return;
642585
}
643586

644-
const column = lines[line].indexOf(str);
645-
return new vscode.Position(line, column);
587+
return new vscode.Position(line, lines[line].indexOf(str));
646588
}
647589

648590
let purgedContent = content;
@@ -669,9 +611,6 @@ suite("CommentCompletion Test Suite", () => {
669611

670612
await vscode.window.showTextDocument(doc);
671613

672-
// Save the document so we can clean it up when the test finishes
673-
document = doc;
674-
675614
return { document: doc, positions };
676615
}
677616
});

test/integration-tests/tasks/SwiftTaskProvider.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ suite("SwiftTaskProvider Test Suite", () => {
142142
test("includes product debug task", async () => {
143143
const tasks = await vscode.tasks.fetchTasks({ type: "swift" });
144144
const task = tasks.find(t => t.name === "Build Debug PackageExe (defaultPackage)");
145+
expect(
146+
task,
147+
'expected to find a task named "Build Debug PackageExe (defaultPackage)", instead found ' +
148+
tasks.map(t => t.name)
149+
).to.not.be.undefined;
145150
expect(task?.detail).to.include("swift build --product PackageExe");
146151
});
147152

@@ -151,6 +156,11 @@ suite("SwiftTaskProvider Test Suite", () => {
151156
new vscode.CancellationTokenSource().token
152157
);
153158
const task = tasks.find(t => t.name === "Build Release PackageExe (defaultPackage)");
159+
expect(
160+
task,
161+
'expected to find a task named "Build Release PackageExe (defaultPackage)", instead found ' +
162+
tasks.map(t => t.name)
163+
).to.not.be.undefined;
154164
expect(task?.detail).to.include("swift build -c release --product PackageExe");
155165
});
156166

0 commit comments

Comments
 (0)