Files
pnpm/lockfile/fs/test/yamlDocuments.test.ts
Zoltan Kochan 31858c544b refactor: merge env lockfile into pnpm-lock.yaml (#10964)
Instead of a separate pnpm-lock.env.yaml file, the env lockfile
(configDependencies and packageManagerDependencies) is now stored as
the first YAML document in pnpm-lock.yaml, separated by `---`.

The combined file starts with `---\n` when an env document is present,
allowing pnpm to check just the first 4 bytes to know whether
the file contains an env document. Reading uses streaming I/O that
stops as soon as the document separator is found, avoiding parsing
of the full lockfile.

Writing preserves both documents: when the env lockfile is updated
the main lockfile portion is kept, and vice versa.
2026-03-15 01:44:20 +01:00

85 lines
3.1 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { temporaryDirectory } from 'tempy'
import {
extractMainDocument,
streamReadFirstYamlDocument,
} from '../lib/yamlDocuments.js'
describe('streamReadFirstYamlDocument', () => {
test('returns the first document content from a two-document file', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
fs.writeFileSync(filePath, '---\nfoo: bar\n---\nlockfileVersion: 9.0\n')
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBe('foo: bar')
})
test('returns null for a file that does not start with ---', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
fs.writeFileSync(filePath, 'lockfileVersion: 9.0\n')
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBeNull()
})
test('returns null for a non-existent file', async () => {
const dir = temporaryDirectory()
const result = await streamReadFirstYamlDocument(path.join(dir, 'nonexistent.yaml'))
expect(result).toBeNull()
})
test('returns null when file starts with --- but has no second separator', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
fs.writeFileSync(filePath, '---\nfoo: bar\n')
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBeNull()
})
test('handles file with BOM prefix', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
fs.writeFileSync(filePath, '\uFEFF---\nfoo: bar\n---\nlockfileVersion: 9.0\n')
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBe('foo: bar')
})
test('returns null for empty file', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
fs.writeFileSync(filePath, '')
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBeNull()
})
test('returns multiline first document content', async () => {
const dir = temporaryDirectory()
const filePath = path.join(dir, 'test.yaml')
const envContent = 'lockfileVersion: env-1.0\nimporters:\n .:\n foo: bar'
fs.writeFileSync(filePath, `---\n${envContent}\n---\nlockfileVersion: 9.0\n`)
const result = await streamReadFirstYamlDocument(filePath)
expect(result).toBe(envContent)
})
})
describe('extractMainDocument', () => {
test('returns entire content when it does not start with ---', () => {
const content = 'lockfileVersion: 9.0\npackages: {}\n'
expect(extractMainDocument(content)).toBe(content)
})
test('returns empty string when content starts with --- but has no separator', () => {
const content = '---\nfoo: bar\n'
expect(extractMainDocument(content)).toBe('')
})
test('returns the second document from a combined file', () => {
const mainContent = 'lockfileVersion: 9.0\npackages: {}\n'
const combined = `---\nfoo: bar\n---\n${mainContent}`
expect(extractMainDocument(combined)).toBe(mainContent)
})
})