forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspaceContext.test.ts
236 lines (212 loc) · 10.1 KB
/
WorkspaceContext.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021-2022 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import * as assert from "assert";
import { testAssetUri } from "../fixtures";
import { FolderOperation, WorkspaceContext } from "../../src/WorkspaceContext";
import { createBuildAllTask } from "../../src/tasks/SwiftTaskProvider";
import { Version } from "../../src/utilities/version";
import { SwiftExecution } from "../../src/tasks/SwiftExecution";
import { activateExtensionForSuite } from "./utilities/testutilities";
import { FolderContext } from "../../src/FolderContext";
import { assertContains } from "./testexplorer/utilities";
function assertContainsArg(execution: SwiftExecution, arg: string) {
assert(execution?.args.find(a => a === arg));
}
function assertNotContainsArg(execution: SwiftExecution, arg: string) {
assert.equal(
execution?.args.find(a => a.includes(arg)),
undefined
);
}
suite("WorkspaceContext Test Suite", () => {
let workspaceContext: WorkspaceContext;
const packageFolder: vscode.Uri = testAssetUri("defaultPackage");
suite("Folder Events", () => {
activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
},
// No default assets as we want to verify against a clean workspace.
testAssets: [],
});
test("Add", async () => {
let observer: vscode.Disposable | undefined;
const recordedFolders: {
folder: FolderContext | null;
operation: FolderOperation;
}[] = [];
try {
observer = workspaceContext.onDidChangeFolders(changedFolderRecord => {
recordedFolders.push(changedFolderRecord);
});
const workspaceFolder = vscode.workspace.workspaceFolders?.values().next().value;
assert.ok(workspaceFolder, "No workspace folders found in workspace");
await workspaceContext.addPackageFolder(testAssetUri("package2"), workspaceFolder);
const foldersNames = recordedFolders.map(({ folder }) => folder?.swiftPackage.name);
assertContains(foldersNames, "package2");
const addedCount = recordedFolders.filter(
({ operation }) => operation === FolderOperation.add
).length;
assert.strictEqual(
addedCount,
1,
`Expected only one add folder operation, instead got folders: ${recordedFolders.map(folder => folder.folder?.name)}`
);
} finally {
observer?.dispose();
}
}).timeout(60000 * 2);
});
suite("Tasks", async function () {
activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
},
});
// Was hitting a timeout in suiteSetup during CI build once in a while
this.timeout(5000);
const swiftConfig = vscode.workspace.getConfiguration("swift");
suiteTeardown(async () => {
await swiftConfig.update("buildArguments", undefined);
await swiftConfig.update("packageArguments", undefined);
await swiftConfig.update("path", undefined);
await swiftConfig.update("diagnosticsStyle", undefined);
});
test("Default Task values", async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", undefined);
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution;
assert.strictEqual(buildAllTask.definition.type, "swift");
assert.strictEqual(buildAllTask.name, "Build All (defaultPackage)");
assertContainsArg(execution, "build");
assertContainsArg(execution, "--build-tests");
assertContainsArg(execution, "-Xswiftc");
assertContainsArg(execution, "-diagnostic-style=llvm");
assert.strictEqual(buildAllTask.scope, folder.workspaceFolder);
});
test('"default" diagnosticsStyle', async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", "default");
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution;
assert.strictEqual(buildAllTask.definition.type, "swift");
assert.strictEqual(buildAllTask.name, "Build All (defaultPackage)");
assertContainsArg(execution, "build");
assertContainsArg(execution, "--build-tests");
assertNotContainsArg(execution, "-diagnostic-style");
assert.strictEqual(buildAllTask.scope, folder.workspaceFolder);
});
test('"swift" diagnosticsStyle', async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", "swift");
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution;
assert.strictEqual(buildAllTask.definition.type, "swift");
assert.strictEqual(buildAllTask.name, "Build All (defaultPackage)");
assertContainsArg(execution, "build");
assertContainsArg(execution, "--build-tests");
assertContainsArg(execution, "-Xswiftc");
assertContainsArg(execution, "-diagnostic-style=swift");
assert.strictEqual(buildAllTask.scope, folder.workspaceFolder);
});
test("Build Settings", async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", undefined);
await swiftConfig.update("buildArguments", ["--sanitize=thread"]);
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution as SwiftExecution;
assertContainsArg(execution, "--sanitize=thread");
await swiftConfig.update("buildArguments", []);
});
test("Package Arguments Settings", async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", undefined);
await swiftConfig.update("packageArguments", ["--replace-scm-with-registry"]);
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution as SwiftExecution;
assertContainsArg(execution, "--replace-scm-with-registry");
await swiftConfig.update("packageArguments", []);
});
test("Swift Path", async () => {
/* Temporarily disabled (need swift path to update immediately for this to work)
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("path", "/usr/bin/swift");
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution as SwiftExecution;
assert.strictEqual(execution?.command, "/usr/bin/swift");
await swiftConfig.update("path", "");*/
});
});
suite("Toolchain", () => {
activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
},
});
test("get project templates", async () => {
// This is only supported in swift versions >=5.8.0
const swiftVersion = workspaceContext.toolchain.swiftVersion;
if (swiftVersion.isLessThan(new Version(5, 8, 0))) {
assert.deepEqual(await workspaceContext.toolchain.getProjectTemplates(), []);
return;
}
// The output of `swift package init --help` will probably change at some point.
// Just make sure that the most complex portions of the output are parsed correctly.
const projectTemplates = await workspaceContext.toolchain.getProjectTemplates();
// Contains multi-line description
const toolTemplate = projectTemplates.find(template => template.id === "tool");
assert(toolTemplate);
assert.deepEqual(toolTemplate, {
id: "tool",
name: "Tool",
description:
"A package with an executable that uses Swift Argument Parser. Use this template if you plan to have a rich set of command-line arguments.",
});
// build-tool-plugin is only available in swift versions >=5.9.0
if (swiftVersion.isLessThan(new Version(5, 9, 0))) {
return;
}
// Name conversion includes dashes
const buildToolPluginTemplate = projectTemplates.find(
t => t.id === "build-tool-plugin"
);
assert(buildToolPluginTemplate);
assert.deepEqual(buildToolPluginTemplate, {
id: "build-tool-plugin",
name: "Build Tool Plugin",
description: "A package that vends a build tool plugin.",
});
}).timeout(1000);
});
}).timeout(10000);