Skip to content

Commit

Permalink
Added wrap-plop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 23, 2021
1 parent 4a14c4d commit f83dbed
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 25 deletions.
7 changes: 5 additions & 2 deletions tests/actions.spec.js
Expand Up @@ -6,7 +6,6 @@ const fs = require("fs");
test("Plop to add and rename files", async () => {
const { findByText, fireEvent } = await renderPlop(["addAndNameFile"], {
cwd: resolve(__dirname, "./examples/add-action"),
debug: true,
});

expect(await findByText("What should the file name be?")).toBeTruthy();
Expand All @@ -33,7 +32,6 @@ test("Plop to add and rename files", async () => {
test("Plop to add and change file contents", async () => {
const { findByText, fireEvent } = await renderPlop(["addAndChangeFile"], {
cwd: resolve(__dirname, "./examples/add-action"),
debug: true,
});

expect(await findByText("What's your name?")).toBeTruthy();
Expand All @@ -56,3 +54,8 @@ test("Plop to add and change file contents", async () => {

fireEvent.sigterm();
});

test.todo("Test modify");
test.todo("Test append");
test.todo("Test built-in helpers");
test.todo("Test custom helpers");
19 changes: 19 additions & 0 deletions tests/examples/wrap-plop/index.js
@@ -0,0 +1,19 @@
#!/usr/bin/env node
const path = require("path");
const args = process.argv.slice(2);
// This would be `require('plop')` in prod
const { Plop, run } = require("../../../instrumented/src/plop.js");
const argv = require("minimist")(args);

Plop.launch(
{
cwd: argv.cwd,
// In order for `plop` to always pick up the `plopfile.js` despite the CWD, you must use `__dirname`
configPath: path.join(__dirname, "plopfile.js"),
require: argv.require,
completion: argv.completion,
// This will merge the `plop` argv and the generator argv.
// This means that you don't need to use `--` anymore
},
(env) => run(env, undefined, true)
);
Empty file.
3 changes: 3 additions & 0 deletions tests/examples/wrap-plop/package.json
@@ -0,0 +1,3 @@
{
"name": "plop-example-wrap"
}
37 changes: 37 additions & 0 deletions tests/examples/wrap-plop/plopfile.js
@@ -0,0 +1,37 @@
module.exports = function (plop) {
plop.setGenerator("test", {
description: "this is a test",
prompts: [
{
type: "input",
name: "name",
message: "What is your name?",
validate: function (value) {
if (/.+/.test(value)) {
return true;
}
return "name is required";
},
},
{
type: "checkbox",
name: "toppings",
message: "What pizza toppings do you like?",
choices: [
{ name: "Cheese", value: "cheese", checked: true },
{ name: "Pepperoni", value: "pepperoni" },
{ name: "Pineapple", value: "pineapple" },
{ name: "Mushroom", value: "mushroom" },
{ name: "Bacon", value: "bacon", checked: true },
],
},
],
actions: [
{
type: "add",
path: "./output/added.txt",
templateFile: "./templates/to-add.txt",
},
],
});
};
1 change: 1 addition & 0 deletions tests/examples/wrap-plop/templates/to-add.txt
@@ -0,0 +1 @@
Hello
2 changes: 2 additions & 0 deletions tests/input-processing.spec.js
Expand Up @@ -101,3 +101,5 @@ test("Should bypass prompt by name", async () => {

fireEvent.sigterm();
});

test.todo("Dynamic actions");
52 changes: 29 additions & 23 deletions tests/render.js
@@ -1,29 +1,35 @@
const { render, ...props } = require("cli-testing-library");
const { resolve } = require("path");

module.exports = {
...props,
/**
* @param {Array} args
* @param {Object} opts
*/
renderPlop(args = [], opts = {}) {
const { cwd = __dirname } = opts;
/**
* @param {String} script
* @param {Array} args
* @param {Object} opts
*/
function renderScript(script, args = [], opts = {}) {
const { cwd = __dirname } = opts;

const rendered = render("npx", ["nyc", "--silent", "node", script, ...args], {
cwd,
});

const rendered = render(
"npx",
[
"nyc",
"--silent",
"node",
resolve(__dirname, "../instrumented/bin/plop.js"),
...args,
],
{
cwd,
}
);
return rendered;
}

return rendered;
},
/**
* @param {Array} args
* @param {Object} opts
*/
function renderPlop(args = [], opts = {}) {
return renderScript(
resolve(__dirname, "../instrumented/bin/plop.js"),
args,
opts
);
}

module.exports = {
...props,
renderScript,
renderPlop,
};
73 changes: 73 additions & 0 deletions tests/wrapper.spec.js
@@ -0,0 +1,73 @@
const { renderScript, renderPlop } = require("./render");
const { resolve } = require("path");
const { waitFor } = require("cli-testing-library");
const fs = require("fs");

const renderWrapper = (...props) => {
return renderScript(
resolve(__dirname, "./examples/wrap-plop/index.js"),
...props
);
};

test("wrapper should show version on v flag", async () => {
const { findByText } = await renderWrapper(["-v"]);

expect(await findByText(/^[\w\.-]+$/)).toBeTruthy();
});

test("wrapper should prompts", async () => {
const { findByText, fireEvent } = await renderWrapper([""], {
cwd: resolve(__dirname, "./examples/wrap-plop"),
});

expect(await findByText("What is your name?")).toBeTruthy();
fireEvent.sigterm();
});

test("wrapper should bypass prompts with index", async () => {
const { findByText, queryByText, fireEvent } = await renderWrapper(
["Corbin"],
{
cwd: resolve(__dirname, "./examples/wrap-plop"),
}
);

expect(await queryByText("What is your name?")).toBeFalsy();
expect(await findByText("What pizza toppings do you like?")).toBeTruthy();
fireEvent.sigterm();
});

test("wrapper should bypass prompts with name", async () => {
const { findByText, queryByText, fireEvent } = await renderWrapper(
["--name", "Corbin"],
{
cwd: resolve(__dirname, "./examples/wrap-plop"),
}
);

expect(await queryByText("What is your name?")).toBeFalsy();
expect(await findByText("What pizza toppings do you like?")).toBeTruthy();
fireEvent.sigterm();
});

test("can run actions (add)", async () => {
const { fireEvent } = await renderPlop(["Test", "Cheese"], {
cwd: resolve(__dirname, "./examples/wrap-plop"),
});

const expectedFilePath = resolve(
__dirname,
"./examples/wrap-plop/output/added.txt"
);

await waitFor(() => fs.promises.stat(expectedFilePath));

const data = fs.readFileSync(expectedFilePath, "utf8");

expect(data).toMatch(/Hello/);

fs.unlinkSync(expectedFilePath);

fireEvent.sigterm();
});

0 comments on commit f83dbed

Please sign in to comment.