Skip to content

Commit

Permalink
[ADD] Adding testing for turbo add
Browse files Browse the repository at this point in the history
     - It was necessary to move the logic to other file. When require was execute in the test file, the command was triggered.
  • Loading branch information
alancampora committed Oct 20, 2016
1 parent b9c776b commit a8c5164
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 72 deletions.
79 changes: 79 additions & 0 deletions lib/commands/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env node
/**
* How porcelain works
* When a new file is created , it will add a ?? before the file
* When a file is modified, it shows ' M'. Yes, one space and M (this has a reason)
* When a file was modified and added to the staging area, and modified once again, it shows MM
* (that's why there is an space for the modified files, to show the output aligned)
*/
(function () {
'use strict';

var childProcess = require('child_process'),
inquirer = require('inquirer'),
utils = require('../../bin/utils')();

function Add(){

this.init = function(){

var gitStatusPorcelain = childProcess.exec('git status --porcelain');

gitStatusPorcelain.stdout.on('data', function (data) {
var files = this.getNoStagedFiles(data);

if (!files) {
utils.showError('No files to add');
return;
}
this.promptFileSelection(files);
}.bind(this));
};

/**
* returns string 'file1 file2 file2'
* @param data
*/
this.getNoStagedFiles = function(data) {
var newFiles = data.match(this.newFilesRegex(data)) || [], //with ?? and M string
formattedArray = [];

newFiles.forEach(function (item, index) {
item = item.replace(/^ M/, '');
item = item.replace(/^MM/, '');
item = item.replace(/\?\?./, '');
formattedArray.push(item);
});
return formattedArray;
};

this.newFilesRegex = function () {
return new RegExp('(\\?\\?.*)|(^( M|MM).*)', 'mg');
};

this.promptFileSelection = function (files) {
inquirer.prompt([
{
type: 'checkbox',
message: 'Select files to add',
name: 'files',
choices: files
}
]).then(function (selection) {
var command = 'git add ' + selection.files.join(' '),
gitAdd = childProcess.exec(command);

gitAdd.stdout.on('data', function (data) {
console.log(data);
});

gitAdd.stderr.on('err', function (err) {
console.log(err);
});
});
};
}

module.exports = Add;

})();
78 changes: 6 additions & 72 deletions lib/turbo-add.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,9 @@
#!/usr/bin/env node
/**
* How porcelain works
* When a new file is created , it will add a ?? before the file
* When a file is modified, it shows ' M'. Yes, one space and M (this has a reason)
* When a file was modified and added to the staging area, and modified once again, it shows MM
* (that's why there is an space for the modified files, to show the output aligned)
*/
(function () {
(function(){
'use strict';

var childProcess = require('child_process'),
inquirer = require('inquirer'),
utils = require('../bin/utils')();


init();

function init() {
var gitStatusPorcelain = childProcess.exec('git status --porcelain');

gitStatusPorcelain.stdout.on('data', function (data) {
var files = getNoStagedFiles(data);

if (!files) {
utils.showError('No files to add');
return;
}
promptFileSelection(files);
});
}

/**
* returns string 'file1 file2 file2'
* @param data
*/
function getNoStagedFiles(data) {
var newFiles = data.match(newFilesRegex(data)) || [], //with ?? and M string
formattedArray = [];

newFiles.forEach(function (item, index) {
item = item.replace(/^ M/, '');
item = item.replace(/^MM/, '');
item = item.replace(/\?\?./, '');
formattedArray.push(item);
});
return formattedArray;
}

function newFilesRegex() {
return new RegExp('(\\?\\?.*)|(^( M|MM).*)', 'mg');
}

function promptFileSelection(files) {
inquirer.prompt([
{
type: 'checkbox',
message: 'Select files to add',
name: 'files',
choices: files
}
]).then(function (selection) {
var command = 'git add ' + selection.files.join(' '),
gitAdd = childProcess.exec(command);

gitAdd.stdout.on('data', function (data) {
console.log(data);
});

gitAdd.stderr.on('err', function (err) {
console.log(err);
});
});
}

var Add = require('./commands/add');
var addCommand = new Add();
addCommand.init();
})();

24 changes: 24 additions & 0 deletions test/turbo-add.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node
var consoleMock = require('console-mock'),
consoleTest = consoleMock.create(),
turboAdd = require('../lib/turbo-add'),
childProcess = require('child_process');

describe('turbo-add', function(){
'use strict';

it('init', function(){
var gitPorcelainOutput = '?? file1.js \n?? file2.js';
var getNoStagedFilesValue = ['file1.js','file2.js'];

spyOn(childProcess, "exec").andReturn(gitPorcelainOutput);

spyOn("getNoStagedFiles").andReturn(getNoStagedFilesValue);

turboAdd.init();

spyOn('promptFileSelection').toHaveBeenCalledWith(getNoStagedFilesValue);
});

});

0 comments on commit a8c5164

Please sign in to comment.