Skip to content

Commit d061ff0

Browse files
committed
mock out get-write-flag on nodes where filemap is known
Can't overwrite or modify a constant, they're very... constant.
1 parent c517cc3 commit d061ff0

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/get-write-flag.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
// Only supported in Node v12.9.0 and above.
99
const platform = process.env.__FAKE_PLATFORM__ || process.platform
1010
const isWindows = platform === 'win32'
11-
const fs = require('fs')
11+
const fs = global.__FAKE_TESTING_FS__ || require('fs')
12+
13+
/* istanbul ignore next */
1214
const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP = 0 } = fs.constants
15+
1316
const fMapEnabled = isWindows && !!UV_FS_O_FILEMAP
1417
const fMapLimit = 512 * 1024
1518
const fMapFlag = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY

test/get-write-flag.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ const t = require('tap')
88
const fs = require('fs')
99
const hasFmap = !!fs.constants.UV_FS_O_FILEMAP
1010
const platform = process.platform
11+
const UV_FS_O_FILEMAP = 0x20000000
1112

1213
switch (process.argv[2]) {
1314
case 'win32-fmap': {
14-
if (!hasFmap)
15-
fs.constants.UV_FS_O_FILEMAP = 0x20000000
16-
const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP } = fs.constants
15+
if (!hasFmap) {
16+
global.__FAKE_TESTING_FS__ = {
17+
constants: {
18+
...fs.constants,
19+
...{ UV_FS_O_FILEMAP },
20+
}
21+
}
22+
}
23+
const { O_CREAT, O_TRUNC, O_WRONLY } = fs.constants
1724
if (platform !== 'win32')
1825
process.env.__FAKE_PLATFORM__ = 'win32'
1926
const getFlag = require('../lib/get-write-flag.js')
@@ -23,8 +30,14 @@ switch (process.argv[2]) {
2330
}
2431

2532
case 'win32-nofmap': {
26-
if (hasFmap)
27-
fs.constants.UV_FS_O_FILEMAP = 0
33+
if (hasFmap) {
34+
global.__FAKE_TESTING_FS__ = {
35+
constants: {
36+
...fs.constants,
37+
...{ UV_FS_O_FILEMAP: 0 },
38+
}
39+
}
40+
}
2841
if (platform !== 'win32')
2942
process.env.__FAKE_PLATFORM__ = 'win32'
3043
const getFlag = require('../lib/get-write-flag.js')

0 commit comments

Comments
 (0)