Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Add clone method to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
TripleSpeeder committed Sep 4, 2019
1 parent 764d7f2 commit 9ccbd0f
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/types/workspaces/Workspace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import fse from "fs-extra";
import UUID from "uuid";

import WorkspaceSettings from "../settings/WorkspaceSettings";
import ContractCache from "../contracts/ContractCache";
Expand Down Expand Up @@ -117,6 +118,40 @@ class Workspace {
// a solution here
}
}

clone(cloneName) {
const sanitizedName = Workspace.getSanitizedName(cloneName);
const configDirectory = path.join(this.workspaceDirectory, "..", "..");
const cloneDirectory = path.join(
configDirectory,
"workspaces",
sanitizedName,
);

try {
// copy workspace directory, make sure not to overwrite any existing data
fse.copySync(this.workspaceDirectory, cloneDirectory, {
overwrite: false,
errorOnExist: true,
});
} catch (e) {
// Failed to copy directory. Most likely target already exists
// TODO: Handle this error more gracefully/provide user feedback
return;
}

// update settings of cloned workspace to match new location
const db_path = path.join(cloneDirectory, "chaindata");
let settings = new WorkspaceSettings(cloneDirectory, db_path);
settings.bootstrap();
// set new db_path
settings.set("server.db_path", db_path);
// generate new uuid
settings.set("uuid", UUID.v4());
// set new name
settings.set("name", cloneName);
return new Workspace(cloneName, configDirectory);
}
}

export default Workspace;
98 changes: 98 additions & 0 deletions test/mocha/workspaces/clone-workspace.subtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import Workspace from "../../../src/main/types/workspaces/Workspace";
import temp from "temp";
import assert from "assert";
import fs from "fs";
import path from "path";
import ganacheLib from "ganache-core";
import Web3 from "web3";

describe("Clone Workspace", () => {
let configDirectory = "/";
let workspace;
let clonedWorkspace;
const cloneName = "cloned workspace";

before("create folder where workspace will live", async () => {
temp.track();
configDirectory = temp.mkdirSync("ganache-temp-workspaces");
fs.mkdirSync(path.join(configDirectory, "workspaces"));
});

before("created and bootstrapped new workspace", async () => {
workspace = new Workspace("Temp Workspace", configDirectory);
workspace.bootstrap();
});

it("did not clone workspace into existing directory", async () => {
clonedWorkspace = workspace.clone(workspace.name);
assert.strictEqual(
clonedWorkspace,
undefined,
"Workspace was cloned into existing directory",
);
});

it("cloned workspace without error", async () => {
clonedWorkspace = workspace.clone(cloneName);
assert(
fs.existsSync(clonedWorkspace.workspaceDirectory),
"Cloned workspace directory wasn't created",
);
const settingsFile = path.join(
clonedWorkspace.workspaceDirectory,
"Settings",
);
assert(
fs.existsSync(settingsFile),
"Cloned Workspace Settings file wasn't created",
);
});

it("applied correct settings to cloned workspace", async () => {
assert.notEqual(
clonedWorkspace.settings.get("uuid"),
workspace.settings.get("uuid"),
"The uuid of the cloned workspace should be different from the original",
);
assert.equal(
clonedWorkspace.name,
cloneName,
"Cloned workspace should have the correct name set",
);
assert.equal(
clonedWorkspace.settings.get("name"),
cloneName,
"Cloned workspace settings should have correct name set",
);
assert.notEqual(
workspace.settings.get("server.db_path"),
clonedWorkspace.settings.get("server.db_path"),
"Cloned workspace db_path is the same like original workspace",
);
assert.equal(
clonedWorkspace.settings.get("server.db_path"),
path.join(clonedWorkspace.workspaceDirectory, "chaindata"),
"Cloned workspace db_path is not correct",
);
assert.equal(
clonedWorkspace.settings.get("server.mnemonic"),
workspace.settings.get("server.mnemonic"),
"Mnemeonic of cloned workspace is different from original",
);
});

it("started and stopped cloned workspace with no errors", done => {
var web3 = new Web3();
web3.setProvider(ganacheLib.provider(clonedWorkspace.settings.getAll()));

web3.eth.getAccounts(function(err, result) {
if (err) return done(err);
assert(
result.length,
10,
"The number of accounts created should be 10 (the default)",
);
done();
});
});
});
1 change: 1 addition & 0 deletions test/mocha/workspaces/workspaces.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe("Workspaces", function() {
require("./new-workspace.subtest");
require("./clone-workspace.subtest");
require("./workspace-manager.subtest");
});

0 comments on commit 9ccbd0f

Please sign in to comment.