1- var assert = require ( 'assert' ) ,
2- broccoli = require ( 'broccoli' ) ,
3- ConfigReplace = require ( '..' ) ,
4- join = require ( 'path' ) . join ,
5- fs = require ( 'fs' ) ,
6- tmp = require ( 'tmp-sync' ) ;
1+ var assert = require ( 'assert' ) ;
2+ var broccoli = require ( 'broccoli' ) ;
3+ var ConfigReplace = require ( '..' ) ;
4+ var join = require ( 'path' ) . join ;
5+ var fs = require ( 'fs' ) ;
6+ var tmp = require ( 'tmp-sync' ) ;
7+ var expect = require ( 'chai' ) . expect ;
78
89afterEach ( function ( ) {
910 if ( this . builder ) {
@@ -13,10 +14,16 @@ afterEach(function() {
1314
1415function writeExample ( options ) {
1516 var root = tmp . in ( join ( process . cwd ( ) , 'tmp' ) ) ;
17+
1618 fs . writeFileSync ( join ( root , 'config.json' ) , options . config ) ;
1719 fs . writeFileSync ( join ( root , 'index.html' ) , options . index ) ;
20+
1821 return root ;
19- } ;
22+ }
23+
24+ function read ( fullPath ) {
25+ return fs . readFileSync ( fullPath , 'UTF8' ) ;
26+ }
2027
2128function makeConfigReplace ( root , patterns ) {
2229 return new ConfigReplace (
@@ -29,52 +36,53 @@ function makeConfigReplace(root, patterns) {
2936 patterns : patterns
3037 }
3138 ) ;
32- } ;
39+ }
3340
3441function makeBuilder ( root , patterns ) {
3542 var configReplace = makeConfigReplace ( root , patterns ) ;
3643 return new broccoli . Builder ( configReplace ) ;
37- } ;
44+ }
3845
39- var expectEquals = function ( expected ) {
46+ function expectEquals ( expected ) {
4047 return function ( results ) {
4148 var resultsPath = join ( results . directory , 'index.html' ) ,
4249 contents = fs . readFileSync ( resultsPath , { encoding : 'utf8' } ) ;
4350
4451 assert . equal ( contents . trim ( ) , expected ) ;
52+ return results ;
4553 } ;
46- } ;
54+ }
4755
4856describe ( 'config-replace' , function ( ) {
49- it ( 'replaces with text from config.json' , function ( done ) {
57+ it ( 'replaces with text from config.json' , function ( ) {
5058 var root = writeExample ( {
5159 config : '{"color":"red"}' ,
5260 index : '{{color}}'
5361 } ) ;
5462
55- makeBuilder ( root , [ {
63+ return makeBuilder ( root , [ {
5664 match : / \{ \{ c o l o r \} \} / g,
5765 replacement : function ( config ) { return config . color ; }
5866 } ] ) . build ( ) . then (
5967 expectEquals ( 'red' )
60- ) . then ( done ) . catch ( console . log ) ;
68+ ) ;
6169 } ) ;
6270
63- it ( 'replaces with string passed in via options' , function ( done ) {
71+ it ( 'replaces with string passed in via options' , function ( ) {
6472 var root = writeExample ( {
6573 config : '{}' ,
6674 index : '{{name}}'
6775 } ) ;
6876
69- makeBuilder ( root , [ {
77+ return makeBuilder ( root , [ {
7078 match : / \{ \{ n a m e \} \} / g,
7179 replacement : 'hari'
7280 } ] ) . build ( ) . then (
7381 expectEquals ( 'hari' )
74- ) . then ( done ) . catch ( console . log ) ;
82+ ) ;
7583 } ) ;
7684
77- it ( 'rebuilds if the config file changes' , function ( done ) {
85+ it ( 'rebuilds if the config file changes' , function ( ) {
7886 var root = writeExample ( {
7987 config : '{"pokemon":"diglet"}' ,
8088 index : '{{pokemon}}'
@@ -85,17 +93,17 @@ describe('config-replace', function() {
8593 replacement : function ( config ) { return config . pokemon ; }
8694 } ] ) ;
8795
88- builder . build ( ) . then (
96+ return builder . build ( ) . then (
8997 expectEquals ( 'diglet' )
9098 ) . then ( function ( ) {
9199 fs . writeFileSync ( join ( root , 'config.json' ) , '{"pokemon":"jigglypuff"}' ) ;
92100 return builder . build ( ) ;
93101 } ) . then (
94102 expectEquals ( 'jigglypuff' )
95- ) . then ( done ) . catch ( console . log ) ;
103+ ) ;
96104 } ) ;
97105
98- it ( 'caches the result' , function ( done ) {
106+ it ( 'caches the result' , function ( ) {
99107 var root , configReplace , builder , key , entry ;
100108
101109 root = writeExample ( {
@@ -109,14 +117,46 @@ describe('config-replace', function() {
109117 } ] ) ;
110118
111119 builder = new broccoli . Builder ( configReplace ) ;
112- builder . build ( ) . then (
120+
121+ var indexStat ;
122+ return builder . build ( ) . then (
113123 expectEquals ( 'nyc' )
114124 ) . then ( function ( ) {
115- key = Object . keys ( configReplace . _cache ) [ 0 ] ;
116- entry = configReplace . _cache [ key ] ;
125+ indexStat = fs . statSync ( join ( root , 'index.html' ) ) ;
126+
117127 return builder . build ( ) ;
118128 } ) . then ( function ( ) {
119- assert . equal ( entry , configReplace . _cache [ key ] ) ;
120- } ) . then ( done ) . catch ( console . log ) ;
129+ var nextStat = fs . statSync ( join ( root , 'index.html' ) ) ;
130+ assert . deepEqual ( indexStat , nextStat ) ;
131+ } ) ;
132+ } ) ;
133+
134+ it ( 'evicts after change' , function ( ) {
135+ var root , configReplace , builder , key , entry ;
136+
137+ root = writeExample ( {
138+ config : '{"city":"nyc"}' ,
139+ index : '{{city}}'
140+ } ) ;
141+
142+ configReplace = makeConfigReplace ( root , [ {
143+ match : / \{ \{ c i t y \} \} / g,
144+ replacement : function ( config ) { return config . city ; }
145+ } ] ) ;
146+
147+ builder = new broccoli . Builder ( configReplace ) ;
148+
149+ var oldContent ;
150+
151+ return builder . build ( ) . then ( function ( results ) {
152+ oldContent = read ( results . directory + '/index.html' ) ;
153+
154+ fs . writeFileSync ( root + '/index.html' , 'foo' ) ;
155+ return builder . build ( ) ;
156+ } ) . then ( function ( results ) {
157+
158+ var newContent = read ( results . directory + '/index.html' ) ;
159+ expect ( newContent ) . to . not . equal ( oldContent ) ;
160+ } ) ;
121161 } ) ;
122162} ) ;
0 commit comments