UNPKG

3.63 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 'moduleName'"
38 args: []
39 expected: [
40 source: 'stdout'
41 message: "Missing required positional argument: 'moduleName'\n"
42 ,
43 source: 'stdout'
44 message: 'Usage: axiom <moduleName> <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 positional argument: 'serviceName'\n"
52 ,
53 source: 'stdout'
54 message: 'Usage: axiom <moduleName> <serviceName> [<--arg> <value> ...]\n\n\n'
55 ]
56 ,
57 description: 'should start the server'
58 args: ['hello', 'world']
59 expected: [{source: 'stdout', message: 'Hello, world!\n'}]
60]
61
62logTests = [
63 description: "should accept '--log=info'"
64 args: ['hello', 'world', '--log=info']
65 expected: [
66 source: 'stdout'
67 message: 'Hello, world!\n'
68 ]
69 # DISABLED: console does not behave as expected due to buffering.
70 #
71 #,
72 #description: "should accept '--log=debug'"
73 #args: ['hello', 'world', '--log=debug']
74 #expected: [
75 #source: 'stdout'
76 #message: "Calling 'core.init' with args: { config: undefined, retriever: undefined }\n"
77 #,
78 #source: 'stdout'
79 #message: "Calling 'core.load' with args: { moduleName: 'base' }\n"
80 #,
81 #source: 'stdout'
82 #message: "Calling 'core.load' with args: { moduleName: 'hello' }\n"
83 #,
84 #source: 'stdout'
85 #message: "Calling 'core.respond' with args: { channel: 'hello.world' }\n"
86 #,
87 #source: 'stdout'
88 #message: "Calling 'core.request' with args: { channel: 'hello.world', data: { log: 'debug' } }\n"
89 #,
90 #source: 'stdout'
91 #message: "Calling 'core.send' with args: { channel: 'hello.world', data: { log: 'debug' } }\n"
92 #,
93 #source: 'stdout'
94 #message: 'Hello, world!\n'
95 #]
96]
97
98describe 'cli', ->
99 @timeout 2000
100
101 # General tests
102 for test in tests
103 do (test) ->
104 # Given a set of arguments
105 {description, args, expected} = test
106
107 it description, (done) ->
108 # When we run the CLI with those arguments
109 {cli, output} = cliRun args
110
111 cli.on 'close', ->
112 # Teh result should be what we expect
113 output.should.eql expected
114
115 done()
116
117 # Log-level tests
118 for test in logTests
119 do (test) ->
120 # Given an expected sequence of unprefixed log statements
121 {description, args, expected} = test
122
123 it description, (done) ->
124 # When we run the CLI at a given log level
125 {cli, output} = cliRun args
126
127 cli.on 'close', ->
128 # The result should be what we expect, after
129 # stripping the prefixed timestamps.
130 removeTimestamps(output).should.eql expected
131
132 done()