Skip to content

Commit d7c6930

Browse files
authored
chore: remove redundant copyFile test-util (#1857)
1 parent 3907517 commit d7c6930

File tree

2 files changed

+14
-68
lines changed

2 files changed

+14
-68
lines changed

test/utils/test-utils.js

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const isWebpack5 = version.startsWith('5');
1919
* @param {Boolean} setOutput Boolean that decides if a default output path will be set or not
2020
* @returns {Object} The webpack output or Promise when nodeOptions are present
2121
*/
22-
function run(testCase, args = [], setOutput = true, nodeArgs = [], env) {
22+
const run = (testCase, args = [], setOutput = true, nodeArgs = [], env) => {
2323
const cwd = path.resolve(testCase);
2424

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

3636
return result;
37-
}
37+
};
3838

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

4242
const outputPath = path.resolve(testCase, 'bin');
@@ -70,9 +70,9 @@ function runWatch({ testCase, args = [], setOutput = true, outputKillStr = 'Time
7070
reject(error);
7171
});
7272
});
73-
}
73+
};
7474

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

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

9494
return webpackProc;
95-
}
95+
};
9696
/**
9797
* runPromptWithAnswers
9898
* @param {string} location location of current working directory
@@ -186,14 +186,14 @@ const runPromptWithAnswers = (location, args, answers, waitForOutput = true) =>
186186
* @returns {undefined}
187187
* @throws - throw an Error if file does not exist
188188
*/
189-
function appendDataIfFileExists(testCase, file, data) {
189+
const appendDataIfFileExists = (testCase, file, data) => {
190190
const filePath = path.resolve(testCase, file);
191191
if (fs.existsSync(filePath)) {
192192
fs.appendFileSync(filePath, data);
193193
} else {
194194
throw new Error(`Oops! ${filePath} does not exist!`);
195195
}
196-
}
196+
};
197197

198198
/**
199199
* fs.copyFileSync was added in Added in: v8.5.0
@@ -203,7 +203,7 @@ function appendDataIfFileExists(testCase, file, data) {
203203
* @returns {String} - absolute file path of new file
204204
* @throws - throw an Error if file copy fails
205205
*/
206-
async function copyFileAsync(testCase, file) {
206+
const copyFileAsync = async (testCase, file) => {
207207
const fileToChangePath = path.resolve(testCase, file);
208208
const fileMetaData = path.parse(file);
209209
const fileCopyName = fileMetaData.name.concat('_copy').concat(fileMetaData.ext);
@@ -214,35 +214,13 @@ async function copyFileAsync(testCase, file) {
214214
const data = fs.readFileSync(fileToChangePath);
215215
fs.writeFileSync(copyFilePath, data);
216216
return copyFilePath;
217-
}
218-
219-
/**
220-
* fs.copyFileSync was added in Added in: v8.5.0
221-
* We should refactor the below code once our minimal supported version is v8.5.0
222-
* @param {String} testCase - testCase directory
223-
* @param {String} file - file relative to testCase which is going to be copied
224-
* @returns {String} - absolute file path of new file
225-
* @throws - throw an Error if file copy fails
226-
*/
227-
function copyFile(testCase, file) {
228-
const fileToChangePath = path.resolve(testCase, file);
229-
const fileMetaData = path.parse(file);
230-
const fileCopyName = fileMetaData.name.concat('_copy').concat(fileMetaData.ext);
231-
const copyFilePath = path.resolve(testCase, fileCopyName);
232-
if (fs.existsSync(fileToChangePath)) {
233-
const fileData = fs.readFileSync(fileToChangePath).toString();
234-
fs.writeFileSync(copyFilePath, fileData);
235-
return copyFilePath;
236-
} else {
237-
throw new Error(`Oops! ${fileToChangePath} does not exist!`);
238-
}
239-
}
217+
};
240218

241-
async function runInstall(cwd) {
219+
const runInstall = async (cwd) => {
242220
await execa('yarn', {
243221
cwd,
244222
});
245-
}
223+
};
246224

247225
const runServe = (args, testPath) => {
248226
return runWatch({
@@ -273,7 +251,6 @@ module.exports = {
273251
runAndGetWatchProc,
274252
runPromptWithAnswers,
275253
appendDataIfFileExists,
276-
copyFile,
277254
copyFileAsync,
278255
runInstall,
279256
runInfo,

test/utils/test-utils.test.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

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

77
describe('appendFile', () => {
@@ -32,37 +32,6 @@ describe('appendFile', () => {
3232
});
3333
});
3434

35-
describe('copyFile', () => {
36-
describe('positive test-cases', () => {
37-
const originalFile = 'junkFile.js';
38-
const originalFilePath = resolve(__dirname, originalFile);
39-
const originalFileData = 'initial junk data';
40-
let copyFilePath;
41-
42-
beforeEach(() => {
43-
writeFileSync(originalFilePath, originalFileData);
44-
});
45-
afterEach(() => {
46-
unlinkSync(originalFilePath);
47-
if (existsSync(copyFilePath)) {
48-
unlinkSync(copyFilePath);
49-
}
50-
});
51-
it('should copy file if file exists', () => {
52-
copyFilePath = copyFile(__dirname, originalFile);
53-
const actualData = readFileSync(copyFilePath).toString();
54-
55-
expect(actualData).toBe(originalFileData);
56-
});
57-
});
58-
59-
describe('negative test-cases', () => {
60-
it('should throw error if file does not exist', () => {
61-
expect(() => copyFile(__dirname, 'does-not-exist.js')).toThrowError();
62-
});
63-
});
64-
});
65-
6635
describe('run function', () => {
6736
it('should work correctly by default', () => {
6837
const { command, stdout, stderr } = run(__dirname);

0 commit comments

Comments
 (0)