-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-runner.test.ts
More file actions
395 lines (327 loc) · 11.5 KB
/
test-runner.test.ts
File metadata and controls
395 lines (327 loc) · 11.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { existsSync, readFileSync } from 'fs'
import { detectTestRunner, detectPackageManager, detectMonorepo, runTests, stripLocalUvSources } from '../test-runner.js'
// Helper: create a temp directory with specific files
function createTempDir(): string {
return mkdtempSync(join(tmpdir(), 'wright-test-'))
}
function touchFile(dir: string, name: string, content = ''): void {
const filePath = join(dir, name)
const parent = filePath.substring(0, filePath.lastIndexOf('/'))
mkdirSync(parent, { recursive: true })
writeFileSync(filePath, content)
}
describe('detectTestRunner', () => {
let tempDir: string
beforeEach(() => {
tempDir = createTempDir()
})
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true })
})
it('detects playwright from playwright.config.ts', () => {
touchFile(tempDir, 'playwright.config.ts')
expect(detectTestRunner(tempDir)).toBe('playwright')
})
it('detects playwright from playwright.config.js', () => {
touchFile(tempDir, 'playwright.config.js')
expect(detectTestRunner(tempDir)).toBe('playwright')
})
it('detects cargo-test from Cargo.toml', () => {
touchFile(tempDir, 'Cargo.toml', '[package]\nname = "test"')
expect(detectTestRunner(tempDir)).toBe('cargo-test')
})
it('detects go-test from go.mod', () => {
touchFile(tempDir, 'go.mod', 'module example.com/test\ngo 1.21')
expect(detectTestRunner(tempDir)).toBe('go-test')
})
it('detects pytest from pyproject.toml', () => {
touchFile(tempDir, 'pyproject.toml', '[project]\nname = "test"')
expect(detectTestRunner(tempDir)).toBe('pytest')
})
it('detects pytest from setup.py', () => {
touchFile(tempDir, 'setup.py', 'from setuptools import setup')
expect(detectTestRunner(tempDir)).toBe('pytest')
})
it('detects vitest from package.json devDependencies', () => {
touchFile(
tempDir,
'package.json',
JSON.stringify({
devDependencies: { vitest: '^1.0.0' },
}),
)
expect(detectTestRunner(tempDir)).toBe('vitest')
})
it('detects jest from package.json devDependencies', () => {
touchFile(
tempDir,
'package.json',
JSON.stringify({
devDependencies: { jest: '^29.0.0' },
}),
)
expect(detectTestRunner(tempDir)).toBe('jest')
})
it('detects @playwright/test from package.json dependencies', () => {
touchFile(
tempDir,
'package.json',
JSON.stringify({
dependencies: { '@playwright/test': '^1.0.0' },
}),
)
expect(detectTestRunner(tempDir)).toBe('playwright')
})
it('defaults to jest for package.json without recognized test deps', () => {
touchFile(
tempDir,
'package.json',
JSON.stringify({
dependencies: { express: '^4.0.0' },
}),
)
expect(detectTestRunner(tempDir)).toBe('jest')
})
it('returns custom for empty directory', () => {
expect(detectTestRunner(tempDir)).toBe('custom')
})
it('prioritizes playwright config over package.json vitest', () => {
touchFile(tempDir, 'playwright.config.ts')
touchFile(
tempDir,
'package.json',
JSON.stringify({
devDependencies: { vitest: '^1.0.0' },
}),
)
expect(detectTestRunner(tempDir)).toBe('playwright')
})
it('prioritizes Cargo.toml over package.json', () => {
touchFile(tempDir, 'Cargo.toml', '[package]\nname = "test"')
touchFile(tempDir, 'package.json', JSON.stringify({}))
expect(detectTestRunner(tempDir)).toBe('cargo-test')
})
})
describe('detectPackageManager', () => {
let tempDir: string
beforeEach(() => {
tempDir = createTempDir()
})
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true })
})
it('detects uv from uv.lock', () => {
touchFile(tempDir, 'uv.lock')
expect(detectPackageManager(tempDir)).toBe('uv')
})
it('detects poetry from poetry.lock', () => {
touchFile(tempDir, 'poetry.lock')
expect(detectPackageManager(tempDir)).toBe('poetry')
})
it('detects pip from Pipfile.lock', () => {
touchFile(tempDir, 'Pipfile.lock')
expect(detectPackageManager(tempDir)).toBe('pip')
})
it('detects pip from requirements.txt', () => {
touchFile(tempDir, 'requirements.txt')
expect(detectPackageManager(tempDir)).toBe('pip')
})
it('detects cargo from Cargo.toml', () => {
touchFile(tempDir, 'Cargo.toml')
expect(detectPackageManager(tempDir)).toBe('cargo')
})
it('detects go from go.mod', () => {
touchFile(tempDir, 'go.mod')
expect(detectPackageManager(tempDir)).toBe('go')
})
it('detects pnpm from pnpm-lock.yaml', () => {
touchFile(tempDir, 'pnpm-lock.yaml')
expect(detectPackageManager(tempDir)).toBe('pnpm')
})
it('detects yarn from yarn.lock', () => {
touchFile(tempDir, 'yarn.lock')
expect(detectPackageManager(tempDir)).toBe('yarn')
})
it('detects npm from package-lock.json', () => {
touchFile(tempDir, 'package-lock.json')
expect(detectPackageManager(tempDir)).toBe('npm')
})
it('detects npm from package.json (fallback)', () => {
touchFile(tempDir, 'package.json', '{}')
expect(detectPackageManager(tempDir)).toBe('npm')
})
it('detects uv from pyproject.toml when no lockfile', () => {
touchFile(tempDir, 'pyproject.toml')
expect(detectPackageManager(tempDir)).toBe('uv')
})
it('returns none for empty directory', () => {
expect(detectPackageManager(tempDir)).toBe('none')
})
it('prioritizes uv.lock over pyproject.toml', () => {
touchFile(tempDir, 'uv.lock')
touchFile(tempDir, 'pyproject.toml')
expect(detectPackageManager(tempDir)).toBe('uv')
})
it('prioritizes pnpm-lock.yaml over package.json', () => {
touchFile(tempDir, 'pnpm-lock.yaml')
touchFile(tempDir, 'package.json', '{}')
expect(detectPackageManager(tempDir)).toBe('pnpm')
})
})
describe('detectMonorepo', () => {
let tempDir: string
beforeEach(() => {
tempDir = createTempDir()
})
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true })
})
it('detects turborepo from turbo.json', () => {
touchFile(tempDir, 'turbo.json', '{"pipeline":{}}')
expect(detectMonorepo(tempDir)).toBe('turborepo')
})
it('detects pnpm-workspace from pnpm-workspace.yaml', () => {
touchFile(tempDir, 'pnpm-workspace.yaml', 'packages:\n - "apps/*"')
expect(detectMonorepo(tempDir)).toBe('pnpm-workspace')
})
it('returns none for a plain repo', () => {
touchFile(tempDir, 'package.json', '{}')
expect(detectMonorepo(tempDir)).toBe('none')
})
it('returns none for an empty directory', () => {
expect(detectMonorepo(tempDir)).toBe('none')
})
it('prioritizes turborepo when both turbo.json and pnpm-workspace.yaml exist', () => {
touchFile(tempDir, 'turbo.json', '{"pipeline":{}}')
touchFile(tempDir, 'pnpm-workspace.yaml', 'packages:\n - "apps/*"')
expect(detectMonorepo(tempDir)).toBe('turborepo')
})
})
describe('stripLocalUvSources', () => {
let tempDir: string
beforeEach(() => {
tempDir = createTempDir()
})
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true })
})
it('strips path-based source entries from pyproject.toml', () => {
const pyprojectContent = `[project]
name = "test-project"
dependencies = [
"openadapt-ml>=0.11.0",
"openadapt-consilium>=0.3.2",
]
[tool.uv.sources]
openadapt-consilium = { git = "https://github.com/OpenAdaptAI/openadapt-consilium.git" }
openadapt-ml = { path = "../openadapt-ml", editable = true }
[tool.hatch.build.targets.wheel]
packages = ["my_package"]
`
touchFile(tempDir, 'pyproject.toml', pyprojectContent)
touchFile(tempDir, 'uv.lock', 'some lock content')
stripLocalUvSources(tempDir)
const result = readFileSync(join(tempDir, 'pyproject.toml'), 'utf-8')
// Path-based source override should be removed
expect(result).not.toContain('path = "../openadapt-ml"')
// But the dependency itself in [project.dependencies] should remain
expect(result).toContain('openadapt-ml>=0.11.0')
// Git-based entry should be preserved
expect(result).toContain('openadapt-consilium')
expect(result).toContain('git = "https://github.com/')
// Other sections should be preserved
expect(result).toContain('[project]')
expect(result).toContain('[tool.hatch.build.targets.wheel]')
// uv.lock should be deleted
expect(existsSync(join(tempDir, 'uv.lock'))).toBe(false)
})
it('preserves pyproject.toml without [tool.uv.sources]', () => {
const pyprojectContent = `[project]
name = "test-project"
dependencies = ["requests>=2.28.0"]
`
touchFile(tempDir, 'pyproject.toml', pyprojectContent)
touchFile(tempDir, 'uv.lock', 'some lock content')
stripLocalUvSources(tempDir)
const result = readFileSync(join(tempDir, 'pyproject.toml'), 'utf-8')
expect(result).toBe(pyprojectContent)
// uv.lock should NOT be deleted when no changes were made
expect(existsSync(join(tempDir, 'uv.lock'))).toBe(true)
})
it('preserves pyproject.toml with only git sources', () => {
const pyprojectContent = `[project]
name = "test-project"
[tool.uv.sources]
consilium = { git = "https://github.com/example/consilium.git" }
`
touchFile(tempDir, 'pyproject.toml', pyprojectContent)
touchFile(tempDir, 'uv.lock', 'some lock content')
stripLocalUvSources(tempDir)
const result = readFileSync(join(tempDir, 'pyproject.toml'), 'utf-8')
expect(result).toBe(pyprojectContent)
// uv.lock should NOT be deleted when no path sources were stripped
expect(existsSync(join(tempDir, 'uv.lock'))).toBe(true)
})
it('handles multiple path-based sources', () => {
const pyprojectContent = `[project]
name = "test-project"
[tool.uv.sources]
pkg-a = { path = "../pkg-a", editable = true }
pkg-b = { git = "https://github.com/example/pkg-b.git" }
pkg-c = { path = "/absolute/path/to/pkg-c" }
`
touchFile(tempDir, 'pyproject.toml', pyprojectContent)
stripLocalUvSources(tempDir)
const result = readFileSync(join(tempDir, 'pyproject.toml'), 'utf-8')
expect(result).not.toContain('pkg-a')
expect(result).not.toContain('pkg-c')
expect(result).toContain('pkg-b')
expect(result).toContain('git = "https://github.com/')
})
it('does nothing when pyproject.toml does not exist', () => {
// Should not throw
expect(() => stripLocalUvSources(tempDir)).not.toThrow()
})
})
describe('runTests with real commands', () => {
let tempDir: string
beforeEach(() => {
tempDir = createTempDir()
})
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true })
})
it('handles a passing custom test (exit code 0)', () => {
// Create a trivial script that exits 0
touchFile(
tempDir,
'package.json',
JSON.stringify({
scripts: { test: 'echo "all good"' },
}),
)
const results = runTests(tempDir, 'custom', 'npm', 30)
expect(results.passed).toBe(1)
expect(results.failed).toBe(0)
expect(results.total).toBe(1)
expect(results.duration).toBeGreaterThan(0)
})
it('handles a failing custom test (exit code 1)', () => {
touchFile(
tempDir,
'package.json',
JSON.stringify({
scripts: { test: 'echo "FAILURE" && exit 1' },
}),
)
const results = runTests(tempDir, 'custom', 'npm', 30)
expect(results.passed).toBe(0)
expect(results.failed).toBe(1)
expect(results.total).toBe(1)
expect(results.failures.length).toBe(1)
})
})