From eaadb82aa28d3a820fc0fc2d91d5c800ab9c5202 Mon Sep 17 00:00:00 2001
From: atao
Date: Sun, 24 Jan 2021 12:28:14 +0100
Subject: [PATCH] test(emptydir): add minimal testing of task emptyDir
test for #8
---
test/cli.test.ts | 74 ++++++++++++++++++++++++++++++++++++------------
1 file changed, 56 insertions(+), 18 deletions(-)
diff --git a/test/cli.test.ts b/test/cli.test.ts
index 749245d..fe25808 100644
--- a/test/cli.test.ts
+++ b/test/cli.test.ts
@@ -26,6 +26,8 @@ function createTempDir(format: string, path = TMP_DIR, suffix = 'fse-cli-test-')
}
}
+const DEBUG = env.CLI_TEST_DEBUG && env.CLI_TEST_DEBUG === 'true';
+
describe("The fs-extra CLI", () => {
describe("Calling CLI", () => {
@@ -35,9 +37,9 @@ describe("The fs-extra CLI", () => {
const args = {};
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
const foundRegex = new RegExp('^ERROR:\\s+No\\s+task\\s+specified', 'm');
run(
@@ -62,9 +64,9 @@ describe("The fs-extra CLI", () => {
const args = { app: [unknownTask] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
const foundRegex = new RegExp(`^ERROR:\\s+Unknown\\s+task\\s+'${unknownTask}'`, 'm');
run(
@@ -83,6 +85,42 @@ describe("The fs-extra CLI", () => {
});
});
+ describe("Calling 'emptyDir'", () => {
+ it("When existing directory, it will be purged", function (done) { // don't pass 'done' as argument! with async/await
+ const dirToBePurged = createTempDir("Test 'Purge a directory', unable to create a temporary directory based on '%s' & '%s': %s");
+ const fileToBeRemoved = join(dirToBePurged, 'toBeRemoved.txt');
+ closeSync(openSync(fileToBeRemoved, 'w'));
+ const script = `${LIB_DIR}`; // ie index.js
+ const args = { app: ['emptyDir', dirToBePurged] };
+ const userInputs = [];
+ const options = {
+ // timeout: 200, // 100 ms by default
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
+ };
+ run(
+ script,
+ args,
+ userInputs,
+ options
+ )
+ .then(() => {
+ expect(existsSync(dirToBePurged), "directory should still exists").to.be.true;
+ expect(!existsSync(fileToBeRemoved), "file should have been removed").to.be.true;
+ done();
+ })
+ .catch(error => {
+ done(error);
+ })
+ .finally(() => {
+ try {
+ rmdirSync(dirToBePurged, { recursive: true });
+ } catch (e) { /* do nothing */ }
+ });
+
+ });
+ });
+
describe("Calling 'remove'", () => {
// first check if the jog is done
it("When existing directory, it will be removed", function (done) { // don't pass 'done' as argument! with async/await
@@ -92,9 +130,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['remove', dirToBeRemoved] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
run(
script,
@@ -127,9 +165,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['mkdirp', dirToBeCreated] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
run(
script,
@@ -164,9 +202,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['ensureFile', fileToBeCreated] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0 , // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
run(
script,
@@ -220,9 +258,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['copy', fileToBeCopied, fileCopy] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
run(
script,
@@ -278,9 +316,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['move', fileToBeMovedPath, movedFilePath] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
run(
script,
@@ -320,9 +358,9 @@ describe("The fs-extra CLI", () => {
const args = { app: ['version'] };
const userInputs = [];
const options = {
- // env: { DEBUG: true }, // false by default
// timeout: 200, // 100 ms by default
- // maxTimeout: 0 // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ env: { DEBUG } // false by default
};
const foundRegex = new RegExp(`^@atao60/fse-cli\\s+${semverPattern}\\s+\\(fs-extra\\s+${semverPattern}\\)$`);
run(
@@ -349,9 +387,9 @@ describe("The fs-extra CLI", () => {
const userInputs = [];
const options = {
// timeout: 200, // 100 ms by default
- // maxTimeout: 0, // 10 s by default; if "0" then no timeout
+ // maxTimeout: 0, // 10 s by default; if "0" then no timeout
env: {
- // DEBUG: true, // false by default
+ DEBUG, // false by default
FORCE_COLOR: supportsColor.stdout.level //
}
};