Skip to content

Commit

Permalink
chore: remove redundant copyFile test-util (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Oct 3, 2020
1 parent 3907517 commit d7c6930
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 68 deletions.
47 changes: 12 additions & 35 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const isWebpack5 = version.startsWith('5');
* @param {Boolean} setOutput Boolean that decides if a default output path will be set or not
* @returns {Object} The webpack output or Promise when nodeOptions are present
*/
function run(testCase, args = [], setOutput = true, nodeArgs = [], env) {
const run = (testCase, args = [], setOutput = true, nodeArgs = [], env) => {
const cwd = path.resolve(testCase);

const outputPath = path.resolve(testCase, 'bin');
Expand All @@ -34,9 +34,9 @@ function run(testCase, args = [], setOutput = true, nodeArgs = [], env) {
});

return result;
}
};

function runWatch({ testCase, args = [], setOutput = true, outputKillStr = 'Time' }) {
const runWatch = ({ testCase, args = [], setOutput = true, outputKillStr = 'Time' }) => {
const cwd = path.resolve(testCase);

const outputPath = path.resolve(testCase, 'bin');
Expand Down Expand Up @@ -70,9 +70,9 @@ function runWatch({ testCase, args = [], setOutput = true, outputKillStr = 'Time
reject(error);
});
});
}
};

function runAndGetWatchProc(testCase, args = [], setOutput = true, input = '', forcePipe = false) {
const runAndGetWatchProc = (testCase, args = [], setOutput = true, input = '', forcePipe = false) => {
const cwd = path.resolve(testCase);

const outputPath = path.resolve(testCase, 'bin');
Expand All @@ -92,7 +92,7 @@ function runAndGetWatchProc(testCase, args = [], setOutput = true, input = '', f
const webpackProc = execa(WEBPACK_PATH, argsWithOutput, options);

return webpackProc;
}
};
/**
* runPromptWithAnswers
* @param {string} location location of current working directory
Expand Down Expand Up @@ -186,14 +186,14 @@ const runPromptWithAnswers = (location, args, answers, waitForOutput = true) =>
* @returns {undefined}
* @throws - throw an Error if file does not exist
*/
function appendDataIfFileExists(testCase, file, data) {
const appendDataIfFileExists = (testCase, file, data) => {
const filePath = path.resolve(testCase, file);
if (fs.existsSync(filePath)) {
fs.appendFileSync(filePath, data);
} else {
throw new Error(`Oops! ${filePath} does not exist!`);
}
}
};

/**
* fs.copyFileSync was added in Added in: v8.5.0
Expand All @@ -203,7 +203,7 @@ function appendDataIfFileExists(testCase, file, data) {
* @returns {String} - absolute file path of new file
* @throws - throw an Error if file copy fails
*/
async function copyFileAsync(testCase, file) {
const copyFileAsync = async (testCase, file) => {
const fileToChangePath = path.resolve(testCase, file);
const fileMetaData = path.parse(file);
const fileCopyName = fileMetaData.name.concat('_copy').concat(fileMetaData.ext);
Expand All @@ -214,35 +214,13 @@ async function copyFileAsync(testCase, file) {
const data = fs.readFileSync(fileToChangePath);
fs.writeFileSync(copyFilePath, data);
return copyFilePath;
}

/**
* fs.copyFileSync was added in Added in: v8.5.0
* We should refactor the below code once our minimal supported version is v8.5.0
* @param {String} testCase - testCase directory
* @param {String} file - file relative to testCase which is going to be copied
* @returns {String} - absolute file path of new file
* @throws - throw an Error if file copy fails
*/
function copyFile(testCase, file) {
const fileToChangePath = path.resolve(testCase, file);
const fileMetaData = path.parse(file);
const fileCopyName = fileMetaData.name.concat('_copy').concat(fileMetaData.ext);
const copyFilePath = path.resolve(testCase, fileCopyName);
if (fs.existsSync(fileToChangePath)) {
const fileData = fs.readFileSync(fileToChangePath).toString();
fs.writeFileSync(copyFilePath, fileData);
return copyFilePath;
} else {
throw new Error(`Oops! ${fileToChangePath} does not exist!`);
}
}
};

async function runInstall(cwd) {
const runInstall = async (cwd) => {
await execa('yarn', {
cwd,
});
}
};

const runServe = (args, testPath) => {
return runWatch({
Expand Down Expand Up @@ -273,7 +251,6 @@ module.exports = {
runAndGetWatchProc,
runPromptWithAnswers,
appendDataIfFileExists,
copyFile,
copyFileAsync,
runInstall,
runInfo,
Expand Down
35 changes: 2 additions & 33 deletions test/utils/test-utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { appendDataIfFileExists, copyFile, run, runAndGetWatchProc } = require('./test-utils');
const { writeFileSync, unlinkSync, readFileSync, existsSync } = require('fs');
const { appendDataIfFileExists, run, runAndGetWatchProc } = require('./test-utils');
const { writeFileSync, unlinkSync, readFileSync } = require('fs');
const { resolve } = require('path');

describe('appendFile', () => {
Expand Down Expand Up @@ -32,37 +32,6 @@ describe('appendFile', () => {
});
});

describe('copyFile', () => {
describe('positive test-cases', () => {
const originalFile = 'junkFile.js';
const originalFilePath = resolve(__dirname, originalFile);
const originalFileData = 'initial junk data';
let copyFilePath;

beforeEach(() => {
writeFileSync(originalFilePath, originalFileData);
});
afterEach(() => {
unlinkSync(originalFilePath);
if (existsSync(copyFilePath)) {
unlinkSync(copyFilePath);
}
});
it('should copy file if file exists', () => {
copyFilePath = copyFile(__dirname, originalFile);
const actualData = readFileSync(copyFilePath).toString();

expect(actualData).toBe(originalFileData);
});
});

describe('negative test-cases', () => {
it('should throw error if file does not exist', () => {
expect(() => copyFile(__dirname, 'does-not-exist.js')).toThrowError();
});
});
});

describe('run function', () => {
it('should work correctly by default', () => {
const { command, stdout, stderr } = run(__dirname);
Expand Down

0 comments on commit d7c6930

Please sign in to comment.