-
Notifications
You must be signed in to change notification settings - Fork 341
report nice error message when dotnet is not installed or not valid #5477
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
Merged
+258
−56
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
81589b3
report nice error message when dotnet is not installed
chunyu3 57ca9c0
change the diagonosic code
chunyu3 e68c4d9
Merge branch 'main' of https://github.com/microsoft/typespec into Fix…
chunyu3 176c45f
Merge branch 'main' into Fix5364
chunyu3 3adef56
Merge branch 'main' of https://github.com/microsoft/typespec into Fix…
chunyu3 a6d54ca
check dotnet runtime when generate fail
chunyu3 4dee9a5
use semver to parse and compare version
chunyu3 80bc6c7
Merge branch 'main' into Fix5364
chunyu3 1a3e0a2
Update packages/http-client-csharp/emitter/src/lib/lib.ts
chunyu3 35adcaa
Update packages/http-client-csharp/emitter/src/constants.ts
chunyu3 e9c0ed2
Update packages/http-client-csharp/emitter/src/emitter.ts
chunyu3 a477162
Update packages/http-client-csharp/emitter/src/lib/lib.ts
chunyu3 10cd636
Update packages/http-client-csharp/emitter/src/emitter.ts
chunyu3 6127a1b
change text
chunyu3 caedb3f
Update packages/http-client-csharp/emitter/src/emitter.ts
chunyu3 0942fdb
Merge branch 'main' into Fix5364
chunyu3 e34ce6f
update comment
chunyu3 e5c7d12
Merge branch 'Fix5364' of https://github.com/chunyu3/typespec into Fi…
chunyu3 75ef4ef
update the dotnet download url
chunyu3 8934ce3
use dotnet --version to check dotnet sdk
chunyu3 eee758f
Merge branch 'main' into Fix5364
chunyu3 f45887f
compare as number
chunyu3 f6b3410
remove extra quote in error message
chunyu3 8b0bb82
add Unit test for validateDotNetSDK
chunyu3 0fff783
Merge branch 'main' into Fix5364
chunyu3 81a4380
Update packages/http-client-csharp/emitter/src/lib/lib.ts
chunyu3 3694213
typo
chunyu3 e87f80f
mock dotnet not installed scenario
chunyu3 bca3546
Update packages/http-client-csharp/emitter/src/emitter.ts
chunyu3 057cece
format
chunyu3 4650160
add UT for different dotnet sdk installation scenarios with mock
chunyu3 b4eb6df
simple mock
chunyu3 2c9ab1f
Merge branch 'main' into Fix5364
chunyu3 1729cfa
Merge branch 'main' into Fix5364
chunyu3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/http-client-csharp/emitter/test/Unit/dotnet-sdk-validation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { Program } from "@typespec/compiler"; | ||
| import { TestHost } from "@typespec/compiler/testing"; | ||
| import { strictEqual } from "assert"; | ||
| import { SpawnOptions } from "child_process"; | ||
| import { afterEach, beforeEach, describe, expect, it, Mock, vi } from "vitest"; | ||
| import { validateDotNetSdk } from "../../src/emitter.js"; | ||
| import { execAsync } from "../../src/lib/utils.js"; | ||
| import { createEmitterTestHost, typeSpecCompile } from "./utils/test-util.js"; | ||
|
|
||
| describe("Test validateDotNetSdk", () => { | ||
| let runner: TestHost; | ||
| let program: Program; | ||
| const minVersion = 8; | ||
|
|
||
| vi.mock("../../src/lib/utils.js", () => ({ | ||
| execAsync: vi.fn(), | ||
| })); | ||
|
|
||
| beforeEach(async () => { | ||
| runner = await createEmitterTestHost(); | ||
| program = await typeSpecCompile( | ||
| ` | ||
| op test( | ||
| @query | ||
| @encode(DurationKnownEncoding.ISO8601) | ||
| input: duration | ||
| ): NoContentResponse; | ||
| `, | ||
| runner, | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Restore all mocks after each test | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("should return false and report diagnostic when dotnet SDK is not installed.", async () => { | ||
| /* mock the scenario that dotnet SDK is not installed, so execAsync will throw exception with error ENOENT */ | ||
| const error: any = new Error("ENOENT: no such file or directory"); | ||
| error.code = "ENOENT"; | ||
| (execAsync as Mock).mockRejectedValue(error); | ||
| const result = await validateDotNetSdk(program, minVersion); | ||
| expect(result).toBe(false); | ||
| strictEqual(program.diagnostics.length, 1); | ||
| strictEqual( | ||
| program.diagnostics[0].code, | ||
| "@typespec/http-client-csharp/invalid-dotnet-sdk-dependency", | ||
| ); | ||
| strictEqual( | ||
| program.diagnostics[0].message, | ||
| "The dotnet command was not found in the PATH. Please install the .NET SDK version 8 or above. Guidance for installing the .NET SDK can be found at https://dotnet.microsoft.com/.", | ||
| ); | ||
| }); | ||
|
|
||
| it("should return true for installed SDK version whose major equals min supported version", async () => { | ||
chunyu3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /* mock the scenario that the installed SDK version whose major equals min supported version */ | ||
| (execAsync as Mock).mockResolvedValue({ | ||
| exitCode: 0, | ||
| stdio: "", | ||
| stdout: "8.0.204", | ||
| stderr: "", | ||
| proc: { pid: 0, output: "", stdout: "", stderr: "", stdin: "" }, | ||
| }); | ||
| const result = await validateDotNetSdk(program, minVersion); | ||
| expect(result).toBe(true); | ||
| /* no diagnostics */ | ||
| strictEqual(program.diagnostics.length, 0); | ||
| }); | ||
|
|
||
| it("should return true for installed SDK version whose major greaters than min supported version", async () => { | ||
| /* mock the scenario that the installed SDK version whose major greater than min supported version */ | ||
| (execAsync as Mock).mockImplementation( | ||
| (command: string, args: string[] = [], options: SpawnOptions = {}) => { | ||
| return { | ||
| exitCode: 0, | ||
| stdio: "", | ||
| stdout: "9.0.102", | ||
| stderr: "", | ||
| proc: { pid: 0, output: "", stdout: "", stderr: "", stdin: "" }, | ||
| }; | ||
| }, | ||
| ); | ||
| const result = await validateDotNetSdk(program, minVersion); | ||
| expect(result).toBe(true); | ||
| /* no diagnostics */ | ||
| strictEqual(program.diagnostics.length, 0); | ||
| }); | ||
|
|
||
| it("should return false and report diagnostic for invalid .NET SDK version", async () => { | ||
| /* mock the scenario that the installed SDK version whose major less than min supported version */ | ||
| (execAsync as Mock).mockResolvedValue({ | ||
| exitCode: 0, | ||
| stdio: "", | ||
| stdout: "5.0.408", | ||
| stderr: "", | ||
| proc: { pid: 0, output: "", stdout: "", stderr: "", stdin: "" }, | ||
| }); | ||
| const result = await validateDotNetSdk(program, minVersion); | ||
| expect(result).toBe(false); | ||
| strictEqual(program.diagnostics.length, 1); | ||
| strictEqual( | ||
| program.diagnostics[0].code, | ||
| "@typespec/http-client-csharp/invalid-dotnet-sdk-dependency", | ||
| ); | ||
| strictEqual( | ||
| program.diagnostics[0].message, | ||
| "The .NET SDK found is version 5.0.408. Please install the .NET SDK 8 or above and ensure there is no global.json in the file system requesting a lower version. Guidance for installing the .NET SDK can be found at https://dotnet.microsoft.com/.", | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.