Skip to content

Commit db616cf

Browse files
committed
v0.10.2
1 parent 411faa1 commit db616cf

File tree

5 files changed

+271
-74
lines changed

5 files changed

+271
-74
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ When submitting code, please keep commits small, and do not modify the README fi
230230
* CLI: Fix `--cors` flag no longer sets CORP header.
231231
* CLI: Fix `--originalpath` flag sets proper value.
232232
* CLI: Fix some typos in CLI help
233+
* Add tests for CLI arguments and reworked CLI code to allow for testing.
233234

234235
### 0.10.1
235236
* Fix errant debug message in CLI

lib/command.coffee

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1+
pjson = require('../package.json')
2+
livereload = require './livereload'
3+
resolve = require('path').resolve
4+
15
runner = ->
2-
pjson = require('../package.json')
3-
livereload = require './livereload'
4-
resolve = require('path').resolve
5-
opts = require 'opts'
6+
res = parseArgsAndCreateServer(true)
7+
server = res.server
8+
path = res.path
9+
10+
console.log "Starting LiveReload v#{pjson.version} for #{path} on #{server.host}:#{server.port}."
11+
12+
server.on 'error', (err) ->
13+
14+
if err.code == "EADDRINUSE"
15+
console.log("The port LiveReload wants to use is used by something else.")
16+
else
17+
throw err
18+
19+
process.exit(1)
20+
21+
server.watch(path)
22+
23+
# Parse the arguments and create the server.
24+
# shouldListen option exists so we can start the server under CLI, but not when
25+
# we use this from the entrypoint for tests
26+
parseArgsAndCreateServer = (shouldListen = true) ->
27+
opts = require 'opts'
628

729
args = [
830
{
@@ -104,7 +126,7 @@ runner = ->
104126
}
105127
]
106128

107-
opts.parse(options.reverse(), args, true)
129+
opts.parse(options.reverse(), args, true)
108130

109131
path = (opts.arg('path') || '.')
110132
.split(/\s*,\s*/)
@@ -136,21 +158,40 @@ runner = ->
136158
originalPath: originalPath
137159
cors: cors
138160
corp: corp
139-
noListen: true # no listening when creating the server so we can test the options.
161+
noListen: not shouldListen
140162
})
141163

142-
server.listen()
143164

144-
console.log "Starting LiveReload v#{pjson.version} for #{path} on #{host}:#{port}."
165+
{
166+
server: server
167+
path: path
168+
}
145169

146-
server.on 'error', (err) ->
147-
if err.code == "EADDRINUSE"
148-
console.log("The port LiveReload wants to use is used by something else.")
149-
else
150-
throw err
151-
process.exit(1)
152170

153-
server.watch(path)
171+
# This is the entrypoint that tests use to verify our option parsing.
172+
# It doesn't start the server.
173+
# The opts library directly parses process.argv. That means
174+
# when we run tests with Mocha it gets the Mocha args, not our args.
175+
# So this is hacky indirection that lets us send our own args
176+
# from the tests and still run them through the same parsing function
177+
# the CLI uses.
178+
createServerFromArgs = (testArgv) ->
179+
# Save original process.argv and parse with test arguments
180+
originalArgv = process.argv
181+
182+
# replace the args with our test args
183+
process.argv = ['node', 'test'].concat(testArgv)
184+
185+
# Reset opts internal state by requiring a fresh instance
186+
delete require.cache[require.resolve('opts')]
187+
188+
try
189+
result = parseArgsAndCreateServer(false) # false = don't listen, for testing
190+
return result
191+
finally
192+
# Always restore original process.argv
193+
process.argv = originalArgv
154194

155195
module.exports =
156196
run: runner
197+
createServerFromArgs: createServerFromArgs

lib/command.js

Lines changed: 59 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/command.test.coffee

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
command = require '../lib/command'
2+
should = require 'should'
3+
4+
describe 'CLI argument parsing', ->
5+
6+
it 'should create server with configuration options', ->
7+
result = command.createServerFromArgs(['--port', '45001'])
8+
result.server.config.port.should.equal(45001)
9+
result.server.config.host.should.equal('localhost')
10+
result.server.config.debug.should.equal(false)
11+
result.server.config.usePolling.should.equal(false)
12+
result.server.config.delay.should.equal(0)
13+
result.server.config.originalPath.should.equal('')
14+
result.server.config.cors.should.equal(false)
15+
result.server.config.corp.should.equal(false)
16+
result.path.should.be.an.Array()
17+
18+
it 'should parse port option correctly', ->
19+
result = command.createServerFromArgs(['--port', '8080'])
20+
result.server.config.port.should.equal(8080)
21+
22+
it 'should parse short port option correctly', ->
23+
result = command.createServerFromArgs(['-p', '45003'])
24+
result.server.config.port.should.equal(45003)
25+
26+
it 'should parse host option correctly', ->
27+
result = command.createServerFromArgs(['--bind', '0.0.0.0'])
28+
result.server.config.host.should.equal('0.0.0.0')
29+
30+
it 'should parse short host option correctly', ->
31+
result = command.createServerFromArgs(['-b', '127.0.0.1'])
32+
result.server.config.host.should.equal('127.0.0.1')
33+
34+
it 'should parse debug flag correctly', ->
35+
result = command.createServerFromArgs(['--debug'])
36+
result.server.config.debug.should.equal(true)
37+
38+
it 'should parse short debug flag correctly', ->
39+
result = command.createServerFromArgs([ '-d'])
40+
result.server.config.debug.should.equal(true)
41+
42+
it 'should parse exts option correctly', ->
43+
result = command.createServerFromArgs(['--exts', 'html,css,js'])
44+
result.server.config.exts.should.eql(['html', 'css', 'js'])
45+
46+
it 'should parse short exts option correctly', ->
47+
result = command.createServerFromArgs(['-e', 'md,txt'])
48+
result.server.config.exts.should.eql(['md', 'txt'])
49+
50+
it 'should parse extraExts option correctly', ->
51+
result = command.createServerFromArgs(['--extraExts', 'scss,less'])
52+
expectedExts = ['scss', 'less', 'html', 'css', 'js', 'png', 'gif', 'jpg', 'php', 'php5', 'py', 'rb', 'erb', 'coffee']
53+
result.server.config.exts.should.eql(expectedExts)
54+
55+
it 'should parse short extraExts option correctly', ->
56+
result = command.createServerFromArgs(['-ee', 'vue'])
57+
expectedExts = ['vue', 'html', 'css', 'js', 'png', 'gif', 'jpg', 'php', 'php5', 'py', 'rb', 'erb', 'coffee']
58+
result.server.config.exts.should.eql(expectedExts)
59+
60+
it 'should parse filesToReload option correctly', ->
61+
result = command.createServerFromArgs(['--filesToReload', 'index.html,app.js'])
62+
result.server.config.filesToReload.should.eql(['index.html', 'app.js'])
63+
64+
it 'should parse short filesToReload option correctly', ->
65+
result = command.createServerFromArgs(['-f', 'config.json'])
66+
result.server.config.filesToReload.should.eql(['config.json'])
67+
68+
it 'should parse usepolling flag correctly', ->
69+
result = command.createServerFromArgs(['--usepolling'])
70+
result.server.config.usePolling.should.equal(true)
71+
72+
it 'should parse short usepolling flag correctly', ->
73+
result = command.createServerFromArgs(['-u'])
74+
result.server.config.usePolling.should.equal(true)
75+
76+
it 'should parse wait option correctly', ->
77+
result = command.createServerFromArgs(['--wait', '1000'])
78+
result.server.config.delay.should.equal(1000)
79+
80+
it 'should parse short wait option correctly', ->
81+
result = command.createServerFromArgs(['-w', '500'])
82+
result.server.config.delay.should.equal(500)
83+
84+
it 'should parse originalpath option correctly', ->
85+
result = command.createServerFromArgs(['--originalpath', 'http://example.com'])
86+
result.server.config.originalPath.should.equal('http://example.com')
87+
88+
it 'should parse short originalpath option correctly', ->
89+
result = command.createServerFromArgs(['-op', 'http://localhost:3000'])
90+
result.server.config.originalPath.should.equal('http://localhost:3000')
91+
92+
it 'should parse corp flag correctly', ->
93+
result = command.createServerFromArgs(['--corp'])
94+
result.server.config.corp.should.equal(true)
95+
96+
it 'should parse short corp flag correctly', ->
97+
result = command.createServerFromArgs(['-cp'])
98+
result.server.config.corp.should.equal(true)
99+
100+
it 'should parse cors option correctly', ->
101+
result = command.createServerFromArgs(['--cors', 'http://localhost:8080'])
102+
result.server.config.cors.should.equal('http://localhost:8080')
103+
104+
it 'should parse short cors option correctly', ->
105+
result = command.createServerFromArgs(['-cs', '*'])
106+
result.server.config.cors.should.equal('*')
107+
108+
109+
it 'should parse exclusions option correctly', ->
110+
result = command.createServerFromArgs(['--port', '45025', '--exclusions', '\\.tmp,\\.log'])
111+
result.server.config.exclusions.should.have.length(5) # 2 user + 3 defaults
112+
result.server.config.exclusions[0].should.be.an.instanceOf(RegExp)
113+
result.server.config.exclusions[1].should.be.an.instanceOf(RegExp)
114+
115+
it 'should parse short exclusions option correctly', ->
116+
result = command.createServerFromArgs(['--port', '45026', '-x', 'node_modules'])
117+
result.server.config.exclusions.should.have.length(4) # 1 user + 3 defaults
118+
result.server.config.exclusions[0].should.be.an.instanceOf(RegExp)
119+
120+
it 'should handle multiple options correctly', ->
121+
result = command.createServerFromArgs(['-p', '8080', '-d', '--cors', '*', '--exts', 'html,js'])
122+
result.server.config.port.should.equal(8080)
123+
result.server.config.debug.should.equal(true)
124+
result.server.config.cors.should.equal('*')
125+
result.server.config.exts.should.eql(['html', 'js'])
126+
127+
it 'should handle path argument correctly', ->
128+
result = command.createServerFromArgs(['./src'])
129+
result.path.should.be.an.Array()
130+
result.path[0].should.match(/\/src$/)
131+
132+
it 'should handle multiple comma-separated paths correctly', ->
133+
result = command.createServerFromArgs(['./src,./public'])
134+
result.path.should.have.length(2)
135+
result.path[0].should.match(/\/src$/)
136+
result.path[1].should.match(/\/public$/)
137+
138+
it 'should use default port when no port specified', ->
139+
result = command.createServerFromArgs([])
140+
result.server.config.port.should.equal(35729)

0 commit comments

Comments
 (0)