OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / test / clone-command.test.js

48 lines · javascript ref:
// pgh clone — gip-first clone path with legacy-URL fallback (P6).

import test from 'brittle'
import { run } from '../lib/commands/clone.js'

function fakeClient (getRepo) {
  return { async call (m, p) { if (m === 'get-repo') return getRepo(p); throw new Error('unexpected ' + m) } }
}
function recordExec () {
  const calls = []
  return { calls, exec: (file, args) => { calls.push({ file, args }) } }
}

test('clones from the local gip-materialized bare repo when present', async (t) => {
  const client = fakeClient(() => ({ name: 'gip-test', punchUrl: 'punch://srv/gip-test', info: { path: '/home/u/git-repos/gip-test.git' } }))
  const r = recordExec()
  await run(client, ['gip-test'], { exec: r.exec, exists: (p) => p === '/home/u/git-repos/gip-test.git' })
  t.alike(r.calls, [{ file: 'git', args: ['clone', '--', '/home/u/git-repos/gip-test.git'] }], 'cloned from local path, no upload-pack')
})

test('passes a dest through to git clone', async (t) => {
  const client = fakeClient(() => ({ info: { path: '/r/gip-test.git' } }))
  const r = recordExec()
  await run(client, ['gip-test', 'mydir'], { exec: r.exec, exists: () => true })
  t.alike(r.calls[0].args, ['clone', '--', '/r/gip-test.git', 'mydir'])
})

test('falls back to the punch URL when not materialized locally', async (t) => {
  const client = fakeClient(() => ({ punchUrl: 'punch://srv/gip-test', info: { path: '/r/gip-test.git' } }))
  const r = recordExec()
  await run(client, ['gip-test'], { exec: r.exec, exists: () => false }) // path not present
  t.alike(r.calls, [{ file: 'git', args: ['clone', '--', 'punch://srv/gip-test'] }], 'legacy URL clone')
})

test('errors when neither local repo nor a clone URL is available', async (t) => {
  const client = fakeClient(() => ({ info: {} }))
  const r = recordExec()
  await t.exception(run(client, ['gip-test'], { exec: r.exec, exists: () => false }), /join.*first|no clone URL/i)
  t.is(r.calls.length, 0)
})

test('rejects a clone URL with an unrecognised scheme', async (t) => {
  const client = fakeClient(() => ({ punchUrl: '--upload-pack=/evil', info: {} }))
  const r = recordExec()
  await t.exception(run(client, ['gip-test'], { exec: r.exec, exists: () => false }), /unrecognised scheme/i)
  t.is(r.calls.length, 0)
})