forked from tschaub/mock-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
120 lines (105 loc) · 2.5 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const chai = require('chai');
const constants = require('constants');
const semver = require('semver');
const fs = require('fs');
const hasPromise = !!fs.promises;
/** @type {boolean} */
chai.config.includeStack = true;
/**
* Chai's assert function configured to include stacks on failure.
* @type {function}
*/
exports.assert = chai.assert;
function run(func) {
func();
}
function noRun() {}
const TEST = {
it: it,
xit: xit,
describe: describe,
xdescribe: xdescribe,
run: run
};
const NO_TEST = {
it: xit,
xit: xit,
describe: xdescribe,
xdescribe: xdescribe,
run: noRun
};
exports.inVersion = function(range) {
if (semver.satisfies(process.version, range)) {
return TEST;
} else {
return NO_TEST;
}
};
exports.withPromise = hasPromise ? TEST : NO_TEST;
/**
* Convert a string to flags for fs.open.
* @param {string} str String.
* @return {number} Flags.
*/
exports.flags = function(str) {
switch (str) {
case 'r':
return constants.O_RDONLY;
case 'rs':
return constants.O_RDONLY | constants.O_SYNC;
case 'r+':
return constants.O_RDWR;
case 'rs+':
return constants.O_RDWR | constants.O_SYNC;
case 'w':
return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY;
case 'wx': // fall through
case 'xw':
return (
constants.O_TRUNC |
constants.O_CREAT |
constants.O_WRONLY |
constants.O_EXCL
);
case 'w+':
return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR;
case 'wx+': // fall through
case 'xw+':
return (
constants.O_TRUNC |
constants.O_CREAT |
constants.O_RDWR |
constants.O_EXCL
);
case 'a':
return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY;
case 'ax': // fall through
case 'xa':
return (
constants.O_APPEND |
constants.O_CREAT |
constants.O_WRONLY |
constants.O_EXCL
);
case 'a+':
return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR;
case 'ax+': // fall through
case 'xa+':
return (
constants.O_APPEND |
constants.O_CREAT |
constants.O_RDWR |
constants.O_EXCL
);
default:
throw new Error('Unsupported flag: ' + str);
}
};
exports.assertEqualPaths = function(actual, expected) {
if (process.platform === 'win32') {
chai.assert.equal(actual.toLowerCase(), expected.toLowerCase());
} else {
chai.assert(actual, expected);
}
};