Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
234 lines (173 loc) · 8.71 KB
/
Copy pathtest.js
File metadata and controls
234 lines (173 loc) · 8.71 KB
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
233
234
import path from 'node:path'
import os from 'node:os'
import fs from 'node:fs'
import { expect } from 'chai'
import { GDF, defaults } from '../gdf.js'
import '../fly.js'
import { execSync } from 'node:child_process'
// bun projects are only detected as such when `bun --version` works; the
// version itself is masked below, so a stub will do when bun isn't installed
try {
execSync('bun --version', { stdio: 'ignore' })
} catch {
const bindir = fs.mkdtempSync(path.join(os.tmpdir(), 'bun-stub-'))
fs.writeFileSync(path.join(bindir, 'bun'), '#!/bin/sh\necho 1.2.0\n', { mode: 0o755 })
process.env.PATH = [bindir, process.env.PATH].join(path.delimiter)
process.on('exit', () => fs.rmSync(bindir, { recursive: true, force: true }))
}
for (const group of fs.readdirSync('test', { withFileTypes: true })) {
if (!group.isDirectory()) continue
process.env.NODE_ENV = 'test'
for (const entry of fs.readdirSync(path.join('test', group.name), { withFileTypes: true })) {
if (!fs.existsSync(path.join('test', group.name, entry.name, 'package.json'))) continue
describe(`${group.name}: ${entry.name}`, function() {
const workdir = path.join(os.tmpdir(), group.name, entry.name)
const testdir = path.join('test', group.name, entry.name)
if (fs.existsSync(workdir)) fs.rmSync(workdir, { recursive: true })
fs.cpSync(testdir, workdir, { recursive: true })
const pj = fs.readFileSync(path.join(testdir, 'package.json'), 'utf-8')
const options = { ...defaults, ...(JSON.parse(pj).dockerfile || {}) }
if (options.envs) options.vars = options.envs
options.force = true
it('should produce a dockerfile', async function() {
await new GDF().run(workdir, options)
let argmask = /^(ARG\s+\w+\s*=).*?(\s*\\?)$/gm
if (entry.name === 'version') argmask = /()xxx()/g
const actualResults = fs.readFileSync(path.join(workdir, 'Dockerfile'), 'utf-8')
.replaceAll(argmask, '$1xxx$2')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, 'Dockerfile'), actualResults)
}
const expectedResults = fs.readFileSync(path.join(testdir, 'Dockerfile'), 'utf-8')
.replaceAll(argmask, '$1xxx$2')
expect(expectedResults).to.equal(actualResults)
})
it('should produce a .dockerignore', async function() {
await new GDF().run(workdir, options)
const actualResults = fs.readFileSync(path.join(workdir, '.dockerignore'), 'utf-8')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, '.dockerignore'), actualResults)
}
const expectedResults = fs.readFileSync(path.join(testdir, '.dockerignore'), 'utf-8')
expect(expectedResults).to.equal(actualResults)
})
if (fs.existsSync(path.join(testdir, 'docker-entrypoint.js'))) {
it('should produce a docker-entrypoint', async function() {
await new GDF().run(workdir, options)
let entrypoint = path.join(workdir, 'docker-entrypoint.js')
const other = path.join(workdir, 'other', 'docker-entrypoint.js')
if (fs.existsSync(other)) entrypoint = other
const actualResults = fs.readFileSync(entrypoint, 'utf-8')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, 'docker-entrypoint.js'), actualResults)
}
const expectedResults = fs.readFileSync(path.join(testdir, 'docker-entrypoint.js'), 'utf-8')
expect(expectedResults).to.equal(actualResults)
})
}
if (fs.existsSync(path.join(testdir, 'litefs.yml'))) {
it('should produce a litefs.yml', async function() {
await new GDF().run(workdir, options)
const actualResults = fs.readFileSync(path.join(workdir, 'litefs.yml'), 'utf-8')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, 'litefs.yml'), actualResults)
}
const expectedResults = fs.readFileSync(path.join(testdir, 'litefs.yml'), 'utf-8')
expect(expectedResults).to.equal(actualResults)
})
}
if (fs.existsSync(path.join(testdir, 'litestream.yml'))) {
it('should produce a litestream.yml', async function() {
await new GDF().run(workdir, options)
const actualResults = fs.readFileSync(path.join(workdir, 'litestream.yml'), 'utf-8')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, 'litestream.yml'), actualResults)
}
const expectedResults = fs.readFileSync(path.join(testdir, 'litestream.yml'), 'utf-8')
expect(expectedResults).to.equal(actualResults)
})
}
if (fs.existsSync(path.join(testdir, 'fly.toml'))) {
it('should produce a fly.toml', async function() {
let expectedResults = fs.readFileSync(path.join(testdir, 'fly.toml'), 'utf-8')
fs.writeFileSync(path.join(workdir, 'fly.toml'), '')
await new GDF().run(workdir, options)
const actualResults = fs.readFileSync(path.join(workdir, 'fly.toml'), 'utf-8')
if (process.env.TEST_CAPTURE) {
fs.writeFileSync(path.join(testdir, 'fly.toml'), actualResults)
expectedResults = actualResults
}
expect(expectedResults).to.equal(actualResults)
})
}
})
}
}
describe('fly: shopify', function() {
const savedPath = process.env.PATH
const savedCwd = process.cwd()
let workdir, bindir, log
// write an executable stub that logs its arguments
function stub(name, script = '') {
const file = path.join(bindir, name)
fs.writeFileSync(file, `#!/bin/sh\necho "${name} $*" >> "${log}"\n${script}`)
fs.chmodSync(file, 0o755)
}
beforeEach(function() {
workdir = fs.mkdtempSync(path.join(os.tmpdir(), 'shopify-'))
bindir = fs.mkdtempSync(path.join(os.tmpdir(), 'shopify-bin-'))
log = path.join(bindir, 'log')
fs.writeFileSync(log, '')
fs.writeFileSync(path.join(workdir, 'package.json'), JSON.stringify({ name: 'shopify-app', scripts: { start: 'node index.js' } }))
fs.writeFileSync(path.join(workdir, 'fly.toml'), 'app = "shopify-test"\n')
fs.writeFileSync(path.join(workdir, 'shopify.app.toml'), [
'client_id = "abc123"',
'application_url = "https://example.trycloudflare.com"',
'',
'[auth]',
'redirect_urls = ["https://example.trycloudflare.com/auth/callback"]',
''
].join('\n'))
stub('flyctl', 'case "$*" in *" list "*) echo "[]";; esac\n')
// fly.js looks for shopify.app*.toml in the current directory
process.chdir(workdir)
process.env.PATH = [bindir, path.dirname(process.execPath), '/usr/bin', '/bin'].join(path.delimiter)
})
afterEach(function() {
process.chdir(savedCwd)
process.env.PATH = savedPath
fs.rmSync(workdir, { recursive: true })
fs.rmSync(bindir, { recursive: true })
})
it('deploys with --allow-updates', async function() {
// Shopify CLI 4.x rejects --force
stub('shopify', 'case "$*" in *--help*) echo "--allow-updates";; *--force*) exit 2;; esac\n')
await new GDF().run(workdir, { ...defaults, force: true })
const calls = fs.readFileSync(log, 'utf-8')
expect(calls).to.include('shopify app env show --config shopify.app.toml')
expect(calls).to.include('shopify app deploy --allow-updates --config shopify.app.toml')
expect(calls).not.to.include('--force')
const config = fs.readFileSync(path.join(workdir, 'shopify.app.toml'), 'utf-8')
expect(config).to.include('application_url = "https://shopify-test.fly.dev"')
expect(config).to.include('"https://shopify-test.fly.dev/auth/callback"')
})
it('deploys with --force on Shopify CLI before 3.90', async function() {
stub('shopify', 'case "$*" in *--help*) echo "--force";; *--allow-updates*) exit 2;; esac\n')
await new GDF().run(workdir, { ...defaults, force: true })
const calls = fs.readFileSync(log, 'utf-8')
expect(calls).to.include('shopify app deploy --force --config shopify.app.toml')
expect(calls).not.to.include('deploy --allow-updates')
})
it('continues when shopify app deploy fails', async function() {
stub('shopify', 'exit 1\n')
await new GDF().run(workdir, { ...defaults, force: true })
const config = fs.readFileSync(path.join(workdir, 'shopify.app.toml'), 'utf-8')
expect(config).to.include('application_url = "https://shopify-test.fly.dev"')
expect(fs.existsSync(path.join(workdir, 'Dockerfile'))).to.equal(true)
})
it('continues when the shopify CLI is not installed', async function() {
await new GDF().run(workdir, { ...defaults, force: true })
const config = fs.readFileSync(path.join(workdir, 'shopify.app.toml'), 'utf-8')
expect(config).to.include('application_url = "https://shopify-test.fly.dev"')
})
})