Skip to content

Commit

Permalink
refactor(tests): remove some eslint exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
atao60 committed Jan 18, 2021
1 parent 7f812d1 commit f9ac158
Showing 1 changed file with 27 additions and 66 deletions.
93 changes: 27 additions & 66 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
/*eslint @typescript-eslint/restrict-template-expressions: ["off"]*/

import { expect } from 'chai';
import { closeSync, existsSync, mkdtempSync, mkdirSync, openSync, readFileSync, rmdirSync, statSync, unlinkSync, writeSync } from 'graceful-fs';
import { describe, it } from 'mocha';
import { join } from 'path';
import { env } from 'process';
import { format as printf } from 'util';
import supportsColor from 'supports-color';

import { execute as run } from './cmd';

const LIB_DIR = join(__dirname, env.APP_CODE_PATH || '../../dist');
// os.tmpdir() is not used here, to stay inside project folder
const TMP_DIR = join(__dirname, env.PROJECT_TARGET_PATH || '../.tmp-dir');

// See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
/*eslint max-len: ["off"]*/
const semverPattern = /(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/.source;

function createTempDir(format: string, path = TMP_DIR, suffix = 'fse-cli-test-') {
try {
mkdirSync(path, { recursive: true });
const newDirPath = mkdtempSync(join(path, suffix));
return newDirPath;
} catch (e) {
throw new Error(printf(format, path, suffix, e));
}
}

describe("The fs-extra CLI", () => {

describe("Calling CLI", () => {
Expand Down Expand Up @@ -76,15 +86,8 @@ describe("The fs-extra CLI", () => {
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
let newDirPath: string = null;
try {
mkdirSync(TMP_DIR, { recursive: true });
newDirPath = mkdtempSync(join(TMP_DIR, 'fse-cli-test-'));
} catch (e) {
throw new Error(`Test 'Remove a directory', unable to create a temporary directory '${newDirPath}': ${e}`);
}

const dirToBeRemoved = newDirPath;
const dirToBeRemoved = createTempDir("Test 'Remove a directory', unable to create a temporary directory based on '%s' & '%s': %s");
const script = `${LIB_DIR}`; // ie index.js
const args = { app: ['remove', dirToBeRemoved] };
const userInputs = [];
Expand Down Expand Up @@ -117,16 +120,8 @@ describe("The fs-extra CLI", () => {
describe("Calling 'mkdir'", () => {
// first check if the jog is done
it("When non existing directory, it will be created", function (done) { // don't pass 'done' as argument! with async/await
let newDirPath: string = null;
try {
mkdirSync(TMP_DIR, { recursive: true });
newDirPath = mkdtempSync(join(TMP_DIR, 'fse-cli-test-'));
} catch (e) {
throw new Error("Test 'Create a directory', " +
`before starting unable to create a temporary parent directory '${newDirPath}': ${e}`);
}

const baseDir = newDirPath;
const baseDir = createTempDir("Test 'Create a directory', unable to create a temporary parent directory based on '%s' & '%s': %s");
const dirToBeCreated = join(baseDir, 'theNewDir');
const script = `${LIB_DIR}`; // ie index.js
const args = { app: ['mkdirp', dirToBeCreated] };
Expand Down Expand Up @@ -161,16 +156,9 @@ describe("The fs-extra CLI", () => {
describe("Calling 'touch'", () => {
// first check if the jog is done
it("When non existing file, it will be created", function (done) { // don't pass 'done' as argument! with async/await
let newDirPath: string = null;
try {
mkdirSync(TMP_DIR, { recursive: true });
newDirPath = mkdtempSync(join(TMP_DIR, 'fse-cli-test-'));
} catch (e) {
throw new Error("Test 'Create a directory', " +
`before starting unable to create a temporary parent directory '${newDirPath}': ${e}`);
}

const baseDir = newDirPath;

const baseDir = createTempDir("Test 'Create a file', unable to create a temporary parent directory based on '%s' & '%s': %s");
const fileToBeCreated = join(baseDir, 'theNewFile');
const script = `${LIB_DIR}`; // ie index.js
const args = { app: ['ensureFile', fileToBeCreated] };
Expand Down Expand Up @@ -207,38 +195,25 @@ describe("The fs-extra CLI", () => {
describe("Calling 'copy'", () => {
// first check if the jog is done
it("When destination file doesn't exist, a new file is created with the same content", function (done) { // don't pass 'done' as argument! with async/await
const destDirName = "sub-dir";

const destDirName = "sub-dir-";
const srcFileName = "theFileToBeCopied";
const srcFileContent = 'theFileToBeCopied-content';
const destFileName = "theFileCopy";
let newDirPath: string = null;
try {
mkdirSync(TMP_DIR, { recursive: true });
newDirPath = mkdtempSync(join(TMP_DIR, 'fse-cli-test-'));
} catch (e) {
throw new Error("Test 'Copy a file', " +
`before starting unable to create a temporary source directory '${newDirPath}': ${e}`);
}

const baseDir = newDirPath;
const destDirPath = join(baseDir, destDirName);
const baseDir = createTempDir("Test 'Copy a file', unable to create a temporary source directory based on '%s' & '%s': %s");
const destDirPath = createTempDir("Test 'Copy a file', unable to create a temporary destination directory based on '%s' & '%s': %s", baseDir, destDirName);

const fileToBeCopied = join(baseDir, srcFileName);
// dixit fs-extra doc, "if src is a file, dest cannot be a directory"
const fileCopy = join(destDirPath, destFileName);

try {
mkdirSync(join(newDirPath, destDirName));
} catch (e) {
throw new Error("Test 'Copy a file', " +
`before starting unable to create a temporary destination directory '${destDirPath}': ${e}`);
}
try {// srcFileContent
const fileToBeCopiedDescriptor = openSync(fileToBeCopied, 'w');
writeSync(fileToBeCopiedDescriptor, srcFileContent, undefined, 'utf8');
closeSync(fileToBeCopiedDescriptor);
} catch (e) {
throw new Error("Test 'Copy a file', " +
`before starting unable to create a temporary file '${fileToBeCopied}': ${e}`);
throw new Error(printf("Test 'Copy a file', unable to create a temporary file '%s': %s", fileToBeCopied, e));
}

const script = `${LIB_DIR}`; // ie index.js
Expand Down Expand Up @@ -281,36 +256,22 @@ describe("The fs-extra CLI", () => {
describe("Calling 'move'", () => {
// first check if the jog is done
it("Then source has been removed, destinaton exists and content is untouched", function (done) { // don't pass 'done' as argument! with async/await
const destDirName = "sub-dir";

const destDirName = "sub-dir-";
const srcFileName = "theFileToBeMoved";
const srcFileContent = 'theFileToBeMoved-content';
let newDirPath: string = null;
try {
mkdirSync(TMP_DIR, { recursive: true });
newDirPath = mkdtempSync(join(TMP_DIR, 'fse-cli-test-'));
} catch (e) {
throw new Error("Test 'Move a file', " +
`before starting unable to create a temporary source directory '${newDirPath}': ${e}`);
}

const baseDir = newDirPath;
const destDirPath = join(baseDir, destDirName);
const baseDir = createTempDir("Test 'Move a file', unable to create a temporary source directory based on '%s' & '%s': %s");
const destDirPath = createTempDir("Test 'Move a file', unable to create a temporary destination directory based on '%s' & '%s': %s", baseDir, destDirName);
const fileToBeMovedPath = join(baseDir, srcFileName);
const movedFilePath = join(destDirPath, srcFileName);

try {
mkdirSync(join(newDirPath, destDirName));
} catch (e) {
throw new Error("Test 'Move a file', " +
`before starting unable to create a temporary destination directory '${destDirPath}': ${e}`);
}
try {
const fileToBeMovedDescriptor = openSync(fileToBeMovedPath, 'w');
writeSync(fileToBeMovedDescriptor, srcFileContent, undefined, 'utf8');
closeSync(fileToBeMovedDescriptor);
} catch (e) {
throw new Error("Test 'Move a file', " +
`before starting unable to create a temporary file '${fileToBeMovedPath}': ${e}`);
throw new Error(printf("Test 'Move a file', unable to create a temporary file '%s': %s", fileToBeMovedPath, e));
}

const script = `${LIB_DIR}`; // ie index.js
Expand Down

0 comments on commit f9ac158

Please sign in to comment.