blob: 99e2278e8b1abcd9128e2f856648f248de9326a4 [file] [log] [blame]
{"version":3,"sources":["../../../src/common/tools/crawl.ts"],"names":["fs","path","validQueryPart","assert","unreachable","specFileSuffix","__filename","endsWith","crawlFilesRecursively","dir","subpathInfo","Promise","all","promises","readdir","map","d","p","join","stats","stat","isDirectory","isFile","files","filter","i","sep","concat","reduce","a","b","resolve","crawl","suiteDir","validate","existsSync","console","error","process","exit","filesToEnumerate","f","relative","replace","sort","entries","file","filepathWithoutExtension","substring","length","suite","basename","filename","env","STANDALONE_DEV_SERVER","mod","description","undefined","g","pathSegments","split","test","push","dirname","readme","readFileSync","trim","makeListing"],"mappings":";AAAA;AACA,G,CADA;AACA;AACA;AAEA,OAAO,KAAKA,EAAZ,MAAoB,IAApB,CACA,OAAO,KAAKC,IAAZ,MAAsB,MAAtB;;;AAGA,SAASC,cAAT,QAA+B,qCAA/B;;AAEA,SAASC,MAAT,EAAiBC,WAAjB,QAAoC,iBAApC;;AAEA,MAAMC,cAAc,GAAGC,UAAU,CAACC,QAAX,CAAoB,KAApB,IAA6B,UAA7B,GAA0C,UAAjE;;AAEA,eAAeC,qBAAf,CAAqCC,GAArC,EAAqE;AACnE,QAAMC,WAAW,GAAG,MAAMC,OAAO,CAACC,GAAR;AACxB,GAAC,MAAMZ,EAAE,CAACa,QAAH,CAAYC,OAAZ,CAAoBL,GAApB,CAAP,EAAiCM,GAAjC,CAAqC,OAAMC,CAAN,KAAW;AAC9C,UAAMC,CAAC,GAAGhB,IAAI,CAACiB,IAAL,CAAUT,GAAV,EAAeO,CAAf,CAAV;AACA,UAAMG,KAAK,GAAG,MAAMnB,EAAE,CAACa,QAAH,CAAYO,IAAZ,CAAiBH,CAAjB,CAApB;AACA,WAAO;AACLhB,MAAAA,IAAI,EAAEgB,CADD;AAELI,MAAAA,WAAW,EAAEF,KAAK,CAACE,WAAN,EAFR;AAGLC,MAAAA,MAAM,EAAEH,KAAK,CAACG,MAAN,EAHH,EAAP;;AAKD,GARD,C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B,YAA5B,EAA0C;AAC/C,YAAMiB,OAAO,GAAG9D,IAAI,CAAC8D,OAAL,CAAajB,IAAb,CAAhB;AACA,YAAMkB,MAAM,GAAGhE,EAAE,CAACiE,YAAH,CAAgBhE,IAAI,CAACiB,IAAL,CAAUe,QAAV,EAAoBa,IAApB,CAAhB,EAA2C,MAA3C,EAAmDoB,IAAnD,EAAf;;AAEA,YAAMP,YAAY,GAAGI,OAAO,KAAK,GAAZ,GAAkBA,OAAO,CAACH,KAAR,CAAc,GAAd,CAAlB,GAAuC,EAA5D;AACAf,MAAAA,OAAO,CAACiB,IAAR,CAAa,EAAEhB,IAAI,EAAEa,YAAR,EAAsBK,MAAtB,EAAb;AACD,KANM,MAMA;AACL5D,MAAAA,WAAW,CAAE,oCAAmC0C,IAAK,EAA1C,CAAX;AACD;AACF;;AAED,SAAOD,OAAP;AACD;;AAED,OAAO,SAASsB,WAAT,CAAqBf,QAArB,EAAkE;AACvE;AACA;AACA,SAAOpB,KAAK,CAAC/B,IAAI,CAAC8D,OAAL,CAAaX,QAAb,CAAD,EAAyB,KAAzB,CAAZ;AACD","sourcesContent":["// Node can look at the filesystem, but JS in the browser can't.\n// This crawls the file tree under src/suites/${suite} to generate a (non-hierarchical) static\n// listing file that can then be used in the browser to load the modules containing the tests.\n\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { SpecFile } from '../internal/file_loader.js';\nimport { validQueryPart } from '../internal/query/validQueryPart.js';\nimport { TestSuiteListingEntry, TestSuiteListing } from '../internal/test_suite_listing.js';\nimport { assert, unreachable } from '../util/util.js';\n\nconst specFileSuffix = __filename.endsWith('.ts') ? '.spec.ts' : '.spec.js';\n\nasync function crawlFilesRecursively(dir: string): Promise<string[]> {\n const subpathInfo = await Promise.all(\n (await fs.promises.readdir(dir)).map(async d => {\n const p = path.join(dir, d);\n const stats = await fs.promises.stat(p);\n return {\n path: p,\n isDirectory: stats.isDirectory(),\n isFile: stats.isFile(),\n };\n })\n );\n\n const files = subpathInfo\n .filter(\n i =>\n i.isFile &&\n (i.path.endsWith(specFileSuffix) ||\n i.path.endsWith(`${path.sep}README.txt`) ||\n i.path === 'README.txt')\n )\n .map(i => i.path);\n\n return files.concat(\n await subpathInfo\n .filter(i => i.isDirectory)\n .map(i => crawlFilesRecursively(i.path))\n .reduce(async (a, b) => (await a).concat(await b), Promise.resolve([]))\n );\n}\n\nexport async function crawl(\n suiteDir: string,\n validate: boolean = true\n): Promise<TestSuiteListingEntry[]> {\n if (!fs.existsSync(suiteDir)) {\n console.error(`Could not find ${suiteDir}`);\n process.exit(1);\n }\n\n // Crawl files and convert paths to be POSIX-style, relative to suiteDir.\n const filesToEnumerate = (await crawlFilesRecursively(suiteDir))\n .map(f => path.relative(suiteDir, f).replace(/\\\\/g, '/'))\n .sort();\n\n const entries: TestSuiteListingEntry[] = [];\n for (const file of filesToEnumerate) {\n // |file| is the suite-relative file path.\n if (file.endsWith(specFileSuffix)) {\n const filepathWithoutExtension = file.substring(0, file.length - specFileSuffix.length);\n\n const suite = path.basename(suiteDir);\n\n if (validate) {\n const filename = `../../${suite}/${filepathWithoutExtension}.spec.js`;\n\n assert(!process.env.STANDALONE_DEV_SERVER);\n const mod = (await import(filename)) as SpecFile;\n assert(mod.description !== undefined, 'Test spec file missing description: ' + filename);\n assert(mod.g !== undefined, 'Test spec file missing TestGroup definition: ' + filename);\n\n mod.g.validate();\n }\n\n const pathSegments = filepathWithoutExtension.split('/');\n for (const p of pathSegments) {\n assert(validQueryPart.test(p), `Invalid directory name ${p}; must match ${validQueryPart}`);\n }\n entries.push({ file: pathSegments });\n } else if (path.basename(file) === 'README.txt') {\n const dirname = path.dirname(file);\n const readme = fs.readFileSync(path.join(suiteDir, file), 'utf8').trim();\n\n const pathSegments = dirname !== '.' ? dirname.split('/') : [];\n entries.push({ file: pathSegments, readme });\n } else {\n unreachable(`Matched an unrecognized filename ${file}`);\n }\n }\n\n return entries;\n}\n\nexport function makeListing(filename: string): Promise<TestSuiteListing> {\n // Don't validate. This path is only used for the dev server and running tests with Node.\n // Validation is done for listing generation and presubmit.\n return crawl(path.dirname(filename), false);\n}\n"],"file":"crawl.js"}