1 | path = require 'path'
|
2 |
|
3 | should = require 'should'
|
4 | mockery = require 'mockery'
|
5 | _ = require 'lodash'
|
6 | logger = require 'torch'
|
7 |
|
8 | core = require '../lib/core'
|
9 | findProjectRoot = require '../lib/findProjectRoot'
|
10 |
|
11 | sampleDir = path.join __dirname, '../sample'
|
12 | sampleProjDir = path.join sampleDir, 'project1'
|
13 | sample = require path.join sampleDir, 'sample'
|
14 |
|
15 | describe 'core.init', ->
|
16 | beforeEach ->
|
17 | process.chdir sampleProjDir
|
18 |
|
19 | @retriever = _.clone require '../lib/retriever'
|
20 | @retriever.projectRoot = findProjectRoot(process.cwd())
|
21 |
|
22 | mockery.enable
|
23 | warnOnReplace: false,
|
24 | warnOnUnregistered: false
|
25 |
|
26 | mockery.registerMock @retriever.rel('node_modules', 'axiom-base'), {
|
27 | services:
|
28 | runtime: (args, next) ->
|
29 | next null, {message: 'axiom-base'}
|
30 | }
|
31 | prefix = @retriever.rel 'node_modules', 'axiom-sample'
|
32 | mockery.registerMock @retriever.rel('node_modules', 'axiom-sample'), sample
|
33 |
|
34 | afterEach ->
|
35 | core.reset()
|
36 | mockery.disable()
|
37 |
|
38 | after ->
|
39 | core.reset()
|
40 |
|
41 | it 'should load axiom-base', (done) ->
|
42 | core.init()
|
43 |
|
44 | core.request 'base.runtime', {}, (err, result) ->
|
45 | should.not.exist err
|
46 | should.exist result
|
47 | result.should.eql {message: 'axiom-base'}
|
48 | done()
|
49 |
|
50 | it 'should dynamically load a module based on name', (done) ->
|
51 | data = {greeting: 'hello!'}
|
52 | config =
|
53 | modules: ['sample']
|
54 | core.init config
|
55 |
|
56 | core.request 'sample.echo', data, (err, result) ->
|
57 | should.not.exist err
|
58 | should.exist result
|
59 | result.should.eql data
|
60 | done()
|
61 |
|
62 | it 'should not init a module that is blacklisted', (done) ->
|
63 | config =
|
64 | blacklist: ['sample']
|
65 | modules: ['sample']
|
66 | core.init config
|
67 |
|
68 | core.request 'sample.echo', {greeting: 'hello!'}, (err, result) ->
|
69 | should.exist err
|
70 | err.message.should.eql "No responders for request: 'sample.echo'"
|
71 |
|
72 | should.not.exist result
|
73 |
|
74 | done()
|
75 |
|
76 | it "should load a global 'axiom' file from the project root", (done) ->
|
77 | internal = require '../lib/core/internal'
|
78 | axiomFile = require path.join(sampleProjDir, 'axiom')
|
79 | should.exist axiomFile
|
80 | core.init @retriever
|
81 | internal.config.should.include axiomFile
|
82 | done()
|
83 |
|
84 | it "should assume an 'axiom_configs' folder containing config overrides", (done) ->
|
85 | core.init {modules: ['sample']}, @retriever
|
86 |
|
87 |
|
88 | defaultSampleConfig = sample.config.whatsMyContext
|
89 | should.exist defaultSampleConfig
|
90 |
|
91 |
|
92 | overrideConfigPath = path.join sampleProjDir, 'axiom_configs', 'sample'
|
93 | overrideConfig = require(overrideConfigPath).whatsMyContext
|
94 | should.exist overrideConfig
|
95 |
|
96 | expectedConfig = _.merge {}, defaultSampleConfig, overrideConfig
|
97 |
|
98 |
|
99 | core.request 'sample.whatsMyContext', {}, (err, config) ->
|
100 | should.not.exist err
|
101 |
|
102 |
|
103 | should.exist config
|
104 | config.should.eql expectedConfig
|
105 |
|
106 | done()
|
107 |
|
108 | it "'retriever' in 'util' should be default instance", (done) ->
|
109 | defaultRetriever = @retriever
|
110 |
|
111 |
|
112 | server =
|
113 | services:
|
114 | "run/prepare": (args, fin) ->
|
115 |
|
116 |
|
117 | should.exist @util
|
118 | @util.should.include defaultRetriever
|
119 | fin()
|
120 |
|
121 |
|
122 | core.init()
|
123 | core.load "server", server
|
124 |
|
125 |
|
126 | core.request "server.run/prepare", {}, (err, result) ->
|
127 | should.not.exist err
|
128 | done()
|
129 |
|
130 | it "should expose an injected 'retriever' in 'util'", (done) ->
|
131 | defaultRetriever = @retriever
|
132 |
|
133 |
|
134 | mockRetriever =
|
135 | retrieve: (name...) -> {}
|
136 | retrieveExtension: (name...) -> {}
|
137 |
|
138 |
|
139 | server =
|
140 | services:
|
141 | "run/prepare": (args, fin) ->
|
142 | should.exist @util
|
143 |
|
144 |
|
145 | @util.should.include mockRetriever
|
146 |
|
147 |
|
148 | @util.should.not.include defaultRetriever
|
149 |
|
150 | fin()
|
151 |
|
152 |
|
153 | core.init {}, mockRetriever
|
154 |
|
155 |
|
156 | core.load "server", server
|
157 |
|
158 |
|
159 | core.request "server.run/prepare", {}, (err, result) ->
|
160 |
|
161 |
|
162 | should.not.exist err
|
163 | done()
|