Skip to content

Commit

Permalink
Merge pull request #26 from xicombd/browser
Browse files Browse the repository at this point in the history
Running the tests in the browser
  • Loading branch information
daviddias committed Jan 21, 2016
2 parents 05ce945 + d6a2e48 commit e24b7f6
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 157 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ before_install:
# Workaround for a permissions issue with Travis virtual machine images
script:
- npm test

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
73 changes: 73 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module.exports = function (config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',

// frameworks to use
frameworks: ['mocha'],

// list of files / patterns to load in the browser
files: [
'tests/browser-tests.js'
],

// list of preprocessors
preprocessors: {
'tests/*': ['webpack']
},

webpack: {
resolve: {
extensions: ['', '.js']
},
externals: {
fs: '{}'
},
node: {
Buffer: true
}
},

webpackMiddleware: {
noInfo: true,
stats: {
colors: true
}
},

// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['spec'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
})
}
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "IPFS Repo implementation",
"main": "src/index.js",
"scripts": {
"test": "mocha tests/*-test.js",
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha tests/node-tests.js",
"test:browser": "karma start karma.conf.js",
"coverage": "istanbul cover --print both -- _mocha tests/*-test.js",
"lint": "standard"
},
Expand All @@ -21,20 +23,32 @@
],
"homepage": "https://github.com/ipfs/js-ipfs-repo",
"devDependencies": {
"async": "^1.5.2",
"bl": "^1.0.0",
"bs58": "^3.0.0",
"chai": "^3.4.1",
"fs-blob-store": "^5.2.1",
"istanbul": "^0.4.1",
"karma": "^0.13.19",
"karma-chrome-launcher": "^0.2.2",
"karma-cli": "^0.1.2",
"karma-firefox-launcher": "^0.1.7",
"karma-mocha": "^0.2.1",
"karma-spec-reporter": "0.0.23",
"karma-webpack": "^1.7.0",
"local-storage-blob-store": "0.0.2",
"lodash": "^4.0.0",
"mocha": "^2.3.4",
"ncp": "^2.0.0",
"pre-commit": "^1.1.1",
"raw-loader": "^0.5.1",
"rimraf": "^2.4.4",
"standard": "^5.1.1"
"standard": "^5.1.1",
"webpack": "^1.12.11"
},
"dependencies": {
"bl": "^1.0.0",
"concat-stream": "^1.5.1",
"fs-blob-store": "^5.2.0",
"level-js": "^2.2.2",
"lockfile": "^1.0.1",
"multihashes": "^0.2.0",
Expand Down
24 changes: 3 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
const stores = require('./stores')
const extend = require('xtend')
const fs = require('fs-blob-store')

exports = module.exports = Repo

function Repo (repoPath, options) {
const base = {
stores: {
keys: fs,
config: fs,
datastore: fs,
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
logs: fs,
locks: fs,
version: fs
}
}

options = extend(base, options)

this.init = (config, callback) => {
if (this.exists()) {
throw new Error('Repo already exists')
}

// TODO
// 1. load remaining stores
// 2. init all of them
}

this.locks = require('./stores').locks.setUp(repoPath, options.stores.locks)
this.locks = stores
.locks
.setUp(repoPath, options.stores.locks)

this.exists = callback => {
this.version.get((err, version) => {
Expand Down
55 changes: 55 additions & 0 deletions tests/browser-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* globals describe, before */

// const expect = require('chai').expect
const async = require('async')
const store = require('local-storage-blob-store')
const tests = require('./repo-test')
const _ = require('lodash')
const IPFSRepo = require('../src')

var repoContext = require.context('raw!./test-repo', true)

describe('IPFS Repo Testson on the Browser', function () {
before(function (done) {
window.localStorage.clear()

var repoData = []
repoContext.keys().forEach(function (key) {
repoData.push({
key: key.replace('./', ''),
value: repoContext(key)
})
})

var mainBlob = store('ipfs')
var blocksBlob = store('ipfs/')

async.eachSeries(repoData, (file, cb) => {
if (_.startsWith(file.key, 'datastore/')) {
return cb()
}

const blob = _.startsWith(file.key, 'blocks/')
? blocksBlob
: mainBlob

blob.createWriteStream({
key: file.key
}).end(file.value, cb)
}, done)
})

const options = {
stores: {
keys: store,
config: store,
datastore: store,
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
logs: store,
locks: store,
version: store
}
}
const repo = new IPFSRepo('ipfs', options)
tests(repo)
})
42 changes: 42 additions & 0 deletions tests/node-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* globals describe, before, after */

const expect = require('chai').expect
const ncp = require('ncp').ncp
const rimraf = require('rimraf')

const IPFSRepo = require('../src')

describe('IPFS Repo Testson on Node.js', () => {
const testRepoPath = __dirname + '/test-repo'
const date = Date.now().toString()
const repoPath = testRepoPath + date

before(done => {
ncp(testRepoPath, repoPath, err => {
expect(err).to.not.exist
done()
})
})

after(done => {
rimraf(repoPath, err => {
expect(err).to.not.exist
done()
})
})

const fs = require('fs-blob-store')
const options = {
stores: {
keys: fs,
config: fs,
datastore: fs,
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
logs: fs,
locks: fs,
version: fs
}
}
const repo = new IPFSRepo(repoPath, options)
require('./repo-test')(repo)
})
Loading

0 comments on commit e24b7f6

Please sign in to comment.