UNPKG

3.92 kBtext/coffeescriptView Raw
1path = require 'path'
2should = require 'should'
3logger = require 'torch'
4{spawn, exec} = require 'child_process'
5
6rel = (name) -> path.join __dirname, '..', name
7
8command = rel 'bin/axiom'
9
10sampleProject = rel 'sample/project2'
11
12cliRun = (args) ->
13 cli = spawn command, args, {cwd: sampleProject}
14 output = []
15 ['stdout', 'stderr'].forEach (source) ->
16 cli[source].setEncoding 'utf8'
17 #cli[source].on 'data', logger.yellow
18 cli[source].on 'data', (message) -> output.push({source, message})
19
20 return {cli, output}
21
22removeTimestamps = (output) ->
23 # For each {source, message} in the collected output
24 output.map ({source, message}) ->
25
26 # If prefixed with a timestamp
27 if /\[.*\].*/.test(message)
28
29 cutoff = (msg) -> msg.replace /^.*\] /, ''
30
31 # Slice off the timestamp prefix
32 message = cutoff(message)
33
34 return {source, message}
35
36tests = [
37 description: "should fail with a help message if missing 'extensionName'"
38 args: []
39 expected: [
40 source: 'stdout'
41 message: "Missing required argument: 'extensionName'\n"
42 ,
43 source: 'stdout'
44 message: 'Usage: axiom <extensionName> <serviceName> [<--arg> <value> ...]\n\n\n'
45 ]
46 ,
47 description: "should fail with a help message if missing 'serviceName'"
48 args: ['hello']
49 expected: [
50 source: 'stdout'
51 message: "Missing required argument: 'serviceName'\n"
52 ,
53 source: 'stdout'
54 message: 'Usage: axiom <extensionName> <serviceName> [<--arg> <value> ...]\n\n\n'
55 ]
56 ,
57 description: 'should start the server'
58 args: ['hello', 'world']
59 expected: [
60 {source: 'stdout', message: 'Hello, world!\n'}
61 {source: 'stdout', message: 'Success!\n'}
62 ]
63 description: 'should pass args'
64 args: ['hello', 'args', '--hello.foo=1']
65 expected: [
66 {source: 'stdout', message: '{ foo: 1 }\n'}
67 {source: 'stdout', message: 'Success!\n'}
68 ]
69]
70
71logTests = [
72 description: "should accept '--log=info'"
73 args: ['hello', 'world', '--log=info']
74 expected: [
75 {source: 'stdout', message: 'Hello, world!\n'}
76 {source: 'stdout', message: 'Success!\n'}
77 ]
78 # DISABLED: console does not behave as expected due to buffering.
79 #
80 #,
81 #description: "should accept '--log=debug'"
82 #args: ['hello', 'world', '--log=debug']
83 #expected: [
84 #source: 'stdout'
85 #message: "Calling 'core.init' with args: { config: undefined, retriever: undefined }\n"
86 #,
87 #source: 'stdout'
88 #message: "Calling 'core.load' with args: { extensionName: 'base' }\n"
89 #,
90 #source: 'stdout'
91 #message: "Calling 'core.load' with args: { extensionName: 'hello' }\n"
92 #,
93 #source: 'stdout'
94 #message: "Calling 'core.respond' with args: { channel: 'hello.world' }\n"
95 #,
96 #source: 'stdout'
97 #message: "Calling 'core.request' with args: { channel: 'hello.world', data: { log: 'debug' } }\n"
98 #,
99 #source: 'stdout'
100 #message: "Calling 'core.send' with args: { channel: 'hello.world', data: { log: 'debug' } }\n"
101 #,
102 #source: 'stdout'
103 #message: 'Hello, world!\n'
104 #]
105]
106
107describe 'cli', ->
108 @timeout 2000
109
110 # General tests
111 for test in tests
112 do (test) ->
113 # Given a set of arguments
114 {description, args, expected} = test
115
116 it description, (done) ->
117 # When we run the CLI with those arguments
118 {cli, output} = cliRun args
119
120 cli.on 'close', ->
121 # Teh result should be what we expect
122 output.should.eql expected
123
124 done()
125
126 # Log-level tests
127 for test in logTests
128 do (test) ->
129 # Given an expected sequence of unprefixed log statements
130 {description, args, expected} = test
131
132 it description, (done) ->
133 # When we run the CLI at a given log level
134 {cli, output} = cliRun args
135
136 cli.on 'close', ->
137 # The result should be what we expect, after
138 # stripping the prefixed timestamps.
139 removeTimestamps(output).should.eql expected
140
141 done()