| 1 | // Los ficheros de texto del repo son texto para git: ninguno lleva un byte NUL |
| 2 | // literal. Con uno dentro, git trata el fichero entero como binario: `git diff` |
| 3 | // solo dice «Binary files differ» y `git grep -I` se lo salta, así que nada de lo |
| 4 | // que cambia en él se ve al revisar ni al barrer el diff antes de publicar. Pasó |
| 5 | // con md.js y arch.js (un centinela '\0' escrito tal cual); dentro de una cadena |
| 6 | // se escribe '\u0000', que es lo mismo para JavaScript. |
| 7 | // |
| 8 | // node tests/texto-plano.mjs |
| 9 | import { readdirSync, readFileSync, statSync } from 'node:fs'; |
| 10 | import { join, relative, extname } from 'node:path'; |
| 11 | import { fileURLToPath } from 'node:url'; |
| 12 | |
| 13 | const RAIZ = fileURLToPath(new URL('..', import.meta.url)); |
| 14 | const TEXTO = new Set(['.js', '.mjs', '.cjs', '.css', '.html', '.json', '.md', '.py', '.sh', '.go', '.txt', '.svg', '.webmanifest', '.yml', '.yaml']); |
| 15 | const FUERA = new Set(['node_modules', '.git']); |
| 16 | |
| 17 | let fallos = 0, vistos = 0; |
| 18 | const ok = (n, c, e = '') => { console.log(`${c ? '✅' : '❌'} ${n}${e ? ' — ' + e : ''}`); if (!c) fallos++; }; |
| 19 | |
| 20 | const conNul = []; |
| 21 | function recorre(dir) { |
| 22 | for (const nombre of readdirSync(dir)) { |
| 23 | if (FUERA.has(nombre)) continue; |
| 24 | const ruta = join(dir, nombre); |
| 25 | const st = statSync(ruta, { throwIfNoEntry: false }); |
| 26 | if (!st) continue; |
| 27 | if (st.isDirectory()) { recorre(ruta); continue; } |
| 28 | if (!TEXTO.has(extname(nombre).toLowerCase())) continue; |
| 29 | vistos++; |
| 30 | if (readFileSync(ruta).includes(0)) conNul.push(relative(RAIZ, ruta)); |
| 31 | } |
| 32 | } |
| 33 | recorre(RAIZ); |
| 34 | |
| 35 | ok(`se revisan los ficheros de texto del repo (${vistos})`, vistos > 50, `${vistos} ficheros`); |
| 36 | ok('ninguno lleva un byte NUL literal (git lo trataría como binario)', conNul.length === 0, conNul.join(', ')); |
| 37 | console.log(fallos ? `\n❌ ${fallos} FALLO(S)` : '\n✅ TEXTO PLANO: git ve todos los ficheros de texto como texto'); |
| 38 | process.exit(fallos ? 1 : 0); |