From b04ade67aae22e3b5cf1efdf086db29b14f0607b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 17 Jul 2022 16:32:51 -0700 Subject: [PATCH] Build vttest, improve runner --- test/Dockerfile | 26 +++++++++-- test/cases/vttest/1_1.sh | 6 +++ test/colors.sh | 96 ---------------------------------------- test/run.sh | 81 ++++++++++++++++++++++++++++----- 4 files changed, 99 insertions(+), 110 deletions(-) create mode 100755 test/cases/vttest/1_1.sh delete mode 100755 test/colors.sh diff --git a/test/Dockerfile b/test/Dockerfile index be8cbc321..19b7c7153 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -1,3 +1,20 @@ +#-------------------------------------------------------------------- +# vttest +#-------------------------------------------------------------------- +FROM alpine:3.16 AS vttest + +RUN apk add --no-cache build-base curl + +RUN curl -o vttest.tar.gz https://invisible-island.net/archives/vttest/vttest-20220215.tgz && \ + tar xvzf vttest.tar.gz && \ + cd vttest-20220215 && \ + ./configure && \ + make && \ + cp ./vttest / + +#-------------------------------------------------------------------- +# main runner +#-------------------------------------------------------------------- FROM alpine:3.16 # Base packages. A good set of this is just to get X (xvfb) and OpenGL @@ -6,6 +23,8 @@ RUN apk add --no-cache \ bash \ grep \ procps \ + font-inconsolata-nerd \ + i3wm \ imagemagick \ libxrandr \ mesa-dev \ @@ -19,7 +38,8 @@ RUN apk add --no-cache \ RUN apk add --no-cache \ alacritty -COPY ./run.sh /run.sh -COPY ./colors.sh /colors.sh +COPY --from=vttest /vttest /usr/bin/vttest -ENTRYPOINT ["/bin/bash"] +COPY ./run.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/test/cases/vttest/1_1.sh b/test/cases/vttest/1_1.sh new file mode 100755 index 000000000..9ddb4d430 --- /dev/null +++ b/test/cases/vttest/1_1.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +function test_do { + xdotool type "vttest" + xdotool key Return +} diff --git a/test/colors.sh b/test/colors.sh deleted file mode 100755 index 76195500d..000000000 --- a/test/colors.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env bash - -# Tom Hale, 2016. MIT Licence. -# Print out 256 colours, with each number printed in its corresponding colour -# See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163 - -set -eu # Fail on errors or undeclared variables - -printable_colours=256 - -# Return a colour that contrasts with the given colour -# Bash only does integer division, so keep it integral -function contrast_colour { - local r g b luminance - colour="$1" - - if (( colour < 16 )); then # Initial 16 ANSI colours - (( colour == 0 )) && printf "15" || printf "0" - return - fi - - # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8 - if (( colour > 231 )); then # Greyscale ramp - (( colour < 244 )) && printf "15" || printf "0" - return - fi - - # All other colours: - # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5] - # See http://stackoverflow.com/a/27165165/5353461 - - # r=$(( (colour-16) / 36 )) - g=$(( ((colour-16) % 36) / 6 )) - # b=$(( (colour-16) % 6 )) - - # If luminance is bright, print number in black, white otherwise. - # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601 - (( g > 2)) && printf "0" || printf "15" - return - - # Uncomment the below for more precise luminance calculations - - # # Calculate percieved brightness - # # See https://www.w3.org/TR/AERT#color-contrast - # # and http://www.itu.int/rec/R-REC-BT.601 - # # Luminance is in range 0..5000 as each value is 0..5 - # luminance=$(( (r * 299) + (g * 587) + (b * 114) )) - # (( $luminance > 2500 )) && printf "0" || printf "15" -} - -# Print a coloured block with the number of that colour -function print_colour { - local colour="$1" contrast - contrast=$(contrast_colour "$1") - printf "\e[48;5;%sm" "$colour" # Start block of colour - printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number - printf "\e[0m " # Reset colour -} - -# Starting at $1, print a run of $2 colours -function print_run { - local i - for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do - print_colour "$i" - done - printf " " -} - -# Print blocks of colours -function print_blocks { - local start="$1" i - local end="$2" # inclusive - local block_cols="$3" - local block_rows="$4" - local blocks_per_line="$5" - local block_length=$((block_cols * block_rows)) - - # Print sets of blocks - for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do - printf "\n" # Space before each set of blocks - # For each block row - for (( row = 0; row < block_rows; row++ )) do - # Print block columns for all blocks on the line - for (( block = 0; block < blocks_per_line; block++ )) do - print_run $(( i + (block * block_length) )) "$block_cols" - done - (( i += block_cols )) # Prepare to print the next row - printf "\n" - done - done -} - -print_run 0 16 # The first 16 colours are spread over the whole spectrum -printf "\n" -print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive -print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey diff --git a/test/run.sh b/test/run.sh index 5ac450f75..95620f19b 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,7 +1,52 @@ #!/usr/bin/env bash +# +# This script runs a given test case and captures a screenshot. This script +# expects to run in the Docker image so it just captures the full screen rather +# than a specific window. +# +# This outputs the captured image to the `--output` value. This will not +# compare the captured output. This is only used to capture the output of +# a test case. -# The child program to execute -CHILD="alacritty" +#-------------------------------------------------------------------- +# Helpers + +function has_func() { + declare -f -F $1 > /dev/null + return $? +} + +#-------------------------------------------------------------------- +# Flag parsing + +while [[ "$#" -gt 0 ]]; do + case $1 in + -e|--exec) ARG_EXEC="$2"; shift ;; + -c|--case) ARG_CASE="$2"; shift ;; + -o|--output) ARG_OUT="$2"; shift ;; + *) echo "Unknown parameter passed: $1"; exit 1 ;; + esac + shift +done + +bad=0 +if [ -z "$ARG_EXEC" ]; then bad=1; fi +if [ -z "$ARG_CASE" ]; then bad=1; fi +if [ -z "$ARG_OUT" ]; then bad=1; fi +if [ $bad -ne 0 ]; then + echo "Usage: run.sh --exec --case --output " + exit 1 +fi + +# Load our test case +source ${ARG_CASE} +if ! has_func "test_do"; then + echo "Test case is invalid." + exit 1 +fi + +echo "Term: ${ARG_EXEC}" +echo "Case: ${ARG_CASE}" #-------------------------------------------------------------------- # Some terminals require XDG be properly setup. We create a new @@ -11,20 +56,34 @@ export XDG_RUNTIME_DIR="${XDG_BASE_DIR}/runtime" mkdir -p ${XDG_BASE_DIR} ${XDG_RUNTIME_DIR} chmod 0700 $XDG_RUNTIME_DIR +# Configure i3 +cat <${XDG_BASE_DIR}/i3.cfg +exec ${ARG_EXEC} +EOF + #-------------------------------------------------------------------- -# Start up the program under test -CHILD_LOG="${XDG_BASE_DIR}/child.log" -${CHILD} -o "window.start_maximized=true" >${CHILD_LOG} 2>&1 & -CHILD_PID=$! -echo "Child pid: ${CHILD_PID}" -echo "Child log: ${CHILD_LOG}" +# Start up the program under test by launching i3. We use i3 so we can +# more carefully control the window settings, test resizing, etc. +WM_LOG="${XDG_BASE_DIR}/wm.log" +i3 -c ${XDG_BASE_DIR}/i3.cfg >${WM_LOG} 2>&1 & +echo +echo "Started window manager..." + +# Wait for startup +# TODO: we can probably use xdotool or wmctrl or something to detect if any +# windows actually launched and make error handling here better. sleep 2 -xdotool type "/colors.sh" -xdotool key Return +# Run our test case (should be defined in test case file) +echo "Executing test case..." +test_do +# Sleep a second to let it render sleep 1 -import -window root /src/screen.jpeg +echo "Capturing screen shot..." +import -window root ${ARG_OUT} + +echo "Done"