Skip to content

Commit

Permalink
tests: add copyFile function in test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hemal7735 authored and evenstensberg committed Feb 5, 2019
1 parent c00386b commit 1b21e81
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
44 changes: 38 additions & 6 deletions test/test-utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { extractHash, appendDataIfFileExists } = require("./testUtils");
const { writeFileSync, unlinkSync, readFileSync } = require("fs");
const { extractHash, appendDataIfFileExists, copyFile } = require("./testUtils");
const { writeFileSync, unlinkSync, readFileSync, existsSync } = require("fs");
const { resolve } = require("path");

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

describe("appendFile functionality", () => {
describe("positive test-cases", () => {
const junkFile = resolve(__dirname, "junkFile.js");
const junkFile = "junkFile.js";
const junkFilePath = resolve(__dirname, junkFile);
const initialJunkData = "initial junk data";
const junkComment = "//junk comment";

beforeEach(() => {
writeFileSync(junkFile, initialJunkData);
writeFileSync(junkFilePath, initialJunkData);
});
afterEach(() => {
unlinkSync(junkFile);
unlinkSync(junkFilePath);
});
it("should append data to file if file exists", () => {
appendDataIfFileExists(__dirname, junkFile, junkComment);
const actualData = readFileSync(junkFile).toString();
const actualData = readFileSync(junkFilePath).toString();

expect(actualData).toBe(initialJunkData + junkComment);
});
Expand All @@ -130,3 +131,34 @@ describe("appendFile functionality", () => {
});
});
});

describe("copyFile functionality", () => {
describe("positive test-cases", () => {
const originalFile = "junkFile.js";
const originalFilePath = resolve(__dirname, originalFile);
const originalFileData = "initial junk data";
var 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();
});
});
});
30 changes: 25 additions & 5 deletions test/testUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const path = require("path");
const fs = require("fs");
const execa = require("execa");
const { sync: spawnSync } = execa;
const { Writable } = require("readable-stream");
Expand Down Expand Up @@ -171,14 +172,33 @@ function extractHash(stdout) {
* @throws - throw an Error if file does not exist
*/
function appendDataIfFileExists(testCase, file, data) {
const { existsSync, appendFileSync } = require("fs");

const filePath = path.resolve(testCase, file);
if (existsSync(filePath)) {
appendFileSync(filePath, data);
if (fs.existsSync(filePath)) {
fs.appendFileSync(filePath, data);
} else {
throw new Error(`Oops! ${filePath} does not exist!`);
}
}

module.exports = { run, runWatch, runAndGetWatchProc, extractHash, extractSummary, appendDataIfFileExists };
/**
* 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 copyFilePath = path.resolve(testCase, "index_copy.js");

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!`);
}
}

module.exports = { run, runWatch, runAndGetWatchProc, extractHash, extractSummary, appendDataIfFileExists, copyFile };

0 comments on commit 1b21e81

Please sign in to comment.