diff --git a/cli/src/commands/workspace/workspace.ts b/cli/src/commands/workspace/workspace.ts index 4625e589ca..a4d4d224ff 100644 --- a/cli/src/commands/workspace/workspace.ts +++ b/cli/src/commands/workspace/workspace.ts @@ -260,7 +260,7 @@ export async function add( Deno.exit(1); } - await addWorkspace( + const added = await addWorkspace( { name: workspaceName, remote: remote, @@ -269,6 +269,9 @@ export async function add( }, opts ); + if (!added) { + return; + } await setActiveWorkspace(workspaceName, opts.configDir); log.info( @@ -278,7 +281,7 @@ export async function add( ); } -export async function addWorkspace(workspace: Workspace, opts: any) { +export async function addWorkspace(workspace: Workspace, opts: any): Promise { workspace.remote = new URL(workspace.remote).toString(); // add trailing slash in all cases! // Check for conflicts before adding @@ -330,7 +333,7 @@ export async function addWorkspace(workspace: Workspace, opts: any) { if (!overwrite) { log.info(colors.yellow("Operation cancelled.")); - return; + return false; } } } @@ -350,6 +353,7 @@ export async function addWorkspace(workspace: Workspace, opts: any) { await file.write(new TextEncoder().encode(JSON.stringify(workspace) + "\n")); file.close(); + return true; } export async function removeWorkspace( diff --git a/cli/test/workspace_conflicts.test.ts b/cli/test/workspace_conflicts.test.ts index 2e550d69f6..1338b7458e 100644 --- a/cli/test/workspace_conflicts.test.ts +++ b/cli/test/workspace_conflicts.test.ts @@ -121,6 +121,45 @@ Deno.test("addWorkspace: allows same workspace (name, remote, workspaceId) with }); }); +Deno.test("addWorkspace: returns true on successful add", async () => { + await withTestConfig(async (testConfigDir) => { + await clearTestRemotes(testConfigDir); + + const workspace = { + name: "return_test", + remote: "http://localhost:8001/", + workspaceId: "test", + token: "token1" + }; + + const result = await addWorkspace(workspace, { force: true, configDir: testConfigDir }); + assertEquals(result, true); + }); +}); + +Deno.test("addWorkspace: returns true when force-overwriting conflict", async () => { + await withTestConfig(async (testConfigDir) => { + await clearTestRemotes(testConfigDir); + + const workspace1 = { + name: "force_test", + remote: "http://localhost:8001/", + workspaceId: "workspace1", + token: "token1" + }; + await addWorkspace(workspace1, { force: true, configDir: testConfigDir }); + + const workspace2 = { + name: "force_test", + remote: "http://localhost:8002/", + workspaceId: "workspace2", + token: "token2" + }; + const result = await addWorkspace(workspace2, { force: true, configDir: testConfigDir }); + assertEquals(result, true); + }); +}); + Deno.test("addWorkspace: allows different workspaces on different remotes", async () => { await withTestConfig(async (testConfigDir) => { await clearTestRemotes(testConfigDir);