Skip to content

Commit 08a95ad

Browse files
committed
=BG= karma wrapper modified to use karma config file
1 parent 5fb03a8 commit 08a95ad

File tree

6 files changed

+165
-140
lines changed

6 files changed

+165
-140
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: 108 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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+
gulpIf = require('gulp-if'),
7+
through = require('through2'),
8+
querystring = require('querystring'),
9+
childProcess = require('child_process');
710

811
/**
912
* Wrap the given value in quotation marks
@@ -14,60 +17,115 @@ function quote(value) {
1417
return '"' + value + '"';
1518
}
1619

20+
var filesAppendRegex = /\/\*+\s*ANGULARITY_FILE_LIST\s*\*+\// ;
21+
1722
/**
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
23+
* Create a through2 object stream.
24+
* Expects all the `*.js` files to be included as the files list in the karma conf
25+
* Creates a new karma config file, based on the karma config file name found in
26+
* the local project root, and augments its file list by replacing
27+
* `ANGULARITY_FILE_LIST` in ablock comment with the actual array of files.
28+
* The new karma config file is added to the stream,
29+
* All input `*.js` files are filtered out of the stream
30+
*
31+
* @param {string} configFileName The project local karma config file to augment
32+
* Defaults to "karma.conf.js"
33+
* @return {stream.Through} The output of this stream is expected to contain
34+
* just one file: the augmented karma config file.
2435
*/
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 || [ ];
36+
function karmaConfig(configFileName) {
37+
configFileName = 'karma.conf.js';
38+
var files = [];
3239

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);
40+
function transformFn(file, encoding, done) {
41+
if (!file || !file.path) {
42+
throw 'Files must have paths';
43+
}
44+
var stream = this;
45+
//filter out all files (nothing added back to the stream)
46+
//but we save file paths for later use in the flush function
47+
if (path.extname(file.path) !== '.map') {
48+
files.push(file.path);
3849
}
3950
done();
51+
}
52+
53+
function flushFn(done) {
54+
var stream = this;
55+
var filesAppend = JSON.stringify(files, null, ' ');
4056

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

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-if": "latest",
5253
"gulp-inject": "latest",
5354
"gulp-jshint": "latest",
5455
"gulp-load-plugins": "latest",

tasks/javascript.js

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22

3-
var path = require('path');
3+
var path = require('path'),
4+
fs = require('fs');
45

56
var gulp = require('gulp'),
7+
gulpIf = require('gulp-if'),
68
jshint = require('gulp-jshint'),
79
rimraf = require('gulp-rimraf'),
10+
gutil = require('gulp-util'),
811
runSequence = require('run-sequence'),
912
combined = require('combined-stream'),
1013
to5ify = require('6to5ify'),
@@ -108,45 +111,6 @@ gulp.task('javascript:lint', function () {
108111
.pipe(jshintReporter.get(cliArgs.reporter));
109112
});
110113

111-
function karmaSpecFile() {
112-
var files = [];
113-
114-
function transformFn(file, encoding, done) {
115-
if (!file || !file.path) {
116-
throw 'Files must have paths';
117-
}
118-
var stream = this;
119-
//filter out all files (nothing added back to the stream)
120-
//but we save file paths for later use in the flush function
121-
files.push(file.path);
122-
done();
123-
}
124-
125-
function flushFn(done) {
126-
var stream = this;
127-
var contentAppend =
128-
'\n\nfiles = (files || []).concat(' +
129-
JSON.stringify(files, null, ' ') +
130-
');\n';
131-
console.log('contentAppend', contentAppend);
132-
133-
//aggregate and append to karma.conf.js in the project root folder
134-
gulp
135-
.src(path.join(process.cwd(), 'karma.conf.js'))
136-
.on('data', function(karmaConfigFile) {
137-
var contents = karmaConfigFile.contents.toString();
138-
contents += contentAppend;
139-
karmaConfigFile.contents = new Buffer(contents);
140-
stream.push(karmaConfigFile);
141-
})
142-
.on('end', function() {
143-
done();
144-
});
145-
}
146-
147-
return through.obj(transformFn, flushFn);
148-
}
149-
150114
// karma unit tests in local library only
151115
gulp.task('javascript:unit', function () {
152116
return combined.create()
@@ -156,27 +120,18 @@ gulp.task('javascript:unit', function () {
156120
dev: true,
157121
read: false
158122
})
159-
.on('data', function(file) {
160-
console.log('test dependency file:', file.path);
161-
})
162123
)
163124
.append(
164125
streams
165126
.jsSpec()
166127
.pipe(browserify
167128
.compile(80, transforms.concat(browserify.jasmineTransform('@')))
168129
.all('index.js'))
169-
.pipe(gulp.dest(streams.TEST))
130+
.pipe(gulpIf('*.js', gulp.dest(streams.TEST)))
170131
)
171-
.pipe(karmaSpecFile())
132+
.pipe(karma.createConfig())
172133
.pipe(gulp.dest(streams.TEST))
173-
.pipe(karma({
174-
// files : .list,
175-
frameworks: ['jasmine'],
176-
reporters : ['spec'],
177-
browsers : ['Chrome'],
178-
logLevel : 'error'
179-
}, 80));
134+
.pipe(karma.run(80));
180135
});
181136

182137
// give a single optimised javascript file in the build directory with source map for each

0 commit comments

Comments
 (0)