Skip to content

Commit a1a8cef

Browse files
authored
Merge pull request koding#11466 from mertaytore/inherit-options
Configure with inheriting options flags
2 parents 0397e89 + 3852d80 commit a1a8cef

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ jobs:
256256
- run:
257257
name: test social worker
258258
command: ./run exec scripts/node-testing/mocha-runner workers/social/lib/social
259+
- run:
260+
name: test config
261+
command: ./run exec scripts/node-testing/mocha-runner config
259262
- run:
260263
name: codecov
261264
command: |

config/inheritOptionFlags.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
traverse = require 'traverse'
2+
3+
module.exports = (credentials, options) ->
4+
options_list = paths(options)
5+
credentials_list = paths(credentials)
6+
7+
for i, val of options_list
8+
if traverse.has(credentials, val)
9+
traverse(credentials).set(val, traverse.get(options, val))
10+
11+
paths = (obj) ->
12+
result = []
13+
traverse.forEach obj, (x) ->
14+
result.push @path if typeof x isnt 'object'
15+
return
16+
result
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
traverse = require 'traverse'
2+
inherit = require './inheritOptionFlags'
3+
4+
{ expect } = require '../workers/social/testhelper'
5+
6+
test_options =
7+
level1a :
8+
level2a : 'test-level2a-value'
9+
level2b :
10+
level3a : 'test-level3a-value'
11+
level2c :
12+
level3b : 'test-level3b-value'
13+
level3c : 'test-level3c-value'
14+
level1b : 'test-level1b-value'
15+
level1d : 'test-level1d-value'
16+
17+
test_credentials =
18+
level1a :
19+
level2a : 'credentials-level2a-value'
20+
level2b :
21+
level3a : 'credentials-level3a-value'
22+
level2c :
23+
level3b : 'credentials-level3b-value'
24+
level3c : 'credentials-level3c-value'
25+
level3d : 'credentials-level3d-value'
26+
level1b : 'credentials-level1b-value'
27+
level1c :
28+
level2d :
29+
level3e : 'credentials-level3e-value'
30+
31+
32+
getValues = (obj) -> traverse(obj).reduce(((values, x) ->
33+
if @isLeaf
34+
values.push x
35+
values
36+
), [])
37+
38+
runTests = ->
39+
credential_vals_before = getValues(test_credentials)
40+
options_values = getValues(test_options)
41+
42+
inherit test_credentials, test_options
43+
44+
credentials_values = getValues(test_credentials)
45+
# Tests for inheritance until third level
46+
it 'should set credentials-level1b-value to test-level1b-value', ->
47+
result = JSON.stringify test_credentials.level1b
48+
expected = JSON.stringify test_options.level1b
49+
expect(result).to.be.equal expected
50+
51+
it 'should set credentials-level2a-value to test-level2a-value', ->
52+
result = JSON.stringify test_credentials.level1a.level2a
53+
expected = JSON.stringify test_options.level1a.level2a
54+
expect(result).to.be.equal expected
55+
56+
it 'should set credentials-level3a-value to test-level3a-value', ->
57+
result = JSON.stringify test_credentials.level1a.level2b.level3a
58+
expected = JSON.stringify test_options.level1a.level2b.level3a
59+
expect(result).to.be.equal expected
60+
# ---
61+
62+
it 'should not set credentials-level3d-value to anything', ->
63+
result = JSON.stringify test_credentials.level1c.level2d.level3d
64+
expected = JSON.stringify test_credentials.level1c.level2d.level3d
65+
expect(result).to.be.equal expected
66+
67+
# No matching property in credentials
68+
it 'should not set any credential values to test-level1d-value', ->
69+
result = 'test-level1d-value' in credentials_values
70+
expected = false
71+
expect(result).to.be.equal expected
72+
73+
# Comparing changed and unchanged values
74+
it 'credential values that are not in options should remain unchanged', ->
75+
not_changed = false
76+
for val in credential_vals_before
77+
if val not in options_values and val in credentials_values
78+
not_changed = true
79+
result = not_changed
80+
expected = true
81+
expect(result).to.be.equal expected
82+
83+
runTests()

config/main.dev.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Configuration = (options = {}) ->
8181
botchannel : yes
8282
gitlab : no
8383

84+
(require './inheritOptionFlags') credentials, options
8485
KONFIG = require('./generateKonfig')(options, credentials)
8586
(require './inheritEnvVars') KONFIG if options.inheritEnvVars
8687
KONFIG.workers = require('./workers')(KONFIG, options, credentials)

0 commit comments

Comments
 (0)