1
1
"use strict" ;
2
2
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" ) ;
5
5
const { resolve } = require ( "path" ) ;
6
6
7
7
describe ( "extractHash functionality" , ( ) => {
@@ -106,19 +106,20 @@ Child ${config2Name}:
106
106
107
107
describe ( "appendFile functionality" , ( ) => {
108
108
describe ( "positive test-cases" , ( ) => {
109
- const junkFile = resolve ( __dirname , "junkFile.js" ) ;
109
+ const junkFile = "junkFile.js" ;
110
+ const junkFilePath = resolve ( __dirname , junkFile ) ;
110
111
const initialJunkData = "initial junk data" ;
111
112
const junkComment = "//junk comment" ;
112
113
113
114
beforeEach ( ( ) => {
114
- writeFileSync ( junkFile , initialJunkData ) ;
115
+ writeFileSync ( junkFilePath , initialJunkData ) ;
115
116
} ) ;
116
117
afterEach ( ( ) => {
117
- unlinkSync ( junkFile ) ;
118
+ unlinkSync ( junkFilePath ) ;
118
119
} ) ;
119
120
it ( "should append data to file if file exists" , ( ) => {
120
121
appendDataIfFileExists ( __dirname , junkFile , junkComment ) ;
121
- const actualData = readFileSync ( junkFile ) . toString ( ) ;
122
+ const actualData = readFileSync ( junkFilePath ) . toString ( ) ;
122
123
123
124
expect ( actualData ) . toBe ( initialJunkData + junkComment ) ;
124
125
} ) ;
@@ -130,3 +131,34 @@ describe("appendFile functionality", () => {
130
131
} ) ;
131
132
} ) ;
132
133
} ) ;
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