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
35 changes: 35 additions & 0 deletions packages/cli/src/services/__tests__/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import path from 'node:path'
import { describe, it, expect } from 'vitest'
import fs from 'node:fs/promises'
import os from 'node:os'

import {
pathToPosix,
isFileSync,
getPlaywrightVersionFromPackage,
findRegexFiles,
} from '../util'

describe('util', () => {
Expand Down Expand Up @@ -46,4 +49,36 @@ describe('util', () => {
expect(version).toMatch(/^\d+\.\d+\.\d+/)
})
})

describe('findRegexFiles()', () => {
it('should correctly handle relative directory paths', async () => {
// Create a temporary directory structure
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-util-'))
const testSubDir = path.join(tmpDir, 'testdir')
await fs.mkdir(testSubDir)
await fs.writeFile(path.join(testSubDir, 'file1.ts'), 'content')
await fs.writeFile(path.join(testSubDir, 'file2.js'), 'content')

try {
// Change to temp directory to test relative paths
const originalCwd = process.cwd()
process.chdir(tmpDir)

// Call with relative directory (no leading ./ or /)
// findRegexFiles internally uses path.relative, so it needs to resolve the directory
const files = await findRegexFiles('testdir', /.*/, [])

// Should get correct relative paths, not paths like ../../absolute/path/...
expect(files).toContain('file1.ts')
expect(files).toContain('file2.js')
expect(files.every(p => !p.startsWith('..'))).toBe(true)

// Restore original cwd
process.chdir(originalCwd)
} finally {
// Cleanup
await fs.rm(tmpDir, { recursive: true, force: true })
}
})
})
})
5 changes: 3 additions & 2 deletions packages/cli/src/services/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ export async function loadPlaywrightProjectFiles (

export async function findRegexFiles (directory: string, regex: RegExp, ignorePattern: string[]):
Promise<string[]> {
const files = await findFilesWithPattern(directory, '**/*.{js,ts,mjs}', ignorePattern)
return files.filter(file => regex.test(file)).map(file => pathToPosix(path.relative(directory, file)))
const resolvedDirectory = path.resolve(directory)
const files = await findFilesWithPattern(resolvedDirectory, '**/*.{js,ts,mjs}', ignorePattern)
return files.filter(file => regex.test(file)).map(file => pathToPosix(path.relative(resolvedDirectory, file)))
}

export async function findFilesWithPattern (
Expand Down