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

Commit

Permalink
Complete preserve integration into @truffle/core
Browse files Browse the repository at this point in the history
- Add dependencies on the preserve packages (-filecoin)
- Add functionality for help function (rather than object)
- Add preserve to the list of commands
- Add tests to main truffle package
  • Loading branch information
rkalis committed Feb 26, 2021
1 parent ec0b672 commit b73efa9
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/core/lib/command.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const TaskError = require("./errors/taskerror");
const yargs = require("yargs/yargs");
const {bundled, core} = require("../lib/version").info();
const { bundled, core } = require("../lib/version").info();
const OS = require("os");
const analytics = require("../lib/services/analytics");
const {extractFlags} = require("./utils/utils"); // Contains utility methods
const { extractFlags } = require("./utils/utils"); // Contains utility methods

class Command {
constructor(commands) {
Expand Down Expand Up @@ -75,6 +75,10 @@ class Command {
async run(inputStrings, options) {
const result = this.getCommand(inputStrings, options.noAliases);

if (typeof result.command.help === "function") {
result.command.help = await result.command.help(options);
}

if (result == null) {
throw new TaskError(
"Cannot find command based on input: " + JSON.stringify(inputStrings)
Expand Down
14 changes: 9 additions & 5 deletions packages/core/lib/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const command = {
run: async function (options) {
const commands = require("./index");
if (options._.length === 0) {
this.displayCommandHelp("help");
await this.displayCommandHelp("help", options);
return;
}
const selectedCommand = options._[0];

if (commands[selectedCommand]) {
this.displayCommandHelp(selectedCommand);
await this.displayCommandHelp(selectedCommand, options);
return;
} else {
console.log(`\n Cannot find the given command '${selectedCommand}'`);
Expand All @@ -33,9 +33,13 @@ const command = {
return;
}
},
displayCommandHelp: function (selectedCommand) {
let commands = require("./index");
var commandHelp = commands[selectedCommand].help;
displayCommandHelp: async function (selectedCommand, options) {
const commands = require("./index");
let commandHelp = commands[selectedCommand].help;

if (typeof commandHelp === "function") {
commandHelp = await commandHelp(options);
}

console.log(`\n Usage: ${commandHelp.usage}`);
console.log(` Description: ${commands[selectedCommand].description}`);
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
networks: require("./networks"),
obtain: require("./obtain"),
opcode: require("./opcode"),
preserve: require("./preserve"),
publish: require("./publish"),
run: require("./run"),
test: require("./test"),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/commands/preserve/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getConfig = options => {
config = Config.default().with(options);
}

config.plugins = [...config.plugins, ...defaultPlugins];
config.plugins = [...(config.plugins || []), ...defaultPlugins];

return config;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"@truffle/interface-adapter": "^0.4.19",
"@truffle/migrate": "^3.2.12",
"@truffle/plugins": "^0.1.1",
"@truffle/preserve": "^0.1.0",
"@truffle/preserve-fs": "^0.1.0",
"@truffle/preserve-to-ipfs": "^0.1.0",
"@truffle/provider": "^0.2.26",
"@truffle/provisioner": "^0.2.13",
"@truffle/require": "^2.0.58",
Expand Down
26 changes: 19 additions & 7 deletions packages/core/test/lib/commands/preserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const { assert } = chai;

describe.only("preserve", () => {
describe("preserve", () => {
// Add mockPlugins folder to require path so stub plugins can be found
originalRequire("app-module-path").addPath(
path.resolve(__dirname, "../../mockPlugins")
Expand All @@ -21,7 +21,14 @@ describe.only("preserve", () => {
"_": ["./test/mockPlugins/dummy-loader"],
"environments": {
development: {
"dummy-recipe": {}
"dummy-recipe": {
selectedEnvironment: "development"
}
},
production: {
"dummy-recipe": {
selectedEnvironment: "production"
}
}
}
};
Expand Down Expand Up @@ -103,15 +110,20 @@ describe.only("preserve", () => {
console.log = originalConsoleLog;
});

it("should call the preserve plugin and propagate environment options", async () => {
it("should call the preserve plugin with default environment options", async () => {
const options = { ...defaultOptions };
options.environments.development["dummy-recipe"] = {
address: "http://localhost:5001"
};

await preserveCommand.run(options);

assert.include(output, "http://localhost:5001");
assert.include(output, "Provided environment name: development");
});

it("should call the preserve plugin with custom environment options", async () => {
const options = { ...defaultOptions, environment: "production" };

await preserveCommand.run(options);

assert.include(output, "Provided environment name: production");
});
});
});
Expand Down
6 changes: 4 additions & 2 deletions packages/core/test/mockPlugins/dummy-recipe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class Recipe {
dependencies = [];

constructor(environment) {
this.address = environment.address;
this.environmentName = environment.selectedEnvironment;
}

async *preserve({ controls }) {
yield* controls.log({ message: `Provided address: ${this.address}` });
yield* controls.log({
message: `Provided environment name: ${this.environmentName}`
});
return "Successfully called dummy-recipe:preserve()";
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/preserve-fs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@truffle/preserve-fs",
"version": "0.1.0-preserves.8",
"version": "0.1.0",
"description": "Truffle `preserve` command support for arbitrary files and directories",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
20 changes: 16 additions & 4 deletions packages/truffle/test/scenarios/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ const CommandRunner = require("../commandrunner");
const MemoryLogger = require("../memorylogger");
let config = {};

describe("truffle help [ @standalone ]", function() {
describe("truffle help [ @standalone ]", function () {
const logger = new MemoryLogger();
beforeEach("set up config for logger", function() {
beforeEach("set up config for logger", function () {
config.logger = logger;
});

describe("when run without arguments", function() {
describe("when run without arguments", function () {
it("displays general help", async () => {
await CommandRunner.run("help", config);
const output = logger.contents();
assert(output.includes("Usage: truffle <command> [options]"));
});
});

describe("when run with an argument", function() {
describe("when run with an argument", function () {
it("tells the user if it doesn't recognize the given command", async () => {
await CommandRunner.run("help eggplant", config);
const output = logger.contents();
Expand All @@ -29,5 +29,17 @@ describe("truffle help [ @standalone ]", function() {
const output = logger.contents();
assert(output.includes("Description: Compile contract source files"));
}).timeout(20000);

it("displays help for the given command with help function", async () => {
await CommandRunner.run("help preserve", config);
const output = logger.contents();
assert(
output.includes(
"Description: Save data to decentralized storage platforms like IPFS and Filecoin"
)
);
assert(output.includes("--environment"));
assert(output.includes("--ipfs"));
});
});
});
111 changes: 111 additions & 0 deletions packages/truffle/test/scenarios/commands/preserve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const assert = require("assert");
const CommandRunner = require("../commandrunner");
const MemoryLogger = require("../memorylogger");
const sandbox = require("../sandbox");
const path = require("path");

const logger = new MemoryLogger();
let config, project;

const loadSandboxLogger = source => {
project = path.join(__dirname, source);
return sandbox.load(project).then(conf => {
config = conf;
config.logger = logger;
});
};

describe("truffle preserve [ @standalone ]", () => {
// These tests are basically duplicates from "truffle run", but for "truffle preserve"
describe("plugin error handling", () => {
it("should throw when plugins are configured but not installed", async () => {
await loadSandboxLogger(
"../../sources/run/mockProjectWithMissingPluginModule"
);
await assert.rejects(CommandRunner.run("preserve . --mock", config));
const output = logger.contents();
assert(output.includes("listed as a plugin, but not found"));
}).timeout(10000);

it("should throw when plugins are missing truffle-plugin.json", async () => {
await loadSandboxLogger(
"../../sources/run/mockProjectWithMissingPluginConfig"
);
await assert.rejects(CommandRunner.run("preserve . --mock", config));
const output = logger.contents();
assert(output.includes("Error: truffle-plugin.json not found"));
}).timeout(10000);

it("should throw if recipe in truffle-plugin.json uses an absolute path", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithAbsolutePath"
);
await assert.rejects(CommandRunner.run("preserve . --mock", config));
const output = logger.contents();
assert(output.includes("Error: Absolute paths not allowed!"));
}).timeout(10000);
});

describe("preserve error handling", () => {
it("should throw when an unknown environment is specified", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await assert.rejects(
CommandRunner.run("preserve . --mock --environment unknown", config)
);
const output = logger.contents();
assert(output.includes("Unknown environment"));
}).timeout(20000);

it("should throw when no recipe is specified", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await assert.rejects(CommandRunner.run("preserve .", config));
const output = logger.contents();
assert(output.includes("No (valid) recipe specified"));
}).timeout(20000);

it("should throw when the specified recipe is not installed", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await assert.rejects(CommandRunner.run("preserve --unknown", config));
const output = logger.contents();
assert(output.includes("No (valid) recipe specified"));
}).timeout(20000);

it("should throw when no target path is specified", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await assert.rejects(CommandRunner.run("preserve --mock", config));
const output = logger.contents();
assert(output.includes("No preserve target specified"));
}).timeout(20000);
});

describe("success", () => {
it("should run the specified recipe with default environment", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await CommandRunner.run("preserve . --mock", config);
const output = logger.contents();
assert(output.includes("Provided environment name: development"));
});

it("should run the specified recipe with a custom environment", async () => {
await loadSandboxLogger(
"../../sources/preserve/mockProjectWithWorkingPlugin"
);
await CommandRunner.run(
"preserve . --mock --environment production",
config
);
const output = logger.contents();
assert(output.includes("Provided environment name: production"));
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ["truffle-mock"]
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
plugins: ["truffle-mock"],
environments: {
development: {
mock: {
selectedEnvironment: "development"
}
},
production: {
mock: {
selectedEnvironment: "production"
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};

0 comments on commit b73efa9

Please sign in to comment.