Skip to content

Commit 1b21e81

Browse files
hemal7735evenstensberg
authored andcommitted
tests: add copyFile function in test-utils
1 parent c00386b commit 1b21e81

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

test/test-utils.test.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
const { extractHash, appendDataIfFileExists } = require("./testUtils");
4-
const { writeFileSync, unlinkSync, readFileSync } = require("fs");
3+
const { extractHash, appendDataIfFileExists, copyFile } = require("./testUtils");
4+
const { writeFileSync, unlinkSync, readFileSync, existsSync } = require("fs");
55
const { resolve } = require("path");
66

77
describe("extractHash functionality", () => {
@@ -106,19 +106,20 @@ Child ${config2Name}:
106106

107107
describe("appendFile functionality", () => {
108108
describe("positive test-cases", () => {
109-
const junkFile = resolve(__dirname, "junkFile.js");
109+
const junkFile = "junkFile.js";
110+
const junkFilePath = resolve(__dirname, junkFile);
110111
const initialJunkData = "initial junk data";
111112
const junkComment = "//junk comment";
112113

113114
beforeEach(() => {
114-
writeFileSync(junkFile, initialJunkData);
115+
writeFileSync(junkFilePath, initialJunkData);
115116
});
116117
afterEach(() => {
117-
unlinkSync(junkFile);
118+
unlinkSync(junkFilePath);
118119
});
119120
it("should append data to file if file exists", () => {
120121
appendDataIfFileExists(__dirname, junkFile, junkComment);
121-
const actualData = readFileSync(junkFile).toString();
122+
const actualData = readFileSync(junkFilePath).toString();
122123

123124
expect(actualData).toBe(initialJunkData + junkComment);
124125
});
@@ -130,3 +131,34 @@ describe("appendFile functionality", () => {
130131
});
131132
});
132133
});
134+
135+
describe("copyFile functionality", () => {
136+
describe("positive test-cases", () => {
137+
const originalFile = "junkFile.js";
138+
const originalFilePath = resolve(__dirname, originalFile);
139+
const originalFileData = "initial junk data";
140+
var copyFilePath;
141+
142+
beforeEach(() => {
143+
writeFileSync(originalFilePath, originalFileData);
144+
});
145+
afterEach(() => {
146+
unlinkSync(originalFilePath);
147+
if (existsSync(copyFilePath)) {
148+
unlinkSync(copyFilePath);
149+
}
150+
});
151+
it("should copy file if file exists", () => {
152+
copyFilePath = copyFile(__dirname, originalFile);
153+
const actualData = readFileSync(copyFilePath).toString();
154+
155+
expect(actualData).toBe(originalFileData);
156+
});
157+
});
158+
159+
describe("negative test-cases", () => {
160+
it("should throw error if file does not exist", () => {
161+
expect(() => copyFile(__dirname, "does-not-exist.js")).toThrowError();
162+
});
163+
});
164+
});

test/testUtils.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const path = require("path");
4+
const fs = require("fs");
45
const execa = require("execa");
56
const { sync: spawnSync } = execa;
67
const { Writable } = require("readable-stream");
@@ -171,14 +172,33 @@ function extractHash(stdout) {
171172
* @throws - throw an Error if file does not exist
172173
*/
173174
function appendDataIfFileExists(testCase, file, data) {
174-
const { existsSync, appendFileSync } = require("fs");
175-
176175
const filePath = path.resolve(testCase, file);
177-
if (existsSync(filePath)) {
178-
appendFileSync(filePath, data);
176+
if (fs.existsSync(filePath)) {
177+
fs.appendFileSync(filePath, data);
179178
} else {
180179
throw new Error(`Oops! ${filePath} does not exist!`);
181180
}
182181
}
183182

184-
module.exports = { run, runWatch, runAndGetWatchProc, extractHash, extractSummary, appendDataIfFileExists };
183+
/**
184+
* fs.copyFileSync was added in Added in: v8.5.0
185+
* We should refactor the below code once our minimal supported version is v8.5.0
186+
* @param {String} testCase - testCase directory
187+
* @param {String} file - file relative to testCase which is going to be copied
188+
* @returns {String} - absolute file path of new file
189+
* @throws - throw an Error if file copy fails
190+
*/
191+
function copyFile(testCase, file) {
192+
const fileToChangePath = path.resolve(testCase, file);
193+
const copyFilePath = path.resolve(testCase, "index_copy.js");
194+
195+
if (fs.existsSync(fileToChangePath)) {
196+
const fileData = fs.readFileSync(fileToChangePath).toString();
197+
fs.writeFileSync(copyFilePath, fileData);
198+
return copyFilePath;
199+
} else {
200+
throw new Error(`Oops! ${fileToChangePath} does not exist!`);
201+
}
202+
}
203+
204+
module.exports = { run, runWatch, runAndGetWatchProc, extractHash, extractSummary, appendDataIfFileExists, copyFile };

0 commit comments

Comments
 (0)