Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit c75695e

Browse files
committed
conditionally spawn xvfb more intelligently
1 parent fbaa57e commit c75695e

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

lib/commands/open.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class Open
2626
args.push("--port", options.port)
2727

2828
utils.spawn(args, {
29-
xvfb: false
3029
detached: true
3130
stdio: ["ignore", "ignore", "ignore"]
3231
})
3332

34-
module.exports = Open
33+
module.exports = Open

lib/utils.coffee

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ module.exports = {
123123
.then(fn)
124124
.catch(fn)
125125

126+
needsXfvb: ->
127+
## do we need xvfb to run?
128+
##
129+
## we only need xvfb on linux
130+
## and when no process.env.DISPLAY is not
131+
os.platform() is "linux" and not process.env.DISPLAY
132+
126133
exec: (args, options = {}) ->
127134
e = null
128135

@@ -149,10 +156,11 @@ module.exports = {
149156
spawn: (args, options = {}) ->
150157
args = [].concat(args)
151158

159+
needsXfvb = @needsXfvb()
160+
152161
_.defaults options,
153162
verify: false
154163
detached: false
155-
xvfb: os.platform() is "linux"
156164
stdio: [process.stdin, process.stdout, "ignore"]
157165

158166
spawn = =>
@@ -162,7 +170,7 @@ module.exports = {
162170
return process.exit()
163171

164172
sp = cp.spawn pathToCypress, args, options
165-
if options.xvfb
173+
if needsXfvb
166174
## make sure we close down xvfb
167175
## when our spawned process exits
168176
sp.on "close", @stopXvfb
@@ -177,8 +185,8 @@ module.exports = {
177185

178186
return sp
179187

180-
if options.xvfb
188+
if needsXfvb
181189
@startXvfb().then(spawn)
182190
else
183191
spawn()
184-
}
192+
}

test/commands/open_spec.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ describe "Open", ->
4141

4242
pathToProject = path.resolve(process.cwd(), "path/to/project")
4343
expect(@spawn).to.be.calledWith(["--project", pathToProject], {
44-
xvfb: false
4544
detached: true
4645
stdio: ["ignore", "ignore", "ignore"]
4746
})

test/utils_spec.coffee

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fs = require("fs-extra")
2+
os = require("os")
23
mockSpawn = require("mock-spawn")
34

45
fs = Promise.promisifyAll(fs)
@@ -25,17 +26,30 @@ describe "utils", ->
2526
utils.spawn("--foo", {foo: "bar"}).then =>
2627
expect(@spawn).to.be.calledWithMatch("/path/to/cypress", ["--foo"], {foo: "bar"})
2728

29+
it "does not call into startXvfb when its not needed", ->
30+
@sandbox.spy(utils, "startXvfb")
31+
@sandbox.stub(utils, "needsXfvb").returns(false)
32+
33+
utils.spawn("--foo")
34+
.then ->
35+
expect(utils.startXvfb).not.to.be.called
36+
2837
it "starts utils.xvfb", ->
29-
utils.spawn("--foo", {xvfb: true}).then =>
38+
@sandbox.stub(utils, "needsXfvb").returns(true)
39+
40+
utils.spawn("--foo")
41+
.then =>
3042
expect(@startAsync).to.be.calledOnce
3143

3244
it "stops utils.xvfb when spawn closes", (done) ->
45+
@sandbox.stub(utils, "needsXfvb").returns(true)
46+
3347
spawn.setDefault (cb) =>
3448
cb(0)
3549
expect(@stopAsync).to.be.calledOnce
3650
done()
3751

38-
utils.spawn("--foo", {xvfb: true})
52+
utils.spawn("--foo")
3953

4054
it "exits with spawned exit code", (done) ->
4155
spawn.setDefault (cb) =>
@@ -53,7 +67,7 @@ describe "utils", ->
5367
unref: unref
5468
})
5569

56-
utils.spawn(null, {detached: true, xvfb: false}).then ->
70+
utils.spawn(null, {detached: true}).then ->
5771
expect(unref).to.be.calledOnce
5872

5973
it "does not unref by default", ->
@@ -64,9 +78,30 @@ describe "utils", ->
6478
unref: unref
6579
})
6680

67-
utils.spawn(null, {xvfb: false}).then ->
81+
utils.spawn(null).then ->
6882
expect(unref).not.to.be.called
6983

84+
context ".needsXfvb", ->
85+
afterEach ->
86+
delete process.env.DISPLAY
87+
88+
it "does not need xvfb on osx", ->
89+
@sandbox.stub(os, "platform").returns("darwin")
90+
91+
expect(utils.needsXfvb()).to.be.false
92+
93+
it "does not need xvfb on linux when DISPLAY is set", ->
94+
@sandbox.stub(os, "platform").returns("linux")
95+
96+
process.env.DISPLAY = ":99"
97+
98+
expect(utils.needsXfvb()).to.be.false
99+
100+
it "does need xvfb on linux when no DISPLAY is set", ->
101+
@sandbox.stub(os, "platform").returns("linux")
102+
103+
expect(utils.needsXfvb()).to.be.true
104+
70105
context "#getCypressPath", ->
71106
beforeEach ->
72107
@log = @sandbox.spy(console, "log")

0 commit comments

Comments
 (0)