|
| 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"; |
1 | 16 | import { expect } from "chai";
|
| 17 | +import * as sinon from "sinon"; |
2 | 18 | import { findBinaryPath } from "../../../src/utilities/shell";
|
| 19 | +import * as utilities from "../../../src/utilities/utilities"; |
3 | 20 |
|
4 | 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 | + |
5 | 32 | suite("findBinaryPath", () => {
|
6 | 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 | + |
7 | 39 | const binaryPath = await findBinaryPath("node");
|
8 |
| - expect(binaryPath).to.be.a("string"); |
9 |
| - expect(binaryPath).to.include("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 | + ]); |
10 | 45 | });
|
11 | 46 |
|
12 | 47 | test("throws for a non-existent binary", async () => {
|
| 48 | + execFileStub.resolves({ |
| 49 | + stdout: "", |
| 50 | + stderr: "sh: type: nonexistentbinary: not found\n", |
| 51 | + }); |
| 52 | + |
13 | 53 | try {
|
14 | 54 | await findBinaryPath("nonexistentbinary");
|
15 | 55 | expect.fail("Expected an error to be thrown for a non-existent binary");
|
16 | 56 | } catch (error) {
|
17 | 57 | expect(error).to.be.an("error");
|
| 58 | + expect((error as Error).message).to.include("nonexistentbinary"); |
18 | 59 | }
|
19 | 60 | });
|
20 | 61 | });
|
|
0 commit comments