Repository: ember-cli/ember-cli
Pull request #6521 (2016-12-04 00:07:19)

fix missing git scenarios
- [x] figure out if this fixes the issue I saw on windows, if not figure out what that issue was and fix it
- [ ] r?


Patch:
diff --git a/lib/tasks/git-init.js b/lib/tasks/git-init.js
index a2e93b2aa..4aebe887a 100644
--- a/lib/tasks/git-init.js
+++ b/lib/tasks/git-init.js
@@ -2,7 +2,7 @@
 
 var Promise  = require('../../lib/ext/promise');
 var Task     = require('../models/task');
-var exec     = Promise.denodeify(require('child_process').exec);
+var _exec    = Promise.denodeify(require('child_process').exec);
 var path     = require('path');
 var pkg      = require('../../package.json');
 var fs       = require('fs');
@@ -15,36 +15,45 @@ var gitEnvironmentVariables = {
 };
 
 module.exports = Task.extend({
-  run: function(commandOptions) {
+  init: function(_options) {
+    var options = _options || {};
+    options.exec = options.exec || _exec;
+    this._super.init.apply(this, arguments);
+  },
+
+  run: function(_commandOptions) {
+    var commandOptions = _commandOptions || {};
     var template = require('lodash.template');
     var chalk  = require('chalk');
     var ui     = this.ui;
+    var exec   = this.exec;
 
     if (commandOptions.skipGit) {
       return Promise.resolve();
     }
 
-    return exec('git --version')
-      .then(function() {
-        return exec('git init')
-          .then(function() {
-            return exec('git add .');
-          })
-          .then(function() {
-            var commitTemplate = fs.readFileSync(path.join(__dirname, '../utilities/COMMIT_MESSAGE.txt'));
-            var commitMessage = template(commitTemplate)(pkg);
-            return exec('git commit -m "' + commitMessage + '"', {env: gitEnvironmentVariables});
-          })
-          .then(function() {
-            ui.writeLine(chalk.green('Successfully initialized git.'));
-          });
-      })
+    var gitVersionWorked = false;
+    return exec('git --version').then(function() {
+      gitVersionWorked = true;
+
+      return exec('git init')
+        .then(function() {
+          return exec('git add .');
+        })
+        .then(function() {
+          var commitTemplate = fs.readFileSync(path.join(__dirname, '../utilities/COMMIT_MESSAGE.txt'));
+          var commitMessage = template(commitTemplate)(pkg);
+          return exec('git commit -m "' + commitMessage + '"', {env: gitEnvironmentVariables});
+        })
+        .then(function() {
+          ui.writeLine(chalk.green('Successfully initialized git.'));
+        });
+    })
       .catch(function(error) {
-        if (commandOptions.logErrors) {
-          ui.writeError(error);
+        if (gitVersionWorked) {
+          throw error;
         }
-        // if git is not found or an error was thrown during the `git`
-        // init process just swallow any errors here
-      });
+        // otherwise git version failed, so we skip git stuff.
+      })
   }
 });


Test patch:
diff --git a/tests/unit/tasks/git-init-test.js b/tests/unit/tasks/git-init-test.js
index 4a4ac00be..aac8c030a 100644
--- a/tests/unit/tasks/git-init-test.js
+++ b/tests/unit/tasks/git-init-test.js
@@ -11,17 +11,21 @@ var path        = require('path');
 var root        = process.cwd();
 var mkTmpDirIn  = require('../../../lib/utilities/mk-tmp-dir-in');
 var tmproot     = path.join(root, 'tmp');
+var td          = require('testdouble');
 
 describe('git-init', function() {
-  var subject, ui, tmpdir;
+  var subject, ui, tmpdir, exec;
 
   beforeEach(function() {
+    exec = td.function('exec');
+
     return mkTmpDirIn(tmproot).then(function(dir) {
       tmpdir  = dir;
       ui      = new MockUI();
       subject = new GitInitTask({
         ui: ui,
-        project: new MockProject()
+        project: new MockProject(),
+        exec: exec
       });
       process.chdir(tmpdir);
     });
@@ -33,20 +37,48 @@ describe('git-init', function() {
     return remove(tmproot);
   });
 
-/**
-* TODO: git commit not working with this test setup, it errors out.
-* We need to be able to
-*/
-  it('skipGit properly skips git-init', function() {
-    return subject.run({skipGit:true}).then(function() {
-      expect(ui.output).to.not.include('Successfully initialized git.');
+  describe('skipGit: true', function() {
+    it('does not initialize git', function() {
+      td.when(exec()).thenResolve();
+
+      return subject.run({
+        skipGit: true
+      }).then(function() {
+        expect(ui.output).to.not.include('Successfully initialized git.');
+        td.verify(exec('git --version'), { times: 0 });
+      });
     });
   });
 
-  it('errors are logged with logErrors', function() {
-    return subject.run({skipGit: false, logErrors: true}).then(function() {
-      expect(ui.output).to.equal('');
-      expect(ui.errors).to.not.equal('');
+  it('correctly initializes git if git is around, and more or less works', function() {
+    td.when(exec(td.matchers.contains('git --version'))).thenResolve();
+    td.when(exec(td.matchers.contains('git init'))).thenResolve();
+    td.when(exec(td.matchers.contains('git add .'))).thenResolve();
+    td.when(exec(td.matchers.contains('git commit -m'))).thenResolve();
+
+    return subject.run().then(function() {
+      td.verify(exec(td.matchers.contains('git --version')));
+      td.verify(exec(td.matchers.contains('git init')));
+      td.verify(exec(td.matchers.contains('git add .')));
+      td.verify(exec(td.matchers.contains('git commit -m "'), td.matchers.anything()));
+
+      expect(ui.output).to.contain('Successfully initialized git.');
+      expect(ui.errors).to.equal('');
+    });
+  });
+
+
+  it('skips initializing git, if `git --version` fails', function() {
+    td.when(exec(td.matchers.contains('git --version'))).thenReject();
+
+    return subject.run().then(function() {
+      td.verify(exec(td.matchers.contains('git --version')), { times: 1 });
+      td.verify(exec(td.matchers.contains('git init')), { times: 0 });
+      td.verify(exec(td.matchers.contains('git add .')), { times: 0 });
+      td.verify(exec(td.matchers.contains('git commit -m "'), td.matchers.anything()), { times: 0 });
+
+      expect(ui.output).to.contain('');
+      expect(ui.errors).to.equal('');
     });
   });
 });