Skip to content

Commit

Permalink
add examples tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizunashi Mana committed May 10, 2016
1 parent 189aaf5 commit f34531f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 24 deletions.
78 changes: 78 additions & 0 deletions gulp/lib/gulp-docs-tester/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var os = require('os');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp').sync;
var reload = require('require-reload')(require);

/**
* escape RegExp string
*
* @param {String} str non escaped string
* @returns {String} escaped string
*/
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

/**
* replace all string
*
* @param {String} str target string
* @param {String} find find string value
* @param {String} replacement replace string value
* @returns {String} replaced new string
*/
function replaceAll(str, find, replacement) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replacement);
}

module.exports.runJsFile = function(filepath, content) {
var result = { status: true };
var tmpfilename = path.join(
os.tmpdir(),
'docs-tester',
path.relative(process.cwd(), filepath)
);

var contenttmp
= '___dirname = __dirname;'
+ "__dirname = '" + path.dirname(filepath) + "';"
;

try {
mkdirp(path.dirname(tmpfilename));

contenttmp += content.toString();
contenttmp = replaceAll(contenttmp, 'console.log', '(function(){})');
contenttmp = contenttmp.replace(
"require('sonparser')",
"require(require('path').join("
+ "require('path').relative(___dirname, process.cwd()),"
+ "'lib/'"
+ '))'
);

// console.log(contenttmp);

fs.writeFileSync(tmpfilename, contenttmp, 0, 'utf-8');
} catch (e) {
return {
status: false,
err: new Error('Failed to run js file.')
};
}

// running
try {
result.value = reload(tmpfilename);
} catch (e) {
result.status = false;
result.err = e;
}

fs.unlink(tmpfilename, function() {});

return result;
}
37 changes: 24 additions & 13 deletions gulp/lib/gulp-docs-tester/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ var gutil = require('gulp-util');
var gdata = require('gulp-data');
var path = require('path');

var runJsFile = require('./context').runJsFile;

module.exports = function tester() {
return gdata(function(file) {
try {
file.docsTester = {
status: true,
value: null,
};
} catch (e) {
file.docsTester = {
status: false,
err: e,
};
}
file.docsTester = runJsFile(file.path, file.contents);
});
}

Expand All @@ -35,16 +27,35 @@ module.exports.reporter = function testerReporter(isFail) {
failures.push(result);
}

console.log(
/*
console.error(
result.err.stack.split(' at Module.require (module.js:')[0]
);
*/
gutil.log(
'[' + gutil.colors.cyan('gulp-docs-tester') + ']',
'[' + path.relative(process.cwd(), file.path) + ']',
gutil.colors.red('error')
gutil.colors.red('error'),
result.err.message
)
}
});

if (isEmitFail) {
resultStream = resultStream.on('end', function() {
var isfail = failures.length > 0;
var spaces = ' ';

gutil.log('jstesting...');

gutil.log(gutil.colors.green(spaces + (count - failures.length) + ' passing'));
gutil.log(gutil.colors.red(spaces + failures.length + ' failing'));

if (isfail) {
throw new gutil.PluginError('gulp-docs-tester', 'emit failure');
}
});
}

return resultStream;
}
7 changes: 0 additions & 7 deletions gulp/lib/gulp-docs-tester/lib.js

This file was deleted.

28 changes: 28 additions & 0 deletions gulp/tasks/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,35 @@ module.exports = (gulp, $, conf) ->
]
.pipe $.typescript tsProject

gulp.task 'test:examples', ->
merge [
gulp.src [
paths.test.exampleJs
]
merge [
gulp.src [
paths.test.exampleTs
]
.pipe $.data (file) ->
file.contents = new Buffer file.contents.toString().replace '''
node_modules/sonparser/lib-typings/sonparser.d.ts
''', "#{
path.relative(path.dirname(file.path), process.cwd())
}/lib-typings/sonparser.d.ts"
file
gulp.src [
paths.src.libTypings
paths.dtsm.typings
]
]
.pipe $.typescript tsProject
.js
]
.pipe do $.docsTester
.pipe $.docsTester.reporter true

gulp.task 'test', (cb) ->
$.runSequence 'test:src'
, 'test:typings'
, 'test:examples'
, cb
4 changes: 2 additions & 2 deletions lib-typings/sonparser-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="sonparser.d.ts" />

import * as sparse from "sonparser";
import * as events from "events";
import * as sparse from 'sonparser';
import * as events from 'events';

var bool: boolean;
var num: number;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"gulp-mocha": "^2.2.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-tslint": "^5.0.0",
"gulp-typedoc": "sierrasoftworks/gulp-typedoc#v1.8.10",
"gulp-typedoc": "^2.0.0",
"gulp-typescript": "^2.13.4",
"gulp-util": "^3.0.7",
"merge2": "^1.0.2",
Expand All @@ -74,7 +74,8 @@
"run-sequence": "^1.1.5",
"source-map-support": "^0.4.0",
"ts-node": "^0.7.2",
"tslint": "^3.9.0",
"tslint": "^3.10.0",
"typedoc": "^0.3.12",
"typescript": "^1.7.3",
"yargs": "^4.7.0"
},
Expand Down

0 comments on commit f34531f

Please sign in to comment.