From 8ac36ced4df3d698e7155a46c4bc6ad40b4c0e0a Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 6 Jan 2015 15:02:03 -0300 Subject: [PATCH] Decouple resin sdk from it's surroundings --- lib/connection/connection.coffee | 5 --- lib/connection/connection.spec.coffee | 31 ---------------- lib/resin/_connection/connection.coffee | 3 ++ lib/resin/_connection/connection.spec.coffee | 37 ++++++++++++++++++++ lib/resin/_server/server.coffee | 2 +- tests/utils/mock.coffee | 2 +- 6 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 lib/resin/_connection/connection.coffee create mode 100644 lib/resin/_connection/connection.spec.coffee diff --git a/lib/connection/connection.coffee b/lib/connection/connection.coffee index f4c2dddf..cd4ee29f 100644 --- a/lib/connection/connection.coffee +++ b/lib/connection/connection.coffee @@ -1,5 +1,4 @@ _ = require('lodash') -isOnline = require('is-online') CONNECTION_PARAMETERS = [ 'network' @@ -7,10 +6,6 @@ CONNECTION_PARAMETERS = [ 'wifiKey' ] -# A wrapper around isOnline in order -# to be able to stub it with Sinon -exports.isOnline = isOnline - validateEthernetConnectionParameters = (parameters = {}) -> return if not parameters.wifiSsid? and not parameters.wifiKey? return new Error('You can only use wifi options if network is wifi') diff --git a/lib/connection/connection.spec.coffee b/lib/connection/connection.spec.coffee index 57c2a33c..f44f93f8 100644 --- a/lib/connection/connection.spec.coffee +++ b/lib/connection/connection.spec.coffee @@ -5,37 +5,6 @@ connection = require('./connection') describe 'Connection:', -> - describe '#isOnline()', -> - - stubIsOnline = (expectations, args...) -> - isOnlineStub = sinon.stub(connection, 'isOnline') - isOnlineStub.yields(args...) - - connection.isOnline (error, isOnline) -> - expectations.apply(null, arguments) - isOnlineStub.restore() - - it 'should be able to return true', (done) -> - stubIsOnline (error, isOnline) -> - expect(error).to.not.exist - expect(isOnline).to.be.true - done() - , null, true - - it 'should be able to return false', (done) -> - stubIsOnline (error, isOnline) -> - expect(error).to.not.exist - expect(isOnline).to.be.false - done() - , null, false - - it 'should be able to return an error', (done) -> - stubIsOnline (error, isOnline) -> - expect(error).to.be.an.instanceof(Error) - expect(isOnline).to.not.exist - done() - , new Error() - describe '#parseConnectionParameters()', -> checkParamsSuccess = (params, done) -> diff --git a/lib/resin/_connection/connection.coffee b/lib/resin/_connection/connection.coffee new file mode 100644 index 00000000..3cb43ace --- /dev/null +++ b/lib/resin/_connection/connection.coffee @@ -0,0 +1,3 @@ +# A wrapper around isOnline in order +# to be able to stub it with Sinon +exports.isOnline = require('is-online') diff --git a/lib/resin/_connection/connection.spec.coffee b/lib/resin/_connection/connection.spec.coffee new file mode 100644 index 00000000..5cd24a6c --- /dev/null +++ b/lib/resin/_connection/connection.spec.coffee @@ -0,0 +1,37 @@ +expect = require('chai').expect +_ = require('lodash') +sinon = require('sinon') +connection = require('./connection') + +describe 'Connection:', -> + + describe '#isOnline()', -> + + stubIsOnline = (expectations, args...) -> + isOnlineStub = sinon.stub(connection, 'isOnline') + isOnlineStub.yields(args...) + + connection.isOnline (error, isOnline) -> + expectations.apply(null, arguments) + isOnlineStub.restore() + + it 'should be able to return true', (done) -> + stubIsOnline (error, isOnline) -> + expect(error).to.not.exist + expect(isOnline).to.be.true + done() + , null, true + + it 'should be able to return false', (done) -> + stubIsOnline (error, isOnline) -> + expect(error).to.not.exist + expect(isOnline).to.be.false + done() + , null, false + + it 'should be able to return an error', (done) -> + stubIsOnline (error, isOnline) -> + expect(error).to.be.an.instanceof(Error) + expect(isOnline).to.not.exist + done() + , new Error() diff --git a/lib/resin/_server/server.coffee b/lib/resin/_server/server.coffee index 01f67a58..accc17e5 100644 --- a/lib/resin/_server/server.coffee +++ b/lib/resin/_server/server.coffee @@ -2,7 +2,7 @@ _ = require('lodash') request = require('request') progress = require('request-progress') async = require('async') -connection = require('../../connection/connection') +connection = require('../_connection/connection') settings = require('../settings') auth = require('../auth/auth') diff --git a/tests/utils/mock.coffee b/tests/utils/mock.coffee index 942ad28b..83477554 100644 --- a/tests/utils/mock.coffee +++ b/tests/utils/mock.coffee @@ -1,7 +1,7 @@ mockFs = require('mock-fs') sinon = require('sinon') resin = require('../../lib/resin') -connection = require('../../lib/connection/connection') +connection = require('../../lib/resin/_connection/connection') exports.fs =