]> git.saurik.com Git - redis.git/blame - tests/integration/redis-cli.tcl
Add a newline to tty output after every reply
[redis.git] / tests / integration / redis-cli.tcl
CommitLineData
f2dd4769
PN
1start_server {tags {"cli"}} {
2 proc open_cli {} {
3 set ::env(TERM) dumb
4 set fd [open [format "|src/redis-cli -p %d -n 9" [srv port]] "r+"]
5 fconfigure $fd -buffering none
6 fconfigure $fd -blocking false
7 fconfigure $fd -translation binary
8 assert_equal "redis> " [read_cli $fd]
9 set _ $fd
10 }
11
12 proc close_cli {fd} {
13 close $fd
14 }
15
16 proc read_cli {fd} {
17 set buf [read $fd]
18 while {[string length $buf] == 0} {
19 # wait some time and try again
20 after 10
21 set buf [read $fd]
22 }
23 set _ $buf
24 }
25
26 proc write_cli {fd buf} {
27 puts $fd $buf
28 flush $fd
29 }
30
31 proc run_command {fd cmd} {
32 write_cli $fd $cmd
33 set lines [split [read_cli $fd] "\n"]
34 assert_equal "redis> " [lindex $lines end]
35 join [lrange $lines 0 end-1] "\n"
36 }
37
38 proc test_interactive_cli {name code} {
ae77016e 39 set ::env(FAKETTY) 1
f2dd4769
PN
40 set fd [open_cli]
41 test "Interactive CLI: $name" $code
42 close_cli $fd
ae77016e 43 unset ::env(FAKETTY)
f2dd4769
PN
44 }
45
123a10f7 46 proc run_nontty_cli {args} {
07242c0c
PN
47 set fd [open [format "|src/redis-cli -p %d -n 9 $args" [srv port]] "r"]
48 fconfigure $fd -buffering none
49 fconfigure $fd -translation binary
50 set resp [read $fd 1048576]
51 close $fd
52 set _ $resp
53 }
54
123a10f7
PN
55 proc test_nontty_cli {name code} {
56 test "Non-interactive non-TTY CLI: $name" $code
57 }
58
59 proc run_tty_cli {args} {
60 set ::env(FAKETTY) 1
61 set resp [run_nontty_cli {*}$args]
62 unset ::env(FAKETTY)
63 set _ $resp
64 }
65
66 proc test_tty_cli {name code} {
67 test "Non-interactive TTY CLI: $name" $code
07242c0c
PN
68 }
69
f2dd4769
PN
70 test_interactive_cli "INFO response should be printed raw" {
71 set lines [split [run_command $fd info] "\n"]
72 foreach line $lines {
73 assert [regexp {^[a-z0-9_]+:[a-z0-9_]+} $line]
74 }
75 }
76
77 test_interactive_cli "Status reply" {
78 assert_equal "OK" [run_command $fd "set key foo"]
79 }
80
81 test_interactive_cli "Integer reply" {
82 assert_equal "(integer) 1" [run_command $fd "incr counter"]
83 }
84
85 test_interactive_cli "Bulk reply" {
86 r set key foo
87 assert_equal "\"foo\"" [run_command $fd "get key"]
88 }
89
90 test_interactive_cli "Multi-bulk reply" {
91 r rpush list foo
92 r rpush list bar
93 assert_equal "1. \"foo\"\n2. \"bar\"" [run_command $fd "lrange list 0 -1"]
94 }
0439d792
PN
95
96 test_interactive_cli "Parsing quotes" {
97 assert_equal "OK" [run_command $fd "set key \"bar\""]
98 assert_equal "bar" [r get key]
99 assert_equal "OK" [run_command $fd "set key \" bar \""]
100 assert_equal " bar " [r get key]
101 assert_equal "OK" [run_command $fd "set key \"\\\"bar\\\"\""]
102 assert_equal "\"bar\"" [r get key]
103 assert_equal "OK" [run_command $fd "set key \"\tbar\t\""]
104 assert_equal "\tbar\t" [r get key]
105
106 # invalid quotation
107 assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"]
108 assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"]
109
110 # quotes after the argument are weird, but should be allowed
111 assert_equal "OK" [run_command $fd "set key\"\" bar"]
112 assert_equal "bar" [r get key]
113 }
07242c0c 114
123a10f7
PN
115 test_tty_cli "Status reply" {
116 assert_equal "OK\n" [run_tty_cli set key bar]
07242c0c
PN
117 assert_equal "bar" [r get key]
118 }
119
123a10f7 120 test_tty_cli "Integer reply" {
07242c0c 121 r del counter
123a10f7 122 assert_equal "(integer) 1\n" [run_tty_cli incr counter]
07242c0c
PN
123 }
124
123a10f7 125 test_tty_cli "Bulk reply" {
07242c0c 126 r set key "tab\tnewline\n"
123a10f7 127 assert_equal "\"tab\\tnewline\\n\"\n" [run_tty_cli get key]
07242c0c
PN
128 }
129
123a10f7 130 test_tty_cli "Multi-bulk reply" {
07242c0c
PN
131 r del list
132 r rpush list foo
133 r rpush list bar
123a10f7 134 assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_tty_cli lrange list 0 -1]
07242c0c 135 }
3a51bff0
PN
136
137 test_nontty_cli "Status reply" {
138 assert_equal "OK" [run_nontty_cli set key bar]
139 assert_equal "bar" [r get key]
140 }
141
142 test_nontty_cli "Integer reply" {
143 r del counter
144 assert_equal "1" [run_nontty_cli incr counter]
145 }
146
147 test_nontty_cli "Bulk reply" {
148 r set key "tab\tnewline\n"
149 assert_equal "tab\tnewline\n" [run_nontty_cli get key]
150 }
151
152 test_nontty_cli "Multi-bulk reply" {
153 r del list
154 r rpush list foo
155 r rpush list bar
156 assert_equal "foo\nbar" [run_nontty_cli lrange list 0 -1]
157 }
f2dd4769 158}