Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Baselines #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
npm-debug.log
node_modules/
out/
tmp/
tmp/
tests/baselines/local/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ src
tmp
out
typings
tests
gulpfile.js
tsconfig.json
55 changes: 54 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global __dirname */

var gulp = require('gulp');
var util = require('gulp-util');
var path = require('path');
var child_process = require('child_process');
var mocha = require('gulp-mocha');
var del = require('del');
var runSequence = require('run-sequence');
Expand Down Expand Up @@ -55,17 +57,68 @@ gulp.task('lkg', function () {
return runSequence('clean', 'test', 'lkg:copy');
});

gulp.task('test', ['build'], function () {
gulp.task('accept-baselines', function () {
return gulp.src(["tests/baselines/local/**/*"])
.pipe(gulp.dest("tests/baselines/reference"));
});

gulp.task('clean-local-baselines', function () {
return del(["tests/baselines/local"]);
});

gulp.task('test', ['build', 'clean-local-baselines'], function () {
return gulp.src(["out/tests/**/*.js"], { read: false })
.pipe(mocha({ timeout: 3000 }));
});

gulp.task('diff', function (cb) {
getDiffTool(function (e, tool) {
if (e) return cb(e);
tool = formatDiffTool(tool, "tests/baselines/reference", "tests/baselines/local");
util.log(tool);
var args = parseCommand(tool);
child_process.spawn(args.shift(), args, { detached: true }).unref();
cb(null);
});
});

gulp.task('dev', ['test'], function () {
return gulp.watch(sources, ['test']);
});

gulp.task('default', ['dev']);

// get the diff tool either from the 'DIFF' environment variable or from git
function getDiffTool(cb) {
var difftool = process.env['DIFF'];
if (difftool) return cb(null, difftool);
child_process.exec('git config diff.tool', function (e, stdout) {
if (e) return cb(e, null);
if (stdout) stdout = stdout.trim();
if (!stdout) return cb(new Error("Add the 'DIFF' environment variable to the path of the program you want to use."), null);
child_process.exec('git config difftool.' + stdout + '.cmd', function (e, stdout) {
if (e) return cb(e, null);
if (stdout) stdout = stdout.trim();
if (!stdout) return cb(new Error("Add the 'DIFF' environment variable to the path of the program you want to use."), null);
return cb(null, stdout);
});
});
}

// format the diff tool path with a left and right comparison path
function formatDiffTool(toolPath, leftPath, rightPath) {
return /\$(local|remote)/i.test(toolPath)
? toolPath.replace(/(\$local)|(\$remote)/gi, function (_, left, right) { return left ? leftPath : rightPath; })
: '"' + toolPath + '" "' + leftPath + '" "' + rightPath + '"';
}

// parse a command line string
function parseCommand(text) {
var re = /"([^"]*)"|[^"\s]+/g, args = [], m;
while (m = re.exec(text)) args.push(m[1] || m[0]);
return args;
}

// reload a node module and any children beneath the same folder
function reload(moduleName) {
var id = require.resolve(moduleName);
Expand Down
60 changes: 60 additions & 0 deletions lib/tests/scenarios.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";
var fs = require("fs");
var path = require("path");
var tsb = require("../");
var chai_1 = require("chai");
var chai_baseline_1 = require("chai-baseline");
chai_1.use(chai_baseline_1.chaiBaseline);
describe("scenario", function () {
var testsdir = path.join(__dirname, "../../tests");
var scenariosdir = path.join(testsdir, "scenarios");
var baselinesdir = path.join(testsdir, "baselines");
if (!fs.existsSync(scenariosdir)) {
// no scenarios
return;
}
var _loop_1 = function (name_1) {
var scenariodir = path.join(scenariosdir, name_1);
var build = path.join(scenariodir, "build.js");
if (fs.existsSync(build)) {
it(name_1, function (done) {
var ended = false;
var files = [];
var participants = [];
var scenario = require(build);
var stream = scenario(tsb);
stream.on("data", function (file) {
var basename = path.basename(file.relative);
var relativedir = path.normalize(path.dirname(file.relative))
.replace(/([\\/])\.($|[\\/])/g, "$1dot$2")
.replace(/(^|[\\/])\.\.($|[\\/])/g, "$1dotDot$2");
var relative = path.join(name_1, relativedir, basename);
files.push(path.normalize(path.join(relativedir, basename)));
participants.push(chai_1.assert.baseline(file.contents, path.join(name_1, relativedir, basename), { base: baselinesdir }));
if (file.sourceMap) {
files.push(path.normalize(path.join(relativedir, basename + ".sourceMap.txt")));
participants.push(chai_1.assert.baseline(normalizeLineEndings(JSON.stringify(file.sourceMap, undefined, " ")), path.join(name_1, relativedir, basename) + ".sourceMap.txt", { base: baselinesdir }));
}
});
stream.on("error", done);
stream.on("close", onend);
stream.on("end", onend);
function onend() {
if (ended)
return;
ended = true;
participants.push(chai_1.assert.baseline(normalizeLineEndings(JSON.stringify(files.sort(), undefined, " ")), path.join(name_1, "files.json"), { base: baselinesdir }));
var waitOne = function () { return participants.length ? participants.shift().then(waitOne, done) : done(); };
waitOne();
}
});
}
};
for (var _i = 0, _a = fs.readdirSync(scenariosdir); _i < _a.length; _i++) {
var name_1 = _a[_i];
_loop_1(name_1);
}
});
function normalizeLineEndings(text) {
return text.replace(/\r?\n/g, "\n");
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
"vinyl": "^0.4.3"
},
"devDependencies": {
"@types/chai": "^3.4.34",
"@types/chalk": "*",
"@types/gulp-util": "*",
"@types/mocha": "*",
"@types/node": "*",
"@types/through": "*",
"@types/vinyl": "*",
"chai": "^3.5.0",
"chai-baseline": "^1.0.0",
"del": "^2.2.2",
"gulp": "^3.8.10",
"gulp-mocha": "^3.0.1",
Expand Down
63 changes: 63 additions & 0 deletions src/tests/scenarios.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as fs from "fs";
import * as path from "path";
import * as events from "events";
import * as tsb from "../";
import { use, assert } from "chai";
import { chaiBaseline } from "chai-baseline";
import File = require("vinyl");

use(chaiBaseline);

describe("scenario", () => {
const testsdir = path.join(__dirname, "../../tests");
const scenariosdir = path.join(testsdir, "scenarios");
const baselinesdir = path.join(testsdir, "baselines");

if (!fs.existsSync(scenariosdir)) {
// no scenarios
return;
}

for (const name of fs.readdirSync(scenariosdir)) {
const scenariodir = path.join(scenariosdir, name);
const build = path.join(scenariodir, "build.js");
if (fs.existsSync(build)) {
it(name, (done) => {
let ended = false;
const files: string[] = [];
const participants: PromiseLike<void>[] = [];
const scenario = require(build) as (tsb_: typeof tsb) => events.EventEmitter;
const stream = scenario(tsb);
stream.on("data", (file: File & { sourceMap?: any }) => {
const basename = path.basename(file.relative);
const relativedir = path.normalize(path.dirname(file.relative))
.replace(/([\\/])\.($|[\\/])/g, "$1dot$2")
.replace(/(^|[\\/])\.\.($|[\\/])/g, "$1dotDot$2");
const relative = path.join(name, relativedir, basename);
files.push(path.normalize(path.join(relativedir, basename)));
participants.push(assert.baseline(file.contents, path.join(name, relativedir, basename), { base: baselinesdir }));
if (file.sourceMap) {
files.push(path.normalize(path.join(relativedir, basename + ".sourceMap.txt")));
participants.push(assert.baseline(normalizeLineEndings(JSON.stringify(file.sourceMap, undefined, " ")), path.join(name, relativedir, basename) + ".sourceMap.txt", { base: baselinesdir }));
}
});
stream.on("error", done);
stream.on("close", onend);
stream.on("end", onend);

function onend() {
if (ended) return;
ended = true;
participants.push(assert.baseline(normalizeLineEndings(JSON.stringify(files.sort(), undefined, " ")), path.join(name, "files.json"), { base: baselinesdir }));
const waitOne = () => participants.length ? participants.shift().then(waitOne, done) : done();
waitOne();
}
});
}
}
});


function normalizeLineEndings(text: string) {
return text.replace(/\r?\n/g, "\n");
}
1 change: 1 addition & 0 deletions tests/baselines/reference/multiFile/a.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const a = 1;
3 changes: 3 additions & 0 deletions tests/baselines/reference/multiFile/a.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/baselines/reference/multiFile/a.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/baselines/reference/multiFile/b.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const b = 2;
3 changes: 3 additions & 0 deletions tests/baselines/reference/multiFile/b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/baselines/reference/multiFile/b.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/baselines/reference/multiFile/files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"a.d.ts",
"a.js",
"a.js.map",
"b.d.ts",
"b.js",
"b.js.map"
]
1 change: 1 addition & 0 deletions tests/scenarios/multiFile/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
1 change: 1 addition & 0 deletions tests/scenarios/multiFile/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 2;
14 changes: 14 additions & 0 deletions tests/scenarios/multiFile/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var vfs = require("vinyl-fs");
var stream = require("stream");
module.exports = function (tsb) {
var build = tsb.create({
module: "commonjs",
target: "es5",
declaration: true,
sourceMap: true,
newLine: "lf"
}, { base: __dirname });
return vfs
.src("**/*.ts", { cwd: __dirname })
.pipe(build());
};