From 10ca07840c2cfddbe80eb53199fb7fc26cd3f200 Mon Sep 17 00:00:00 2001 From: Thomas Mchugh Date: Tue, 7 Apr 2015 15:11:50 -0500 Subject: [PATCH 1/2] added tests to cli --- cli.js | 10 +------ local-cli/__tests__/setupPodfileTests.js | 38 ++++++++++++++++++++++++ local-cli/install.js | 14 +++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 local-cli/__tests__/setupPodfileTests.js diff --git a/cli.js b/cli.js index 4746bdbc8..7a36c4c6e 100644 --- a/cli.js +++ b/cli.js @@ -5,12 +5,4 @@ 'use strict'; -var cli = require('./local-cli/cli.js'); - -function run() { - cli.run(); -} - -module.exports = { - run: run -}; +module.exports = require('./local-cli/cli.js'); diff --git a/local-cli/__tests__/setupPodfileTests.js b/local-cli/__tests__/setupPodfileTests.js new file mode 100644 index 000000000..c970e769c --- /dev/null +++ b/local-cli/__tests__/setupPodfileTests.js @@ -0,0 +1,38 @@ +'use strict'; + +jest.dontMock('../install'); + +var install = require('../install.js'); +var fs = require('fs'); + +describe('setup Podfile', function() { + it('creates a Podfile', function() { + fs.mkdirSync('./podfile-test'); + process.chdir('./podfile-test'); + var setupPodfile = install.setupPodfile(); + + expect(setupPodfile.podfileExists()).toBe(true); + process.chdir('../'); + }); + + it('expects Podfile to containing opening & closing react native tag', function() { + process.chdir('./podfile-test'); + var setupPodfile = install.setupPodfile(); + + var openingReactTag = '#'; + var closingReactTag = '#'; + + expect(setupPodfile.podfileText()).toContain(openingReactTag); + expect(setupPodfile.podfileText()).toContain(closingReactTag); + process.chdir('../'); + }); + + it('expects Podfile to contain React Pod', function() { + process.chdir('./podfile-test'); + var setupPodfile = install.setupPodfile(); + expect(setupPodfile.podfileText()).toContain('pod \'React\''); + process.chdir('../'); + fs.unlink("./podfile-test/Podfile"); + fs.rmdirSync("./podfile-test"); + }); +}); diff --git a/local-cli/install.js b/local-cli/install.js index c3de98aef..e0158620f 100644 --- a/local-cli/install.js +++ b/local-cli/install.js @@ -83,6 +83,20 @@ module.exports = { throw e; } } + + return { + podfileExists: function() { + try { + var podfileText = fs.readFileSync(PODFILE_PATH, 'utf8'); + return true; + } catch(e) { + return false; + } + }, + podfileText: function() { + return podfileText; + } + }; }, init: function(arguement) { // arguement is available for future arguement commands From 800502f2a032edf121a50c10bbea3d397434eb55 Mon Sep 17 00:00:00 2001 From: Thomas Mchugh Date: Tue, 7 Apr 2015 20:26:17 -0500 Subject: [PATCH 2/2] Updates tests to work with new install output --- local-cli/__tests__/install-test.js | 44 ++++++++++++++++++++++++ local-cli/__tests__/setupPodfileTests.js | 38 -------------------- local-cli/install.js | 26 ++++++-------- 3 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 local-cli/__tests__/install-test.js delete mode 100644 local-cli/__tests__/setupPodfileTests.js diff --git a/local-cli/__tests__/install-test.js b/local-cli/__tests__/install-test.js new file mode 100644 index 000000000..b3d374522 --- /dev/null +++ b/local-cli/__tests__/install-test.js @@ -0,0 +1,44 @@ +'use strict'; + +jest.dontMock('../install'); +jest.dontMock('fs'); +jest.dontMock('path'); + +var install = require('../install.js'); + +var openingReactTag = '#'; +var closingReactTag = '#'; + +var fs = require.requireActual('fs'); +var path = require.requireActual('path'); + +process.chdir(__dirname); + +describe('setup Podfile', function() { + + it('creates a Podfile if none exists', function() { + try { + fs.unlinkSync(path.resolve(__dirname, 'Podfile')); + } catch(e) {} + + var setupPodfile = install.setupPodfile(); + + expect(setupPodfile.created).toBe(true); + }); + + it('does not create Podfile if one exists', function() { + var setupPodfile = install.setupPodfile(); + expect(setupPodfile.created).toBe(false); + }); + + it('includes React Native Tags', function() { + var setupPodfile = install.setupPodfile(); + + expect(setupPodfile.podfileText).toContain(openingReactTag); + expect(setupPodfile.podfileText).toContain(closingReactTag); + + try { + fs.unlinkSync(path.resolve(__dirname, 'Podfile')); + } catch(e) {} + }); +}); diff --git a/local-cli/__tests__/setupPodfileTests.js b/local-cli/__tests__/setupPodfileTests.js deleted file mode 100644 index c970e769c..000000000 --- a/local-cli/__tests__/setupPodfileTests.js +++ /dev/null @@ -1,38 +0,0 @@ -'use strict'; - -jest.dontMock('../install'); - -var install = require('../install.js'); -var fs = require('fs'); - -describe('setup Podfile', function() { - it('creates a Podfile', function() { - fs.mkdirSync('./podfile-test'); - process.chdir('./podfile-test'); - var setupPodfile = install.setupPodfile(); - - expect(setupPodfile.podfileExists()).toBe(true); - process.chdir('../'); - }); - - it('expects Podfile to containing opening & closing react native tag', function() { - process.chdir('./podfile-test'); - var setupPodfile = install.setupPodfile(); - - var openingReactTag = '#'; - var closingReactTag = '#'; - - expect(setupPodfile.podfileText()).toContain(openingReactTag); - expect(setupPodfile.podfileText()).toContain(closingReactTag); - process.chdir('../'); - }); - - it('expects Podfile to contain React Pod', function() { - process.chdir('./podfile-test'); - var setupPodfile = install.setupPodfile(); - expect(setupPodfile.podfileText()).toContain('pod \'React\''); - process.chdir('../'); - fs.unlink("./podfile-test/Podfile"); - fs.rmdirSync("./podfile-test"); - }); -}); diff --git a/local-cli/install.js b/local-cli/install.js index e0158620f..63e763667 100644 --- a/local-cli/install.js +++ b/local-cli/install.js @@ -5,11 +5,12 @@ 'use strict'; var fs = require('fs'); +var path = require('path'); var exec = require('child_process').exec; -var NODE_MODULE_PATH = './node_modules'; +var NODE_MODULE_PATH = path.resolve(__dirname, 'node_modules'); -var PODFILE_PATH = './Podfile'; +var PODFILE_PATH = path.resolve(__dirname, 'Podfile'); function addDependency(name, path) { console.log('Found dependency: ' + name); @@ -48,6 +49,10 @@ function installDependecies() { module.exports = { setupPodfile: function() { + var returnArgs = { + created: false + }; + var podfileText; try { podfileText = fs.readFileSync(PODFILE_PATH, 'utf8'); @@ -58,6 +63,7 @@ module.exports = { var reactPodfileBoilerplate = openingReactTag + closingReactTag; if (!podfileText) { + returnArgs.created = true; fs.appendFileSync(PODFILE_PATH, reactPodfileBoilerplate); } else { if (podfileText.indexOf(openingReactTag) === -1 || podfileText.indexOf(closingReactTag) === -1) { @@ -67,6 +73,7 @@ module.exports = { try { podfileText = fs.readFileSync(PODFILE_PATH, 'utf8'); + returnArgs.podfileText = podfileText; } catch(e) {} if (podfileText.indexOf('pod \'React\'') === -1) { @@ -79,24 +86,13 @@ module.exports = { podfileText.slice(indexOfReactComponents)].join(''); fs.writeFileSync(PODFILE_PATH, newPodfileText); + returnArgs.podfileText = newPodfileText; } catch(e) { throw e; } } - return { - podfileExists: function() { - try { - var podfileText = fs.readFileSync(PODFILE_PATH, 'utf8'); - return true; - } catch(e) { - return false; - } - }, - podfileText: function() { - return podfileText; - } - }; + return returnArgs; }, init: function(arguement) { // arguement is available for future arguement commands