Skip to content

Commit 77383dd

Browse files
richardlaubnoordhuis
authored andcommitted
Replace fs.accessSync call to fs.statSync
fs.accessSync does not exist in Node 0.10.x. PR-URL: #955 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 0dba4bd commit 77383dd

File tree

3 files changed

+123
-12
lines changed

3 files changed

+123
-12
lines changed

lib/configure.js

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = exports = configure
2-
module.exports.test = { findPython: findPython }
2+
module.exports.test = { findAccessibleSync: findAccessibleSync,
3+
findPython: findPython }
34

45
/**
56
* Module dependencies.
@@ -20,6 +21,7 @@ var fs = require('graceful-fs')
2021
, execFile = cp.execFile
2122
, win = process.platform == 'win32'
2223
, findNodeDirectory = require('./find-node-directory')
24+
, msgFormat = require('util').format
2325

2426
exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
2527

@@ -226,22 +228,21 @@ function configure (gyp, argv, callback) {
226228
// - the out/Release directory
227229
// - the out/Debug directory
228230
// - the root directory
229-
var node_exp_file = ''
231+
var node_exp_file = undefined
230232
if (process.platform === 'aix') {
231233
var node_root_dir = findNodeDirectory()
232234
var candidates = ['include/node/node.exp',
233235
'out/Release/node.exp',
234236
'out/Debug/node.exp',
235237
'node.exp']
236-
for (var next = 0; next < candidates.length; next++) {
237-
node_exp_file = path.resolve(node_root_dir, candidates[next])
238-
try {
239-
fs.accessSync(node_exp_file, fs.R_OK)
240-
// exp file found, stop looking
241-
break
242-
} catch (exception) {
243-
// this candidate was not found or not readable, do nothing
244-
}
238+
var logprefix = 'find exports file'
239+
node_exp_file = findAccessibleSync(logprefix, node_root_dir, candidates)
240+
if (node_exp_file !== undefined) {
241+
log.verbose(logprefix, 'Found exports file: %s', node_exp_file)
242+
} else {
243+
var msg = msgFormat('Could not find node.exp file in %s', node_root_dir)
244+
log.error(logprefix, 'Could not find exports file')
245+
return callback(new Error(msg))
245246
}
246247
}
247248

@@ -310,6 +311,29 @@ function configure (gyp, argv, callback) {
310311

311312
}
312313

314+
/**
315+
* Returns the first file or directory from an array of candidates that is
316+
* readable by the current user, or undefined if none of the candidates are
317+
* readable.
318+
*/
319+
function findAccessibleSync (logprefix, dir, candidates) {
320+
for (var next = 0; next < candidates.length; next++) {
321+
var candidate = path.resolve(dir, candidates[next])
322+
try {
323+
var fd = fs.openSync(candidate, 'r')
324+
} catch (e) {
325+
// this candidate was not found or not readable, do nothing
326+
log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
327+
continue
328+
}
329+
fs.closeSync(fd)
330+
log.silly(logprefix, 'Found readable %s', candidate)
331+
return candidate
332+
}
333+
334+
return undefined
335+
}
336+
313337
function findPython (python, callback) {
314338
checkPython()
315339

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"devDependencies": {
4444
"tape": "~4.2.0",
4545
"bindings": "~1.2.1",
46-
"nan": "^2.0.0"
46+
"nan": "^2.0.0",
47+
"require-inject": "~1.3.0"
4748
},
4849
"scripts": {
4950
"test": "tape test/test-*"

test/test-find-accessible-sync.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var path = require('path')
5+
var requireInject = require('require-inject')
6+
var configure = requireInject('../lib/configure', {
7+
'graceful-fs': {
8+
'closeSync': function (fd) { return undefined },
9+
'openSync': function (path) {
10+
if (readableFiles.some(function (f) { return f === path} )) {
11+
return 0
12+
} else {
13+
var error = new Error('ENOENT - not found')
14+
throw error
15+
}
16+
}
17+
}
18+
})
19+
20+
var dir = path.sep + 'testdir'
21+
var readableFile = 'readable_file'
22+
var anotherReadableFile = 'another_readable_file'
23+
var readableFileInDir = 'somedir' + path.sep + readableFile
24+
var readableFiles = [
25+
path.resolve(dir, readableFile),
26+
path.resolve(dir, anotherReadableFile),
27+
path.resolve(dir, readableFileInDir)
28+
]
29+
30+
test('find accessible - empty array', function (t) {
31+
t.plan(1)
32+
33+
var candidates = []
34+
var found = configure.test.findAccessibleSync('test', dir, candidates)
35+
t.strictEqual(found, undefined)
36+
})
37+
38+
test('find accessible - single item array, readable', function (t) {
39+
t.plan(1)
40+
41+
var candidates = [ readableFile ]
42+
var found = configure.test.findAccessibleSync('test', dir, candidates)
43+
t.strictEqual(found, path.resolve(dir, readableFile))
44+
})
45+
46+
test('find accessible - single item array, readable in subdir', function (t) {
47+
t.plan(1)
48+
49+
var candidates = [ readableFileInDir ]
50+
var found = configure.test.findAccessibleSync('test', dir, candidates)
51+
t.strictEqual(found, path.resolve(dir, readableFileInDir))
52+
})
53+
54+
test('find accessible - single item array, unreadable', function (t) {
55+
t.plan(1)
56+
57+
var candidates = [ 'unreadable_file' ]
58+
var found = configure.test.findAccessibleSync('test', dir, candidates)
59+
t.strictEqual(found, undefined)
60+
})
61+
62+
63+
test('find accessible - multi item array, no matches', function (t) {
64+
t.plan(1)
65+
66+
var candidates = [ 'non_existent_file', 'unreadable_file' ]
67+
var found = configure.test.findAccessibleSync('test', dir, candidates)
68+
t.strictEqual(found, undefined)
69+
})
70+
71+
72+
test('find accessible - multi item array, single match', function (t) {
73+
t.plan(1)
74+
75+
var candidates = [ 'non_existent_file', readableFile ]
76+
var found = configure.test.findAccessibleSync('test', dir, candidates)
77+
t.strictEqual(found, path.resolve(dir, readableFile))
78+
})
79+
80+
test('find accessible - multi item array, return first match', function (t) {
81+
t.plan(1)
82+
83+
var candidates = [ 'non_existent_file', anotherReadableFile, readableFile ]
84+
var found = configure.test.findAccessibleSync('test', dir, candidates)
85+
t.strictEqual(found, path.resolve(dir, anotherReadableFile))
86+
})

0 commit comments

Comments
 (0)