-
Notifications
You must be signed in to change notification settings - Fork 596
Expand file tree
/
Copy pathnetCoreTool.test.ts
More file actions
240 lines (217 loc) · 10.4 KB
/
Copy pathnetCoreTool.test.ts
File metadata and controls
240 lines (217 loc) · 10.4 KB
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
237
238
239
240
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { expect } from "chai";
import * as os from "os";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import * as sinon from "sinon";
import * as https from "https";
import { EventEmitter } from "events";
import {
NetCoreTool,
DBProjectConfigurationKey,
DotnetInstallLocationKey,
FALLBACK_MICROSOFT_BUILD_SQL_VERSION,
getMicrosoftBuildSqlVersion,
resolveNugetVersion,
} from "../src/tools/netcoreTool";
import { deleteGeneratedTestFolder, generateTestFolderPath } from "./testUtils";
import { createContext, TestContext } from "./testContext";
import * as constants from "../src/common/constants";
let testContext: TestContext;
let sandbox: sinon.SinonSandbox;
suite("NetCoreTool: Net core tests", function (): void {
teardown(function (): void {
sandbox.restore();
});
setup(function (): void {
testContext = createContext();
sandbox = sinon.createSandbox();
});
suiteTeardown(async function (): Promise<void> {
await deleteGeneratedTestFolder();
});
test("Should override dotnet default value with settings", async function (): Promise<void> {
try {
// update settings and validate
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(DotnetInstallLocationKey, "test value path", true);
const netcoreTool = new NetCoreTool(testContext.outputChannel);
sandbox.stub(netcoreTool, "showInstallDialog").returns(Promise.resolve());
expect(netcoreTool.netcoreInstallLocation).to.equal("test value path"); // the path in settings should be taken
expect(await netcoreTool.findOrInstallNetCore()).to.equal(false); // dotnet can not be present at dummy path in settings
} finally {
// clean again
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(DotnetInstallLocationKey, "", true);
}
});
test("Should find right dotnet default paths", async function (): Promise<void> {
const netcoreTool = new NetCoreTool(testContext.outputChannel);
sandbox.stub(netcoreTool, "showInstallDialog").returns(Promise.resolve());
await netcoreTool.findOrInstallNetCore();
if (os.platform() === "win32") {
// check that path should start with c:\program files
let result =
!netcoreTool.netcoreInstallLocation ||
netcoreTool.netcoreInstallLocation.toLowerCase().startsWith("c:\\program files");
expect(result, "dotnet not present in programfiles by default").to.be.true;
}
if (os.platform() === "linux") {
//check that path should start with /usr/share
let result =
!netcoreTool.netcoreInstallLocation ||
netcoreTool.netcoreInstallLocation.toLowerCase() === "/usr/share/dotnet";
expect(result, "dotnet not present in /usr/share").to.be.true;
}
if (os.platform() === "darwin") {
//check that path should start with /usr/local/share
let result =
!netcoreTool.netcoreInstallLocation ||
netcoreTool.netcoreInstallLocation.toLowerCase() === "/usr/local/share/dotnet";
expect(result, "dotnet not present in /usr/local/share").to.be.true;
}
});
test("should run a command successfully", async function (): Promise<void> {
const netcoreTool = new NetCoreTool(testContext.outputChannel);
const dummyFile = path.join(await generateTestFolderPath(this.test), "dummy.dacpac");
try {
await netcoreTool.runStreamedCommand(
process.execPath,
["-e", `require("fs").writeFileSync(${JSON.stringify(dummyFile)}, "test")`],
undefined,
);
const text = await fs.promises.readFile(dummyFile);
expect(text.toString().trim()).to.equal("test");
} finally {
try {
await fs.promises.unlink(dummyFile);
} catch {
console.warn(`Failed to clean up ${dummyFile}`);
}
}
});
suite("getMicrosoftBuildSqlVersion tests", function (): void {
teardown(async function (): Promise<void> {
// Clean up configuration after each test
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(
constants.microsoftBuildSqlVersionKey,
undefined,
vscode.ConfigurationTarget.Global,
);
});
test("Should return valid configured value when set", async function (): Promise<void> {
// Arrange: Set a valid semver version
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(
constants.microsoftBuildSqlVersionKey,
"3.0.0",
vscode.ConfigurationTarget.Global,
);
// Act
const result = getMicrosoftBuildSqlVersion();
// Assert
expect(result).to.equal("3.0.0");
});
test("Should fall back to FALLBACK_MICROSOFT_BUILD_SQL_VERSION when configured value is invalid or empty", async function (): Promise<void> {
// Test with invalid semver
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(
constants.microsoftBuildSqlVersionKey,
"not-a-valid-version",
vscode.ConfigurationTarget.Global,
);
let result = getMicrosoftBuildSqlVersion();
expect(result).to.equal(FALLBACK_MICROSOFT_BUILD_SQL_VERSION);
// Test with empty config
await vscode.workspace
.getConfiguration(DBProjectConfigurationKey)
.update(
constants.microsoftBuildSqlVersionKey,
undefined,
vscode.ConfigurationTarget.Global,
);
result = getMicrosoftBuildSqlVersion();
expect(result).to.equal(FALLBACK_MICROSOFT_BUILD_SQL_VERSION);
});
});
test("resolveNugetVersion: exact version is returned as-is without calling NuGet API", async function (): Promise<void> {
const getSpy = sandbox.spy(https, "get");
// Older user-configured version must be respected — no upgrade to latest
expect(await resolveNugetVersion("Microsoft.Build.Sql", "2.0.0")).to.equal("2.0.0");
expect(await resolveNugetVersion("Microsoft.Build.Sql", "2.1.0")).to.equal("2.1.0");
expect(getSpy.called, "NuGet API should not be called for exact versions").to.be.false;
});
test("resolveNugetVersion: floating version resolves to latest stable match", async function (): Promise<void> {
const versions = ["2.0.0", "2.1.0", "2.2.0", "2.3.0-preview", "3.0.0"];
(sandbox.stub(https, "get") as sinon.SinonStub).callsFake(
(_url: string, callback: (res: EventEmitter) => void) => {
const res = new EventEmitter();
callback(res);
res.emit("data", JSON.stringify({ versions }));
res.emit("end");
return new EventEmitter();
},
);
expect(await resolveNugetVersion("Microsoft.Build.Sql", "2.*")).to.equal("2.2.0");
expect(await resolveNugetVersion("Microsoft.Build.Sql", "2.0.*")).to.equal("2.0.0");
});
test("resolveNugetVersion: throws when no stable versions match", async function (): Promise<void> {
(sandbox.stub(https, "get") as sinon.SinonStub).callsFake(
(_url: string, callback: (res: EventEmitter) => void) => {
const res = new EventEmitter();
callback(res);
res.emit("data", JSON.stringify({ versions: ["3.0.0"] }));
res.emit("end");
return new EventEmitter();
},
);
let threw = false;
try {
await resolveNugetVersion("Microsoft.Build.Sql", "2.*");
} catch {
threw = true;
}
expect(threw, "should throw when no versions match").to.be.true;
});
test("resolveNugetVersion: falls back to FALLBACK_MICROSOFT_BUILD_SQL_VERSION when configured version has no stable match", async function (): Promise<void> {
// Both calls go to the same NuGet index URL; versions contains only 2.x entries.
// First call (for "4.*") finds no 4.x match → triggers fallback.
// Second call (for "2.*") finds 2.2.0 → returned.
(sandbox.stub(https, "get") as sinon.SinonStub).callsFake(
(_url: string, callback: (res: EventEmitter) => void) => {
const res = new EventEmitter();
callback(res);
res.emit("data", JSON.stringify({ versions: ["2.0.0", "2.2.0"] }));
res.emit("end");
return new EventEmitter();
},
);
sandbox.stub(vscode.window, "showWarningMessage").resolves(undefined);
const result = await resolveNugetVersion("Microsoft.Build.Sql", "4.*");
expect(result).to.equal("2.2.0");
});
test("resolveNugetVersion: throws when NuGet API call fails", async function (): Promise<void> {
(sandbox.stub(https, "get") as sinon.SinonStub).callsFake(() => {
const req = new EventEmitter();
setImmediate(() => req.emit("error", new Error("network failure")));
return req;
});
let threw = false;
try {
await resolveNugetVersion("Microsoft.Build.Sql", "2.*");
} catch {
threw = true;
}
expect(threw, "should throw on network error").to.be.true;
});
});