UNPKG

4.68 kBtext/coffeescriptView Raw
1Promise = require 'bluebird'
2# Promise.config longStackTraces:true
3exec = require('child_process').execSync
4md5 = require 'md5'
5fs = require 'fs-jetpack'
6vm = require 'vm'
7path = require 'path'
8mocha = require 'mocha'
9chai = require 'chai'
10expect = chai.expect
11listDir = (dir)-> fs.listAsync(dir).then (files)-> files.filter (file)-> file[0] isnt '.'
12sample = ()-> path.join __dirname,'samples',arguments...
13temp = ()-> path.join __dirname,'temp',arguments...
14process.env.COFFEE_CACHE_DIR = temp('.cache')
15
16runClean = (fn)->
17 deRegister()
18 fn()
19
20
21deRegister = ()->
22 delete require.extensions['.coffee']
23 delete require.extensions['.litcoffee']
24 delete require.extensions['.coffee.md']
25 delete require.cache[require.resolve('coffee-script/register')]
26 delete require.cache[require.resolve('coffee-script/lib/coffee-script/register')]
27 delete require.cache[require.resolve('../')]
28 samples = fs.list(sample())
29 for sampleFile in samples
30 delete require.cache[sample(sampleFile)]
31
32 cached = fs.list(temp('.cache'))
33 for cachedFile in cached
34 delete require.cache[temp('.cache',cachedFile)]
35
36 return
37
38
39suite "coffee-register", ()->
40 suiteTeardown ()-> fs.removeAsync temp()
41 suiteSetup ()-> fs.dirAsync temp()
42 setup ()-> fs.dirAsync temp('.cache'), empty:true
43
44
45 test "basic register", ()->
46 src = ()->
47 require('../')
48 require('./samples/all.coffee')
49
50 Promise.resolve()
51 .then ()-> runClean(src)
52 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
53
54
55 test "index.coffee require", ()->
56 src = ()->
57 require('../')
58 require('./samples/index')
59
60 Promise.resolve()
61 .then ()-> runClean(src)
62 .then (result)-> expect(result).to.equal('theIndex')
63
64
65 test "dir require", ()->
66 src = ()->
67 require('../')
68 require('./samples')
69
70 Promise.resolve()
71 .then ()-> runClean(src)
72 .then (result)-> expect(result).to.equal('theIndex')
73
74
75 test "cached result", ()->
76 src = ()->
77 require('../')
78 require('./samples/all.coffee')
79
80 Promise.resolve()
81 .then ()-> listDir temp('.cache')
82 .then (list)-> expect(list).to.eql []
83 .then ()-> runClean(src)
84 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
85 .then ()-> listDir temp('.cache')
86 .tap (list)-> expect(list.length).to.equal 4
87 .then (list)->
88 hashA = md5 fs.read sample('sampleA.coffee')
89 hashB = md5 fs.read sample('.sampleB.coffee')
90 hashC = md5 fs.read sample('sampleC.extension.coffee')
91 hashAll = md5 fs.read sample('all.coffee')
92 global.hashB = hashB
93
94 expect(list).to.contain "#{hashA}.js"
95 expect(list).to.contain "#{hashB}.js"
96 expect(list).to.contain "#{hashC}.js"
97 expect(list).to.contain "#{hashAll}.js"
98
99 .then ()-> runClean(src)
100 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
101 .then ()-> listDir temp('.cache')
102 .tap (list)-> expect(list.length).to.equal 4
103 .then (list)-> fs.writeAsync temp('.cache',"#{hashB}.js"), fs.read(temp('.cache',"#{hashB}.js")).replace("'sample", "'SAMPLE")
104 .then ()-> runClean(src)
105 .then (result)-> expect(result).to.equal('sampleA-SAMPLEB-sampleC')
106
107
108 test "cached result deletion", ()->
109 src = ()->
110 require('../')
111 require('./samples/all.coffee')
112
113 Promise.resolve()
114 .then ()-> listDir temp('.cache')
115 .then (list)-> expect(list).to.eql []
116 .then ()-> runClean(src)
117 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
118 .then ()-> listDir temp('.cache')
119 .tap (list)-> expect(list.length).to.equal 4
120 .then (list)-> fs.removeAsync temp('.cache',list[1])
121 .then ()-> listDir temp('.cache')
122 .then (list)-> expect(list.length).to.equal 3
123 .then ()-> runClean(src)
124 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
125 .then ()-> listDir temp('.cache')
126 .then (list)-> expect(list.length).to.equal 4
127
128
129 test "cached result update/change", ()->
130 src = ()->
131 require('../')
132 require('./samples/all.coffee')
133
134 Promise.resolve()
135 .then ()-> listDir temp('.cache')
136 .then (list)-> expect(list).to.eql []
137 .then ()-> runClean(src)
138 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
139 .then ()-> listDir temp('.cache')
140 .then (list)-> expect(list.length).to.equal 4
141 .then ()-> fs.read sample('all.coffee')
142 .then (contents)-> fs.write sample('all.coffee'), contents+' '
143 .then ()-> runClean(src)
144 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
145 .then ()-> listDir temp('.cache')
146 .then (list)-> expect(list.length).to.equal 5
147 .then ()-> runClean(src)
148 .then (result)-> expect(result).to.equal('sampleA-sampleB-sampleC')
149 .then ()-> listDir temp('.cache')
150 .then (list)-> expect(list.length).to.equal 5
151
152
153
154
155
156
157