Skip to content

Commit 384ff34

Browse files
Merge pull request #7507 from Shopify/refactor-cloudflared-install-tests-10097903198854477211
[Tests] Refactor install-cloudflared tests to remove filesystem mocks
2 parents 8713ecd + 5c42513 commit 384ff34

1 file changed

Lines changed: 161 additions & 113 deletions

File tree

packages/plugin-cloudflare/src/install-cloudflared.test.ts

Lines changed: 161 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,200 @@
11
import install, {CURRENT_CLOUDFLARE_VERSION, versionIsGreaterThan} from './install-cloudflared.js'
2-
import * as fsActions from '@shopify/cli-kit/node/fs'
32
import * as http from '@shopify/cli-kit/node/http'
4-
import {beforeEach, describe, expect, test, vi} from 'vitest'
5-
import util from 'util'
6-
7-
import {WriteStream} from 'fs'
3+
import {inTemporaryDirectory, readFile, writeFile, fileExists} from '@shopify/cli-kit/node/fs'
4+
import {joinPath} from '@shopify/cli-kit/node/path'
5+
import {describe, expect, test, vi} from 'vitest'
6+
import {Readable} from 'stream'
7+
import {writeFileSync} from 'fs'
88
// eslint-disable-next-line no-restricted-imports
99
import * as childProcess from 'child_process'
1010

11+
vi.mock('@shopify/cli-kit/node/http')
12+
1113
vi.mock('child_process')
12-
vi.mock('stream')
1314

1415
describe('install-cloudflare', () => {
15-
beforeEach(() => {
16-
vi.spyOn(util, 'promisify').mockReturnValue(vi.fn().mockReturnValue(Promise.resolve()))
17-
vi.spyOn(http, 'fetch').mockReturnValue(Promise.resolve({ok: true, body: {pipe: vi.fn()}} as any))
18-
vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(false)
19-
vi.spyOn(fsActions, 'mkdirSync').mockImplementation(() => vi.fn())
20-
vi.spyOn(fsActions, 'unlinkFileSync').mockImplementation(() => vi.fn())
21-
vi.spyOn(fsActions, 'renameFile').mockImplementation(() => Promise.resolve())
22-
vi.spyOn(fsActions, 'chmod').mockImplementation(() => Promise.resolve())
23-
vi.spyOn(fsActions, 'createFileWriteStream').mockReturnValue({pipe: vi.fn()} as unknown as WriteStream)
24-
})
16+
const mockFetch = (ok = true) => {
17+
vi.mocked(http.fetch).mockResolvedValue({
18+
ok,
19+
status: ok ? 200 : 404,
20+
statusText: ok ? 'OK' : 'Not Found',
21+
body: Readable.from(['cloudflared content']),
22+
} as any)
23+
}
2524

2625
test('install is ignored if SHOPIFY_CLI_IGNORE_CLOUDFLARED is present', async () => {
27-
// Given
28-
const env = {SHOPIFY_CLI_IGNORE_CLOUDFLARED: 'true'}
26+
await inTemporaryDirectory(async (_tmpDir) => {
27+
// Given
28+
const env = {SHOPIFY_CLI_IGNORE_CLOUDFLARED: 'true'}
29+
mockFetch()
2930

30-
// When
31-
await install(env)
31+
// When
32+
await install(env)
3233

33-
// Then
34-
expect(http.fetch).not.toHaveBeenCalled()
34+
// Then
35+
expect(http.fetch).not.toHaveBeenCalled()
36+
})
3537
})
3638

37-
test('install works when system is mac and x64', async () => {
38-
// Given
39-
const env = {}
40-
41-
// When
42-
await install(env, 'darwin', 'x64')
43-
44-
// Then
45-
expect(http.fetch).toHaveBeenCalledWith(
46-
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-darwin-amd64.tgz',
47-
expect.anything(),
48-
'slow-request',
49-
)
39+
test('downloads the correct binary for macOS x64', async () => {
40+
await inTemporaryDirectory(async (tmpDir) => {
41+
// Given
42+
const binPath = joinPath(tmpDir, 'cloudflared')
43+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
44+
mockFetch()
45+
vi.mocked(childProcess.execSync).mockImplementation((_command, options) => {
46+
// Simulate tar extracting the file
47+
const cwd = options?.cwd as string
48+
writeFileSync(joinPath(cwd, 'cloudflared'), 'extracted binary')
49+
return Buffer.from('')
50+
})
51+
52+
// When
53+
await install(env, 'darwin', 'x64')
54+
55+
// Then
56+
expect(http.fetch).toHaveBeenCalledWith(
57+
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-darwin-amd64.tgz',
58+
expect.anything(),
59+
'slow-request',
60+
)
61+
await expect(fileExists(binPath)).resolves.toBe(true)
62+
await expect(readFile(binPath)).resolves.toBe('extracted binary')
63+
})
5064
})
5165

52-
test('install works when system is mac and arm64', async () => {
53-
// Given
54-
const env = {}
55-
56-
// When
57-
await install(env, 'darwin', 'arm64')
58-
59-
// Then
60-
expect(http.fetch).toHaveBeenCalledWith(
61-
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-darwin-arm64.tgz',
62-
expect.anything(),
63-
'slow-request',
64-
)
66+
test('downloads the correct binary for macOS arm64', async () => {
67+
await inTemporaryDirectory(async (tmpDir) => {
68+
// Given
69+
const binPath = joinPath(tmpDir, 'cloudflared')
70+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
71+
mockFetch()
72+
vi.mocked(childProcess.execSync).mockImplementation((_command, options) => {
73+
const cwd = options?.cwd as string
74+
writeFileSync(joinPath(cwd, 'cloudflared'), 'extracted binary')
75+
return Buffer.from('')
76+
})
77+
78+
// When
79+
await install(env, 'darwin', 'arm64')
80+
81+
// Then
82+
expect(http.fetch).toHaveBeenCalledWith(
83+
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-darwin-arm64.tgz',
84+
expect.anything(),
85+
'slow-request',
86+
)
87+
await expect(fileExists(binPath)).resolves.toBe(true)
88+
})
6589
})
6690

67-
test('install works when system is linux', async () => {
68-
// Given
69-
const env = {}
70-
71-
// When
72-
await install(env, 'linux', 'x64')
73-
74-
// Then
75-
expect(http.fetch).toHaveBeenCalledWith(
76-
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-linux-amd64',
77-
expect.anything(),
78-
'slow-request',
79-
)
91+
test('downloads the correct binary for linux', async () => {
92+
await inTemporaryDirectory(async (tmpDir) => {
93+
// Given
94+
const binPath = joinPath(tmpDir, 'cloudflared')
95+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
96+
mockFetch()
97+
98+
// When
99+
await install(env, 'linux', 'x64')
100+
101+
// Then
102+
expect(http.fetch).toHaveBeenCalledWith(
103+
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-linux-amd64',
104+
expect.anything(),
105+
'slow-request',
106+
)
107+
await expect(fileExists(binPath)).resolves.toBe(true)
108+
await expect(readFile(binPath)).resolves.toBe('cloudflared content')
109+
})
80110
})
81111

82-
test('install works when system is windows', async () => {
83-
// Given
84-
const env = {}
85-
86-
// When
87-
await install(env, 'win32', 'x64')
88-
89-
// Then
90-
expect(http.fetch).toHaveBeenCalledWith(
91-
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-windows-amd64.exe',
92-
expect.anything(),
93-
'slow-request',
94-
)
112+
test('downloads the correct binary for windows', async () => {
113+
await inTemporaryDirectory(async (tmpDir) => {
114+
// Given
115+
const binPath = joinPath(tmpDir, 'cloudflared.exe')
116+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
117+
mockFetch()
118+
119+
// When
120+
await install(env, 'win32', 'x64')
121+
122+
// Then
123+
expect(http.fetch).toHaveBeenCalledWith(
124+
'https://github.com/cloudflare/cloudflared/releases/download/2024.8.2/cloudflared-windows-amd64.exe',
125+
expect.anything(),
126+
'slow-request',
127+
)
128+
await expect(fileExists(binPath)).resolves.toBe(true)
129+
})
95130
})
96131

97-
test('install ignored if bin exists but current version is up to date', async () => {
98-
// Given
99-
const env = {}
100-
vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(true)
101-
vi.spyOn(childProcess, 'execFileSync').mockReturnValue(
102-
`cloudflared version ${CURRENT_CLOUDFLARE_VERSION} (built 2023-03-13-1444 UTC)`,
103-
)
104-
105-
// When
106-
await install(env, 'win32', 'x64')
107-
108-
// Then
109-
expect(http.fetch).not.toHaveBeenCalled()
132+
test('skips install if bin exists and current version is up to date', async () => {
133+
await inTemporaryDirectory(async (tmpDir) => {
134+
// Given
135+
const binPath = joinPath(tmpDir, 'cloudflared')
136+
await writeFile(binPath, 'existing binary')
137+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
138+
vi.mocked(childProcess.execFileSync).mockReturnValue(
139+
`cloudflared version ${CURRENT_CLOUDFLARE_VERSION} (built 2023-03-13-1444 UTC)`,
140+
)
141+
mockFetch()
142+
143+
// When
144+
await install(env, 'linux', 'x64')
145+
146+
// Then
147+
expect(http.fetch).not.toHaveBeenCalled()
148+
})
110149
})
111150

112-
test('install works if bin exists and current version is not up to date', async () => {
113-
// Given
114-
const env = {}
115-
vi.spyOn(fsActions, 'fileExistsSync').mockReturnValueOnce(true)
116-
vi.spyOn(childProcess, 'execFileSync').mockReturnValue(`cloudflared version 2000.0.0 (built 2023-03-13-1444 UTC)`)
117-
118-
// When
119-
await install(env, 'darwin', 'x64')
120-
121-
// Then
122-
expect(http.fetch).toHaveBeenCalled()
151+
test('updates install if bin exists but current version is not up to date', async () => {
152+
await inTemporaryDirectory(async (tmpDir) => {
153+
// Given
154+
const binPath = joinPath(tmpDir, 'cloudflared')
155+
await writeFile(binPath, 'old binary')
156+
const env = {SHOPIFY_CLI_CLOUDFLARED_PATH: binPath}
157+
vi.mocked(childProcess.execFileSync).mockReturnValue(`cloudflared version 2000.0.0 (built 2023-03-13-1444 UTC)`)
158+
mockFetch()
159+
160+
// When
161+
await install(env, 'linux', 'x64')
162+
163+
// Then
164+
expect(http.fetch).toHaveBeenCalled()
165+
await expect(readFile(binPath)).resolves.toBe('cloudflared content')
166+
})
123167
})
124168

125-
test('install fails if unsupported platform', async () => {
126-
// Given
127-
const env = {}
169+
test('throws an error if the platform is unsupported', async () => {
170+
await inTemporaryDirectory(async (_tmpDir) => {
171+
// Given
172+
const env = {}
128173

129-
// When
130-
const res = install(env, 'freebsd', 'x64')
174+
// When
175+
const res = install(env, 'freebsd', 'x64')
131176

132-
// Then
133-
await expect(res).rejects.toThrow('Unsupported system platform: freebsd')
177+
// Then
178+
await expect(res).rejects.toThrow('Unsupported system platform: freebsd')
179+
})
134180
})
135181

136-
test('install fails if unsupported arch', async () => {
137-
// Given
138-
const env = {}
182+
test('throws an error if the architecture is unsupported', async () => {
183+
await inTemporaryDirectory(async (_tmpDir) => {
184+
// Given
185+
const env = {}
139186

140-
// When
141-
const res = install(env, 'darwin', 'mips')
187+
// When
188+
const res = install(env, 'darwin', 'mips')
142189

143-
// Then
144-
await expect(res).rejects.toThrow('Unsupported system arch: mips')
190+
// Then
191+
await expect(res).rejects.toThrow('Unsupported system arch: mips')
192+
})
145193
})
146194
})
147195

148196
describe('version-compare', () => {
149-
test('versionIsGreaterThan', () => {
197+
test('versionIsGreaterThan correctly compares versions', () => {
150198
expect(versionIsGreaterThan('1.0.0', '0.9.0')).toBe(true)
151199
expect(versionIsGreaterThan('0.9.0', '1.0.0')).toBe(false)
152200
expect(versionIsGreaterThan('1.0.0', '1.0.0')).toBe(false)

0 commit comments

Comments
 (0)