Skip to content

Commit

Permalink
Fix and re-enable the local dependency tests (#1306)
Browse files Browse the repository at this point in the history
More deterministic to see the result on the node, not just look at
the build exit code
  • Loading branch information
award999 authored Jan 13, 2025
1 parent 9a4b2db commit c5c3bd4
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 120 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@

// Configure Prettier
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "esbenp.prettier-vscode",

// Disable for when opening files in this workspace
"swift.disableAutoResolve": true,
"swift.autoGenerateLaunchConfigurations": false
}
6 changes: 3 additions & 3 deletions assets/test/Swift-Markdown/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ let package = Package(
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "PackageLib",
targets: ["PackageLib"]),
name: "MarkdownLib",
targets: ["MarkdownLib"]),
],
targets: [
.target(
name: "PackageLib",
name: "MarkdownLib",
dependencies: []
),
]
Expand Down
2 changes: 1 addition & 1 deletion assets/test/dependencies/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
targets: [
.executableTarget(
name: "dependencies",
dependencies: [.product(name: "PackageLib", package: "Swift-Markdown")],
dependencies: [.product(name: "MarkdownLib", package: "swift-markdown")],
path: "Sources"),
]
)
2 changes: 1 addition & 1 deletion assets/test/dependencies/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PackageLib
import MarkdownLib

print("Test Asset:(dependencies)")
print(a)
4 changes: 2 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
vscode.commands.registerCommand("swift.insertFunctionComment", () =>
insertFunctionComment(ctx)
),
vscode.commands.registerCommand(Commands.USE_LOCAL_DEPENDENCY, item => {
vscode.commands.registerCommand(Commands.USE_LOCAL_DEPENDENCY, (item, dep) => {
if (item instanceof PackageNode) {
return useLocalDependency(item.name, ctx);
return useLocalDependency(item.name, ctx, dep);
}
}),
vscode.commands.registerCommand("swift.editDependency", item => {
Expand Down
29 changes: 16 additions & 13 deletions src/commands/dependencies/useLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ import { executeTaskWithUI } from "../utilities";
*/
export async function useLocalDependency(
identifier: string,
ctx: WorkspaceContext
ctx: WorkspaceContext,
dep: vscode.Uri | undefined
): Promise<boolean> {
const currentFolder = ctx.currentFolder;
if (!currentFolder) {
ctx.outputChannel.log("currentFolder is not set.");
return false;
}
const folders = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
defaultUri: currentFolder.folder,
openLabel: "Select",
title: "Select folder",
});

if (!folders) {
return false;
let folder = dep;
if (!folder) {
const folders = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
defaultUri: currentFolder.folder,
openLabel: "Select",
title: "Select folder",
});
if (!folders) {
return false;
}
folder = folders[0];
}
const folder = folders[0];
const task = createSwiftTask(
["package", "edit", "--path", folder.fsPath, identifier],
"Edit Package Dependency",
Expand Down
157 changes: 58 additions & 99 deletions test/integration-tests/commands/dependency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,146 +18,105 @@ import {
PackageDependenciesProvider,
PackageNode,
} from "../../../src/ui/PackageDependencyProvider";
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities/tasks";
import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider";
import { testAssetUri } from "../../fixtures";
import { FolderContext } from "../../../src/FolderContext";
import { WorkspaceContext } from "../../../src/WorkspaceContext";
import * as sinon from "sinon";
import { Commands } from "../../../src/commands";
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities/tasks";
import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider";

suite("Dependency Commmands Test Suite", function () {
// full workflow's interaction with spm is longer than the default timeout
// 60 seconds for each test should be more than enough
this.timeout(60 * 1000);

suite("spm Update Contract Tests", function () {
let folderContext: FolderContext;
let workspaceContext: WorkspaceContext;

activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
await waitForNoRunningTasks();
folderContext = await folderInRootWorkspace("defaultPackage", workspaceContext);
await workspaceContext.focusFolder(folderContext);
},
});

test("Contract: spm update", async function () {
// Contract: spm update
const result = await vscode.commands.executeCommand(Commands.UPDATE_DEPENDENCIES);
expect(result).to.be.true;
});
let defaultContext: FolderContext;
let depsContext: FolderContext;
let workspaceContext: WorkspaceContext;

activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
defaultContext = await folderInRootWorkspace("defaultPackage", workspaceContext);
depsContext = await folderInRootWorkspace("dependencies", workspaceContext);
await waitForNoRunningTasks();
},
});

suite("spm Resolve Contract Tests", function () {
let folderContext: FolderContext;
let workspaceContext: WorkspaceContext;

activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
await waitForNoRunningTasks();
folderContext = await folderInRootWorkspace("dependencies", workspaceContext);
await workspaceContext.focusFolder(folderContext);
},
});
test("Swift: Update Package Dependencies", async function () {
await workspaceContext.focusFolder(defaultContext);
const result = await vscode.commands.executeCommand(Commands.UPDATE_DEPENDENCIES);
expect(result).to.be.true;
});

test("Contract: spm resolve", async () => {
const result = await vscode.commands.executeCommand(Commands.RESOLVE_DEPENDENCIES);
expect(result).to.be.true;
});
test("Swift: Resolve Package Dependencies", async function () {
await workspaceContext.focusFolder(defaultContext);
const result = await vscode.commands.executeCommand(Commands.RESOLVE_DEPENDENCIES);
expect(result).to.be.true;
});

suite("Full Work Flow Test Suite", function () {
let folderContext: FolderContext;
let workspaceContext: WorkspaceContext;
let tasks: SwiftTask;
suite("Swift: Use Local Dependency", function () {
let treeProvider: PackageDependenciesProvider;
let item: PackageNode;

activateExtensionForSuite({
async setup(ctx) {
// FIXME: Disable this test suite as this is dependent on external git dependency
// and introduces flakinesss when run in the CI setting. The spm command only
// runs if the dependency is remote, which make faking difficult.
// For enabling the test in the future, we would need to set up the environment
// into a pre-resolved state, so spm does not need to visit remote git url.
this.skip();
// Check before each test case start:
// Expect to fail without setting up local version
workspaceContext = ctx;
await waitForNoRunningTasks();
folderContext = await folderInRootWorkspace("dependencies", workspaceContext);
await workspaceContext.focusFolder(folderContext);

tasks = (await getBuildAllTask(folderContext)) as SwiftTask;
const { exitCode, output } = await executeTaskAndWaitForResult(tasks);
expect(exitCode, `${output}`).to.not.equal(0);
expect(output).to.include("PackageLib");
expect(output).to.include("required");

treeProvider = new PackageDependenciesProvider(workspaceContext);

const items = await treeProvider.getChildren();
item = items.find(n => n.name === "swift-markdown") as PackageNode;
},
async teardown() {
treeProvider?.dispose();
},

setup(async () => {
await workspaceContext.focusFolder(depsContext);
await executeTaskAndWaitForResult((await getBuildAllTask(depsContext)) as SwiftTask);
treeProvider = new PackageDependenciesProvider(workspaceContext);
});

async function useLocalDependencyTest() {
// Contract: spm edit with user supplied local version of dependency
const windowMock = sinon.stub(vscode.window, "showOpenDialog");
windowMock.resolves([testAssetUri("Swift-Markdown")]);
let result = await vscode.commands.executeCommand(Commands.USE_LOCAL_DEPENDENCY, item);
expect(result).to.be.true;
windowMock.restore();
teardown(() => {
treeProvider?.dispose();
});

// Make sure new dependencies resolve before building
result = await vscode.commands.executeCommand(Commands.RESOLVE_DEPENDENCIES);
async function getDependency() {
const items = await treeProvider.getChildren();
return items.find(n => n.name === "swift-markdown") as PackageNode;
}

async function useLocalDependencyTest() {
// spm edit with user supplied local version of dependency
const item = await getDependency();
const localDep = testAssetUri("swift-markdown");
const result = await vscode.commands.executeCommand(
Commands.USE_LOCAL_DEPENDENCY,
item,
localDep
);
expect(result).to.be.true;

// This will now pass as we have the required library
const { exitCode, output } = await executeTaskAndWaitForResult(tasks);
expect(exitCode, `${output}`).to.equal(0);
expect(output).to.include("defaultpackage");
expect(output).to.include("not used by any target");
// Make sure using local
expect((await getDependency()).type).to.equal("editing");
}

async function assertDependencyNoLongerExists() {
// Expect to fail again now dependency is missing
const { exitCode, output } = await executeTaskAndWaitForResult(tasks);
expect(exitCode, `${output}`).to.not.equal(0);
expect(output).to.include("PackageLib");
expect(output).to.include("required");
async function assertUsingRemote() {
expect((await getDependency()).type).to.equal("remote");
}

test("Use local dependency - Reset", async function () {
test("Swift: Reset Package Dependencies", async function () {
// spm reset after using local dependency is broken on windows
if (process.platform === "win32") {
this.skip();
}
await useLocalDependencyTest();

// Contract: spm reset
// spm reset
const result = await vscode.commands.executeCommand(Commands.RESET_PACKAGE);
expect(result).to.be.true;

await assertDependencyNoLongerExists();
await assertUsingRemote();
});

test("Use local dependency - Add to workspace - Unedit", async () => {
test("Swift: Revert To Original Version", async () => {
await useLocalDependencyTest();

// Contract: spm unedit
const result = await vscode.commands.executeCommand(Commands.UNEDIT_DEPENDENCY, item);
const result = await vscode.commands.executeCommand(
Commands.UNEDIT_DEPENDENCY,
await getDependency()
);
expect(result).to.be.true;

await assertDependencyNoLongerExists();
await assertUsingRemote();
});
});
});

0 comments on commit c5c3bd4

Please sign in to comment.