Skip to content

Commit 7b0231e

Browse files
author
Aaron Chambers
committed
Merge pull request #19 from dschmidt/pass-through-database-number
Pass through database number
2 parents 3b1f6ed + 7930ca3 commit 7b0231e

File tree

5 files changed

+162
-121
lines changed

5 files changed

+162
-121
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ The Redis port. If [url](#url) is defined, then this option is not needed.
7575

7676
*Default:* `6379`
7777

78+
### database
79+
80+
The Redis database number. If [url](#url) is defined, then this option is not needed.
81+
82+
*Default:* `undefined`
83+
7884
### password
7985

8086
The Redis password. If [url](#url) is defined, then this option is not needed.
@@ -113,9 +119,9 @@ The unique revision number for the version of the file being uploaded to Redis.
113119

114120
### redisDeployClient
115121

116-
The Redis client to be used to upload files to the Redis store. By default this option will use a new instance of the [Redis][3] client unless another client is provided in the `redisDeployClient` property of the deployment context. This allows for injection of a mock client for testing purposes.
122+
The Redis client to be used to upload files to the Redis store. By default this option will use a new instance of the [Redis][3] client. This allows for injection of a mock client for testing purposes.
117123

118-
*Default:* `context.redisDeployClient || new Redis(context.config.redis)`
124+
*Default:* `return new Redis(options)`
119125

120126
### didDeployMessage
121127

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ module.exports = {
3939
return context.commandOptions.revision || context.revisionKey;
4040
},
4141
redisDeployClient: function(context) {
42-
return context.redisDeployClient || new Redis(context.config.redis);
42+
var options = context.config.redis;
43+
var redisLib = context._redisLib;
44+
45+
return new Redis(options, redisLib);
4346
}
4447
},
4548
configure: function(/* context */) {

lib/redis.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ var CoreObject = require('core-object');
22
var Promise = require('ember-cli/lib/ext/promise');
33

44
module.exports = CoreObject.extend({
5-
init: function(options) {
6-
if (options.redisClient) {
7-
this._client = options.redisClient;
8-
} else if (options.url) {
9-
this._client = require('then-redis').createClient(options.url);
5+
init: function(options, lib) {
6+
var redisOptions = options;
7+
var redisLib = lib;
8+
9+
if (options.url) {
10+
redisOptions = options.url;
1011
} else {
11-
var redisOptions = {
12+
redisOptions = {
1213
host: options.host,
1314
port: options.port
1415
};
@@ -17,8 +18,17 @@ module.exports = CoreObject.extend({
1718
redisOptions.password = options.password;
1819
}
1920

20-
this._client = require('then-redis').createClient(redisOptions);
21+
if (options.database) {
22+
redisOptions.database = options.database;
23+
}
24+
}
25+
26+
if (!redisLib) {
27+
redisLib = require('then-redis');
2128
}
29+
30+
this._client = redisLib.createClient(redisOptions);
31+
2232
this._maxNumberOfRecentUploads = 10;
2333
this._allowOverwrite = options.allowOverwrite;
2434
},

tests/unit/index-nodetest.js

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
var Promise = require('ember-cli/lib/ext/promise');
44
var assert = require('ember-cli/tests/helpers/assert');
5+
var CoreObject = require('core-object');
56

67
var stubProject = {
78
name: function(){
89
return 'my-project';
910
}
1011
};
1112

13+
var fakeClient = {
14+
createClient: function(options) {
15+
this.options = options;
16+
return {
17+
get: function(key) {
18+
return Promise.resolve('some-other-value');
19+
},
20+
set: function() { },
21+
lpush: function() { },
22+
ltrim: function() { }
23+
}
24+
}
25+
};
26+
1227
describe('redis plugin', function() {
1328
var subject, mockUi;
1429

@@ -53,7 +68,8 @@ describe('redis plugin', function() {
5368
config: {
5469
redis: {
5570
host: 'somehost',
56-
port: 1234
71+
port: 1234,
72+
database: 4
5773
}
5874
}
5975
};
@@ -62,6 +78,32 @@ describe('redis plugin', function() {
6278
assert.ok(true); // didn't throw an error
6379
});
6480

81+
it('passes through config options', function () {
82+
var plugin = subject.createDeployPlugin({
83+
name: 'redis'
84+
});
85+
86+
var context = {
87+
ui: mockUi,
88+
project: stubProject,
89+
config: {
90+
redis: {
91+
host: 'somehost',
92+
port: 1234,
93+
database: 4
94+
}
95+
},
96+
_redisLib: fakeClient
97+
};
98+
plugin.beforeHook(context);
99+
plugin.configure(context);
100+
plugin.readConfig('redisDeployClient');
101+
102+
assert.equal(fakeClient.options.host, 'somehost');
103+
assert.equal(fakeClient.options.port, 1234);
104+
assert.equal(fakeClient.options.database, 4);
105+
});
106+
65107
describe('resolving revisionKey from the pipeline', function() {
66108
it('uses the config data if it already exists', function() {
67109
var plugin = subject.createDeployPlugin({
@@ -262,11 +304,6 @@ describe('redis plugin', function() {
262304
});
263305

264306
context = {
265-
redisClient: {
266-
upload: function(keyPrefix, revisionKey) {
267-
return Promise.resolve(keyPrefix + ':' + revisionKey);
268-
}
269-
},
270307
ui: mockUi,
271308
project: stubProject,
272309
config: {
@@ -276,7 +313,11 @@ describe('redis plugin', function() {
276313
distDir: 'tests',
277314
revisionKey: '123abc',
278315
redisDeployClient: function(context) {
279-
return context.redisClient || new Redis(context.config.redis);
316+
return {
317+
upload: function(keyPrefix, revisionKey) {
318+
return Promise.resolve(keyPrefix + ':' + revisionKey);
319+
}
320+
};
280321
}
281322
}
282323
}
@@ -300,11 +341,6 @@ describe('redis plugin', function() {
300341
});
301342

302343
var context = {
303-
redisClient: {
304-
activate: function() {
305-
activateCalled = true;
306-
}
307-
},
308344
ui: mockUi,
309345
project: stubProject,
310346
config: {
@@ -313,7 +349,13 @@ describe('redis plugin', function() {
313349
filePattern: 'index.html',
314350
distDir: 'tests',
315351
revisionKey: '123abc',
316-
redisDeployClient: function(context){ return context.redisClient; }
352+
redisDeployClient: function(context){
353+
return {
354+
activate: function() {
355+
activateCalled = true;
356+
}
357+
};
358+
}
317359
}
318360
}
319361
};
@@ -332,11 +374,6 @@ describe('redis plugin', function() {
332374
});
333375

334376
var context = {
335-
redisClient: {
336-
activate: function() {
337-
return Promise.reject('some-error');
338-
}
339-
},
340377
ui: mockUi,
341378
project: stubProject,
342379
config: {
@@ -346,7 +383,11 @@ describe('redis plugin', function() {
346383
distDir: 'tests',
347384
revisionKey: '123abc',
348385
redisDeployClient: function(context) {
349-
return context.redisClient || new Redis(context.config.redis);
386+
return {
387+
activate: function() {
388+
return Promise.reject('some-error');
389+
}
390+
};
350391
}
351392
}
352393
}

0 commit comments

Comments
 (0)