Skip to content

Commit 35ee77d

Browse files
committed
Converting storage to a class that can be configured.
1 parent e06ad8a commit 35ee77d

File tree

2 files changed

+65
-77
lines changed

2 files changed

+65
-77
lines changed

server/utils/storage.js

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@ var tarHelper = require('./tar-helper');
1212
chai.use(require('chai-as-promised'));
1313
var 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-
3015
function 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

406409
if (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

430420
module.exports = Storage;

test/storage-test.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ require('sinon-as-promised')(Bluebird);
1010
var uuid = require('node-uuid');
1111
var PNGImage = Bluebird.promisifyAll(require('pngjs-image'));
1212

13-
var storage = require('../server/utils/storage');
1413
var TarHelper = require('../server/utils/tar-helper');
1514
var dirHelper = require('../server/utils/dir-helper');
1615

1716
describe('module/storage', function() {
18-
before(function() {
19-
storage._dataPath = __TESTDATA__;
17+
var storage;
18+
var dirHelperStub;
19+
20+
beforeEach(function() {
21+
dirHelperStub = {};
22+
23+
var Storage = proxyquire('../server/utils/storage', {
24+
'./dir-helper': dirHelperStub
25+
});
26+
27+
storage = new Storage({
28+
dataPath: __TESTDATA__
29+
});
2030
});
2131

2232
after(function() {
23-
return fs.removeAsync(storage._dataPath);
33+
return fs.removeAsync(__TESTDATA__);
2434
});
2535

2636
describe('path generators', function() {
@@ -35,10 +45,6 @@ describe('module/storage', function() {
3545
});
3646
});
3747

38-
it('has _getBuildsPath', function() {
39-
assert(storage._getBuildsPath !== undefined);
40-
});
41-
4248
describe('#hasProject', function() {
4349
it('resolves false if project does not exist', function() {
4450
return assert.eventually.isFalse(storage.hasProject('foo'));
@@ -775,19 +781,11 @@ describe('module/storage', function() {
775781

776782
describe('#getImagesForShaBrowser', function() {
777783
var project;
778-
var readFilesStub;
779-
var storage;
780784

781785
beforeEach(function() {
782786
project = uuid.v4();
783787

784-
readFilesStub = this.sinon.stub().resolves([]);
785-
786-
storage = proxyquire('../server/utils/storage', {
787-
'./dir-helper': {
788-
readFiles: readFilesStub
789-
}
790-
});
788+
dirHelperStub.readFiles = this.sinon.stub().resolves([]);
791789

792790
this.sinon.stub(storage, 'hasProject').resolves(true);
793791
});
@@ -812,7 +810,7 @@ describe('module/storage', function() {
812810
.then(function() {
813811
var browserPath = path.join(storage._getShasPath(project), sha, browser);
814812

815-
assert.calledOnce(readFilesStub.withArgs(browserPath));
813+
assert.calledOnce(dirHelperStub.readFiles.withArgs(browserPath));
816814
});
817815
});
818816
});

0 commit comments

Comments
 (0)