|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 | import { AzureDeveloperCliCredential } from "@azure/identity"; |
4 | | -import { azureDeveloperCliPublicErrorMessages } from "$internal/credentials/azureDeveloperCliCredential.js"; |
| 4 | +import { |
| 5 | + azureDeveloperCliPublicErrorMessages, |
| 6 | + developerCliCredentialInternals, |
| 7 | +} from "$internal/credentials/azureDeveloperCliCredential.js"; |
5 | 8 | import type { GetTokenOptions } from "@azure/core-auth"; |
6 | 9 | import child_process, { type ChildProcess } from "node:child_process"; |
7 | 10 | import { describe, it, assert, expect, vi, beforeEach, afterEach } from "vitest"; |
@@ -272,4 +275,101 @@ describe("AzureDeveloperCliCredential (internal)", function () { |
272 | 275 | ); |
273 | 276 | }); |
274 | 277 | } |
| 278 | + |
| 279 | + describe("parseAzdStderr", () => { |
| 280 | + it("parses valid JSON with data.message", () => { |
| 281 | + const json = JSON.stringify({ |
| 282 | + type: "consoleMessage", |
| 283 | + timestamp: "2024-01-01T00:00:00Z", |
| 284 | + data: { message: "\nERROR: fetching token: authentication failed\n" }, |
| 285 | + }); |
| 286 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 287 | + assert.equal(result, "ERROR: fetching token: authentication failed"); |
| 288 | + }); |
| 289 | + |
| 290 | + it("trims whitespace from data.message", () => { |
| 291 | + const json = JSON.stringify({ |
| 292 | + type: "consoleMessage", |
| 293 | + timestamp: "2024-01-01T00:00:00Z", |
| 294 | + data: { message: " \n ERROR: test error \n " }, |
| 295 | + }); |
| 296 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 297 | + assert.equal(result, "ERROR: test error"); |
| 298 | + }); |
| 299 | + |
| 300 | + it("returns raw stderr when JSON parsing fails", () => { |
| 301 | + const invalidJson = "not valid json"; |
| 302 | + const result = developerCliCredentialInternals.parseAzdStderr(invalidJson); |
| 303 | + assert.equal(result, "not valid json"); |
| 304 | + }); |
| 305 | + |
| 306 | + it("returns raw stderr when data.message is missing", () => { |
| 307 | + const json = JSON.stringify({ |
| 308 | + type: "consoleMessage", |
| 309 | + timestamp: "2024-01-01T00:00:00Z", |
| 310 | + data: {}, |
| 311 | + }); |
| 312 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 313 | + assert.equal(result, json); |
| 314 | + }); |
| 315 | + |
| 316 | + it("returns raw stderr when data.message is empty", () => { |
| 317 | + const json = JSON.stringify({ |
| 318 | + type: "consoleMessage", |
| 319 | + timestamp: "2024-01-01T00:00:00Z", |
| 320 | + data: { message: "" }, |
| 321 | + }); |
| 322 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 323 | + assert.equal(result, json); |
| 324 | + }); |
| 325 | + |
| 326 | + it("returns raw stderr when data.message is only whitespace", () => { |
| 327 | + const json = JSON.stringify({ |
| 328 | + type: "consoleMessage", |
| 329 | + timestamp: "2024-01-01T00:00:00Z", |
| 330 | + data: { message: " \n \n " }, |
| 331 | + }); |
| 332 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 333 | + assert.equal(result, json); |
| 334 | + }); |
| 335 | + |
| 336 | + it("returns raw stderr when data is missing", () => { |
| 337 | + const json = JSON.stringify({ |
| 338 | + type: "consoleMessage", |
| 339 | + timestamp: "2024-01-01T00:00:00Z", |
| 340 | + }); |
| 341 | + const result = developerCliCredentialInternals.parseAzdStderr(json); |
| 342 | + assert.equal(result, json); |
| 343 | + }); |
| 344 | + }); |
| 345 | + |
| 346 | + describe("error message parsing integration", () => { |
| 347 | + it("parses JSON error message from stderr", async () => { |
| 348 | + stdout = ""; |
| 349 | + stderr = JSON.stringify({ |
| 350 | + type: "consoleMessage", |
| 351 | + timestamp: "2024-01-01T00:00:00Z", |
| 352 | + data: { message: "\nERROR: fetching token: authentication failed\n" }, |
| 353 | + }); |
| 354 | + const credential = new AzureDeveloperCliCredential(); |
| 355 | + try { |
| 356 | + await credential.getToken("https://service/.default"); |
| 357 | + assert.fail("Expected error to be thrown"); |
| 358 | + } catch (error: any) { |
| 359 | + assert.equal(error.message, "ERROR: fetching token: authentication failed"); |
| 360 | + } |
| 361 | + }); |
| 362 | + |
| 363 | + it("uses raw stderr when JSON parsing fails", async () => { |
| 364 | + stdout = ""; |
| 365 | + stderr = "plain text error message"; |
| 366 | + const credential = new AzureDeveloperCliCredential(); |
| 367 | + try { |
| 368 | + await credential.getToken("https://service/.default"); |
| 369 | + assert.fail("Expected error to be thrown"); |
| 370 | + } catch (error: any) { |
| 371 | + assert.equal(error.message, "plain text error message"); |
| 372 | + } |
| 373 | + }); |
| 374 | + }); |
275 | 375 | }); |
0 commit comments