-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathfind-node-version.js
133 lines (113 loc) · 3.4 KB
/
find-node-version.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
121
122
123
124
125
126
127
128
129
130
131
132
133
const Code = require('code')
const Lab = require('lab')
const Proxyquire = require('proxyquire')
const Sinon = require('sinon')
const Path = require('path')
var fsStub = {
readFileSync: Sinon.stub(),
existsSync: Sinon.stub()
}
var FindVersion = Proxyquire('../lib/find-node-version', {
'fs': fsStub
})
var lab = exports.lab = Lab.script()
var describe = lab.describe
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var it = lab.it
var expect = Code.expect
describe('find-node-version', () => {
var options
beforeEach((done) => {
options = {
directory: process.cwd(),
input: process.cwd()
}
done()
})
afterEach((done) => {
fsStub.existsSync.reset()
fsStub.readFileSync.reset()
done()
})
describe('initialization', () => {
beforeEach((done) => {
fsStub.existsSync.returns(false)
done()
})
it('fails if no options object is provided', (done) => {
expect(() => {
FindVersion()
}).to.throw(Error, 'options is required')
done()
})
it('fails if output directory does not exist', (done) => {
options.directory = Path.join(__dirname, 'does_not_exist')
expect(() => {
FindVersion(options)
}).to.throw(Error, 'Output directory not found')
done()
})
})
describe('successfully', () => {
beforeEach((done) => {
fsStub.existsSync = Sinon.stub().returns(true)
fsStub.readFileSync.onCall(0).returns(JSON.stringify({ 'engines': {} }))
fsStub.readFileSync.onCall(1).returns([
'var Fiber = require("fibers");',
'var fs = require("fs");',
'var path = require("path");',
'var Future = require("fibers/future");',
'var _ = require(\'underscore\');',
'var sourcemap_support = require(\'source-map-support\');',
'',
'var bootUtils = require(\'./boot-utils.js\');',
'var files = require(\'./mini-files.js\');',
'',
'// This code is duplicated in tools/main.js.',
'var MIN_NODE_VERSION = \'v0.10.36\';'
].join('\n'))
done()
})
it('sets node version from options.nodeVersion', (done) => {
options.nodeVersion = '4.2.0'
expect(FindVersion(options))
.to.equal(options.nodeVersion)
done()
})
it('sets node version from options.input package.json', (done) => {
var pkg = require(Path.resolve(options.input, 'package.json'))
fsStub.readFileSync.onCall(0).returns(JSON.stringify(pkg))
expect(FindVersion(options)).to.equal(pkg.engines.node)
done()
})
it('finds the node version from boot.js', (done) => {
expect(FindVersion(options)).to.equal('0.10.36')
done()
})
})
describe('unsuccessfully', () => {
beforeEach((done) => {
fsStub.existsSync.returns(true)
fsStub.readFileSync.onCall(0).returns(JSON.stringify({}))
fsStub.readFileSync.onCall(1).returns('')
done()
})
it('finds the node version from boot.js', (done) => {
expect(FindVersion(options)).to.equal('0.10.33')
done()
})
})
describe('throws trying to', () => {
beforeEach((done) => {
fsStub.existsSync = Sinon.stub().returns(true)
fsStub.readFileSync.onCall(0).throws()
fsStub.readFileSync.onCall(1).throws()
done()
})
it('find the node version from boot.js', (done) => {
expect(FindVersion(options)).to.equal('0.10.33')
done()
})
})
})