|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import { beforeEach, afterEach } from "mocha"; |
| 16 | +import { expect } from "chai"; |
| 17 | +import * as sinon from "sinon"; |
| 18 | +import { findBinaryPath } from "../../../src/utilities/shell"; |
| 19 | +import * as utilities from "../../../src/utilities/utilities"; |
| 20 | + |
| 21 | +suite("Shell Unit Test Suite", () => { |
| 22 | + let execFileStub: sinon.SinonStub; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + execFileStub = sinon.stub(utilities, "execFile"); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + sinon.restore(); |
| 30 | + }); |
| 31 | + |
| 32 | + suite("findBinaryPath", () => { |
| 33 | + test("returns the path to a binary in the PATH", async () => { |
| 34 | + execFileStub.resolves({ |
| 35 | + stdout: "node is /usr/local/bin/node\n", |
| 36 | + stderr: "", |
| 37 | + }); |
| 38 | + |
| 39 | + const binaryPath = await findBinaryPath("node"); |
| 40 | + expect(binaryPath).to.equal("/usr/local/bin/node"); |
| 41 | + expect(execFileStub).to.have.been.calledWith("/bin/sh", [ |
| 42 | + "-c", |
| 43 | + "LC_MESSAGES=C type node", |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + test("throws for a non-existent binary", async () => { |
| 48 | + execFileStub.resolves({ |
| 49 | + stdout: "", |
| 50 | + stderr: "sh: type: nonexistentbinary: not found\n", |
| 51 | + }); |
| 52 | + |
| 53 | + try { |
| 54 | + await findBinaryPath("nonexistentbinary"); |
| 55 | + expect.fail("Expected an error to be thrown for a non-existent binary"); |
| 56 | + } catch (error) { |
| 57 | + expect(error).to.be.an("error"); |
| 58 | + expect((error as Error).message).to.include("nonexistentbinary"); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments