Skip to content
Open
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
50 changes: 48 additions & 2 deletions src/features/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import * as tinyglobby from 'tinyglobby'
import { describe, expect, test, vi } from 'vitest'
import { writeFixtures } from '../../tests/utils.ts'
import { toObjectEntry } from './entry.ts'

Expand Down Expand Up @@ -28,20 +29,65 @@ describe('toObjectEntry', () => {
bar: 'bar.ts',
nonexistent: 'nonexistent',
})
expect(Object.keys(result)).toEqual(['foo', 'bar', 'nonexistent'])
})

test('object entry', async (context) => {
const { testDir } = await writeFixtures(context, {
'index.ts': '',
})
const [result] = await toObjectEntry(
{ main: 'index.ts', nonexistent: 'nonexistent' },
{ nonexistent: 'nonexistent', main: 'index.ts' },
testDir,
)
expect(result).toEqual({
main: 'index.ts',
nonexistent: 'nonexistent',
})
expect(Object.keys(result)).toEqual(['nonexistent', 'main'])
})

test('glob entry order is independent of filesystem traversal order', async (context) => {
const { testDir } = await writeFixtures(context, {
'src/a/index.ts': '',
'src/b/index.ts': '',
})
const a = path.join(testDir, 'src/a/index.ts')
const b = path.join(testDir, 'src/b/index.ts')
const glob = vi
.spyOn(tinyglobby, 'glob')
.mockResolvedValueOnce([b, a])
.mockResolvedValueOnce([a, b])
context.onTestFinished(() => glob.mockRestore())

const [first] = await toObjectEntry('src/**/*.ts', testDir)
const [second] = await toObjectEntry('src/**/*.ts', testDir)

expect(Object.entries(first)).toEqual(Object.entries(second))
expect(Object.keys(first)).toEqual(['a/index', 'b/index'])
})

test('object glob entries are sorted within each key', async (context) => {
const { testDir } = await writeFixtures(context, {
'src/a/index.ts': '',
'src/b/index.ts': '',
})
const glob = vi
.spyOn(tinyglobby, 'glob')
.mockResolvedValueOnce(['src/b/index.ts', 'src/a/index.ts'])
.mockResolvedValueOnce(['src/a/index.ts', 'src/b/index.ts'])
context.onTestFinished(() => glob.mockRestore())

const entry = {
z: 'z.ts',
'lib/*': 'src/**/*.ts',
a: 'a.ts',
}
const [first] = await toObjectEntry(entry, testDir)
const [second] = await toObjectEntry(entry, testDir)

expect(Object.entries(first)).toEqual(Object.entries(second))
expect(Object.keys(first)).toEqual(['z', 'lib/a/index', 'lib/b/index', 'a'])
})

test('object entry with globs', async (context) => {
Expand Down
22 changes: 13 additions & 9 deletions src/features/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ async function resolveObjectEntry(
)
}

return files.map((file) => [
slash(
key.replaceAll(
'*',
stripExtname(path.relative(valueGlobBase, file)),
return files
.toSorted()
.map((file) => [
slash(
key.replaceAll(
'*',
stripExtname(path.relative(valueGlobBase, file)),
),
),
),
path.resolve(cwd, file),
])
path.resolve(cwd, file),
])
},
),
)
Expand Down Expand Up @@ -159,7 +161,9 @@ async function resolveArrayEntry(
expandDirectories: false,
absolute: true,
})
).map((file) => path.resolve(file))
)
.map((file) => path.resolve(file))
.toSorted()
} else {
resolvedEntries = stringEntries
}
Expand Down
Loading