Skip to content

Commit

Permalink
Fixed premature deferred.resolve() in lib/replace.js when passed file…
Browse files Browse the repository at this point in the history
…s array contains more than one file by adding filesEvaluated counter. Patched associated nodeunit tests.
  • Loading branch information
drewlustro committed Jan 22, 2014
1 parent c42e17a commit 0761521
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports.init = function(grunt) {
// iterate over all modules that are configured for replacement
config.replaceRequireScript.forEach(function (file, idx) {
var files = grunt.file.expand(file.files);
var filesEvaluated = 0;

// iterate over found html files
files.forEach(function(file) {
Expand All @@ -47,7 +48,12 @@ exports.init = function(grunt) {
var html = hasBody ? window.document.innerHTML : window.document.body.innerHTML;
grunt.log.writeln('Updating requirejs script tag for file', file);
grunt.file.write(file, html);
deferred.resolve(config);

// only resolve after all files have been evaluated
filesEvaluated++;
if (filesEvaluated >= files.length) {
deferred.resolve(config);
}
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/require_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ exports['require'] = {
replaceAlmondInHtmlFiles(config).then(function() {
var replacedFileContents1 = grunt.file.read(config.replaceRequireScript[0].files[0]);
var replacedFileContents2 = grunt.file.read(config.replaceRequireScript[0].files[1]);
test.ok(replacedFileContents1, 'should replace script tag ´src´ contents');
test.ok(replacedFileContents1.search('<script src="js/main.js"></script>') > -1, 'should replace script tag ´src´ contents');
test.ok(replacedFileContents2.search('<script src="js/main.js"></script>') > -1, 'should replace script tag ´src´ contents');
test.done();
});
Expand Down

0 comments on commit 0761521

Please sign in to comment.