1
1
import { test } from 'tape'
2
- import yargs from 'yargs'
3
- import * as exec from '../../commands/exec.js'
4
- import { runPipe } from '../test.js'
5
-
6
- const parser = yargs ( ) . scriptName ( 'xst' ) . command ( exec ) . help ( ) . fail ( false )
7
-
8
- const execCmd = async ( cmd , args ) => {
9
- return await yargs ( )
10
- . scriptName ( 'xst' )
11
- . command ( cmd )
12
- . fail ( false )
13
- . parse ( args )
14
- }
15
-
16
- test ( 'shows help' , async function ( t ) {
17
- // Run the command module with --help as argument
18
- const output = await new Promise ( ( resolve , reject ) => {
19
- parser . parse ( [ 'execute' , '--help' ] , ( err , argv , output ) => {
20
- if ( err ) { return reject ( err ) }
21
- resolve ( output )
22
- } )
23
- } )
24
- const firstLine = output . split ( '\n' ) [ 0 ]
2
+ import { run , runPipe } from '../test.js'
3
+
4
+ test ( "calling 'xst execute --help'" , async ( t ) => {
5
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '--help' ] )
6
+ if ( stderr ) { return t . fail ( stderr ) }
7
+
8
+ const firstLine = stdout . split ( '\n' ) [ 0 ]
25
9
26
10
t . equal ( firstLine , 'xst execute [<query>] [options]' , firstLine )
11
+ t . end ( )
27
12
} )
28
13
29
- test ( 'executes command' , async function ( t ) {
30
- const argv = await new Promise ( ( resolve , reject ) => {
31
- parser . parse ( [ 'execute' , '1+1' ] , ( err , argv , output ) => {
32
- if ( err ) { return reject ( err ) }
33
- resolve ( argv )
34
- } )
35
- } )
14
+ test ( 'executes command' , async ( t ) => {
15
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '1+1' ] )
16
+ if ( stderr ) { return t . fail ( stderr ) }
36
17
37
- t . equal ( argv . query , '1+1 ' )
18
+ t . equal ( stdout , '2\n ' )
38
19
} )
39
20
40
21
test ( 'executes command with alias \'exec\'' , async function ( t ) {
41
- const argv = await new Promise ( ( resolve , reject ) => {
42
- parser . parse ( [ 'exec' , '1+1' ] , ( err , argv , output ) => {
43
- if ( err ) { return reject ( err ) }
44
- resolve ( argv )
45
- } )
46
- } )
47
-
48
- t . equal ( argv . query , '1+1' )
22
+ const { stderr, stdout } = await run ( 'xst' , [ 'exec' , '1+1' ] )
23
+ if ( stderr ) { return t . fail ( stderr ) }
24
+
25
+ t . equal ( stdout , '2\n' )
49
26
} )
50
27
51
28
test ( 'executes command with alias \'run\'' , async function ( t ) {
52
- const argv = await new Promise ( ( resolve , reject ) => {
53
- parser . parse ( [ 'run' , '1+1' ] , ( err , argv , output ) => {
54
- if ( err ) { return reject ( err ) }
55
- resolve ( argv )
56
- } )
57
- } )
58
-
59
- t . equal ( argv . query , '1+1' )
29
+ const { stderr, stdout } = await run ( 'xst' , [ 'run' , '1+1' ] )
30
+ if ( stderr ) { return t . fail ( stderr ) }
31
+
32
+ t . equal ( stdout , '2\n' )
60
33
} )
61
34
62
35
test ( 'executes bound command' , async function ( t ) {
63
- const argv = await new Promise ( ( resolve , reject ) => {
64
- parser . parse ( [ 'exec' , '-b' , '{"a":1}' , '$a+$a' ] , ( err , argv , output ) => {
65
- if ( err ) { return reject ( err ) }
66
- resolve ( argv )
67
- } )
68
- } )
69
- t . plan ( 2 )
70
- t . equal ( argv . query , '$a+$a' )
71
- t . equal ( argv . bind . a , 1 )
36
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-b' , '{"a":1}' , '$a+$a' ] )
37
+ if ( stderr ) { return t . fail ( stderr ) }
38
+
39
+ t . equal ( stdout , '2\n' )
72
40
} )
73
41
74
42
test ( 'bind parse error' , async function ( t ) {
75
- try {
76
- const res = await execCmd ( exec , [ 'exec' , '-b' , '{a:1}' , '$a+$a' ] )
77
- t . notOk ( res )
78
- } catch ( e ) {
79
- t . ok ( e , e )
80
- }
43
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-b' , '{a:1}' , '$a+$a' ] )
44
+ t . notOk ( stdout )
45
+ t . ok ( stderr , stderr )
81
46
} )
82
47
83
48
test ( 'read bind from stdin' , async function ( t ) {
@@ -87,44 +52,27 @@ test('read bind from stdin', async function (t) {
87
52
} )
88
53
89
54
test ( 'cannot read bind from stdin' , async function ( t ) {
90
- try {
91
- const res = await execCmd ( exec , [ 'exec' , '-b' , '-' , '$a+$a' ] )
92
- t . fail ( res )
93
- } catch ( e ) {
94
- t . ok ( e , e )
95
- }
55
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-b' , '-' , '$a+$a' ] )
56
+ if ( stdout ) { return t . fail ( stdout ) }
57
+ t . ok ( stderr , stderr )
96
58
} )
97
59
98
60
test ( 'cannot read query file from stdin' , async function ( t ) {
99
- try {
100
- const res = await execCmd ( exec , [ 'exec' , '-f' , '-' ] )
101
- t . fail ( res )
102
- } catch ( e ) {
103
- t . ok ( e , e )
104
- }
61
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-f' , '-' ] )
62
+ if ( stdout ) { return t . fail ( stdout ) }
63
+ t . ok ( stderr , stderr )
105
64
} )
106
65
107
66
test ( 'read query file' , async function ( t ) {
108
- try {
109
- const argv = await new Promise ( ( resolve , reject ) => {
110
- parser . parse ( [ 'exec' , '-f' , './spec/fixtures/test.xq' ] , ( err , argv , output ) => {
111
- if ( err ) { return reject ( err ) }
112
- resolve ( argv )
113
- } )
114
- } )
115
- t . equals ( argv . f , 'spec/fixtures/test.xq' , 'should be normalized' )
116
- } catch ( e ) {
117
- t . notOk ( e , e )
118
- }
67
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-f' , './spec/fixtures/test.xq' ] )
68
+ if ( stderr ) { return t . fail ( stderr ) }
69
+ t . ok ( stdout , stdout )
119
70
} )
120
71
121
72
test ( 'read query file with query' , async function ( t ) {
122
- try {
123
- const argv = await parser . parse ( [ 'exec' , '-f' , 'spec/fixtures/test.xq' , '1+1' ] )
124
- t . fail ( argv , 'Should not return a result' )
125
- } catch ( e ) {
126
- t . ok ( e , e )
127
- }
73
+ const { stderr, stdout } = await run ( 'xst' , [ 'execute' , '-f' , './spec/fixtures/test.xq' , '1+1' ] )
74
+ if ( stdout ) { return t . fail ( stdout ) }
75
+ t . ok ( stderr , stderr )
128
76
} )
129
77
130
78
test ( 'read file from stdin' , async function ( t ) {
0 commit comments