Skip to content

Commit

Permalink
Fix tests on windows (sourcegraph#6348)
Browse files Browse the repository at this point in the history
## Test plan

1. Run the following command: `pnpm run test`
a) in the root cody workspace dir
b) in the agent dir

1. Run the following command: `pnpm run test:unit`
a) in the root cody workspace dir
b) in the agent dir

In every case before running tests it should rebuild an agent dir.

## Changelog

Fixes errors like this:

https://github.com/sourcegraph/cody/actions/runs/12316198852/job/34378863340
  • Loading branch information
pkukielka authored Dec 16, 2024
1 parent 7153b02 commit d7460c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
20 changes: 2 additions & 18 deletions agent/src/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,7 @@ interface TestClientParams {
extraConfiguration?: Record<string, any>
}

let isBuilt = false
export function buildAgentBinary(): void {
if (isBuilt) {
return
}
isBuilt = true
// Bundle the agent. When running `pnpm run test`, vitest doesn't re-run this step.
//
// ! If this line fails when running unit tests, chances are that the error is being swallowed.
// To see the full error, run this file in isolation:
//
// pnpm test agent/src/index.test.ts
execSync('pnpm run build:for-tests ', {
cwd: getAgentDir(),
stdio: 'inherit',
})

export function setupRecording(): void {
const mayRecord =
process.env.CODY_RECORDING_MODE === 'record' || process.env.CODY_RECORD_IF_MISSING === 'true'
if (mayRecord) {
Expand Down Expand Up @@ -135,7 +119,7 @@ export class TestClient extends MessageHandler {
private secrets = new AgentStatelessSecretStorage()
private extensionConfigurationDuringInitialization: ExtensionConfiguration | undefined
public static create({ bin = 'node', ...params }: TestClientParams): TestClient {
buildAgentBinary()
setupRecording()
const agentDir = getAgentDir()
const recordingDirectory = path.join(agentDir, 'recordings')
const agentScript = path.join(agentDir, 'dist', 'index.js')
Expand Down
3 changes: 1 addition & 2 deletions agent/src/cli/command-chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { afterAll, beforeAll, expect, it } from 'vitest'
import YAML from 'yaml'
import { startPollyRecording } from '../../../vscode/src/testutils/polly'
import { TESTING_CREDENTIALS } from '../../../vscode/src/testutils/testing-credentials'
import { buildAgentBinary, getAgentDir } from '../TestClient'
import { getAgentDir } from '../TestClient'
import { TestWorkspace } from '../TestWorkspace'
import { Streams, StringBufferStream } from './Streams'
import { isWindows } from './command-bench/isWindows'
Expand Down Expand Up @@ -82,7 +82,6 @@ describe('cody chat', () => {

beforeAll(() => {
tmp.beforeAll()
buildAgentBinary()
polly = startPollyRecording({
recordingName: 'cody-chat',
keepUnusedRecordings: process.env.CODY_KEEP_UNUSED_RECORDINGS === 'true',
Expand Down
7 changes: 7 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globalSetup: ['./vitest.setup.ts'],
},
})
13 changes: 13 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { execSync } from 'node:child_process'
import path from 'node:path'

function buildAgentForTests() {
execSync('pnpm run build:for-tests', {
cwd: path.join(__dirname, 'agent'),
stdio: 'inherit',
})
}

export default function setup() {
buildAgentForTests()
}

0 comments on commit d7460c1

Please sign in to comment.