|
| 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 vscode from "vscode"; |
| 16 | +import { testAssetPath, testAssetUri } from "../fixtures"; |
| 17 | +import { waitForNoRunningTasks } from "../utilities/tasks"; |
| 18 | +import { expect } from "chai"; |
| 19 | +import { |
| 20 | + continueSession, |
| 21 | + waitForDebugAdapterRequest, |
| 22 | + waitUntilDebugSessionTerminates, |
| 23 | +} from "../utilities/debug"; |
| 24 | +import { Version } from "../../src/utilities/version"; |
| 25 | +import { activateExtensionForSuite, folderInRootWorkspace } from "./utilities/testutilities"; |
| 26 | +import { WorkspaceContext } from "../../src/WorkspaceContext"; |
| 27 | +import { join } from "path"; |
| 28 | +import { closeAllEditors } from "../utilities/commands"; |
| 29 | + |
| 30 | +function normalizePath(...segments: string[]): string { |
| 31 | + let path = join(...segments); |
| 32 | + if (process.platform === "win32") { |
| 33 | + path = path.endsWith(".exe") ? path : path + ".exe"; |
| 34 | + path = path.replace(/\//g, "\\"); |
| 35 | + } |
| 36 | + return path.toLocaleLowerCase(); // Windows may use d:\ or D:\ |
| 37 | +} |
| 38 | + |
| 39 | +suite("SwiftSnippet Test Suite @slow", function () { |
| 40 | + this.timeout(120000); |
| 41 | + |
| 42 | + const uri = testAssetUri("defaultPackage/Snippets/hello.swift"); |
| 43 | + const breakpoints = [ |
| 44 | + new vscode.SourceBreakpoint(new vscode.Location(uri, new vscode.Position(2, 0))), |
| 45 | + ]; |
| 46 | + let workspaceContext: WorkspaceContext; |
| 47 | + |
| 48 | + activateExtensionForSuite({ |
| 49 | + async setup(ctx) { |
| 50 | + workspaceContext = ctx; |
| 51 | + |
| 52 | + const folder = await folderInRootWorkspace("defaultPackage", workspaceContext); |
| 53 | + if (folder.workspaceContext.toolchain.swiftVersion.isLessThan(new Version(6, 0, 0))) { |
| 54 | + this.skip(); |
| 55 | + } |
| 56 | + await waitForNoRunningTasks(); |
| 57 | + |
| 58 | + // File needs to be open for command to be enabled |
| 59 | + const doc = await vscode.workspace.openTextDocument(uri.fsPath); |
| 60 | + await vscode.window.showTextDocument(doc); |
| 61 | + |
| 62 | + // Set a breakpoint |
| 63 | + vscode.debug.addBreakpoints(breakpoints); |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + suiteTeardown(async () => { |
| 68 | + closeAllEditors(); |
| 69 | + vscode.debug.removeBreakpoints(breakpoints); |
| 70 | + }); |
| 71 | + |
| 72 | + test("Run `Swift: Run Swift Snippet` command for snippet file", async () => { |
| 73 | + const sessionPromise = waitUntilDebugSessionTerminates("Run hello"); |
| 74 | + |
| 75 | + const succeeded = await vscode.commands.executeCommand("swift.runSnippet"); |
| 76 | + |
| 77 | + expect(succeeded).to.be.true; |
| 78 | + const session = await sessionPromise; |
| 79 | + expect(normalizePath(session.configuration.program)).to.equal( |
| 80 | + normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello") |
| 81 | + ); |
| 82 | + expect(session.configuration).to.have.property("noDebug", true); |
| 83 | + }); |
| 84 | + |
| 85 | + test("Run `Swift: Debug Swift Snippet` command for snippet file", async () => { |
| 86 | + const bpPromise = waitForDebugAdapterRequest("Run hello", "stackTrace"); |
| 87 | + const sessionPromise = waitUntilDebugSessionTerminates("Run hello"); |
| 88 | + |
| 89 | + const succeeded = vscode.commands.executeCommand("swift.debugSnippet"); |
| 90 | + |
| 91 | + // Once bp is hit, continue |
| 92 | + await bpPromise.then(() => continueSession()); |
| 93 | + |
| 94 | + await expect(succeeded).to.eventually.be.true; |
| 95 | + |
| 96 | + const session = await sessionPromise; |
| 97 | + expect(normalizePath(session.configuration.program)).to.equal( |
| 98 | + normalizePath(testAssetPath("defaultPackage"), ".build", "debug", "hello") |
| 99 | + ); |
| 100 | + expect(session.configuration).to.not.have.property("noDebug"); |
| 101 | + }); |
| 102 | +}); |
0 commit comments