varnishtest "Test varnishtest itself"

shell -exit 1 -expect {varnishtest [options]} {varnishtest -h}

shell -exit 1 -match {-D.*Define macro} {varnishtest -h}

shell {
	pwd
	echo 'notvarnishtest foo bar' > _.vtc
	echo 'shell "exit 9"' >> _.vtc
}

shell -exit 2 -expect {doesn't start with 'vtest' or 'varnishtes} {
	varnishtest -v _.vtc
}

shell -exit 77 -expect {0 tests failed, 1 tests skipped, 0 tests passed} {
	unset TMPDIR
	varnishtest -k _.vtc
}

# Test external macro-def with a a two-turtle test
shell -expect {__=barf} {
	echo varnishtest foo > _.vtc
	printf 'shell {echo %c{foobar} > ${tmpdir}/__}' '$'  >> _.vtc
	varnishtest -q -Dfoobar=barf _.vtc
	echo __=`cat __`
}

# Test a test failure
shell -exit 2 -expect {TEST _.vtc FAILED} {
	echo varnishtest foo > _.vtc
	echo 'shell {false}' >> _.vtc
	exec varnishtest -v _.vtc || true
}

# Test a test skip
shell -exit 77 -expect {TEST _.vtc skipped} {
	echo varnishtest foo > _.vtc
	echo 'feature cmd false' >> _.vtc
	exec varnishtest -v _.vtc || true
}

# Simple process tests

process p1 "cat" -start
process p1 -writeln "foo"
process p1 -expect-text 2 1 foo
process p1 -stop
process p1 -wait
shell "grep -q foo ${p1_out}"
shell "test -f ${p1_err} -a ! -s ${p1_err}"

process p2 -log "cat" -start
process p2 -writeln "bar"
process p2 -expect-text 2 1 bar
process p2 -write "\x04"
process p2 -wait
shell "grep -q bar ${p2_out}"
shell "test -f ${p2_err} -a ! -s ${p2_err}"

process p3 -dump "cat" -start
process p3 -writeln "baz"
process p3 -expect-text 2 1 baz
process p3 -kill KILL
process p3 -wait
shell "grep -q baz ${p3_out}"
shell "test -f ${p3_err} -a ! -s ${p3_err}"

process p4 -hexdump "cat" -start
process p4 -writeln "b\001z"
process p4 -expect-text 2 1 "b"
process p4 -kill TERM
process p4 -wait -screen_dump

# Curses process tests

process p5 "ps -lw | grep '[p][s]' ; tty ; echo @" -start
process p5 -expect-text 0 0 {@} -screen_dump -wait

process p6 "stty -a ; echo '*'" -start
process p6 -expect-text 0 0 {*} -screen_dump -wait

process p7 -hexdump {stty raw -echo; stty -a ; echo "*" ; cat} -start
process p7 -expect-text 0 0 "*" -screen_dump

process p7 -write "\x1b[H\x1b[2Jzzzzzzz"
process p7 -write "\x0c1\x1b[79C2\x08>\x1b[25;1H3\x1b[25;80H"
process p7 -write "\x1b[H\x1b[2J1\x1b[79C2\x08>\x1b[25;1H3\x1b[25;80H"
process p7 -write "4\x08>\x1b[A\x1b[Cv\x1b[22A^\x1b[79D^\x1b[;2H<\n\n\n\n"
process p7 -write "\n\n\n\n\n\n\n\n\x1b[B\x1b[11B\x08<\x1b[24;Hv\x1b[12;1H"
process p7 -write "111111112222222333333\x0d\x0a111111112"
process p7 -write "222222333333\x0d\x0a111111112222222333333 UTF8: "
process p7 -writehex {c2 a2 20}
process p7 -writehex {e2 82 ac 20}
process p7 -writehex {f0 90 80 80 20}
process p7 -writehex {f0 9f 90 b0 20}
process p7 -writehex {f0 a0 80 80 20}
process p7 -writehex {f0 b0 80 80 20}
process p7 -write "\x1b[22;24;25;27;30;47;49;97;107m"
process p7 -write "\x1b[22;24;25;27;30m"
process p7 -write "\x1b[47;49;97;107m"
process p7 -write "\x0d\x0a111111112222222333333\x0d\x0a\x1b[12"
process p7 -write ";12H\x1b[K\x1b[13;12H\x1b[0K\x1b[14;12H\x1b[1K\x1b"
process p7 -write "[15;12H\x1b[2K\x1b[3;1Hline3 <\x0d\x0a"

process p7 -need-bytes 310 -expect-text 3 1 "line3 <"
process p7 -expect-cursor 4 1
process p7 -expect-cursor 4 0
process p7 -expect-cursor 0 1
process p7 -screen-dump

# Also exercise CONS25 mode
process p7 -write "\x1b[=1T"
process p7 -write "\x1b[=2T"
process p7 -write "\x1b[8z"
process p7 -write "\x1b[0x"
process p7 -write "\x1b[=1A"
process p7 -write "\x1b[=1;2B"
process p7 -write "\x1b[=1;2;3C"
process p7 -write "\x1b[=1;2;3;4C"
process p7 -write "\x1b[=1F"
process p7 -write "\x1b[=1G"
process p7 -write "\x1b[=1S"
process p7 -writehex {0c 08 40 0d 0a 08}

process p7 -expect-text 1 1 "@"
process p7 -expect-cursor 1 80
process p7 -writehex "0c 41 0e 42 0f"
process p7 -expect-text 1 1 "A"
process p7 -expect-text 0 0 "B"
process p7 -write "\x1b[=0T"

process p7 -writehex "0c 0a 0d 43 0a 08 08 0e 44 0f"

process p7 -expect-text 3 1 "C"
process p7 -expect-text 4 1 "D"
process p7 -write "\x1b[2T"
process p7 -expect-text 5 1 "C"
process p7 -expect-text 6 1 "D"
process p7 -write "\x1b[3S"
process p7 -expect-text 3 1 "D"

process p7 -write "\x1b[4;200H%"
process p7 -expect-text 4 80 "%"

process p7 -write "\x1b[7;7H\x09X\x09Y\x09Z\x1b[2ZW\x1b[2Ew\x1b[F*"

process p7 -expect-text 7 17 "W"
process p7 -expect-text 9 1 "w"
process p7 -expect-text 8 1 "*"

process p7 -write "\x1b[10;4HABCDEFGHIJKLMN\x1b(A#$%\x1b)A"
process p7 -write "\x1b[8G\x1b[2X>"
process p7 -expect-text 10 8 ">"
process p7 -screen-dump

# Test responses
process p7 -write "\x1b[3;1HA\x1b[5n"
process p7 -write "\x1b[4;1HB\x1b[6n"
process p7 -write "\x1b[5;1HC\x1b[15n"
process p7 -write "\x1b[6;1HD\x1b[25n"
process p7 -write "\x1b[7;1HE\x1b[26n"
process p7 -write "\x1b[8;1HF\x1b[?26n"
process p7 -write "\x1b[9;1HG\x1bPfutfutfut\x01"
process p7 -write "\x1b[10;1HH\x1b]futfutfut\x01"
process p7 -write "\x1b[11;1HI\x1b[>c"
process p7 -write "\x1b[24;1HW"
process p7 -expect-text 24 1 "W"
process p7 -screen-dump
