Skip to content

Commit

Permalink
unit test crawlPlusFiles() (#2225)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Feb 26, 2025
1 parent a187344 commit 3706364
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@brillout/test-types": "^0.1.13",
"playwright": "^1.50.1",
"prettier": "^3.2.5",
"vitest": "^3.0.5"
"vitest": "^3.0.7"
},
"pnpm": {
"overrides": {
Expand Down
113 changes: 59 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
scriptFileExtensionList,
createDebugger,
deepEqual,
assertUsage
assertUsage,
assertFilePathAbsoluteFilesystem,
assertWarning
} from '../../../../utils.js'
import path from 'path'
import { glob } from 'tinyglobby'
Expand All @@ -34,6 +36,7 @@ async function crawlPlusFiles(
outDirAbsoluteFilesystem: null | string
): Promise<{ filePathAbsoluteUserRootDir: string }[]> {
assertPosixPath(userRootDir)
assertFilePathAbsoluteFilesystem(userRootDir)

//*/
const outDirRelativeFromUserRootDir = null as string | null
Expand Down Expand Up @@ -65,7 +68,15 @@ async function crawlPlusFiles(
: // Fallback to tinyglobby for users that dynamically generate plus files. (Assuming that no plus file is found because of the user's .gitignore list.)
filesGlob
assert(files)
if (debug.isActivated) assert(deepEqual(filesGlob, filesGit), "Git and glob results aren't matching.")
if (debug.isActivated) {
assert(filesGit)
assert(filesGlob)
assertWarning(
deepEqual(filesGlob.slice().sort(), filesGit.slice().sort()),
"Git and glob results aren't matching.",
{ onlyOnce: false }
)
}

// Filter build files
files = files.filter((filePath) => !isTemporaryBuildFile(filePath))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect, describe, it, assert } from 'vitest'
import path from 'path'
import fs from 'fs'
// process.env.DEBUG = 'vike:crawl'
const { crawlPlusFiles } = await import('../crawlPlusFiles')
import { fileURLToPath } from 'url'
const __dirname_ = path.dirname(fileURLToPath(import.meta.url))
const userRootDir = path.join(__dirname_, './test-file-structure')

describe('crawlPlusFiles()', () => {
it('works', async ({ onTestFinished }) => {
const { clean } = createFiles([
'pages/about/+bla.mdx',
'pages/git-ignored/+bla.mdx',
'pages/about/+ignored.telefunc.ts',
'pages/about/+ignored.generated.js',
'pages/ejected/+ignored.js',
'pages/node_modules/+ignored.js'
])
onTestFinished(() => clean())

const filesWithGit = await crawl()
expect(filesWithGit).toMatchInlineSnapshot(`
[
"/+config.js",
"/pages/+config.js",
"/pages/about/+bla.mdx",
]
`)
assert(!JSON.stringify(filesWithGit).includes('ignored'))

process.env.VIKE_CRAWL = '{git:false}'
const filesWithGlob = await crawl()
expect(filesWithGlob).toMatchInlineSnapshot(`
[
"/+config.js",
"/pages/+config.js",
"/pages/about/+bla.mdx",
"/pages/git-ignored/+bla.mdx",
]
`)
})
})

async function crawl() {
return normalize(await crawlPlusFiles(userRootDir, null))
}

function normalize(files: Awaited<ReturnType<typeof crawlPlusFiles>>) {
return files.map((f) => f.filePathAbsoluteUserRootDir).sort()
}

function createFiles(files: string[]) {
const filePaths = files.map((file) => path.join(userRootDir, file))

// Create empty files
filePaths.forEach((filePath) => {
fs.mkdirSync(path.dirname(filePath), { recursive: true })
fs.writeFileSync(filePath, '')
})

return {
clean: () => {
filePaths.forEach((filePath) => {
assert(fs.existsSync(filePath))
fs.unlinkSync(filePath) // Remove filePath
})
removeEmptyDirectories(userRootDir)
}
}
}

function removeEmptyDirectories(dirPath: string): void {
// Read the directory contents
const files = fs.readdirSync(dirPath)

// Iterate through the files and subdirectories
for (const file of files) {
const fullPath = path.join(dirPath, file)

// Check if it's a directory
if (fs.statSync(fullPath).isDirectory()) {
// Recursively clean up the subdirectory
removeEmptyDirectories(fullPath)
}
}

// Re-check the directory; remove it if it's now empty
if (fs.readdirSync(dirPath).length === 0) {
fs.rmdirSync(dirPath)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pages/git-ignored/
Loading

0 comments on commit 3706364

Please sign in to comment.