Skip to content

Commit

Permalink
Made cleanup for files more robust FS management
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 23, 2021
1 parent f83dbed commit 37dadbf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -29,7 +29,7 @@
"prepare": "husky install"
},
"devDependencies": {
"cli-testing-library": "1.0.0-alpha.5",
"cli-testing-library": "1.0.0-alpha.6",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down
25 changes: 10 additions & 15 deletions tests/actions.spec.js
Expand Up @@ -2,8 +2,13 @@ const { renderPlop } = require("./render");
const { resolve } = require("path");
const { waitFor } = require("cli-testing-library");
const fs = require("fs");
const { getFilePath } = require("./file-helper");

test("Plop to add and rename files", async () => {
const expectedFilePath = await getFilePath(
"./examples/add-action/output/new-output.txt"
);

const { findByText, fireEvent } = await renderPlop(["addAndNameFile"], {
cwd: resolve(__dirname, "./examples/add-action"),
});
Expand All @@ -13,23 +18,20 @@ test("Plop to add and rename files", async () => {
fireEvent.type("new-output");
fireEvent.enter();

const expectedFilePath = resolve(
__dirname,
"./examples/add-action/output/new-output.txt"
);

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

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

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

fs.unlinkSync(expectedFilePath);

fireEvent.sigterm();
});

test("Plop to add and change file contents", async () => {
const expectedFilePath = await getFilePath(
"./examples/add-action/output/new-output.txt"
);

const { findByText, fireEvent } = await renderPlop(["addAndChangeFile"], {
cwd: resolve(__dirname, "./examples/add-action"),
});
Expand All @@ -39,19 +41,12 @@ test("Plop to add and change file contents", async () => {
fireEvent.type("Corbin");
fireEvent.enter();

const expectedFilePath = resolve(
__dirname,
"./examples/add-action/output/to-add-change.txt"
);

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

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

expect(data).toMatch(/Hi Corbin!/);

fs.unlinkSync(expectedFilePath);

fireEvent.sigterm();
});

Expand Down
2 changes: 1 addition & 1 deletion tests/examples/add-action/plopfile.js
Expand Up @@ -29,7 +29,7 @@ module.exports = function (plop) {
actions: [
{
type: "add",
path: "./output/to-add-change.txt",
path: "./output/new-output.txt",
templateFile: "./templates/to-add-change.txt",
},
],
Expand Down
27 changes: 27 additions & 0 deletions tests/file-helper.js
@@ -0,0 +1,27 @@
const fs = require("fs");
const { resolve } = require("path");

let cleanupFile = null;

afterEach(() => {
if (!cleanupFile) return;
try {
fs.unlinkSync(cleanupFile);
} catch (e) {}
cleanupFile = null;
});

const getFilePath = async (path) => {
const expectedFilePath = resolve(__dirname, path);

cleanupFile = expectedFilePath;
try {
await fs.promises.unlink(cleanupFile);
} catch (e) {}

return expectedFilePath;
};

module.exports = {
getFilePath,
};
12 changes: 5 additions & 7 deletions tests/wrapper.spec.js
Expand Up @@ -2,6 +2,7 @@ const { renderScript, renderPlop } = require("./render");
const { resolve } = require("path");
const { waitFor } = require("cli-testing-library");
const fs = require("fs");
const { getFilePath } = require("./file-helper");

const renderWrapper = (...props) => {
return renderScript(
Expand Down Expand Up @@ -52,22 +53,19 @@ test("wrapper should bypass prompts with name", async () => {
});

test("can run actions (add)", async () => {
const expectedFilePath = await getFilePath(
"./examples/wrap-plop/output/added.txt"
);

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 37dadbf

Please sign in to comment.