-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile_helper.js
39 lines (31 loc) · 949 Bytes
/
file_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const Helper = codecept_helper;
const assert = require('assert');
const fs = require('fs');
class FileHelper extends Helper {
async writeFileSync(path, data, failOnError = true) {
try {
return fs.writeFileSync(path, data, { flag: 'w+' });
} catch (e) {
if (!failOnError) assert.ok(false, `Could not write into file: ${path}, because of error: ${e}`);
}
return null;
}
async readFileSync(path, failOnError = true) {
try {
return fs.readFileSync(path, 'utf8');
} catch (e) {
if (!failOnError) assert.ok(false, `Could not read file: ${path}, because of error: ${e}`);
}
return null;
}
async fileSize(path, failOnError = true) {
try {
const stats = fs.statSync(path);
return stats.size;
} catch (e) {
if (!failOnError) assert.ok(false, `Could not get file size: ${path}, because of error: ${e}`);
}
return -1;
}
}
module.exports = FileHelper;