Skip to content
Permalink
Browse files

feat(api task): create and expose api task dev:test:server

  • Loading branch information
jyounce committed Oct 1, 2015
1 parent 8995468 commit 702ae853e5426efb207855e7d4f87cff74e244a3
@@ -16,37 +16,46 @@ module.exports = (config) ->
env.is.default = true
when 'test'
env.is.default = true
env.is.test = true
env.is.testClient = true
env.is.testServer = true
when 'test:client'
env.is.default = true
env.is.test = true
env.is.testClient = true
when 'test:server'
env.is.default = true
env.is.test = true
env.is.testServer = true
when 'dev'
env.is.dev = true
when 'dev:test'
env.is.dev = true
env.is.test = true
env.is.testClient = true
env.is.testServer = true
when 'dev:test:client'
env.is.dev = true
env.is.test = true
env.is.testClient = true
when 'dev:test:server'
env.is.dev = true
env.is.test = true
env.is.testServer = true
when 'prod'
env.is.prod = true
when 'prod:test'
env.is.prod = true
env.is.test = true
env.is.testClient = true
env.is.testServer = true
when 'prod:test:client'
env.is.prod = true
env.is.test = true
env.is.testClient = true
when 'prod:test:server'
env.is.prod = true
env.is.test = true
env.is.testServer = true

# init env
@@ -60,6 +69,7 @@ module.exports = (config) ->
default: true
dev: false
prod: false
test: false
testClient: false
testServer: false

@@ -1,4 +1,6 @@
module.exports = (config, rbDir) ->
fs = require 'fs'
path = require 'path'
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()
pkg = require "#{config.req.rb}/package.json"
@@ -10,6 +12,15 @@ module.exports = (config, rbDir) ->
rb.version = pkg.version
rb.dir = rbDir

# is symlink - determine if it has been installed via npm link
# ==========
getIsSymlink = ->
dir = path.join config.req.app, 'node_modules', rb.name
isSymlink = fs.lstatSync(dir).isSymbolicLink()
isSymlink

rb.isSymlink = getIsSymlink()

# api tasks
# =========
rb.tasks = {}
@@ -1,27 +1,54 @@
# API:
# jasmine.init file(s)
# jasmine.execute()
# jasmine.reExecute()
# jasmine.getResults()
# Note:
# 5 seconds is the default spec timeout
# =====================================
module.exports = (config) ->
q = require 'q'
path = require 'path'
Jasmine = require 'jasmine'
Reporter = require 'jasmine-terminal-reporter'
isType = require "#{config.req.helpers}/isType"
moduleHelp = require "#{config.req.helpers}/module"
jasmineExpect = path.join config.node_modules.rb.src.relPath, 'jasmine-expect', 'index.js'

# helpers
# =======
getHelperFilePath = (file) ->
return file unless config.rb.isSymlink
file = file.replace config.node_modules.app.src.dir, config.node_modules.rb.src.dir
file = file.replace config.node_modules.rb.src.relPath, 'node_modules'
file

# return
# ======
jasmine =
# properties
# ==========
defer: q.defer()
files: []
jasmine: null
results: status: null, total: 0, passed: 0, failed: 0, failedSpecs: []

# public
# ======
init: (files) ->
@_setJasmine()
._setConfig files
._setFiles files
._setConfig()
._setOnComplete()
._addReporter()
@

execute: -> # 5 seconds is the default spec timeout
execute: ->
@jasmine.execute()
@defer.promise

reExecute: ->
@_deleteCache()
@jasmine.execute()
@defer.promise

@@ -34,10 +61,14 @@ module.exports = (config) ->
@jasmine = new Jasmine()
@

_setConfig: (files) ->
_setFiles: (files) ->
@files = if not isType.array files then [ files ] else files
@

_setConfig: ->
@jasmine.loadConfig
spec_dir: ''
spec_files: files
spec_files: @files
helpers: [ jasmineExpect ]
@

@@ -61,3 +92,23 @@ module.exports = (config) ->
@results.failedSpecs.push result.fullName
@

_deleteCache: ->
@_deleteCacheSpecFiles()
@_deleteHelperFiles()
@

_deleteCacheSpecFiles: ->
specFiles = @jasmine.specFiles
return @ unless specFiles.length
for file in specFiles
moduleHelp.cache.delete file
@

_deleteHelperFiles: ->
helperFiles = @jasmine.helperFiles
return @ unless helperFiles.length
for file in helperFiles
moduleHelp.cache.delete getHelperFilePath file
@


@@ -1,6 +1,25 @@
module.exports =
cache:
delete: (modulePath) ->
isCached = !!require.cache[modulePath]
delete require.cache[modulePath] if isCached
isCached
get: (id) ->
cache = require.cache
return null unless cache or cache.length
cache[id]

getIds: ->
cache = require.cache
return [] unless cache or cache.length
Object.keys cache

delete: (id) -> # recursively, id = module's absolute path
return unless id
files = require.cache[id]
isCached = !!files
# console.log "is cached #{isCached}:", id
return unless isCached

children = files.children
if children.length
for file in children
@delete file.id

delete require.cache[id]
@@ -31,6 +31,19 @@ module.exports = (gulp, config) ->
cb
) -> defer.resolve()

# test dev - server
# =================
gulp.task config.rb.tasks['dev:test:server'], ["#{config.rb.prefix.task}common"], (cb) ->
gulpSequence(
"#{config.rb.prefix.task}common-server"
"#{config.rb.prefix.task}copy-server-tests"
"#{config.rb.prefix.task}start-server:dev"
"#{config.rb.prefix.task}browser-sync"
"#{config.rb.prefix.task}watch"
"#{config.rb.prefix.task}run-server-tests"
cb
) -> defer.resolve() unless task.wasCalledFrom config.rb.tasks['dev:test']

# prod
# ====
gulp.task config.rb.tasks.prod, ["#{config.rb.prefix.task}common"], (cb) ->
@@ -30,7 +30,7 @@
"dependencies": {
"body-parser": "1.14.1",
"bower": "1.5.3",
"browser-sync": "2.9.7",
"browser-sync": "2.9.8",
"coffee-script": "1.10.0",
"colors": "1.1.2",
"copy-paste": "1.1.3",
@@ -12,4 +12,5 @@ module.exports = (server, config) ->
msg = 'Hello Server!'
return res.send msg unless config.build.client
return res.send msg if config.exclude.spa
return res.send msg if config.env.is.test and not config.env.is.testClient
res.sendFile spa, root: server.paths.client
@@ -9,6 +9,8 @@ module.exports = (gulp, config, browserSync) ->
ignoreDirs = [
config.node_modules.rb.dist.dir
config.node_modules.app.dist.dir
config.dist.rb.server.test.dir
config.dist.app.server.test.dir
]

# register task
@@ -1,9 +1,12 @@
module.exports = (gulp, config) ->
q = require 'q'
babel = require 'gulp-babel'
coffee = require 'gulp-coffee'
plumber = require 'gulp-plumber'
tasks = require("#{config.req.helpers}/tasks")()
module.exports = (gulp, config, watchFile={}) ->
q = require 'q'
babel = require 'gulp-babel'
coffee = require 'gulp-coffee'
plumber = require 'gulp-plumber'
tasks = require("#{config.req.helpers}/tasks")()
promiseHelp = require "#{config.req.helpers}/promise"
forWatchFile = !!watchFile.path
runTest = require "#{config.req.tasks}/test/server/run-server-tests" if forWatchFile

coffeeTask = (src, dest) ->
defer = q.defer()
@@ -30,6 +33,24 @@ module.exports = (gulp, config) ->
.on 'end', -> defer.resolve()
defer.promise

compileWatchFile = ->
ext = watchFile.extname.replace '.', ''
src = watchFile.path
dest = watchFile.rbDistDir
switch ext
when 'js' then jsTask src, dest
when 'es6' then es6Task src, dest
when 'coffee' then coffeeTask src, dest

runSingle = -> # synchronously
defer = q.defer()
tasks = [
-> compileWatchFile()
-> runTest gulp, config, watchFile.rbDistPath
]
tasks.reduce(q.when, q()).done -> defer.resolve()
defer.promise

runMulti = ->
defer = q.defer()
q.all([
@@ -41,5 +62,11 @@ module.exports = (gulp, config) ->

# register task
# =============
return runSingle() if forWatchFile

gulp.task "#{config.rb.prefix.task}copy-server-tests", ->
runMulti()
return promiseHelp.get() unless config.build.server
runMulti()



@@ -1,11 +1,12 @@
module.exports = (gulp, config) ->
q = require 'q'
del = require 'del'
path = require 'path'
fse = require 'fs-extra'
promiseHelp = require "#{config.req.helpers}/promise"
jasmine = require("#{config.req.helpers}/jasmine") config
resultsFile = path.join config.dist.app.server.dir, 'test-results.json'
module.exports = (gulp, config, watchFilePath) ->
q = require 'q'
del = require 'del'
path = require 'path'
fse = require 'fs-extra'
promiseHelp = require "#{config.req.helpers}/promise"
jasmine = require("#{config.req.helpers}/jasmine") config
resultsFile = path.join config.dist.app.server.dir, 'test-results.json'
forWatchFile = !!watchFilePath

# tasks
# =====
@@ -32,6 +33,9 @@ module.exports = (gulp, config) ->
console.error msg.error
.exit 1

runSingle = (file) ->
jasmine.init(file).reExecute() # returns promise

runMulti = -> # synchronously
defer = q.defer()
tasks = [
@@ -45,7 +49,10 @@ module.exports = (gulp, config) ->

# register task
# =============
return runSingle watchFilePath if forWatchFile

gulp.task "#{config.rb.prefix.task}run-server-tests", ->
return promiseHelp.get() unless config.build.server
runMulti()


0 comments on commit 702ae85

Please sign in to comment.
You can’t perform that action at this time.