@@ -12,21 +12,6 @@ var tarHelper = require('./tar-helper');
1212chai . use ( require ( 'chai-as-promised' ) ) ;
1313var assert = chai . assert ;
1414
15- var root = path . join ( __dirname , '..' , '..' ) ;
16- var dataPath = path . join ( root , 'data' ) ;
17-
18- function getProjectPath ( project ) {
19- return path . join ( dataPath , project ) ;
20- }
21-
22- function getBuildsPath ( project ) {
23- return path . join ( getProjectPath ( project ) , 'builds' ) ;
24- }
25-
26- function getShasPath ( project ) {
27- return path . join ( getProjectPath ( project ) , 'shas' ) ;
28- }
29-
3015function getImageFromPath ( path ) {
3116 return new Bluebird ( function ( resolve , reject ) {
3217 var domain = require ( 'domain' ) . create ( ) ;
@@ -36,7 +21,7 @@ function getImageFromPath(path) {
3621
3722 domain . run ( function ( ) {
3823 PNGImage . readImageAsync ( path )
39- . then ( function ( image ) {
24+ . then ( function ( image ) {
4025 resolve ( image . getImage ( ) ) ;
4126 } )
4227 . catch ( function ( err ) {
@@ -46,12 +31,31 @@ function getImageFromPath(path) {
4631 } ) ;
4732}
4833
49- var Storage = {
34+ function Storage ( options ) {
35+ assert . isObject ( options ) ;
36+ assert . isString ( options . dataPath ) ;
37+
38+ this . _options = options ;
39+ }
40+
41+ Storage . prototype = {
42+ _getProjectPath : function ( project ) {
43+ return path . join ( this . _options . dataPath , project ) ;
44+ } ,
45+
46+ _getBuildsPath : function ( project ) {
47+ return path . join ( this . _getProjectPath ( project ) , 'builds' ) ;
48+ } ,
49+
50+ _getShasPath : function ( project ) {
51+ return path . join ( this . _getProjectPath ( project ) , 'shas' ) ;
52+ } ,
53+
5054 createProject : function ( options ) {
5155 assert . isObject ( options ) ;
5256
5357 var guid = uuid . v4 ( ) ;
54- var projectFile = path . join ( getProjectPath ( guid ) , 'project.json' ) ;
58+ var projectFile = path . join ( this . _getProjectPath ( guid ) , 'project.json' ) ;
5559 options . project = guid ;
5660
5761 return fs . outputJSONAsync ( projectFile , options )
@@ -63,7 +67,7 @@ var Storage = {
6367 } ,
6468
6569 hasProject : function ( project ) {
66- return fs . statAsync ( path . join ( dataPath , project , 'project.json' ) )
70+ return fs . statAsync ( path . join ( this . _getProjectPath ( project ) , 'project.json' ) )
6771 . then ( function ( stat ) {
6872 return stat . isFile ( ) ;
6973 } )
@@ -76,11 +80,11 @@ var Storage = {
7680 assert . isString ( project ) ;
7781
7882 return assert . eventually . isTrue ( this . hasProject ( project ) , 'Unknown Project' )
79- . then ( function ( ) {
80- var buildFile = path . join ( getProjectPath ( project ) , 'project.json' ) ;
83+ . then ( ( function ( ) {
84+ var buildFile = path . join ( this . _getProjectPath ( project ) , 'project.json' ) ;
8185
8286 return fs . readJSONAsync ( buildFile ) ;
83- } ) ;
87+ } ) . bind ( this ) ) ;
8488 } ,
8589
8690 startBuild : function ( options ) {
@@ -93,10 +97,9 @@ var Storage = {
9397 var guid ;
9498
9599 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
96- . then ( function ( ) {
100+ . then ( ( function ( ) {
97101 guid = uuid . v4 ( ) ;
98-
99- var buildFile = path . join ( getBuildsPath ( options . project ) , guid , 'build.json' ) ;
102+ var buildFile = path . join ( this . _getBuildsPath ( options . project ) , guid , 'build.json' ) ;
100103
101104 return fs . outputJSONAsync ( buildFile , {
102105 build : guid ,
@@ -105,7 +108,7 @@ var Storage = {
105108 numBrowsers : options . numBrowsers ,
106109 status : 'pending'
107110 } ) ;
108- } )
111+ } ) . bind ( this ) )
109112 . then ( ( function ( ) {
110113 return Bluebird . all ( [
111114 this . addBuildToSha ( {
@@ -133,9 +136,9 @@ var Storage = {
133136 assert . isString ( options . build ) ;
134137
135138 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
136- . then ( function ( ) {
137- return fs . statAsync ( path . join ( getBuildsPath ( options . project ) , options . build , 'build.json' ) ) ;
138- } )
139+ . then ( ( function ( ) {
140+ return fs . statAsync ( path . join ( this . _getBuildsPath ( options . project ) , options . build , 'build.json' ) ) ;
141+ } ) . bind ( this ) )
139142 . then ( function ( stat ) {
140143 return stat . isFile ( ) ;
141144 } )
@@ -158,7 +161,7 @@ var Storage = {
158161 var build = options . build ;
159162 var sha = options . sha ;
160163
161- var shaBuildsPath = getShasPath ( options . project ) ;
164+ var shaBuildsPath = this . _getShasPath ( options . project ) ;
162165 var shaBuildsFile = path . join ( shaBuildsPath , sha , 'builds.json' ) ;
163166
164167 return fs . ensureDirAsync ( shaBuildsPath )
@@ -188,10 +191,10 @@ var Storage = {
188191 assert . isString ( options . sha ) ;
189192
190193 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
191- . then ( function ( ) {
194+ . then ( ( function ( ) {
192195 var sha = options . sha ;
193196
194- var shaPath = path . join ( getShasPath ( options . project ) , sha ) ;
197+ var shaPath = path . join ( this . _getShasPath ( options . project ) , sha ) ;
195198
196199 return fs . statAsync ( shaPath )
197200 . then ( function ( stat ) {
@@ -208,7 +211,7 @@ var Storage = {
208211 return [ ] ;
209212 }
210213 } ) ;
211- } ) ;
214+ } ) . bind ( this ) ) ;
212215 } ,
213216
214217 getBuildInfo : function ( options ) {
@@ -220,11 +223,11 @@ var Storage = {
220223 project : options . project ,
221224 build : options . build
222225 } ) )
223- . then ( function ( ) {
224- var buildFile = path . join ( getBuildsPath ( options . project ) , options . build , 'build.json' ) ;
226+ . then ( ( function ( ) {
227+ var buildFile = path . join ( this . _getBuildsPath ( options . project ) , options . build , 'build.json' ) ;
225228
226229 return fs . readJSONAsync ( buildFile ) ;
227- } )
230+ } ) . bind ( this ) )
228231 . catch ( function ( ) {
229232 throw Error ( 'Unknown Build' ) ;
230233 } ) ;
@@ -242,7 +245,7 @@ var Storage = {
242245
243246 var status = options . status ;
244247 var diffs = options . diffs ;
245- var buildFile = path . join ( getBuildsPath ( options . project ) , options . build , 'build.json' ) ;
248+ var buildFile = path . join ( this . _getBuildsPath ( options . project ) , options . build , 'build.json' ) ;
246249
247250 return assert . eventually . isTrue ( this . hasBuild ( {
248251 project : options . project ,
@@ -266,7 +269,7 @@ var Storage = {
266269 assert . isString ( options . sha ) ;
267270 assert . isString ( options . tarPath ) ;
268271
269- var extractPath = path . join ( getShasPath ( options . project ) , options . sha , options . browser ) ;
272+ var extractPath = path . join ( this . _getShasPath ( options . project ) , options . sha , options . browser ) ;
270273
271274 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
272275 . then ( function ( ) {
@@ -282,7 +285,7 @@ var Storage = {
282285 assert . isString ( options . project ) ;
283286 assert . isString ( options . sha ) ;
284287
285- var shaPath = path . join ( getShasPath ( options . project ) , options . sha ) ;
288+ var shaPath = path . join ( this . _getShasPath ( options . project ) , options . sha ) ;
286289
287290 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
288291 . then ( function ( ) {
@@ -306,10 +309,10 @@ var Storage = {
306309 var browser = options . browser ;
307310
308311 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
309- . then ( function ( ) {
310- var browserPath = path . join ( getShasPath ( project ) , sha , browser ) ;
312+ . then ( ( function ( ) {
313+ var browserPath = path . join ( this . _getShasPath ( project ) , sha , browser ) ;
311314 return dirHelper . readFiles ( browserPath ) ;
312- } ) ;
315+ } ) . bind ( this ) ) ;
313316 } ,
314317
315318 /*
@@ -327,7 +330,7 @@ var Storage = {
327330 var browser = options . browser ;
328331 var image = options . image ;
329332
330- var imagePath = path . join ( getShasPath ( project ) , sha , browser , image ) ;
333+ var imagePath = path . join ( this . _getShasPath ( project ) , sha , browser , image ) ;
331334
332335 return assert . eventually . isTrue ( this . hasProject ( options . project ) )
333336 . then ( function ( ) {
@@ -350,7 +353,7 @@ var Storage = {
350353 var browser = options . browser ;
351354 var image = options . image ;
352355
353- var imagePath = path . join ( getBuildsPath ( project ) , build , browser , image ) ;
356+ var imagePath = path . join ( this . _getBuildsPath ( project ) , build , browser , image ) ;
354357
355358 return assert . eventually . isTrue ( this . hasBuild ( {
356359 project : options . project ,
@@ -382,7 +385,7 @@ var Storage = {
382385 var imageName = options . imageName ;
383386 var imageData = options . imageData ;
384387
385- var folder = path . join ( getBuildsPath ( project ) , build , browser ) ;
388+ var folder = path . join ( this . _getBuildsPath ( project ) , build , browser ) ;
386389 var imagePath = path . join ( folder , imageName ) ;
387390
388391 return assert . eventually . isTrue ( this . hasBuild ( {
@@ -404,27 +407,14 @@ var Storage = {
404407} ;
405408
406409if ( process . env . NODE_ENV === 'test' ) {
407- Object . defineProperty ( Storage , '_dataPath' , {
408- get : function ( ) {
409- return dataPath ;
410- } ,
411- set : function ( newPath ) {
412- dataPath = newPath ;
413- }
414- } ) ;
415-
416- Object . defineProperty ( Storage , '_getImageFromPath' , {
410+ Object . defineProperty ( Storage . prototype , '_getImageFromPath' , {
417411 get : function ( ) {
418412 return getImageFromPath ;
419413 } ,
420414 set : function ( newFunc ) {
421415 getImageFromPath = newFunc ;
422416 }
423417 } ) ;
424-
425- Storage . _getProjectPath = getProjectPath ;
426- Storage . _getBuildsPath = getBuildsPath ;
427- Storage . _getShasPath = getShasPath ;
428418}
429419
430420module . exports = Storage ;
0 commit comments