Skip to content

Commit

Permalink
Add infrastructure to allow for testing, and a build step
Browse files Browse the repository at this point in the history
The build step allows us to have external dependencies, and selectively
compile them into the distribution source for consumption by other
software.

Add unit tests, and inject http (jquery ajax method provider) dependency

Since we inject jquery (or some substitute) in the constructor, we no
longer depend on "real" jquery. Thus, we remove it from our bower deps,
and other configuration.
  • Loading branch information
clalimarmo committed Dec 10, 2014
1 parent b69e735 commit 1658309
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 82 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory": "build/bower_components"
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
coverage
147 changes: 147 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*global module:false*/
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),

jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
unused: true,
boss: true,
eqnull: true,
browser: true,
expr: true,
trailing: true,
},
gruntfile: {
src: 'Gruntfile.js',
options: {
globals: {
require: false,
},
},
},
lib: {
src: ['lib/**/*.js'],
options: {
globals: {
requirejs: false,
define: false,
window: false,
},
},
},
spec: {
src: ['spec/**/*.js'],
options: {
globals: {
requirejs: false,
describe: false,
it: false,
xit: false,
context: false,
before: false,
beforeEach: false,
after: false,
afterEach: false,
define: false,
expect: false,
},
},
},
},

karma: {
options: {
configFile: 'karma.conf.js'
},
unit: {
coverageReporter: {
type: 'text-summary'
}
},
coverage: {
coverageReporter: {
type: 'html'
}
},
},

watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
dev: {
files: ['lib/**/*.js', 'spec/**/*.js', 'lib/**/*.html'],
tasks: ['build', 'karma:unit', 'jshint']
},
copy: {
files: [
'app/lib',
],
tasks: ['copy:dev'],
},
bower: {
files: ['bower.json'],
tasks: ['build:full', 'karma:unit', 'jshint'],
},
},

copy: {
dev: {
files: [
{expand: true, cwd: 'lib', src: ['**/*.js'], dest: 'build'},
{expand: true, cwd: 'lib', src: ['**/*.json'], dest: 'build'},
],
},
dist: {
files: [
],
},
},

requirejs: {
compile: {
options: {
baseUrl: 'build',
name: 'json-gcs',
mainConfigFile: 'build/json-gcs.js',
out: 'dist/json-gcs.js',
optimize: 'uglify2',
},
},
},

bower: {
install: {
}
},

clean: ['.tmp', 'build', 'coverage'],
});

// These plugins provide necessary tasks.
// load all grunt tasks matching the `grunt-*` pattern
require('load-grunt-tasks')(grunt);

// Build tasks.
grunt.registerTask('build', ['copy:dev']);
grunt.registerTask('build:full', ['clean', 'build', 'bower:install']);
grunt.registerTask('build:dist', ['clean', 'copy:dev', 'bower:install', 'requirejs']);

// Default task.
grunt.registerTask('default', ['build:full', 'karma:unit', 'jshint']);
grunt.registerTask('coverage', 'Detailed test coverage information', ['build:full', 'karma:coverage']);

};
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Google OAuth2 token, which grants the necessary permissions to the named
bucket.

JsonGCS = require('json-gcs');
$ = require('jquery');

var cloudStore = JsonGCS({
authenticator: {
token: function() { return userOAuth2Token; },
}
bucketName: "myBucket"
},
bucketName: "myBucket",
http: $
});

cloudStore.put(objectName, jsonDocument);
Expand All @@ -26,15 +28,23 @@ bucket.
//do something with retrieved json document
});

## Dependencies

The only dependency at the moment is jQuery, whose `ajax` method is used to
send request to the Google Cloud Storage JSON API.

At some point this dependency might be removed. Until then, the module
expects to be able to `require('jquery')`.

## Notes

The module is provided as an AMD module, with simplified CommonJS wrapping.
It's compatible with RequireJS, I haven't tried it in other contexts.

So that there isn't a package dependency on jquery, we pass it into the
initialization function. We only use the `ajax` method of jquery, so if
you don't already use jquery, and don't want to introduce it to your project
you can provide your own http adapter, as long as the `ajax` method matches
jquery's signature and behavior. (This also simplifies testing).

## Development

To run the tests:

grunt

To build the dist package:

grunt build:dist
5 changes: 1 addition & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"name": "json-gcs",
"version": "0.0.0",
"homepage": "https://github.com/clalimarmo/json-gcs",
"authors": [
"Carlos Lalimarmo <[email protected]>"
],
"description": "Store JSON documents to Google Cloud Storage",
"main": "dist/json-gcs.js",
"dependencies": {
"jquery": "~2.1.1"
},
"dependencies": {},
"moduleType": [
"amd"
],
Expand Down
69 changes: 1 addition & 68 deletions dist/json-gcs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Karma configuration
// Generated on Thu Feb 06 2014 09:46:59 GMT-0500 (EST)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: [
// NB: requirejs should come before chai in the list below, to avoid a
// known issue: https://github.com/xdissent/karma-chai/issues/5
'requirejs',
'mocha',
'chai',
],


// list of files / patterns to load in the browser
files: [
//our code
{pattern: 'build/**/*.js', included: false},
{pattern: 'spec/**/*_spec.js', included: false},
{pattern: 'spec/**/*_spec.coffee', included: false},

'spec/main.js',
],


// list of files to exclude
exclude: [
'**/*.swp',
'build/bower_components/**/test/**/*js', //DO NOT run tests for third party libraries
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'spec/**/*.coffee': ['coffee'],
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'coverage'],

coverageReporter: {
reporters:[
{type: 'html', dir:'coverage/'},
{type: 'text-summary'},
],
},


// 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: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
Loading

0 comments on commit 1658309

Please sign in to comment.