Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
Escape new lines and quotes for teamcity
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Akselsson committed Aug 29, 2014
1 parent c683f5a commit d994460
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Gruntfile.js
Expand Up @@ -68,6 +68,13 @@ module.exports = function(grunt) {
grunt.registerTask('isolated_test', function(){
grunt.log.warn('foo-bar-uniqe-string');
});
grunt.registerTask('isolated_test_single_quotes', function(){
grunt.log.warn("single'quote");
});
grunt.registerTask('isolated_test_line_breaks', function(){
grunt.log.warn("new\n line");
});

grunt.registerTask('isolated_test_parse_twice', function(){
grunt.log.warn("##teamcity['someMessage']");
});
Expand Down
10 changes: 7 additions & 3 deletions tasks/teamcity.js
Expand Up @@ -77,9 +77,13 @@ module.exports = function(grunt) {
return text;
}

return ("##teamcity[message text='" + text +
"' errorDetails='" + (errorDetails || '') +
"' status='" + status + "']\n").grey;
return ("##teamcity[message text='" + escape(text) +
"' errorDetails='" + escape(errorDetails || '') +
"' status='" + escape(status) + "']\n").grey;
}

function escape(text){
return (text || '').replace("'","|'").replace("\n","|n").replace("\r","|r").replace("\f","|f");
}

// verbatum from grunt.log
Expand Down
30 changes: 30 additions & 0 deletions test/teamcity_test.js
Expand Up @@ -83,5 +83,35 @@ exports.teamcity = {
test.equal(tcMsgCount, 0, 'Teamcity messages are not parsed twice');
test.done();
});
},
single_quote_escaping: function(test) {
test.expect(1);
exec('grunt teamcity:default_options isolated_test_single_quotes --no-color', function(err, stdout){
var tcMsgCount = 0;
stdout.split('\n').forEach(function(line){
console.log(line);
if (line.contains("##teamcity[message text='single|'quote' errorDetails='' status='ERROR'")) {
tcMsgCount++;
}
});

test.equal(tcMsgCount, 1, 'Single quotes are encoded');
test.done();
});
},
line_break_escaping: function(test) {
test.expect(1);
exec('grunt teamcity:default_options isolated_test_line_breaks --no-color', function(err, stdout){
var tcMsgCount = 0;
stdout.split('\n').forEach(function(line){
console.log(line);
if (line.contains("##teamcity[message text='new|n line' errorDetails='' status='ERROR'")) {
tcMsgCount++;
}
});

test.equal(tcMsgCount, 1, 'Line breaks are stripped');
test.done();
});
}
};

0 comments on commit d994460

Please sign in to comment.