Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ export class DomStack {
this.#src = src
this.#dest = dest

const copyDirs = opts?.copy ?? []
const copyDirs = (opts?.copy ?? []).map(dir => resolve(dir))

this.opts = {
...opts,
copy: copyDirs,
ignore: [
...DEFAULT_IGNORES,
basename(dest),
Expand All @@ -172,12 +173,11 @@ export class DomStack {
],
}

if (copyDirs && copyDirs.length > 0) {
if (copyDirs.length > 0) {
const absDest = resolve(this.#dest)
for (const copyDir of copyDirs) {
// Copy dirs can be in the src dir (nested builds), but not in the dest dir.
const absCopyDir = resolve(copyDir)
const relToDest = relative(absDest, absCopyDir)
const relToDest = relative(absDest, copyDir)
if (relToDest === '' || !relToDest.startsWith('..')) {
throw new Error(`copyDir ${copyDir} is within the dest directory`)
}
Expand Down
34 changes: 34 additions & 0 deletions test-cases/constructor-copy-paths/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test } from 'node:test'
import assert from 'node:assert'
import { isAbsolute } from 'node:path'
import { DomStack } from '../../index.js'

test.describe('DomStack constructor - copy path resolution', () => {
test('resolves a relative copy path to an absolute path', () => {
const ds = new DomStack('/tmp/test-src', '/tmp/test-dest', {
copy: ['some-relative-copy-dir'],
})
Comment thread
bcomnes marked this conversation as resolved.
Outdated

assert.strictEqual(ds.opts.copy.length, 1, 'one copy entry')
assert.ok(isAbsolute(ds.opts.copy[0]), `copy path should be absolute, got: "${ds.opts.copy[0]}"`)

Check failure on line 13 in test-cases/constructor-copy-paths/index.test.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, lts/*)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 13 in test-cases/constructor-copy-paths/index.test.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 23)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 13 in test-cases/constructor-copy-paths/index.test.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, lts/*)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 13 in test-cases/constructor-copy-paths/index.test.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 23)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
})

test('leaves an already-absolute copy path unchanged', () => {
const ds = new DomStack('/tmp/test-src', '/tmp/test-dest', {
copy: ['/absolute/copy/dir'],
})

assert.strictEqual(ds.opts.copy[0], '/absolute/copy/dir', 'absolute path is preserved')
})
Comment thread
bcomnes marked this conversation as resolved.
Outdated

test('resolves multiple mixed copy paths', () => {
const ds = new DomStack('/tmp/test-src', '/tmp/test-dest', {
copy: ['relative-dir', '/absolute/dir'],
})

assert.strictEqual(ds.opts.copy.length, 2, 'two copy entries')
for (const p of ds.opts.copy) {
assert.ok(isAbsolute(p), `each copy path should be absolute, got: "${p}"`)
}
})
})
Loading