From cc5aeb3b50d7a3c6930afb8fa8ea0913be162b93 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 16 May 2018 15:51:55 -0700 Subject: [PATCH] Allow .ts and .tsx test fixtures. --- packages/babel-helper-fixtures/src/index.js | 72 ++++++++------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/packages/babel-helper-fixtures/src/index.js b/packages/babel-helper-fixtures/src/index.js index d27772495e..91284ecd0d 100644 --- a/packages/babel-helper-fixtures/src/index.js +++ b/packages/babel-helper-fixtures/src/index.js @@ -1,4 +1,3 @@ -import assert from "assert"; import cloneDeep from "lodash/cloneDeep"; import trimEnd from "lodash/trimEnd"; import resolve from "try-resolve"; @@ -56,6 +55,24 @@ function shouldIgnore(name, blacklist?: Array) { ); } +const EXTENSIONS = [".js", ".mjs", ".ts", ".tsx"]; + +function findFile(filepath: string, allowJSON: boolean) { + const matches = []; + + for (const ext of EXTENSIONS.concat(allowJSON ? ".json" : [])) { + const name = filepath + ext; + + if (fs.existsSync(name)) matches.push(name); + } + + if (matches.length > 1) { + throw new Error(`Found conflicting file matches: ${matches.join(", ")}`); + } + + return matches[0] || filepath + ".js"; +} + export default function get(entryLoc): Array { const suites = []; @@ -84,54 +101,25 @@ export default function get(entryLoc): Array { } function push(taskName, taskDir) { - let actualLocAlias = suiteName + "/" + taskName + "/input.js"; - let expectLocAlias = suiteName + "/" + taskName + "/output.js"; - let execLocAlias = suiteName + "/" + taskName + "/exec.js"; + const actualLoc = findFile(taskDir + "/input"); + const expectLoc = findFile(taskDir + "/output", true /* allowJSON */); + let execLoc = findFile(taskDir + "/exec"); - let actualLoc = taskDir + "/input.js"; - let expectLoc = taskDir + "/output.js"; - let execLoc = taskDir + "/exec.js"; - - const hasExecJS = fs.existsSync(execLoc); - const hasExecMJS = fs.existsSync(asMJS(execLoc)); - if (hasExecMJS) { - assert(!hasExecJS, `${asMJS(execLoc)}: Found conflicting .js`); - - execLoc = asMJS(execLoc); - execLocAlias = asMJS(execLocAlias); - } - - const hasExpectJS = fs.existsSync(expectLoc); - const hasExpectMJS = fs.existsSync(asMJS(expectLoc)); - if (hasExpectMJS) { - assert(!hasExpectJS, `${asMJS(expectLoc)}: Found conflicting .js`); - - expectLoc = asMJS(expectLoc); - expectLocAlias = asMJS(expectLocAlias); - } - - const hasActualJS = fs.existsSync(actualLoc); - const hasActualMJS = fs.existsSync(asMJS(actualLoc)); - if (hasActualMJS) { - assert(!hasActualJS, `${asMJS(actualLoc)}: Found conflicting .js`); - - actualLoc = asMJS(actualLoc); - actualLocAlias = asMJS(actualLocAlias); - } + const actualLocAlias = + suiteName + "/" + taskName + "/" + path.basename(actualLoc); + const expectLocAlias = + suiteName + "/" + taskName + "/" + path.basename(actualLoc); + let execLocAlias = + suiteName + "/" + taskName + "/" + path.basename(actualLoc); if (fs.statSync(taskDir).isFile()) { const ext = path.extname(taskDir); - if (ext !== ".js" && ext !== ".mjs") return; + if (EXTENSIONS.indexOf(ext) === -1) return; execLoc = taskDir; execLocAlias = suiteName + "/" + taskName; } - if (resolve.relative(expectLoc + "on")) { - expectLoc += "on"; - expectLocAlias += "on"; - } - const taskOpts = cloneDeep(suite.options); const taskOptsLoc = resolve(taskDir + "/options"); @@ -222,10 +210,6 @@ export function multiple(entryLoc, ignore?: Array) { return categories; } -function asMJS(filepath) { - return filepath.replace(/\.js$/, ".mjs"); -} - export function readFile(filename) { if (fs.existsSync(filename)) { let file = trimEnd(fs.readFileSync(filename, "utf8"));