microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/npm/qsharp/test/diagnostics.js
92lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //@ts-check |
| 5 | |
| 6 | import assert from "node:assert/strict"; |
| 7 | import { test } from "node:test"; |
| 8 | import { QdkDiagnostics } from "../dist/diagnostics.js"; |
| 9 | import { log } from "../dist/log.js"; |
| 10 | import { |
| 11 | getCompiler, |
| 12 | getCompilerWorker, |
| 13 | getProjectLoader, |
| 14 | } from "../dist/main.js"; |
| 15 | |
| 16 | /** @type {import("../dist/log.js").TelemetryEvent[]} */ |
| 17 | const telemetryEvents = []; |
| 18 | log.setLogLevel("warn"); |
| 19 | log.setTelemetryCollector((event) => telemetryEvents.push(event)); |
| 20 | |
| 21 | /** |
| 22 | * @returns {import ("../dist/compiler/compiler.js").ProgramConfig} |
| 23 | */ |
| 24 | function getInvalidQirProgramConfig() { |
| 25 | /** @type {[string, string][]} */ |
| 26 | const sources = [ |
| 27 | ["test.qs", `namespace Test { function Main() : Int { return 1; } }`], |
| 28 | ]; |
| 29 | return { |
| 30 | sources, |
| 31 | languageFeatures: [], |
| 32 | profile: "base", |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | test("getQir throws QdkDiagnostics", async () => { |
| 37 | const compiler = getCompiler(); |
| 38 | const invalidConfig = getInvalidQirProgramConfig(); |
| 39 | await assert.rejects( |
| 40 | () => compiler.getQir(invalidConfig), |
| 41 | (err) => { |
| 42 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 43 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 44 | assert.match(err.message, /cannot use an integer value as an output/); |
| 45 | return true; |
| 46 | }, |
| 47 | "getQir should throw on invalid input", |
| 48 | ); |
| 49 | }); |
| 50 | |
| 51 | test("getQir throws QdkDiagnostics - worker", async () => { |
| 52 | const compiler = getCompilerWorker(); |
| 53 | const invalidConfig = getInvalidQirProgramConfig(); |
| 54 | try { |
| 55 | await assert.rejects( |
| 56 | () => compiler.getQir(invalidConfig), |
| 57 | (err) => { |
| 58 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 59 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 60 | assert.match(err.message, /cannot use an integer value as an output/); |
| 61 | return true; |
| 62 | }, |
| 63 | "getQir should throw on invalid input", |
| 64 | ); |
| 65 | } finally { |
| 66 | compiler.terminate(); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | // Minimal IProjectHost implementation for testing |
| 71 | const dummyHost = { |
| 72 | readFile: async () => null, |
| 73 | listDirectory: async () => [], |
| 74 | resolvePath: async (a, b) => b, |
| 75 | fetchGithub: async () => "", |
| 76 | findManifestDirectory: async () => null, |
| 77 | }; |
| 78 | |
| 79 | test("loadQSharpProject throws QdkDiagnostics", async () => { |
| 80 | const loader = getProjectLoader(dummyHost); |
| 81 | await assert.rejects( |
| 82 | () => loader.loadQSharpProject("/not/a/real/dir"), |
| 83 | (err) => { |
| 84 | assert(err instanceof QdkDiagnostics, "Error should be QdkDiagnostics"); |
| 85 | assert(err.diagnostics.length > 0, "diagnostics should not be empty"); |
| 86 | assert.match(err.message, /Failed to parse manifest/i); |
| 87 | return true; |
| 88 | }, |
| 89 | "loadQSharpProject should throw on invalid directory", |
| 90 | ); |
| 91 | loader.dispose(); |
| 92 | }); |
| 93 | |