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" ) ;
55const { resolve } = require ( "path" ) ;
66
77describe ( "extractHash functionality" , ( ) => {
@@ -106,19 +106,20 @@ Child ${config2Name}:
106106
107107describe ( "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+ } ) ;
0 commit comments