/
linux_spec.ts
232 lines (192 loc) · 6.36 KB
/
linux_spec.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require('../spec_helper')
import _ from 'lodash'
import * as linuxHelper from '../../lib/linux'
import 'chai-as-promised'
import { log } from '../log'
import { detect } from '../../lib/detect'
import { goalBrowsers } from '../fixtures'
import { expect } from 'chai'
import { utils } from '../../lib/utils'
import os from 'os'
import sinon, { SinonStub } from 'sinon'
import mockFs from 'mock-fs'
describe('linux browser detection', () => {
let execa: SinonStub
let cachedEnv = { ...process.env }
beforeEach(() => {
execa = sinon.stub(utils, 'getOutput')
sinon.stub(os, 'platform').returns('linux')
sinon.stub(os, 'release').returns('1.0.0')
execa.withArgs('test-browser', ['--version'])
.resolves({ stdout: 'test-browser v100.1.2.3' })
execa.withArgs('foo-browser', ['--version'])
.resolves({ stdout: 'foo-browser v100.1.2.3' })
execa.withArgs('foo-bar-browser', ['--version'])
.resolves({ stdout: 'foo-browser v100.1.2.3' })
execa.withArgs('/foo/bar/browser', ['--version'])
.resolves({ stdout: 'foo-browser v9001.1.2.3' })
})
afterEach(() => {
Object.assign(process.env, cachedEnv)
mockFs.restore()
sinon.restore()
})
it('detects browser by running --version', () => {
const goal = goalBrowsers[0]
const checkBrowser = (browser) => {
expect(browser).to.deep.equal({
name: 'test-browser-name',
path: 'test-browser',
version: '100.1.2.3',
})
}
// @ts-ignore
return linuxHelper.detect(goal).then(checkBrowser)
})
// https://github.com/cypress-io/cypress/pull/7039
it('sets profilePath on snapcraft chromium', () => {
execa.withArgs('chromium', ['--version'])
.resolves({ stdout: 'Chromium 64.2.3 snap' })
sinon.stub(os, 'homedir').returns('/home/foo')
const checkBrowser = ([browser]) => {
expect(browser).to.deep.equal({
channel: 'stable',
name: 'chromium',
family: 'chromium',
displayName: 'Chromium',
majorVersion: 64,
minSupportedVersion: 64,
path: 'chromium',
profilePath: '/home/foo/snap/chromium/current',
version: '64.2.3',
})
}
return detect().then(checkBrowser)
})
// https://github.com/cypress-io/cypress/issues/19793
context('sets profilePath on snapcraft firefox', () => {
const expectedSnapFirefox = {
channel: 'stable',
name: 'firefox',
family: 'firefox',
displayName: 'Firefox',
majorVersion: 99,
minSupportedVersion: 86,
path: 'firefox',
profilePath: '/home/foo/snap/firefox/current',
version: '99.2.3',
}
beforeEach(() => {
execa.withArgs('firefox', ['--version'])
.resolves({ stdout: 'Mozilla Firefox 99.2.3' })
sinon.stub(os, 'homedir').returns('/home/foo')
})
it('with shim script', async () => {
process.env.PATH = '/bin'
mockFs({
'/bin/firefox': mockFs.symlink({ path: '/usr/bin/firefox' }),
'/usr/bin/firefox': mockFs.file({ mode: 0o777, content: 'foo bar foo bar foo bar\nexec /snap/bin/firefox\n' }),
})
const [browser] = await detect()
expect(browser).to.deep.equal(expectedSnapFirefox)
})
it('with /snap/bin in path', async () => {
process.env.PATH = '/bin:/snap/bin'
mockFs({
'/snap/bin/firefox': mockFs.file({ mode: 0o777, content: 'binary' }),
})
const [browser] = await detect()
expect(browser).to.deep.equal(expectedSnapFirefox)
})
it('with symlink to /snap/bin in path', async () => {
process.env.PATH = '/bin'
mockFs({
'/bin/firefox': mockFs.symlink({ path: '/snap/bin/firefox' }),
'/snap/bin/firefox': mockFs.file({ mode: 0o777, content: 'binary' }),
})
const [browser] = await detect()
expect(browser).to.deep.equal(expectedSnapFirefox)
})
})
// https://github.com/cypress-io/cypress/issues/6669
it('detects browser if the --version stdout is multiline', () => {
execa.withArgs('multiline-foo', ['--version'])
.resolves({
stdout: `
Running without a11y support!
foo-browser v9001.1.2.3
`,
})
const goal = _.defaults({ binary: 'multiline-foo' }, _.find(goalBrowsers, { name: 'foo-browser' }))
const checkBrowser = (browser) => {
expect(browser).to.deep.equal({
name: 'foo-browser',
path: 'multiline-foo',
version: '9001.1.2.3',
})
}
// @ts-ignore
return linuxHelper.detect(goal).then(checkBrowser)
})
// despite using detect(), this test is in linux/spec instead of detect_spec because it is
// testing side effects that occur within the Linux-specific detect function
// https://github.com/cypress-io/cypress/issues/1400
it('properly eliminates duplicates', () => {
const expected = [
{
displayName: 'Test Browser',
name: 'test-browser-name',
version: '100.1.2.3',
path: 'test-browser',
majorVersion: 100,
},
{
displayName: 'Foo Browser',
name: 'foo-browser',
version: '100.1.2.3',
path: 'foo-browser',
majorVersion: 100,
},
]
// @ts-ignore
return detect(goalBrowsers).then((browsers) => {
log('Browsers: %o', browsers)
log('Expected browsers: %o', expected)
expect(browsers).to.deep.equal(expected)
})
})
it('considers multiple binary names', () => {
const goalBrowsers = [
{
name: 'foo-browser',
versionRegex: /v(\S+)$/,
binary: ['foo-browser', 'foo-bar-browser'],
},
]
const expected = [
{
name: 'foo-browser',
version: '100.1.2.3',
path: 'foo-browser',
majorVersion: 100,
},
]
// @ts-ignore
return detect(goalBrowsers).then((browsers) => {
log('Browsers: %o', browsers)
log('Expected browsers: %o', expected)
expect(browsers).to.deep.equal(expected)
})
})
context('#getVersionString', () => {
it('runs the command with `--version` and returns trimmed output', async () => {
execa.withArgs('foo', ['--version']).resolves({ stdout: ' bar ' })
expect(await linuxHelper.getVersionString('foo')).to.eq('bar')
})
it('rejects with errors', async () => {
const err = new Error()
execa.withArgs('foo', ['--version']).rejects(err)
await expect(linuxHelper.getVersionString('foo')).to.be.rejectedWith(err)
})
})
})