|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
2 | 2 | import * as vscode from "vscode" |
3 | 3 | import { userInfo } from "os" |
4 | | -import { getShell } from "../shell" |
| 4 | +import { getShell, getAutomationShell } from "../shell" |
5 | 5 |
|
6 | 6 | // Mock vscode module |
7 | 7 | vi.mock("vscode", () => ({ |
@@ -485,4 +485,213 @@ describe("Shell Detection Tests", () => { |
485 | 485 | expect(result).toBe("/bin/bash") // Should fall back to safe default |
486 | 486 | }) |
487 | 487 | }) |
| 488 | + |
| 489 | + // -------------------------------------------------------------------------- |
| 490 | + // Automation Shell Detection Tests |
| 491 | + // -------------------------------------------------------------------------- |
| 492 | + describe("Automation Shell Detection", () => { |
| 493 | + describe("Windows Automation Shell", () => { |
| 494 | + beforeEach(() => { |
| 495 | + Object.defineProperty(process, "platform", { value: "win32" }) |
| 496 | + }) |
| 497 | + |
| 498 | + it("uses automation profile path when configured", () => { |
| 499 | + const mockConfig = { |
| 500 | + get: vi.fn((key: string) => { |
| 501 | + if (key === "automationProfile.windows") { |
| 502 | + return { path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe" } |
| 503 | + } |
| 504 | + return undefined |
| 505 | + }), |
| 506 | + } |
| 507 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 508 | + |
| 509 | + expect(getAutomationShell()).toBe("C:\\Program Files\\PowerShell\\7\\pwsh.exe") |
| 510 | + }) |
| 511 | + |
| 512 | + it("handles array path in automation profile", () => { |
| 513 | + const mockConfig = { |
| 514 | + get: vi.fn((key: string) => { |
| 515 | + if (key === "automationProfile.windows") { |
| 516 | + return { path: ["C:\\Program Files\\Git\\bin\\bash.exe", "bash.exe"] } |
| 517 | + } |
| 518 | + return undefined |
| 519 | + }), |
| 520 | + } |
| 521 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 522 | + |
| 523 | + expect(getAutomationShell()).toBe("C:\\Program Files\\Git\\bin\\bash.exe") |
| 524 | + }) |
| 525 | + |
| 526 | + it("returns null when no automation profile is configured", () => { |
| 527 | + const mockConfig = { |
| 528 | + get: vi.fn((key: string) => { |
| 529 | + if (key === "automationProfile.windows") { |
| 530 | + return null |
| 531 | + } |
| 532 | + return undefined |
| 533 | + }), |
| 534 | + } |
| 535 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 536 | + |
| 537 | + expect(getAutomationShell()).toBeNull() |
| 538 | + }) |
| 539 | + |
| 540 | + it("returns null when automation profile has no path", () => { |
| 541 | + const mockConfig = { |
| 542 | + get: vi.fn((key: string) => { |
| 543 | + if (key === "automationProfile.windows") { |
| 544 | + return { source: "PowerShell" } // No path property |
| 545 | + } |
| 546 | + return undefined |
| 547 | + }), |
| 548 | + } |
| 549 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 550 | + |
| 551 | + expect(getAutomationShell()).toBeNull() |
| 552 | + }) |
| 553 | + |
| 554 | + it("returns null when automation profile path is not in allowlist", () => { |
| 555 | + const mockConfig = { |
| 556 | + get: vi.fn((key: string) => { |
| 557 | + if (key === "automationProfile.windows") { |
| 558 | + return { path: "C:\\malicious\\shell.exe" } |
| 559 | + } |
| 560 | + return undefined |
| 561 | + }), |
| 562 | + } |
| 563 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 564 | + |
| 565 | + expect(getAutomationShell()).toBeNull() |
| 566 | + }) |
| 567 | + }) |
| 568 | + |
| 569 | + describe("macOS Automation Shell", () => { |
| 570 | + beforeEach(() => { |
| 571 | + Object.defineProperty(process, "platform", { value: "darwin" }) |
| 572 | + }) |
| 573 | + |
| 574 | + it("uses automation profile path when configured", () => { |
| 575 | + const mockConfig = { |
| 576 | + get: vi.fn((key: string) => { |
| 577 | + if (key === "automationProfile.osx") { |
| 578 | + return { path: "/bin/bash" } |
| 579 | + } |
| 580 | + return undefined |
| 581 | + }), |
| 582 | + } |
| 583 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 584 | + |
| 585 | + expect(getAutomationShell()).toBe("/bin/bash") |
| 586 | + }) |
| 587 | + |
| 588 | + it("handles array path in automation profile", () => { |
| 589 | + const mockConfig = { |
| 590 | + get: vi.fn((key: string) => { |
| 591 | + if (key === "automationProfile.osx") { |
| 592 | + return { path: ["/usr/local/bin/bash", "/bin/bash"] } |
| 593 | + } |
| 594 | + return undefined |
| 595 | + }), |
| 596 | + } |
| 597 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 598 | + |
| 599 | + expect(getAutomationShell()).toBe("/usr/local/bin/bash") |
| 600 | + }) |
| 601 | + |
| 602 | + it("returns null when no automation profile is configured", () => { |
| 603 | + const mockConfig = { |
| 604 | + get: vi.fn(() => undefined), |
| 605 | + } |
| 606 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 607 | + |
| 608 | + expect(getAutomationShell()).toBeNull() |
| 609 | + }) |
| 610 | + }) |
| 611 | + |
| 612 | + describe("Linux Automation Shell", () => { |
| 613 | + beforeEach(() => { |
| 614 | + Object.defineProperty(process, "platform", { value: "linux" }) |
| 615 | + }) |
| 616 | + |
| 617 | + it("uses automation profile path when configured", () => { |
| 618 | + const mockConfig = { |
| 619 | + get: vi.fn((key: string) => { |
| 620 | + if (key === "automationProfile.linux") { |
| 621 | + return { path: "/bin/bash" } |
| 622 | + } |
| 623 | + return undefined |
| 624 | + }), |
| 625 | + } |
| 626 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 627 | + |
| 628 | + expect(getAutomationShell()).toBe("/bin/bash") |
| 629 | + }) |
| 630 | + |
| 631 | + it("handles array path in automation profile", () => { |
| 632 | + const mockConfig = { |
| 633 | + get: vi.fn((key: string) => { |
| 634 | + if (key === "automationProfile.linux") { |
| 635 | + return { path: ["/usr/bin/zsh", "/bin/zsh"] } |
| 636 | + } |
| 637 | + return undefined |
| 638 | + }), |
| 639 | + } |
| 640 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 641 | + |
| 642 | + expect(getAutomationShell()).toBe("/usr/bin/zsh") |
| 643 | + }) |
| 644 | + |
| 645 | + it("returns null when automation profile path is empty array", () => { |
| 646 | + const mockConfig = { |
| 647 | + get: vi.fn((key: string) => { |
| 648 | + if (key === "automationProfile.linux") { |
| 649 | + return { path: [] } |
| 650 | + } |
| 651 | + return undefined |
| 652 | + }), |
| 653 | + } |
| 654 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 655 | + |
| 656 | + expect(getAutomationShell()).toBeNull() |
| 657 | + }) |
| 658 | + |
| 659 | + it("validates automation shell against allowlist", () => { |
| 660 | + const mockConfig = { |
| 661 | + get: vi.fn((key: string) => { |
| 662 | + if (key === "automationProfile.linux") { |
| 663 | + return { path: "/usr/bin/evil-shell" } |
| 664 | + } |
| 665 | + return undefined |
| 666 | + }), |
| 667 | + } |
| 668 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 669 | + |
| 670 | + expect(getAutomationShell()).toBeNull() |
| 671 | + }) |
| 672 | + }) |
| 673 | + |
| 674 | + describe("Unknown Platform Automation Shell", () => { |
| 675 | + it("returns null for unknown platforms", () => { |
| 676 | + Object.defineProperty(process, "platform", { value: "sunos" }) |
| 677 | + const mockConfig = { |
| 678 | + get: vi.fn(() => undefined), |
| 679 | + } |
| 680 | + vi.mocked(vscode.workspace.getConfiguration).mockReturnValue(mockConfig as any) |
| 681 | + |
| 682 | + expect(getAutomationShell()).toBeNull() |
| 683 | + }) |
| 684 | + }) |
| 685 | + |
| 686 | + describe("Error Handling in Automation Shell", () => { |
| 687 | + it("handles configuration errors gracefully", () => { |
| 688 | + Object.defineProperty(process, "platform", { value: "win32" }) |
| 689 | + vscode.workspace.getConfiguration = () => { |
| 690 | + throw new Error("Configuration error") |
| 691 | + } |
| 692 | + |
| 693 | + expect(getAutomationShell()).toBeNull() |
| 694 | + }) |
| 695 | + }) |
| 696 | + }) |
488 | 697 | }) |
0 commit comments