forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarwin_spec.ts
175 lines (141 loc) · 5.43 KB
/
darwin_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
import _ from 'lodash'
import os from 'os'
import cp from 'child_process'
import * as darwinHelper from '../../lib/darwin'
import * as linuxHelper from '../../lib/linux'
import * as darwinUtil from '../../lib/darwin/util'
import { utils } from '../../lib/utils'
import { expect } from 'chai'
import sinon, { SinonStub } from 'sinon'
import { launch } from '../../lib/browsers'
import { knownBrowsers } from '../../lib/known-browsers'
import Bluebird from 'bluebird'
import fse from 'fs-extra'
import snapshot from 'snap-shot-it'
import { PassThrough } from 'stream'
import { FoundBrowser } from '@packages/types'
function generatePlist (key, value) {
return `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>${key}</key>
<string>${value}</string>
</dict>
</plist>
`
}
function stubBrowser (findAppParams: darwinUtil.FindAppParams) {
(utils.getOutput as unknown as SinonStub)
.withArgs(`mdfind 'kMDItemCFBundleIdentifier=="${findAppParams.appId}"' | head -1`)
.resolves({ stdout: `/Applications/${findAppParams.appName}` })
;(fse.readFile as SinonStub)
.withArgs(`/Applications/${findAppParams.appName}/Contents/Info.plist`)
.resolves(generatePlist(findAppParams.versionProperty, 'someVersion'))
}
function spawnStub () {
const stub: any = {
on: sinon.stub(),
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: sinon.stub(),
}
stub.once = stub.on
stub.on.withArgs('exit').callsArgAsync(1)
stub.on.withArgs('close').callsArgAsync(1)
stub.stderr.end()
stub.stdout.end()
return stub as cp.ChildProcess
}
describe('darwin browser detection', () => {
let getOutput: SinonStub
beforeEach(() => {
sinon.stub(fse, 'readFile').rejects({ code: 'ENOENT' })
getOutput = sinon.stub(utils, 'getOutput').resolves({ stdout: '' })
})
it('detects browsers as expected', async () => {
// this test uses the macOS detectors to stub out the expected calls
_.forEach(darwinHelper.browsers, (channels) => {
_.forEach(channels, stubBrowser)
})
// then, it uses the main browsers list to attempt detection of all browsers, which should succeed
const detected = (await Bluebird.mapSeries(knownBrowsers, (browser) => {
return darwinHelper.detect(browser)
.then((foundBrowser) => {
const findAppParams = darwinHelper.browsers[browser.name][browser.channel]
return _.merge(browser, foundBrowser, { findAppParams })
})
}))
snapshot(detected)
})
it('getVersionString is re-exported from linuxHelper', () => {
expect(darwinHelper.getVersionString).to.eq(linuxHelper.getVersionString)
})
context('forces correct architecture', () => {
function stubForArch (arch: 'arm64' | 'x64') {
sinon.stub(process, 'env').value({ env2: 'false', env3: 'true' })
sinon.stub(os, 'arch').returns(arch)
sinon.stub(os, 'platform').returns('darwin')
getOutput.restore()
return sinon.stub(cp, 'spawn').returns(spawnStub())
}
context('in version detection', () => {
it('uses arch and ARCHPREFERENCE on arm64', async () => {
const cpSpawn = stubForArch('arm64')
// this will error since we aren't setting stdout
await (darwinHelper.detect(knownBrowsers[0]).catch(() => {}))
// first call is mdfind, second call is getVersionString
const { args } = cpSpawn.getCall(1)
expect(args[0]).to.eq('arch')
expect(args[1]).to.deep.eq([knownBrowsers[0].binary, '--version'])
expect(args[2].env).to.deep.include({
ARCHPREFERENCE: 'arm64,x86_64',
env2: 'false',
env3: 'true',
})
})
it('does not use `arch` on x64', async () => {
const cpSpawn = stubForArch('x64')
// this will error since we aren't setting stdout
await (darwinHelper.detect(knownBrowsers[0]).catch(() => {}))
// first call is mdfind, second call is getVersionString
const { args } = cpSpawn.getCall(1)
expect(args[0]).to.eq(knownBrowsers[0].binary)
expect(args[1]).to.deep.eq(['--version'])
expect(args[2].env).to.deep.include({
env2: 'false',
env3: 'true',
})
})
})
context('in browser launching', () => {
it('uses arch and ARCHPREFERENCE on arm64', async () => {
const cpSpawn = stubForArch('arm64')
await launch({ path: 'chrome' } as unknown as FoundBrowser, 'url', 123, ['arg1'], { env1: 'true', env2: 'true' })
const { args } = cpSpawn.getCall(0)
expect(args[0]).to.eq('arch')
expect(args[1]).to.deep.eq(['chrome', 'url', 'arg1'])
expect(args[2].env).to.deep.include({
ARCHPREFERENCE: 'arm64,x86_64',
env1: 'true',
env2: 'false',
env3: 'true',
})
})
it('does not use `arch` on x64', async () => {
const cpSpawn = stubForArch('x64')
await launch({ path: 'chrome' } as unknown as FoundBrowser, 'url', 123, ['arg1'], { env1: 'true', env2: 'true' })
const { args } = cpSpawn.getCall(0)
expect(args[0]).to.eq('chrome')
expect(args[1]).to.deep.eq(['url', 'arg1'])
expect(args[2].env).to.deep.include({
env1: 'true',
env2: 'false',
env3: 'true',
})
expect(args[2].env).to.not.have.property('ARCHPREFERENCE')
})
})
})
})