Skip to content

Commit

Permalink
Add "glob" to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed May 27, 2016
1 parent 9c90013 commit cf3b997
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "examplejs",
"version": "0.0.24",
"version": "0.0.25",
"description": "A tool for converting example code into test cases",
"homepage": "https://github.com/zswang/jtests",
"authors": {
Expand Down
34 changes: 24 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env node

'use strict';
var jtests = require('./');
var examplejs = require('./');
var optimist = require('optimist');
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var util = require('util');
var glob = require('glob');

var argv = optimist
.usage('$0 input1.js [input2.js] -o output')
.usage('$0 glob1 [glob2] -o output [a]')
.alias('d', 'desc')
.describe('desc', 'describe text.')
.string('d')
Expand Down Expand Up @@ -42,26 +43,39 @@ if (argv._.length < 1) {
return;
}

var contents = [];
var filenames = [];
var header;
if (argv.head) {
header = String(fs.readFileSync(argv.head));
}

argv._.forEach(function (filename) {
filenames.push(filename);
contents.push(jtests.build(fs.readFileSync(filename), {
new glob(filename, {
cwd: __dirname,
sync: true
}).forEach(function (item) {
if (filenames.indexOf(item) < 0) {
filenames.push(item);
}
});
});

var contents = [];
if (header) {
contents.push(header);
}
filenames.forEach(function (filename) {
contents.push(examplejs.build(fs.readFileSync(filename), {
desc: argv.desc || filename,
timeout: argv.timeout,
header: header
}), null, ' ');
}));
});

var content = contents.join('\n');
if (argv.output) {
mkdirp(path.dirname(argv.output));
fs.writeFileSync(argv.output, content);
console.log(util.format('%j jtests output complete.', filenames));
}
else {
console.log(util.format('%j examplejs output complete.', filenames));
} else {
console.log(content);
}
4 changes: 2 additions & 2 deletions examplejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* A tool for converting example code into test cases
* @author
* zswang (http://weibo.com/zswang)
* @version 0.0.24
* @date 2016-05-18
* @version 0.0.25
* @date 2016-05-27
*/
/**
* @example build():content empty
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "examplejs",
"version": "0.0.24",
"version": "0.0.25",
"description": "A tool for converting example code into test cases",
"main": "examplejs.js",
"keywords": [
Expand All @@ -25,6 +25,7 @@
"repository": "https://github.com/zswang/examplejs.git",
"license": "MIT",
"dependencies": {
"glob": "^7.0.3",
"mkdirp": "^0.5.0",
"colors": "^1.0.3",
"optimist": "^0.6.1"
Expand All @@ -36,7 +37,6 @@
"gulp-uglify": "^1.5.3",
"gulp-replace": "^0.5.4",
"gulp-examplejs": "^0.0.4",

"jsdom": "^9.1.0",
"mocha": "^2.0.1",
"istanbul": "^0.3.17",
Expand Down
60 changes: 0 additions & 60 deletions test/readme.js
Original file line number Diff line number Diff line change
@@ -1,60 +0,0 @@
describe("README.md", function () {
var assert = require('should');
var util = require('util');
var examplejs_printLines;
function examplejs_print() {
examplejs_printLines.push(util.format.apply(util, arguments));
}

it("xx", function() {
examplejs_printLines = [];
var a = 1;
var b = 2;
examplejs_print(a === b); // false
});
it("表达式相等预判", function() {
examplejs_printLines = [];
var a = 1;
var b = 2;
examplejs_print(a === b);
assert.equal(examplejs_printLines.join("\n"), "false"); examplejs_printLines = [];
});
it("表达式结果预判", function() {
examplejs_printLines = [];
var a = 1;
var b = 2;
examplejs_print(a + b);
assert.equal(examplejs_printLines.join("\n"), "3"); examplejs_printLines = [];
});
it("表达式类型预判", function() {
examplejs_printLines = [];
var a = 1;
examplejs_print(JSON.stringify(a + '1'));
assert.equal(examplejs_printLines.join("\n"), "\"11\""); examplejs_printLines = [];
});
it("批量表达式预判", function() {
examplejs_printLines = [];
for (var i = 0; i < 5; i++) {
examplejs_print(i);
}
assert.equal(examplejs_printLines.join("\n"), "0\n1\n2\n3\n4"); examplejs_printLines = [];
});
it("异步执行预判", function(done) {
examplejs_printLines = [];
var a = 1;
setTimeout(function () {
examplejs_print(a);
assert.equal(examplejs_printLines.join("\n"), "2"); examplejs_printLines = [];
done();
}, 1000);
a++;
});
it("异常执行预判", function() {
examplejs_printLines = [];

(function() {
var a = JSON.parse('#error');
// * throw
}).should.throw();
});
});

0 comments on commit cf3b997

Please sign in to comment.