]> git.saurik.com Git - redis.git/blob - tests/integration/redis-cli.tcl
Let the output mode depend on having a tty or not
[redis.git] / tests / integration / redis-cli.tcl
1 start_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} {
39 set fd [open_cli]
40 test "Interactive CLI: $name" $code
41 close_cli $fd
42 }
43
44 proc run_nontty_cli {args} {
45 set fd [open [format "|src/redis-cli -p %d -n 9 $args" [srv port]] "r"]
46 fconfigure $fd -buffering none
47 fconfigure $fd -translation binary
48 set resp [read $fd 1048576]
49 close $fd
50 set _ $resp
51 }
52
53 proc test_nontty_cli {name code} {
54 test "Non-interactive non-TTY CLI: $name" $code
55 }
56
57 proc run_tty_cli {args} {
58 set ::env(FAKETTY) 1
59 set resp [run_nontty_cli {*}$args]
60 unset ::env(FAKETTY)
61 set _ $resp
62 }
63
64 proc test_tty_cli {name code} {
65 test "Non-interactive TTY CLI: $name" $code
66 }
67
68 test_interactive_cli "INFO response should be printed raw" {
69 set lines [split [run_command $fd info] "\n"]
70 foreach line $lines {
71 assert [regexp {^[a-z0-9_]+:[a-z0-9_]+} $line]
72 }
73 }
74
75 test_interactive_cli "Status reply" {
76 assert_equal "OK" [run_command $fd "set key foo"]
77 }
78
79 test_interactive_cli "Integer reply" {
80 assert_equal "(integer) 1" [run_command $fd "incr counter"]
81 }
82
83 test_interactive_cli "Bulk reply" {
84 r set key foo
85 assert_equal "\"foo\"" [run_command $fd "get key"]
86 }
87
88 test_interactive_cli "Multi-bulk reply" {
89 r rpush list foo
90 r rpush list bar
91 assert_equal "1. \"foo\"\n2. \"bar\"" [run_command $fd "lrange list 0 -1"]
92 }
93
94 test_interactive_cli "Parsing quotes" {
95 assert_equal "OK" [run_command $fd "set key \"bar\""]
96 assert_equal "bar" [r get key]
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 \"\tbar\t\""]
102 assert_equal "\tbar\t" [r get key]
103
104 # invalid quotation
105 assert_equal "Invalid argument(s)" [run_command $fd "get \"\"key"]
106 assert_equal "Invalid argument(s)" [run_command $fd "get \"key\"x"]
107
108 # quotes after the argument are weird, but should be allowed
109 assert_equal "OK" [run_command $fd "set key\"\" bar"]
110 assert_equal "bar" [r get key]
111 }
112
113 test_tty_cli "Status reply" {
114 assert_equal "OK\n" [run_tty_cli set key bar]
115 assert_equal "bar" [r get key]
116 }
117
118 test_tty_cli "Integer reply" {
119 r del counter
120 assert_equal "(integer) 1\n" [run_tty_cli incr counter]
121 }
122
123 test_tty_cli "Bulk reply" {
124 r set key "tab\tnewline\n"
125 assert_equal "\"tab\\tnewline\\n\"\n" [run_tty_cli get key]
126 }
127
128 test_tty_cli "Multi-bulk reply" {
129 r del list
130 r rpush list foo
131 r rpush list bar
132 assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_tty_cli lrange list 0 -1]
133 }
134 }