Skip to content

Commit 250151f

Browse files
committed
=BG= karma reports can be specified using both absolute paths and project local installations
1 parent efa3970 commit 250151f

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

lib/test/karma.js

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,68 @@ function quote(value) {
1616
return '"' + value + '"';
1717
}
1818

19+
var defaultReporterName = 'angularity-karma-reporter';
1920
var filesAppendRegex = /\/\*+\s*ANGULARITY_FILE_LIST\s*\*+\// ;
21+
var reportersAppendRegex = /\/\*+\s*ANGULARITY_REPORTER_LIST\s*\*+\// ;
22+
var pluginsAppendRegex = /\/\*+\s*ANGULARITY_PLUGIN_LIST\s*\*+\// ;
23+
var karmaReporterMatchNameRegex = /^karma-(.+)-reporter$/ ;
24+
25+
/**
26+
* Determine the `require` path for a plugin.
27+
*
28+
* @param {string} reporterName Either a package name,
29+
* *OR* an absolute path, for a karma reporter.
30+
* @return {string} In karma config file, `plugins` will be inline `require`d
31+
* using this return value.
32+
*/
33+
function getKarmaReporterPluginPath(reporterName) {
34+
if (typeof reporterName !== 'string') {
35+
throw 'Get Karma Reporter Plugin Path: Reporter name is unspecified';
36+
}
37+
38+
if (reporterName === defaultReporterName) {
39+
return defaultReporterName;
40+
}
41+
else {
42+
var reporterPath = (path.dirname(reporterName) === '.') ?
43+
path.resolve('node_modules', 'karma-'+reporterName+'-reporter') :
44+
reporterName;
45+
try {
46+
require(reporterPath);
47+
}
48+
catch (ex) {
49+
throw 'Get Karma Reporter Plugin Path: Attempt to require reporter from path '+reporterPath+' with no success.';
50+
}
51+
return reporterPath;
52+
}
53+
}
54+
55+
/**
56+
* Determine the registered reporter name.
57+
*
58+
* @param {string} reporterName Either the registered reporter name,
59+
* *OR* an absolute path, for a karma reporter.
60+
* @return {string} The registered reporter name for use in
61+
* the `reporters` section of the karma config file.
62+
*/
63+
function getKarmaReporterName(reporterName) {
64+
if (typeof reporterName !== 'string') {
65+
throw 'Get Karma Reporter Name: Reporter name is unspecified';
66+
}
67+
68+
var name;
69+
if (path.dirname(reporterName) === '.') {
70+
name = reporterName;
71+
}
72+
else {
73+
name = path.basename(reporterName);
74+
}
75+
var match = karmaReporterMatchNameRegex.exec(name);
76+
if (!!match && match.length === 2) {
77+
name = match[1];
78+
}
79+
return name
80+
}
2081

2182
/**
2283
* Create a through2 object stream.
@@ -27,13 +88,15 @@ var filesAppendRegex = /\/\*+\s*ANGULARITY_FILE_LIST\s*\*+\// ;
2788
* The new karma config file is added to the stream,
2889
* All input `*.js` files are filtered out of the stream
2990
*
30-
* @param {string} configFileName The project local karma config file to augment
91+
* @param {Array.<string>} [reporters] The name of the karma reporter to use
92+
* @param {string} [configFileName] The project local karma config file to augment
3193
* Defaults to "karma.conf.js"
3294
* @return {stream.Through} The output of this stream is expected to contain
3395
* just one file: the augmented karma config file.
3496
*/
35-
function karmaCreateConfig(configFileName) {
36-
configFileName = 'karma.conf.js';
97+
function karmaCreateConfig(reporters, configFileName) {
98+
reporters = reporters || [];
99+
configFileName = configFileName || 'karma.conf.js';
37100
var files = [];
38101

39102
function transformFn(file, encoding, done) {
@@ -47,13 +110,20 @@ function karmaCreateConfig(configFileName) {
47110
function flushFn(done) {
48111
var stream = this;
49112
var filesAppend = JSON.stringify(files, null, ' ');
113+
var reportersAppend = JSON.stringify(reporters.map(getKarmaReporterName), null, ' ');
114+
var pluginsAppend = '[\n' + reporters.map(function(reporter) {
115+
return 'require("'+getKarmaReporterPluginPath(reporter)+'")';
116+
}).join(',\n ') + '\n]';
50117

51118
//aggregate and append to karma.conf.js in the project root folder
52119
gulp
53120
.src(path.resolve(configFileName))
54121
.on('data', function(karmaConfigFile) {
55122
var contents = karmaConfigFile.contents.toString();
56123
contents = contents.replace(filesAppendRegex, filesAppend);
124+
contents = contents.replace(reportersAppendRegex, reportersAppend);
125+
contents = contents.replace(reportersAppendRegex, reportersAppend);
126+
contents = contents.replace(pluginsAppendRegex, pluginsAppend);
57127
karmaConfigFile.contents = new Buffer(contents);
58128
stream.push(karmaConfigFile);
59129
})
@@ -70,13 +140,15 @@ function karmaCreateConfig(configFileName) {
70140
* No output. Ends when the Karma process ends.
71141
* Runs karma in a child process, to avoid `process.exit()` called by karma.
72142
*
143+
* @param {Array.<string>} [reporters] The name of the karma reporter to use
73144
* @param {number} [bannerWidth] The width of banner comment, zero or omitted for none
74145
* @returns {stream.Through} A through strconcateam that performs the operation of a gulp stream
75146
*/
76-
function karmaRun(bannerWidth) {
147+
function karmaRun(reporters, bannerWidth) {
77148
var options = {
78149
configFile: undefined
79150
};
151+
reporters = reporters || [];
80152

81153
return through.obj(function transformFn(file, encoding, done) {
82154
options.configFile = file.path;
@@ -87,23 +159,32 @@ function karmaRun(bannerWidth) {
87159
var data = querystring.escape(JSON.stringify(options));
88160
var command = [ 'node', quote(appPath), data ].join(' ');
89161

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
94162
childProcess.exec(command, { cwd: process.cwd() }, function reporter(stderr, stdout) {
163+
if (reporters.length > 0) {
164+
if (stdout) {
165+
process.stdout.write(stdout);
166+
}
167+
if (stderr) {
168+
process.stderr.write(stderr);
169+
}
170+
done();
171+
return;
172+
}
173+
174+
//TODO @bguiz replace reporter function with a standard karma reporter,
175+
//and extract it to own module
176+
177+
//default reporter by parsing the stdout and stderr of karma
95178
var report;
96179
if (stdout) {
97-
console.log(stdout);
98180
report = stdout
99181
.replace(/^\s+/gm, '') // remove leading whitespace
100182
.replace(/^(LOG.*\n$)/gm, options.silent ? '' : '$1') // remove logging
101183
.replace(/at\s+null\..*/gm, '') // remove file reference
102184
.replace(/\n\n/gm, '\n') // consolidate consecutive line breaks
103185
.replace(/^\n|\n$/g, ''); // remove leading and trailing line breaks overall
104-
} else if (stderr) {
105-
console.log(stderr);
106-
186+
}
187+
else if (stderr) {
107188
var analysis = /$Error\:\s*(.*)$/mg.exec(stderr);
108189
report = analysis ? analysis[1] : stderr;
109190
}

lib/util/jshint-reporter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var yargsOptionDefiniton = {
3535
var jsHintReporter;
3636
function getJsHintReporter(reporterName) {
3737
if (typeof reporterName !== 'string') {
38-
throw 'Reporter name is unspecified';
38+
throw 'Get JsHint Reporter: Reporter name is unspecified';
3939
}
4040
if (jsHintReporter) {
4141
return jsHintReporter; //cached copy

tasks/javascript.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ gulp.task('javascript:lint', function () {
113113

114114
// karma unit tests in local library only
115115
gulp.task('javascript:unit', function () {
116+
var reporters = [];
116117
return combined.create()
117118
.append(
118119
streams
@@ -130,9 +131,9 @@ gulp.task('javascript:unit', function () {
130131
.pipe(gulp.dest(streams.TEST))
131132
.pipe(gulpFilter('*.js'))
132133
)
133-
.pipe(karma.createConfig())
134+
.pipe(karma.createConfig(reporters))
134135
.pipe(gulp.dest(streams.TEST))
135-
.pipe(karma.run(80));
136+
.pipe(karma.run(reporters, 80));
136137
});
137138

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

templates/angularity/karma.conf.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ module.exports = function(config) {
1919
// list of files to exclude
2020
exclude: [],
2121

22+
// register any plugins which are not siblings of karma in angularity global installation
23+
// and thus need to be registered manually
24+
plugins: config.plugins.concat(/*ANGULARITY_PLUGIN_LIST*/),
25+
2226
// use dots reporter, as travis terminal does not support escaping sequences
2327
// possible values: 'dots', 'progress', 'junit', 'teamcity'
24-
reporter: [],
28+
reporters: [].concat(/*ANGULARITY_REPORTER_LIST*/),
2529

2630
// web server port
2731
port: <%= port %>,

0 commit comments

Comments
 (0)