|
| 1 | +let test = require('tape') |
| 2 | +let { join } = require('path') |
| 3 | +let { mkdirSync, writeFileSync, rmSync, globSync, statSync } = require('node:fs') |
| 4 | +let { pathToUnix } = require('@architect/utils') |
| 5 | + |
| 6 | +/** |
| 7 | + * Feature: migrate-to-fs-globsync, Property 1: Directory exclusion correctness |
| 8 | + * Validates: Requirements 2.2 |
| 9 | + * |
| 10 | + * Property: For any glob pattern that requires directory exclusion, |
| 11 | + * all returned paths should be files and not directories |
| 12 | + */ |
| 13 | + |
| 14 | +test('Property test: Directory exclusion correctness (100 iterations)', t => { |
| 15 | + const iterations = 100 |
| 16 | + const testRoot = join(process.cwd(), '.test-property-temp') |
| 17 | + |
| 18 | + // Clean up any previous test artifacts |
| 19 | + try { |
| 20 | + rmSync(testRoot, { recursive: true, force: true }) |
| 21 | + } |
| 22 | + catch { |
| 23 | + // Ignore cleanup errors |
| 24 | + } |
| 25 | + |
| 26 | + let passedIterations = 0 |
| 27 | + let failedIterations = [] |
| 28 | + |
| 29 | + for (let i = 0; i < iterations; i++) { |
| 30 | + const testDir = join(testRoot, `iteration-${i}`) |
| 31 | + |
| 32 | + try { |
| 33 | + // Create a test directory structure with varying complexity |
| 34 | + mkdirSync(testDir, { recursive: true }) |
| 35 | + |
| 36 | + // Generate random structure: files and directories |
| 37 | + const numFiles = Math.floor(Math.random() * 5) + 1 |
| 38 | + const numDirs = Math.floor(Math.random() * 3) + 1 |
| 39 | + |
| 40 | + // Create files |
| 41 | + for (let f = 0; f < numFiles; f++) { |
| 42 | + const fileName = `file-${i}-${f}.txt` |
| 43 | + writeFileSync(join(testDir, fileName), `content ${i}-${f}`) |
| 44 | + } |
| 45 | + |
| 46 | + // Create directories (some empty, some with files) |
| 47 | + for (let d = 0; d < numDirs; d++) { |
| 48 | + const dirName = `dir-${i}-${d}` |
| 49 | + const dirPath = join(testDir, dirName) |
| 50 | + mkdirSync(dirPath, { recursive: true }) |
| 51 | + |
| 52 | + // Randomly add files to some directories |
| 53 | + if (Math.random() > 0.5) { |
| 54 | + const nestedFile = `nested-${i}-${d}.txt` |
| 55 | + writeFileSync(join(dirPath, nestedFile), `nested content ${i}-${d}`) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Apply the same filtering logic as in src/static/publish/index.js |
| 60 | + let path = pathToUnix(testDir + '/**/*') |
| 61 | + let globbed = globSync(path) |
| 62 | + |
| 63 | + // Filter out directories (this is the logic we're testing) |
| 64 | + let filtered = globbed.filter(filePath => { |
| 65 | + try { |
| 66 | + return statSync(filePath).isFile() |
| 67 | + } |
| 68 | + catch { |
| 69 | + return false |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + // Property check: All filtered results must be files, not directories |
| 74 | + let allAreFiles = true |
| 75 | + let failedPath = null |
| 76 | + |
| 77 | + for (let filePath of filtered) { |
| 78 | + try { |
| 79 | + const stats = statSync(filePath) |
| 80 | + if (!stats.isFile()) { |
| 81 | + allAreFiles = false |
| 82 | + failedPath = filePath |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + catch { |
| 87 | + // If we can't stat it, it shouldn't have been in the results |
| 88 | + allAreFiles = false |
| 89 | + failedPath = filePath |
| 90 | + break |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (allAreFiles) { |
| 95 | + passedIterations++ |
| 96 | + } |
| 97 | + else { |
| 98 | + failedIterations.push({ |
| 99 | + iteration: i, |
| 100 | + failedPath, |
| 101 | + message: `Found non-file in filtered results: ${failedPath}`, |
| 102 | + }) |
| 103 | + } |
| 104 | + } |
| 105 | + catch (err) { |
| 106 | + failedIterations.push({ |
| 107 | + iteration: i, |
| 108 | + error: err.message, |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Clean up test artifacts |
| 114 | + try { |
| 115 | + rmSync(testRoot, { recursive: true, force: true }) |
| 116 | + } |
| 117 | + catch { |
| 118 | + // Ignore cleanup errors |
| 119 | + } |
| 120 | + |
| 121 | + // Report results |
| 122 | + t.equal(passedIterations, iterations, |
| 123 | + `All ${iterations} iterations should pass directory exclusion property`) |
| 124 | + |
| 125 | + if (failedIterations.length > 0) { |
| 126 | + console.log('\nFailed iterations:', JSON.stringify(failedIterations, null, 2)) |
| 127 | + } |
| 128 | + |
| 129 | + t.end() |
| 130 | +}) |
0 commit comments