Repository navigation
Expand file tree
/
Copy pathindex.test.js
More file actions
162 lines (143 loc) · 7.84 KB
/
Copy pathindex.test.js
File metadata and controls
162 lines (143 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/** @import { DomStackOpts } from './lib/builder.js' */
import { test } from 'node:test'
import assert from 'node:assert'
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { isAbsolute, resolve, join } from 'node:path'
import { tmpdir } from 'node:os'
import { DomStack, fingerprint, testBuild } from './index.js'
const tmpSrc = join(tmpdir(), 'domstack-test-src')
const tmpDest = join(tmpdir(), 'domstack-test-dest')
for (const { name, options, expected } of [
{ name: 'omitted', options: {}, expected: [] },
{ name: 'undefined', options: { ignore: undefined }, expected: [] },
{ name: 'null', options: { ignore: null }, expected: [] },
{ name: 'empty array', options: { ignore: [] }, expected: [] },
{ name: 'array', options: { ignore: ['private', '*.secret'] }, expected: ['private', '*.secret'] },
{ name: 'single string', options: { ignore: 'private' }, expected: ['private'] },
]) {
test(`DomStack constructor normalizes ${name} ignore options`, () => {
const defaults = new DomStack(tmpSrc, tmpDest).opts.ignore ?? []
// JavaScript callers have historically been able to pass null or a single string.
const ds = new DomStack(tmpSrc, tmpDest, /** @type {DomStackOpts} */ (options))
assert.deepStrictEqual(ds.opts.ignore, [...defaults, ...expected])
if (Array.isArray(options.ignore)) {
assert.deepStrictEqual(options.ignore, expected, 'the caller\'s array is not mutated')
assert.notStrictEqual(ds.opts.ignore, options.ignore)
}
})
}
test.describe('DomStack constructor - copy path resolution', () => {
test('resolves a relative copy path to an absolute path', () => {
const ds = new DomStack(tmpSrc, tmpDest, {
copy: ['some-relative-copy-dir'],
})
const copy = ds.opts.copy ?? []
assert.strictEqual(copy.length, 1, 'one copy entry')
const copyPath = /** @type {string} */ (copy[0])
assert.ok(isAbsolute(copyPath), `copy path should be absolute, got: "${copyPath}"`)
})
test('leaves an already-absolute copy path normalized', () => {
const absPath = join(tmpdir(), 'absolute', 'copy', 'dir')
const ds = new DomStack(tmpSrc, tmpDest, {
copy: [absPath],
})
const copy = ds.opts.copy ?? []
assert.strictEqual(copy[0], resolve(absPath), 'absolute path is preserved and normalized')
})
test('resolves multiple mixed copy paths', () => {
const absPath = join(tmpdir(), 'absolute', 'dir')
const ds = new DomStack(tmpSrc, tmpDest, {
copy: ['relative-dir', absPath],
})
const copy = ds.opts.copy ?? []
assert.strictEqual(copy.length, 2, 'two copy entries')
for (const p of copy) {
assert.ok(typeof p === 'string' && isAbsolute(p), `each copy path should be absolute, got: "${String(p)}"`)
}
})
})
test('exports fingerprint for global-data cacheability checks', () => {
const hash = fingerprint({ posts: [{ slug: 'hello', title: 'Hello' }] })
assert.strictEqual(typeof hash, 'string')
assert.strictEqual(hash, fingerprint({ posts: [{ title: 'Hello', slug: 'hello' }] }))
assert.strictEqual(fingerprint({ publishedAt: new Date() }), null)
})
test('copy directories only add anchored ignore patterns when inside src', async (t) => {
const outsideCopy = join(tmpdir(), 'domstack-outside-images')
const nestedCopy = join(tmpSrc, 'nested', 'legacy')
const ds = new DomStack(tmpSrc, tmpDest, { copy: [outsideCopy, nestedCopy] })
assert.ok(!ds.opts.ignore?.includes('domstack-outside-images'), 'external copy dir basename is not added to ignore')
assert.ok(!ds.opts.ignore?.includes('legacy'), 'nested copy dir bare basename is not added to ignore')
assert.ok(ds.opts.ignore?.includes('/nested/legacy'), 'nested copy dir is ignored via anchored src-relative path')
// End-to-end verification: external `--copy images` does not hide `src/blog/images/`,
// and nested `--copy src/legacy` does not hide `src/blog/legacy/`.
const root = await mkdtemp(join(import.meta.dirname, 'tmp-copy-ignore-'))
t.after(() => rm(root, { recursive: true, force: true }))
const src = join(root, 'src')
const externalImages = join(root, 'images')
const nestedLegacy = join(src, 'legacy')
await mkdir(join(src, 'blog/images'), { recursive: true })
await mkdir(join(src, 'blog/legacy'), { recursive: true })
await mkdir(externalImages, { recursive: true })
await mkdir(nestedLegacy, { recursive: true })
await writeFile(join(src, 'root.layout.js'), 'export default ({ children }) => children\n')
await writeFile(join(src, 'page.md'), '# Home\n')
await writeFile(join(src, 'blog/images/photo.txt'), 'src-image')
await writeFile(join(src, 'blog/legacy/page.md'), '# Blog legacy\n')
await writeFile(join(externalImages, 'banner.txt'), 'copied-banner')
await writeFile(join(nestedLegacy, 'archived.html'), '<p>archived</p>')
const build = await testBuild(src, { copy: [externalImages, nestedLegacy] })
t.after(() => build.cleanup())
assert.strictEqual(await build.readOutput('blog/images/photo.txt'), 'src-image')
assert.match(await build.readOutput('blog/legacy/index.html'), /Blog legacy/)
assert.strictEqual(await build.readOutput('banner.txt'), 'copied-banner')
assert.strictEqual(await build.readOutput('archived.html'), '<p>archived</p>')
})
test('copy entries support destination subpath mappings via string and object syntax', async (t) => {
const root = await mkdtemp(join(import.meta.dirname, 'tmp-copy-map-'))
t.after(() => rm(root, { recursive: true, force: true }))
const src = join(root, 'src')
const images = join(root, 'images')
const media = join(root, 'media')
await mkdir(src, { recursive: true })
await mkdir(join(images, '2026'), { recursive: true })
await mkdir(media, { recursive: true })
await writeFile(join(src, 'root.layout.js'), 'export default ({ children }) => children\n')
await writeFile(join(src, 'page.md'), '# Home\n')
await writeFile(join(images, '2026/logo.txt'), 'logo-bytes')
await writeFile(join(media, 'clip.txt'), 'clip-bytes')
const build = await testBuild(src, {
copy: [
`${images}:images`,
{ from: media, to: 'assets/media' },
],
})
t.after(() => build.cleanup())
assert.strictEqual(await build.readOutput('images/2026/logo.txt'), 'logo-bytes')
assert.strictEqual(await build.readOutput('assets/media/clip.txt'), 'clip-bytes')
})
test('dest only adds an anchored ignore pattern when inside src', async (t) => {
const outsideDs = new DomStack(tmpSrc, tmpDest)
assert.ok(!outsideDs.opts.ignore?.includes('domstack-test-dest'), 'external dest basename is not added to ignore')
const nestedDest = join(tmpSrc, 'build', 'public')
const nestedDs = new DomStack(tmpSrc, nestedDest)
assert.ok(!nestedDs.opts.ignore?.includes('public'), 'nested dest bare basename is not added to ignore')
assert.ok(nestedDs.opts.ignore?.includes('/build/public'), 'nested dest is ignored via anchored src-relative path')
// End-to-end verification: external `dest = public` does not hide `src/docs/public/page.md`,
// and nested `dest = src/public` does not hide `src/docs/public/page.md`.
const root = await mkdtemp(join(import.meta.dirname, 'tmp-dest-ignore-'))
t.after(() => rm(root, { recursive: true, force: true }))
const src = join(root, 'src')
const externalDest = join(root, 'public')
await mkdir(join(src, 'docs/public'), { recursive: true })
await writeFile(join(src, 'root.layout.js'), 'export default ({ children }) => children\n')
await writeFile(join(src, 'page.md'), '# Home\n')
await writeFile(join(src, 'docs/public/page.md'), '# Docs public\n')
const externalBuild = new DomStack(src, externalDest)
await externalBuild.build()
assert.match(await readFile(join(externalDest, 'docs/public/index.html'), 'utf8'), /Docs public/)
const nestedBuildDest = join(src, 'public')
const nestedBuild = new DomStack(src, nestedBuildDest)
await nestedBuild.build()
assert.match(await readFile(join(nestedBuildDest, 'docs/public/index.html'), 'utf8'), /Docs public/)
})