From 0125830664c38206599427751eb8855a217192e6 Mon Sep 17 00:00:00 2001 From: Alex Smagin Date: Tue, 13 Feb 2018 22:34:47 -0600 Subject: [PATCH] Fix build-test script for windows (#48) --- config/exports/build-tests.js | 46 ++++++++++++++++++++++------------- package.json | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/config/exports/build-tests.js b/config/exports/build-tests.js index 4aebfda..980487b 100644 --- a/config/exports/build-tests.js +++ b/config/exports/build-tests.js @@ -1,25 +1,25 @@ // this script watches the tests exported by typescript, copies them to the test directories, and modifies the require("PKG.NAME") statements to test each build const cpx = require("cpx"); -const separator = require("path").sep; +const path = require("path"); const Transform = require("stream").Transform; const pkg = require('../../package'); -const req = (path) => 'require("' + path + '")'; -const pathUp = (levels) => Array.from(Array(levels), () => '../').join(''); + +const req = (path) => `require("${path}")`; // replace instances of pkg.name with the proper route to the build being tested -const makeTransform = (filePath, buildPath) => { - const buildPathParts = buildPath.split(separator); - // filePath includes build/main (-2), test/BUILD is 2 deep (+2), - // remove filename (-1). Total is length - 2 - const pathToRoot = pathUp(filePath.split(separator).length - 1); - const placeholder = req(pkg.name); +const makeTransform = (rootDir, buildPath, specPath) => { + const specDir = path.dirname(specPath) + const testDir = specDir.replace(path.join(rootDir, 'build'), path.join(rootDir, 'test')) + + const newRequire = path.relative(testDir, buildPath) + .split(path.sep).join('/') + return new Transform({ transform(chunk, encoding, done) { - const str = chunk.toString(); - const parts = str.split(placeholder) - const newPath = req(pathToRoot + buildPath) - const result = parts.join(newPath); - this.push(result); + const str = chunk + .toString() + .replace(req(pkg.name), req(newRequire)) + this.push(str); done(); } }); @@ -31,11 +31,23 @@ const watchMode = process.argv.indexOf('-w') !== -1 ? true : false; const browserTests = process.argv.indexOf('--no-browser') !== -1 ? true : false; const task = watchMode ? cpx.watch : cpx.copy; +const rootDir = path.resolve('.'); +const mainBuildPath = path.resolve(pkg.main); + task(testsFromRoot, 'test/main', { - transform: (filePath) => makeTransform(filePath, pkg.main) + transform: (specPath) => makeTransform( + rootDir, + mainBuildPath, + path.resolve(specPath)) }); + if (!browserTests) { + const browserBuildPath = path.resolve(pkg.browser); + task(testsFromRoot, 'test/browser', { - transform: (filePath) => makeTransform(filePath, pkg.browser.replace('.js', '.cjs.js')) + transform: (specPath) => makeTransform( + rootDir, + browserBuildPath.replace('.js', '.cjs.js'), + path.resolve(specPath)) }); -} +} \ No newline at end of file diff --git a/package.json b/package.json index fa5b4b3..bed56ac 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "unit": "yarn build && yarn build:tests && nyc ava", "check-coverage": "nyc check-coverage --lines 100 --functions 100 --branches 100", "test": "yarn lint && yarn unit && yarn check-coverage", - "watch": "yarn build && yarn build:tests -- --no-browser && concurrently -r --kill-others 'npm run --silent build:main -- -w' 'npm run --silent build:tests -- -w --no-browser' 'sleepms 2000 && ava --watch'", + "watch": "yarn build && yarn build:tests -- --no-browser && concurrently -r --kill-others \"npm run --silent build:main -- -w\" \"npm run --silent build:tests -- -w --no-browser\" \"sleepms 2000 && ava --watch\"", "cov": "yarn unit && yarn html-coverage && opn coverage/index.html", "html-coverage": "nyc report --reporter=html", "send-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",