Skip to content

Commit 64ded38

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 5d4cf02 + e2758ae commit 64ded38

File tree

7 files changed

+292
-114
lines changed

7 files changed

+292
-114
lines changed

lib/config/streams.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ function jsApp(opts) {
4545
}
4646

4747
function jsLib(opts) {
48-
return gulp.src(getGlob(['**/*.js', '!*.js']), opts);
48+
return gulp.src(getGlob(['**/*.js', '!*.js', '!**/*.spec.js']), opts);
4949
}
5050

5151
function jsSpec(opts) {
52-
return gulp.src(getGlob(['**/*.spec.js', '!*.spec.js'], APP), opts);
52+
return gulp.src(getGlob(['**/*.spec.js', '!*.spec.js']), opts);
5353
}
5454

5555
function scssApp(opts) {
@@ -81,4 +81,4 @@ module.exports = {
8181
jsSpec : jsSpec,
8282
testDependencies: testDependencies,
8383
getGlob : getGlob
84-
};
84+
};

lib/test/karma-background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
var server = require('karma').server;
66
var decoded = require('querystring').unescape(process.argv[2]);
77
var data = JSON.parse(decoded);
8-
server.start(data);
8+
server.start(data);

lib/test/karma.js

Lines changed: 102 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

3-
var path = require('path');
4-
var through = require('through2');
5-
var querystring = require('querystring');
6-
var childProcess = require('child_process');
3+
var path = require('path');
4+
5+
var gulp = require('gulp'),
6+
through = require('through2'),
7+
querystring = require('querystring'),
8+
childProcess = require('child_process');
79

810
/**
911
* Wrap the given value in quotation marks
@@ -14,60 +16,110 @@ function quote(value) {
1416
return '"' + value + '"';
1517
}
1618

19+
var filesAppendRegex = /\/\*+\s*ANGULARITY_FILE_LIST\s*\*+\// ;
20+
1721
/**
18-
* Run karma once only with the given <code>options</code> and the files from the stream appended.
19-
* Removes any logging from the output.
20-
* No output. Ends when the Karma process ends.
21-
* @param {object} options Karma options
22-
* @param {number} [bannerWidth] The width of banner comment, zero or omitted for none
23-
* @returns {stream.Through} A through strconcateam that performs the operation of a gulp stream
22+
* Create a through2 object stream.
23+
* Expects all the `*.js` files to be included as the files list in the karma conf
24+
* Creates a new karma config file, based on the karma config file name found in
25+
* the local project root, and augments its file list by replacing
26+
* `ANGULARITY_FILE_LIST` in ablock comment with the actual array of files.
27+
* The new karma config file is added to the stream,
28+
* All input `*.js` files are filtered out of the stream
29+
*
30+
* @param {string} configFileName The project local karma config file to augment
31+
* Defaults to "karma.conf.js"
32+
* @return {stream.Through} The output of this stream is expected to contain
33+
* just one file: the augmented karma config file.
2434
*/
25-
module.exports = function (options, bannerWidth) {
26-
27-
// setup
28-
options.singleRun = true;
29-
options.autoWatch = false;
30-
options.configFile = options.configFile ? path.resolve(options.configFile) : undefined;
31-
options.files = options.files || [ ];
35+
function karmaCreateConfig(configFileName) {
36+
configFileName = 'karma.conf.js';
37+
var files = [];
3238

33-
// add unique all stream JS files to the options.files list
34-
return through.obj(function(file, encoding, done) {
35-
var isValid = !(file.isNull()) && (path.extname(file.path) === '.js');
36-
if (isValid && (options.files.indexOf(file.path) < 0)) {
37-
options.files.push(file.path);
39+
function transformFn(file, encoding, done) {
40+
if (!file || !file.path) {
41+
throw 'Files must have paths';
3842
}
43+
files.push(file.path);
3944
done();
45+
}
4046

41-
// run once at the end of the stream
42-
}, function(done) {
43-
if (options.files.length) {
44-
var appPath = path.join(__dirname, 'karma-background.js');
45-
var data = querystring.escape(JSON.stringify(options));
46-
var command = [ 'node', quote(appPath), data ].join(' ');
47-
childProcess.exec(command, { cwd: process.cwd() }, function (stderr, stdout) {
48-
var report;
49-
if (stdout) {
50-
report = stdout
51-
.replace(/^\s+/gm, '') // remove leading whitespace
52-
.replace(/^(LOG.*\n$)/gm, options.silent ? '' : '$1') // remove logging
53-
.replace(/at\s+null\..*/gm, '') // remove file reference
54-
.replace(/\n\n/gm, '\n') // consolidate consecutive line breaks
55-
.replace(/^\n|\n$/g, ''); // remove leading and trailing line breaks overall
56-
} else if (stderr) {
57-
var analysis = /$Error\:\s*(.*)$/mg.exec(stderr);
58-
report = analysis ? analysis[1] : stderr;
59-
}
60-
if (report) {
61-
var width = Number(bannerWidth) || 0;
62-
var hr = new Array(width + 1); // this is a good trick to repeat a character N times
63-
var start = (width > 0) ? (hr.join('\u25BC') + '\n') : '';
64-
var stop = (width > 0) ? (hr.join('\u25B2') + '\n') : '';
65-
process.stdout.write(start + '\n' + report + '\n\n' + stop);
66-
}
47+
function flushFn(done) {
48+
var stream = this;
49+
var filesAppend = JSON.stringify(files, null, ' ');
50+
51+
//aggregate and append to karma.conf.js in the project root folder
52+
gulp
53+
.src(path.resolve(configFileName))
54+
.on('data', function(karmaConfigFile) {
55+
var contents = karmaConfigFile.contents.toString();
56+
contents = contents.replace(filesAppendRegex, filesAppend);
57+
karmaConfigFile.contents = new Buffer(contents);
58+
stream.push(karmaConfigFile);
59+
})
60+
.on('end', function() {
6761
done();
6862
});
69-
} else {
63+
}
64+
65+
return through.obj(transformFn, flushFn);
66+
}
67+
68+
/**
69+
* Run karma once only with the karma config file present in the input stream.
70+
* No output. Ends when the Karma process ends.
71+
* Runs karma in a child process, to avoid `process.exit()` called by karma.
72+
*
73+
* @param {number} [bannerWidth] The width of banner comment, zero or omitted for none
74+
* @returns {stream.Through} A through strconcateam that performs the operation of a gulp stream
75+
*/
76+
function karmaRun(bannerWidth) {
77+
var options = {
78+
configFile: undefined
79+
};
80+
81+
return through.obj(function transformFn(file, encoding, done) {
82+
options.configFile = file.path;
83+
done();
84+
},
85+
function streamFn(done) {
86+
var appPath = path.join(__dirname, 'karma-background.js');
87+
var data = querystring.escape(JSON.stringify(options));
88+
var command = [ 'node', quote(appPath), data ].join(' ');
89+
90+
//TODO @bguiz replace reporter function with a standard karma reporter,
91+
//and extract it to own module
92+
//perhaps extend the spec reporter to do what is being done here, instead of post processing its output here
93+
//check if there is a webstorm/ teamcity reporter
94+
childProcess.exec(command, { cwd: process.cwd() }, function reporter(stderr, stdout) {
95+
var report;
96+
if (stdout) {
97+
console.log(stdout);
98+
report = stdout
99+
.replace(/^\s+/gm, '') // remove leading whitespace
100+
.replace(/^(LOG.*\n$)/gm, options.silent ? '' : '$1') // remove logging
101+
.replace(/at\s+null\..*/gm, '') // remove file reference
102+
.replace(/\n\n/gm, '\n') // consolidate consecutive line breaks
103+
.replace(/^\n|\n$/g, ''); // remove leading and trailing line breaks overall
104+
} else if (stderr) {
105+
console.log(stderr);
106+
107+
var analysis = /$Error\:\s*(.*)$/mg.exec(stderr);
108+
report = analysis ? analysis[1] : stderr;
109+
}
110+
if (report) {
111+
var width = Number(bannerWidth) || 0;
112+
var hr = new Array(width + 1); // this is a good trick to repeat a character N times
113+
var start = (width > 0) ? (hr.join('\u25BC') + '\n') : '';
114+
var stop = (width > 0) ? (hr.join('\u25B2') + '\n') : '';
115+
process.stdout.write(start + '\n' + report + '\n\n' + stop);
116+
}
70117
done();
71-
}
118+
});
72119
});
73120
};
121+
122+
module.exports = {
123+
createConfig: karmaCreateConfig,
124+
run: karmaRun
125+
};

npm-shrinkwrap.json

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

100755100644
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"convert-source-map": "latest",
5050
"gulp": "latest",
5151
"gulp-concat": "latest",
52+
"gulp-filter": "latest",
5253
"gulp-inject": "latest",
5354
"gulp-jshint": "latest",
5455
"gulp-load-plugins": "latest",

0 commit comments

Comments
 (0)