Skip to content

The agent that grows with you

1.5 KB
Raw Blame
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, expect, it, vi } from 'vitest'

import { run } from '../.github/actions/setup-pm/prune/index.mjs'

const directories = []
afterEach(() => {
  for (const path of directories.splice(0)) rmSync(path, { recursive: true, force: true })
})

it('prunes the saved uv cache to the lock at teardown, never during registration', () => {
  const directory = mkdtempSync(join(tmpdir(), 'pm-post-'))
  directories.push(directory)
  const state = join(directory, 'state')
  const execute = vi.fn(() => ({ status: 0 }))
  const cache = join(directory, 'cache with spaces')
  const python = join(directory, 'prepared Python')
  const lockSource = join(directory, 'checkout')
  run({ GITHUB_STATE: state, INPUT_PYTHON: python, INPUT_CACHE: cache, 'INPUT_LOCK-SOURCE': lockSource }, execute)
  expect(execute).not.toHaveBeenCalled()
  const saved = Object.fromEntries(readFileSync(state, 'utf8').trim().split('\n').map(line => {
    const index = line.indexOf('=')
    return [`STATE_${line.slice(0, index)}`, line.slice(index + 1)]
  }))
  run({ ...saved, UV_CACHE_DIR: 'a later unrelated cache' }, execute)
  expect(execute).toHaveBeenCalledWith(python, ['-m', 'pm.build_env', '--exact-lock', '--cache', cache, '--lock-source', lockSource], expect.objectContaining({
    env: expect.objectContaining(saved),
    stdio: 'inherit',
  }))
  execute.mockReturnValueOnce({ status: 1 })
  expect(() => run(saved, execute)).toThrow('PM cache prune failed')
})