]>
git.saurik.com Git - redis.git/blob - tests/integration/redis-cli.tcl
1 start_server
{tags
{"cli"}} {
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]
18 while {[string length
$buf] == 0} {
19 # wait some time and try again
26 proc write_cli
{fd buf
} {
31 # Helpers to run tests in interactive mode
32 proc run_command
{fd cmd
} {
34 set lines
[split [read_cli
$fd] "\n"]
35 assert_equal
"redis> " [lindex $lines end
]
36 join [lrange $lines 0 end-1
] "\n"
39 proc test_interactive_cli
{name code
} {
42 test
"Interactive CLI: $name" $code
47 # Helpers to run tests where stdout is not a tty
48 proc write_tmpfile
{contents
} {
49 set tmp
[tmpfile
"cli"]
50 set tmpfd
[open $tmp "w"]
51 puts -nonewline $tmpfd $contents
56 proc _run_cli
{opts args
} {
57 set cmd
[format "src/redis-cli -p %d -n 9 $args" [srv port
]]
58 foreach {key value
} $opts {
60 set cmd
"sh -c \"$value | $cmd\""
63 set cmd
"$cmd < $value"
67 set fd
[open "|$cmd" "r"]
68 fconfigure $fd -buffering none
69 fconfigure $fd -translation binary
70 set resp
[read $fd 1048576]
79 proc run_cli_with_input_pipe
{cmd args
} {
80 _run_cli
[list pipe
$cmd] {*}$args
83 proc run_cli_with_input_file
{path args
} {
84 _run_cli
[list path
$path] {*}$args
87 proc test_nontty_cli
{name code
} {
88 test
"Non-interactive non-TTY CLI: $name" $code
91 # Helpers to run tests where stdout is a tty (fake it)
92 proc test_tty_cli
{name code
} {
94 test
"Non-interactive TTY CLI: $name" $code
98 test_interactive_cli
"INFO response should be printed raw" {
99 set lines
[split [run_command
$fd info] "\n"]
100 foreach line
$lines {
101 assert
[regexp {^
[a-z0-9_
]+:[a-z0-9_
]+} $line]
105 test_interactive_cli
"Status reply" {
106 assert_equal
"OK" [run_command
$fd "set key foo"]
109 test_interactive_cli
"Integer reply" {
110 assert_equal
"(integer) 1" [run_command
$fd "incr counter"]
113 test_interactive_cli
"Bulk reply" {
115 assert_equal
"\"foo\"" [run_command
$fd "get key"]
118 test_interactive_cli
"Multi-bulk reply" {
121 assert_equal
"1. \"foo\"\n2. \"bar\"" [run_command
$fd "lrange list 0 -1"]
124 test_interactive_cli
"Parsing quotes" {
125 assert_equal
"OK" [run_command
$fd "set key \"bar\""]
126 assert_equal
"bar" [r get key
]
127 assert_equal
"OK" [run_command
$fd "set key \" bar \""]
128 assert_equal
" bar " [r get key
]
129 assert_equal
"OK" [run_command
$fd "set key \"\\\"bar\\\"\""]
130 assert_equal
"\"bar\"" [r get key
]
131 assert_equal
"OK" [run_command
$fd "set key \"\tbar\t\""]
132 assert_equal
"\tbar\t" [r get key
]
135 assert_equal
"Invalid argument(s)" [run_command
$fd "get \"\"key"]
136 assert_equal
"Invalid argument(s)" [run_command
$fd "get \"key\"x"]
138 # quotes after the argument are weird, but should be allowed
139 assert_equal
"OK" [run_command
$fd "set key\"\" bar"]
140 assert_equal
"bar" [r get key
]
143 test_tty_cli
"Status reply" {
144 assert_equal
"OK\n" [run_cli
set key bar
]
145 assert_equal
"bar" [r get key
]
148 test_tty_cli
"Integer reply" {
150 assert_equal
"(integer) 1\n" [run_cli
incr counter
]
153 test_tty_cli
"Bulk reply" {
154 r
set key
"tab\tnewline\n"
155 assert_equal
"\"tab\\tnewline\\n\"\n" [run_cli get key
]
158 test_tty_cli
"Multi-bulk reply" {
162 assert_equal
"1. \"foo\"\n2. \"bar\"\n" [run_cli
lrange list 0 -1]
165 test_tty_cli
"Read last argument from pipe" {
166 assert_equal
"OK\n" [run_cli_with_input_pipe
"echo foo" set key
]
167 assert_equal
"foo\n" [r get key
]
170 test_tty_cli
"Read last argument from file" {
171 set tmpfile
[write_tmpfile
"from file"]
172 assert_equal
"OK\n" [run_cli_with_input_file
$tmpfile set key
]
173 assert_equal
"from file" [r get key
]
176 test_nontty_cli
"Status reply" {
177 assert_equal
"OK" [run_cli
set key bar
]
178 assert_equal
"bar" [r get key
]
181 test_nontty_cli
"Integer reply" {
183 assert_equal
"1" [run_cli
incr counter
]
186 test_nontty_cli
"Bulk reply" {
187 r
set key
"tab\tnewline\n"
188 assert_equal
"tab\tnewline\n" [run_cli get key
]
191 test_nontty_cli
"Multi-bulk reply" {
195 assert_equal
"foo\nbar" [run_cli
lrange list 0 -1]
198 test_nontty_cli
"Read last argument from pipe" {
199 assert_equal
"OK" [run_cli_with_input_pipe
"echo foo" set key
]
200 assert_equal
"foo\n" [r get key
]
203 test_nontty_cli
"Read last argument from file" {
204 set tmpfile
[write_tmpfile
"from file"]
205 assert_equal
"OK" [run_cli_with_input_file
$tmpfile set key
]
206 assert_equal
"from file" [r get key
]