Browse Source

Fix code coverage

Move custom server to grunt connect task with custom middleware allows
us to utilize the keepalive feature to fix code coverage generation
Jason Chen 10 years ago
parent
commit
4fbf0f5d3e
8 changed files with 71 additions and 61 deletions
  1. 1 1
      .gitignore
  2. 1 1
      .npmignore
  3. 1 1
      .travis.yml
  4. 5 9
      Gruntfile.coffee
  5. 60 0
      grunt/server.coffee
  6. 2 1
      grunt/test.coffee
  7. 1 1
      package.json
  8. 0 47
      scripts/server.js

+ 1 - 1
.gitignore

@@ -1,5 +1,5 @@
 /.build
-/coverage
+/.coverage
 /dist
 /lib
 /node_modules

+ 1 - 1
.npmignore

@@ -1,10 +1,10 @@
 .build
+.coverage
 .git
 .travis.yml
 _*
 bower.json
 *.log
-coverage
 examples
 grunt
 src

+ 1 - 1
.travis.yml

@@ -6,7 +6,7 @@ before_install:
   - npm install -g grunt-cli
 before_script:
   - grunt lodash
-  - node scripts/server.js &
+  - grunt connect:server:keepalive &
 branches:
   only:
     - master

+ 5 - 9
Gruntfile.coffee

@@ -1,5 +1,3 @@
-child_process = require('child_process')
-
 module.exports = (grunt) ->
   require('load-grunt-tasks')(grunt)
 
@@ -8,18 +6,15 @@ module.exports = (grunt) ->
   )
 
   require('./grunt/build')(grunt)
+  require('./grunt/server')(grunt)
   require('./grunt/test')(grunt)
 
-  grunt.registerTask('dev', 'All the tasks for Quill development', ->
-    done = this.async()
-    child_process.spawn('grunt', ['test:karma'], { stdio: 'inherit'})
-    child_process.spawn('grunt', ['server'], { stdio: 'inherit' })
-  )
+  grunt.registerTask('dev', ['connect:server', 'test:karma'])
 
   grunt.registerTask('dist', ['clean', 'lodash', 'browserify', 'uglify', 'stylus', 'concat'])
   grunt.registerTask('release', ['dist', 'shell:examples', 'copy', 'compress'])
 
-  grunt.registerTask('server', ['shell:server'])
+  grunt.registerTask('server', ['connect:server:keepalive'])
 
   grunt.registerTask('test', ['karma:test'])
 
@@ -27,4 +22,5 @@ module.exports = (grunt) ->
   grunt.registerTask('test:unit', ['karma:test'])
   grunt.registerTask('test:unit:remote', ['karma:remote-mac', 'karma:remote-windows', 'karma:remote-linux', 'karma:remote-mobile', 'karma:remote-legacy'])
 
-  grunt.registerTask('test:coverage', ['coffee:src', 'shell:instrument', 'karma:coverage', 'clean:coffee', 'clean:coverage'])
+  grunt.registerTask('test:coverage', ['coffee:src', 'shell:instrument', 'connect:server', 'karma:coverage', 'clean:coffee', 'clean:coverage'])
+

+ 60 - 0
grunt/server.coffee

@@ -0,0 +1,60 @@
+_ = require('lodash')
+coffeeify = require('coffeeify')
+fs = require('fs')
+harp = require('harp')
+stylus = require('stylus')
+watchify = require('watchify')
+
+
+bundle = (w) ->
+  return w.bundle({ standalone: 'Quill' })
+
+serve = (connect, req, res, next) ->
+  watchers = connect.watchers
+  switch req.url
+    when '/quill.js'
+      res.setHeader('Content-Type', 'application/javascript')
+      bundle(watchers['src']).pipe(res)
+    when '/test/quill.js'
+      res.setHeader('Content-Type', 'application/javascript')
+      bundle(watchers['test']).pipe(res)
+    when '/quill.snow.css'
+      res.setHeader('Content-Type', 'text/css')
+      fs.readFile('./src/themes/snow/snow.styl', (err, data) ->
+        s = stylus(data.toString())
+        s.include('./src/themes/snow')
+        s.define('url', stylus.url())
+        s.render((err, css) ->
+          res.write(css)
+          res.end()
+        )
+      )
+    else
+      next()
+
+
+module.exports = (grunt) ->
+  grunt.config('connect',
+    options:
+      onCreateServer: (server, connect, options) ->
+        connect.watchers = _.reduce(['src', 'test'], (watchers, type) ->
+          w = watchify("./#{type}/quill.coffee", { extensions: ['.js', '.coffee'] })
+          watchers[type] = w
+          w.require('./.build/lodash.js', { expose: 'lodash' })
+          w.transform(coffeeify)
+          w.on('update', bundle.bind(this, w))
+          bundle(w) if options.keepalive
+          return watchers
+        , {})
+      middleware: (connect, options, middlewares) ->
+        middlewares.push(serve.bind(this, connect))
+        middlewares.push(harp.mount(__dirname + '/..'))
+        return middlewares
+      debug: true
+    server:
+      options:
+        port: 9000
+    coverage:
+      options:
+        port: 9001
+  )

+ 2 - 1
grunt/test.coffee

@@ -13,7 +13,7 @@ remoteBrowserGroups =
 remoteConfigs =
   browserDisconnectTimeout: 10000
   browserDisconnectTolerance: 2
-  browserNoActivityTimeout: 30000
+  browserNoActivityTimeout: 60000
   reporters: remoteReporters
 
 remoteKarma = _.reduce(remoteBrowserGroups, (memo, browsers, group) ->
@@ -37,6 +37,7 @@ module.exports = (grunt) ->
     local:
       browsers: ['Chrome', 'Firefox', 'Safari']
     coverage:
+      browserNoActivityTimeout: 30000
       browsers: ['PhantomJS']
       reporters: ['coverage']
   ))

+ 1 - 1
package.json

@@ -18,13 +18,13 @@
   "devDependencies": {
     "coffee-script": "~1.7.1",
     "coffeeify": "~0.6.0",
-    "connect": "~3.0.1",
     "grunt": "~0.4.3",
     "grunt-browserify": "~2.1.0",
     "grunt-contrib-clean": "~0.5.0",
     "grunt-contrib-coffee": "~0.10.1",
     "grunt-contrib-compress": "~0.9.1",
     "grunt-contrib-concat": "~0.4.0",
+    "grunt-contrib-connect": "~0.8.0",
     "grunt-contrib-copy": "~0.5.0",
     "grunt-contrib-stylus": "~0.18.0",
     "grunt-contrib-uglify": "~0.5.0",

+ 0 - 47
scripts/server.js

@@ -1,47 +0,0 @@
-var _ = require('lodash');
-var coffeeify = require('coffeeify');
-var connect = require('connect');
-var fs = require('fs');
-var harp = require('harp');
-var stylus = require('stylus');
-var watchify = require('watchify');
-
-var opts = { extensions: ['.js', '.coffee'] };
-var bundle = function(w) {
-  return w.bundle({ standalone: 'Quill' });
-};
-var watchers = {
-  'main': watchify('./src/quill.coffee', opts),
-  'test': watchify('./test/quill.coffee', opts)
-};
-_.each(watchers, function(w) {
-  w.require('./.build/lodash.js', { expose: 'lodash' });
-  w.transform(coffeeify);
-  w.on('update', bundle.bind(this, w));
-  bundle(w);
-});
-
-var app = connect();
-var respond = function(type, req, res, next) {
-  res.setHeader('Content-Type', 'application/javascript');
-  bundle(watchers[type]).pipe(res);
-};
-
-app.use('/quill.js', respond.bind(this, 'main'));
-app.use('/test/quill.js', respond.bind(this, 'test'));
-app.use('/quill.snow.css', function(req, res, next) {
-  res.setHeader('Content-Type', 'text/css');
-  fs.readFile('./src/themes/snow/snow.styl', function(err, data) {
-    var s = stylus(data.toString());
-    s.include('./src/themes/snow');
-    s.define('url', stylus.url());
-    s.render(function(err, css) {
-      res.write(css);
-      res.end();
-    })
-  });
-});
-app.use(harp.mount(__dirname + '/..'));
-
-app.listen(9000);
-console.info('Quill development server listening on port 9000');