From 58cc0be0c94330565d12415d97930bdfa40b07a8 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 14 Nov 2014 15:48:37 -0400 Subject: [PATCH] Cleanup tests --- lib/cli-modules/auth/auth.spec.coffee | 7 +++++++ lib/config.coffee | 1 + lib/data/data-prefix.spec.coffee | 7 ++++--- lib/data/data.spec.coffee | 23 +++++++++-------------- lib/fs-utils/fs-utils.spec.coffee | 16 ++++++++-------- lib/server/server.spec.coffee | 6 ++++++ lib/token/token.spec.coffee | 7 +++++++ tests/utils/mock.coffee | 18 ++++++++++++++++++ 8 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 tests/utils/mock.coffee diff --git a/lib/cli-modules/auth/auth.spec.coffee b/lib/cli-modules/auth/auth.spec.coffee index de12400f..9b2bb855 100644 --- a/lib/cli-modules/auth/auth.spec.coffee +++ b/lib/cli-modules/auth/auth.spec.coffee @@ -3,11 +3,18 @@ nock = require('nock') async = require('async') auth = require('./auth') config = require('../../config') +mock = require('../../../tests/utils/mock') johnDoeFixture = require('../../../tests/fixtures/johndoe') janeDoeFixture = require('../../../tests/fixtures/janedoe') describe 'Auth:', -> + beforeEach -> + mock.fs.init() + + afterEach -> + mock.fs.restore() + describe 'given valid credentials', -> beforeEach -> diff --git a/lib/config.coffee b/lib/config.coffee index a9aa190e..108f59c2 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -2,3 +2,4 @@ module.exports = # TODO: Should be configurable baseUrl: 'https://staging.resin.io' + dataPrefix: '~/.resin' diff --git a/lib/data/data-prefix.spec.coffee b/lib/data/data-prefix.spec.coffee index f8bcefec..0abcb66a 100644 --- a/lib/data/data-prefix.spec.coffee +++ b/lib/data/data-prefix.spec.coffee @@ -1,11 +1,12 @@ expect = require('chai').expect _ = require('lodash') dataPrefix = require('./data-prefix') +config = require('../config') PREFIXES = - main: '~/.resin' - new: '~/.resin-new' - invalid: { path: '~/.resin' } + main: config.dataPrefix + new: "#{config.dataPrefix}-new" + invalid: { path: '/abc' } describe 'DataPrefix:', -> diff --git a/lib/data/data.spec.coffee b/lib/data/data.spec.coffee index e5ccfc8c..7a2cf27e 100644 --- a/lib/data/data.spec.coffee +++ b/lib/data/data.spec.coffee @@ -1,12 +1,11 @@ expect = require('chai').expect _ = require('lodash') fsUtils = require('../fs-utils/fs-utils') +mock = require('../../tests/utils/mock') async = require('async') -mockFs = require('mock-fs') +config = require('../config') data = require('./data') -PREFIX = '~/.resin' - FILES_FIXTURES = hello: filename: 'hello_world.test' @@ -17,15 +16,15 @@ FILES_FIXTURES = FILESYSTEM = text: - name: "#{PREFIX}/text" + name: "#{config.dataPrefix}/text" contents: 'Hello World' key: 'text' directory: - name: "#{PREFIX}/directory" - contents: mockFs.directory() + name: "#{config.dataPrefix}/directory" + contents: {} key: 'directory' nested: - name: "#{PREFIX}/nested/text" + name: "#{config.dataPrefix}/nested/text" contents: 'Nested Hello World' key: 'nested/text' @@ -60,15 +59,11 @@ describe 'Data:', -> describe 'given a prefix', -> beforeEach -> - mockFsOptions = {} - for key, value of FILESYSTEM - mockFsOptions[value.name] = value.contents - mockFs(mockFsOptions) - - data.prefix.set(PREFIX) + mock.fs.init(FILESYSTEM) + data.prefix.set(config.dataPrefix) afterEach -> - mockFs.restore() + mock.fs.restore() data.prefix.clear() describe '#get()', -> diff --git a/lib/fs-utils/fs-utils.spec.coffee b/lib/fs-utils/fs-utils.spec.coffee index 669a8c7d..0a5a6095 100644 --- a/lib/fs-utils/fs-utils.spec.coffee +++ b/lib/fs-utils/fs-utils.spec.coffee @@ -1,6 +1,8 @@ expect = require('chai').expect -mockFs = require('mock-fs') +mock = require('../../tests/utils/mock') fsUtils = require('./fs-utils') +config = require('../config') +data = require('../data/data') FILESYSTEM = text: @@ -8,7 +10,7 @@ FILESYSTEM = contents: 'Hello World' directory: name: '/tmp/directory' - contents: mockFs.directory() + contents: {} describe 'FsUtils:', -> @@ -28,8 +30,8 @@ describe 'FsUtils:', -> it 'should return true for valid paths', -> for validPath in [ + config.dataPrefix '/Users/johndoe' - '~/.resin' '../parent' './file/../file2' ] @@ -38,13 +40,11 @@ describe 'FsUtils:', -> describe '#isDirectory()', -> beforeEach -> - mockFsOptions = {} - for key, value of FILESYSTEM - mockFsOptions[value.name] = value.contents - mockFs(mockFsOptions) + mock.fs.init(FILESYSTEM) + data.prefix.set(config.dataPrefix) afterEach -> - mockFs.restore() + mock.fs.restore() it 'should return true if directory', (done) -> fsUtils.isDirectory FILESYSTEM.directory.name, (error, isDirectory) -> diff --git a/lib/server/server.spec.coffee b/lib/server/server.spec.coffee index a1fd42f0..29cf5582 100644 --- a/lib/server/server.spec.coffee +++ b/lib/server/server.spec.coffee @@ -3,6 +3,7 @@ nock = require('nock') server = require('./server') config = require('../config') token = require('../token/token') +mock = require('../../tests/utils/mock') johnDoeFixture = require('../../tests/fixtures/johndoe.json') TEST_URI = config.baseUrl @@ -38,6 +39,11 @@ describe 'Server:', -> lowercaseMethod = method.toLowerCase() nock(TEST_URI)[lowercaseMethod](URI.ok).reply(200, status: STATUS.ok) + mock.fs.init() + + afterEach -> + mock.fs.restore() + describe '#request()', -> it 'should make a real HTTP request', (done) -> diff --git a/lib/token/token.spec.coffee b/lib/token/token.spec.coffee index c4b01bbc..4b7b86fc 100644 --- a/lib/token/token.spec.coffee +++ b/lib/token/token.spec.coffee @@ -1,12 +1,19 @@ expect = require('chai').expect async = require('async') token = require('./token') +mock = require('../../tests/utils/mock') johnDoeFixture = require('../../tests/fixtures/johndoe.json') janeDoeFixture = require('../../tests/fixtures/janedoe.json') describe 'Token:', -> + beforeEach -> + mock.fs.init() + + afterEach -> + mock.fs.restore() + describe 'given a user that is logged in', -> beforeEach (done) -> diff --git a/tests/utils/mock.coffee b/tests/utils/mock.coffee new file mode 100644 index 00000000..1c36b5c6 --- /dev/null +++ b/tests/utils/mock.coffee @@ -0,0 +1,18 @@ +mockFs = require('mock-fs') +config = require('../../lib/config') + +exports.fs = + + init: (filesystemConfig = {}) -> + mockFsOptions = {} + + # Mock data prefix automatically to remove + # duplication in most of the tests + mockFsOptions[config.dataPrefix] = mockFs.directory() + + for key, value of filesystemConfig + mockFsOptions[value.name] = value.contents + mockFs(mockFsOptions) + + restore: -> + mockFs.restore()