|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 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 * as assert from "assert"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import * as path from "path"; |
| 18 | +import { anything, objectContaining, spy, verify, when } from "ts-mockito"; |
| 19 | +import { mockNamespace } from "../../unit-tests/MockUtils"; |
| 20 | +import { openPackage } from "../../../src/commands/openPackage"; |
| 21 | +import { Version } from "../../../src/utilities/version"; |
| 22 | +import * as fs from "../../../src/utilities/filesystem"; |
| 23 | + |
| 24 | +suite("OpenPackage Command Test Suite", () => { |
| 25 | + const workspaceMock = mockNamespace(vscode, "workspace"); |
| 26 | + const windowMock = mockNamespace(vscode, "window"); |
| 27 | + const fsSpy = spy(fs); |
| 28 | + |
| 29 | + async function runTestWithMockFs(version: Version, expected: string, paths: string[]) { |
| 30 | + const basePath = "/test"; |
| 31 | + const expectedPath = path.join(basePath, expected); |
| 32 | + paths.forEach(p => { |
| 33 | + when(fsSpy.fileExists(path.join(basePath, p))).thenResolve(true); |
| 34 | + }); |
| 35 | + when(windowMock.showTextDocument(anything())).thenResolve(); |
| 36 | + await openPackage(version, vscode.Uri.file(basePath)); |
| 37 | + |
| 38 | + verify(workspaceMock.openTextDocument(objectContaining({ fsPath: expectedPath }))).once(); |
| 39 | + verify(windowMock.showTextDocument(anything())).once(); |
| 40 | + } |
| 41 | + |
| 42 | + test("Opens nothing when there is no package.swift", async () => { |
| 43 | + await openPackage(new Version(6, 0, 0), vscode.Uri.file("/test")); |
| 44 | + |
| 45 | + verify(windowMock.showTextDocument(anything())).never(); |
| 46 | + }); |
| 47 | + |
| 48 | + test("Opens Package.swift file", async () => { |
| 49 | + await runTestWithMockFs(new Version(6, 0, 0), "Package.swift", ["Package.swift"]); |
| 50 | + }); |
| 51 | + |
| 52 | + test("Opens [email protected] file", async () => { |
| 53 | + await runTestWithMockFs(new Version(6, 0, 0), "[email protected]", [ |
| 54 | + "Package.swift", |
| 55 | + |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + test("Opens [email protected] file", async () => { |
| 60 | + await runTestWithMockFs(new Version(6, 1, 2), "[email protected]", [ |
| 61 | + "Package.swift", |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + ]); |
| 66 | + }); |
| 67 | +}); |
0 commit comments