From a8c51640573a8fa0a9b1be22d79048323b3cb2b1 Mon Sep 17 00:00:00 2001 From: Alan Campora Date: Wed, 19 Oct 2016 22:33:16 -0300 Subject: [PATCH] [ADD] Adding testing for turbo add - It was necessary to move the logic to other file. When require was execute in the test file, the command was triggered. --- lib/commands/add.js | 79 ++++++++++++++++++++++++++++++++++++++++++ lib/turbo-add.js | 78 ++++------------------------------------- test/turbo-add.spec.js | 24 +++++++++++++ 3 files changed, 109 insertions(+), 72 deletions(-) create mode 100644 lib/commands/add.js create mode 100644 test/turbo-add.spec.js diff --git a/lib/commands/add.js b/lib/commands/add.js new file mode 100644 index 0000000..bfb5b0b --- /dev/null +++ b/lib/commands/add.js @@ -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; + +})(); diff --git a/lib/turbo-add.js b/lib/turbo-add.js index abfa160..efe5cf6 100644 --- a/lib/turbo-add.js +++ b/lib/turbo-add.js @@ -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(); })(); + diff --git a/test/turbo-add.spec.js b/test/turbo-add.spec.js new file mode 100644 index 0000000..af01806 --- /dev/null +++ b/test/turbo-add.spec.js @@ -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); + }); + +}); +