Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { listSpaces } from './space'
import { dataDir, datacoreLib } from './paths'
import { findPython } from './python'

const DATA_DIR = dataDir()
// Resolved per call, like upgrade.ts: freezing it at import made the tests
// read whichever installation the first importer happened to see.
const DATA_DIR = () => dataDir()

/**
* Locate the ledger transport, or null on an installation that predates it.
Expand All @@ -22,7 +24,7 @@ const DATA_DIR = dataDir()
function findLedgerTransport(): { python: string; script: string } | null {
const script = join(datacoreLib(), 'ledger_transport.py')
if (!existsSync(script)) return null
const python = findPython(DATA_DIR)
const python = findPython(DATA_DIR())
if (!python) return null
return { python, script }
}
Expand Down Expand Up @@ -283,8 +285,8 @@ function getGitRepos(): Array<{ path: string; name: string }> {
const repos: Array<{ path: string; name: string }> = []

// Data root
if (isGitRepo(DATA_DIR)) {
repos.push({ path: DATA_DIR, name: 'Data' })
if (isGitRepo(DATA_DIR())) {
repos.push({ path: DATA_DIR(), name: 'Data' })
}

// All spaces with git
Expand Down
56 changes: 42 additions & 14 deletions tests/sync.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
/**
* Tests for git sync utilities.
*
* Runs against a fixture installation, not the developer's ~/Data: against the
* live one, statusAll ran `git status` over every real space and timed out at
* 5s often enough to block a publish on the publisher's disk state.
*/

import { describe, it, expect } from 'bun:test'
import { statusAll, getRepoStatus } from '../src/lib/sync'
import { describe, it, expect, beforeAll, afterAll } from 'bun:test'
import { mkdtempSync, mkdirSync, rmSync } from 'fs'
import { execFileSync } from 'child_process'
import { tmpdir } from 'os'
import { join } from 'path'
import { statusAll, getRepoStatus } from '../src/lib/sync'

let root: string
const savedRoot = process.env.DATACORE_ROOT

const DATA_DIR = join(process.env.HOME || '~', 'Data')
function gitInit(dir: string) {
mkdirSync(dir, { recursive: true })
execFileSync('git', ['init', '-q', '-b', 'main', dir])
execFileSync('git', ['-C', dir, '-c', 'core.hooksPath=/dev/null', '-c', 'user.email=t@example.com', '-c', 'user.name=T',
'commit', '-q', '--allow-empty', '-m', 'init'])
}

beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'dc-cli-sync-'))
gitInit(root)
gitInit(join(root, '0-personal'))
process.env.DATACORE_ROOT = root
})

afterAll(() => {
if (savedRoot === undefined) delete process.env.DATACORE_ROOT
else process.env.DATACORE_ROOT = savedRoot
rmSync(root, { recursive: true, force: true })
})

describe('Sync Utilities', () => {
describe('statusAll', () => {
it('returns an array', () => {
const statuses = statusAll()
expect(Array.isArray(statuses)).toBe(true)
it('finds the root repo and each space repo', () => {
const paths = statusAll().map((s) => s.path).sort()
expect(paths).toEqual([root, join(root, '0-personal')].sort())
})

it('statuses have required properties', () => {
const statuses = statusAll()
expect(statuses.length).toBeGreaterThan(0)
for (const status of statuses) {
expect(typeof status.path).toBe('string')
expect(typeof status.name).toBe('string')
Expand All @@ -31,17 +60,16 @@ describe('Sync Utilities', () => {

describe('getRepoStatus', () => {
it('returns null for non-git directory', () => {
const status = getRepoStatus('/tmp')
expect(status).toBeNull()
const dir = mkdtempSync(join(tmpdir(), 'dc-cli-nogit-'))
expect(getRepoStatus(dir)).toBeNull()
rmSync(dir, { recursive: true, force: true })
})

it('returns status for git directory', () => {
const status = getRepoStatus(DATA_DIR)
// Data directory may or may not be a git repo
if (status) {
expect(typeof status.branch).toBe('string')
expect(typeof status.dirty).toBe('boolean')
}
const status = getRepoStatus(root)
expect(status).not.toBeNull()
expect(status!.branch).toBe('main')
expect(status!.dirty).toBe(false)
})
})
})
Loading