mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Remove duplicated and constant-only tests
This commit is contained in:
1 parent
ee4ef8217d
commit
71bde0378a
8 files changed
+17
-366
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { spawn } from 'child_process'
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'fs'
|
||||
import { mkdtempSync, readFileSync, rmSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import * as http from 'http'
|
||||
@@ -213,153 +213,3 @@ describe('askpass-main', () => {
|
||||
})
|
||||
})
|
||||
|
||||
const askpassEnvKeys = [
|
||||
'VSCODE_GIT_ASKPASS_PIPE',
|
||||
'VSCODE_GIT_ASKPASS_TYPE',
|
||||
'VSCODE_GIT_IPC_HANDLE',
|
||||
'OCM_GIT_REPO_ID',
|
||||
'OCM_GIT_REPO_CWD',
|
||||
] as const
|
||||
|
||||
async function runAskpassInProcess(overrides: Record<string, string | undefined>): Promise<{ code: number | undefined; output: string | undefined }> {
|
||||
const nextEnv = buildEnv(overrides)
|
||||
const previousEnv: Record<string, string | undefined> = {}
|
||||
for (const key of askpassEnvKeys) {
|
||||
previousEnv[key] = process.env[key]
|
||||
const next = nextEnv[key]
|
||||
if (next === undefined) {
|
||||
delete process.env[key]
|
||||
} else {
|
||||
process.env[key] = next
|
||||
}
|
||||
}
|
||||
|
||||
let resolveExit: (code: number | undefined) => void = () => {}
|
||||
const exitPromise = new Promise<number | undefined>((resolve) => {
|
||||
resolveExit = resolve
|
||||
})
|
||||
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const originalExit = process.exit
|
||||
Object.defineProperty(process, 'exit', {
|
||||
value: (code?: string | number | null) => {
|
||||
const exitCode = z.number().safeParse(code)
|
||||
resolveExit(exitCode.success ? exitCode.data : undefined)
|
||||
},
|
||||
configurable: true,
|
||||
writable: true,
|
||||
})
|
||||
|
||||
vi.resetModules()
|
||||
try {
|
||||
await import('../../scripts/askpass-main')
|
||||
const code = await exitPromise
|
||||
return { code, output: existsSync(outputPath) ? readFileSync(outputPath, 'utf-8') : undefined }
|
||||
} finally {
|
||||
Object.defineProperty(process, 'exit', { value: originalExit, configurable: true, writable: true })
|
||||
errorSpy.mockRestore()
|
||||
for (const key of askpassEnvKeys) {
|
||||
const previous = previousEnv[key]
|
||||
if (previous === undefined) {
|
||||
delete process.env[key]
|
||||
} else {
|
||||
process.env[key] = previous
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('askpass-main in-process', () => {
|
||||
it('exits 1 when the askpass pipe is missing', async () => {
|
||||
const result = await runAskpassInProcess({})
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBeUndefined()
|
||||
})
|
||||
|
||||
it('exits 1 when the askpass type is missing', async () => {
|
||||
const result = await runAskpassInProcess({ VSCODE_GIT_ASKPASS_PIPE: outputPath })
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBeUndefined()
|
||||
})
|
||||
|
||||
it('exits 1 for an invalid askpass type', async () => {
|
||||
const result = await runAskpassInProcess({ VSCODE_GIT_ASKPASS_PIPE: outputPath, VSCODE_GIT_ASKPASS_TYPE: 'ftp' })
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBeUndefined()
|
||||
})
|
||||
|
||||
it('writes a newline and exits 0 when no IPC handle is configured', async () => {
|
||||
const result = await runAskpassInProcess({ VSCODE_GIT_ASKPASS_PIPE: outputPath, VSCODE_GIT_ASKPASS_TYPE: 'https' })
|
||||
|
||||
expect(result.code).toBe(0)
|
||||
expect(result.output).toBe('\n')
|
||||
})
|
||||
|
||||
it('writes the handler result and exits 0 on IPC success', async () => {
|
||||
const server = await startIPCServer()
|
||||
server.registerHandler('askpass', { handle: async () => 'in-process-token' })
|
||||
|
||||
const result = await runAskpassInProcess({
|
||||
VSCODE_GIT_ASKPASS_PIPE: outputPath,
|
||||
VSCODE_GIT_ASKPASS_TYPE: 'https',
|
||||
VSCODE_GIT_IPC_HANDLE: server.ipcHandlePath,
|
||||
})
|
||||
|
||||
expect(result.code).toBe(0)
|
||||
expect(result.output).toBe('in-process-token\n')
|
||||
})
|
||||
|
||||
it('writes a newline and exits 0 when the handler returns an empty body', async () => {
|
||||
const server = await startIPCServer()
|
||||
server.registerHandler('askpass', { handle: async () => undefined })
|
||||
|
||||
const result = await runAskpassInProcess({
|
||||
VSCODE_GIT_ASKPASS_PIPE: outputPath,
|
||||
VSCODE_GIT_ASKPASS_TYPE: 'https',
|
||||
VSCODE_GIT_IPC_HANDLE: server.ipcHandlePath,
|
||||
})
|
||||
|
||||
expect(result.code).toBe(0)
|
||||
expect(result.output).toBe('\n')
|
||||
})
|
||||
|
||||
it('writes a newline and exits 1 when the IPC server returns a non-200 status', async () => {
|
||||
const server = await startIPCServer()
|
||||
|
||||
const result = await runAskpassInProcess({
|
||||
VSCODE_GIT_ASKPASS_PIPE: outputPath,
|
||||
VSCODE_GIT_ASKPASS_TYPE: 'https',
|
||||
VSCODE_GIT_IPC_HANDLE: server.ipcHandlePath,
|
||||
})
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBe('\n')
|
||||
})
|
||||
|
||||
it('writes a newline and exits 1 when the response body is not JSON', async () => {
|
||||
const socketPath = join(dir, 'raw-in-process.sock')
|
||||
await startRawServer(socketPath, 'not-json')
|
||||
|
||||
const result = await runAskpassInProcess({
|
||||
VSCODE_GIT_ASKPASS_PIPE: outputPath,
|
||||
VSCODE_GIT_ASKPASS_TYPE: 'https',
|
||||
VSCODE_GIT_IPC_HANDLE: socketPath,
|
||||
})
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBe('\n')
|
||||
})
|
||||
|
||||
it('writes a newline and exits 1 when the IPC connection fails', async () => {
|
||||
const result = await runAskpassInProcess({
|
||||
VSCODE_GIT_ASKPASS_PIPE: outputPath,
|
||||
VSCODE_GIT_ASKPASS_TYPE: 'https',
|
||||
VSCODE_GIT_IPC_HANDLE: join(dir, 'missing-in-process.sock'),
|
||||
})
|
||||
|
||||
expect(result.code).toBe(1)
|
||||
expect(result.output).toBe('\n')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user