Skip to content
File

Blob: src/workerd/server/tests/compile-tests/compile-test.sh

bash80 lines
1#!/bin/bash
2 
3set -o errexit
4set -o nounset
5set -o pipefail
6 
7# $1 -> workerd binary path
8# $2 -> path to desired file to compile
9# $3 -> port to curl
10# $4 -> desired output
11 
12# Help Function
13function show_help() {
14 echo "
15The Compile Test script is designed to aid in testing compiled workerd binaries.
16It works by verifying that the result of a curl command on a running compiled binary matches an expected output.
17usage: compile-test.sh [-d] [-h] <workerd command> <file-to-compile> <port-to-curl> <expected-output-file>
18 options:
19 -d print out where tmp files are created and do not delete them
20 -h this help message
21
22 Note: all flags must occur before arguments
23"
24}
25 
26while getopts "h" option; do
27 case ${option} in
28 h)
29 show_help
30 exit
31 ;;
32 esac
33done
34 
35shift $(expr $OPTIND - 1)
36WORKERD_BINARY=$1
37CAPNP_SOURCE=$2
38EXPECTED=$3
39 
40CAPNP_BINARY=$(mktemp)
41PORT_FILE=$(mktemp)
42 
43# Compile the app
44$WORKERD_BINARY compile $CAPNP_SOURCE > $CAPNP_BINARY
45 
46# Run the app
47$CAPNP_BINARY -shttp=localhost:0 --control-fd=1 > $PORT_FILE &
48KILL=$!
49 
50# Make intermediate files before trying to wait on the bindings
51OUTPUT=$(mktemp)
52FIXED_OUTPUT=$(mktemp)
53FIXED_EXPECTED=$(mktemp)
54 
55# Wait on the port bindings to occur
56while ! grep \"socket\"\:\"http\" $PORT_FILE; do
57 sleep .1
58done
59 
60# Identify the port chosen by the binary
61PORT=`grep \"socket\"\:\"http\" $PORT_FILE | sed 's/^.*\"port\"://g' | sed 's/\}//g' |head -n 1`
62 
63# Request output
64curl localhost:$PORT -o $OUTPUT
65 
66# Compare the tests to the expected output
67sed -e's/[[:space:]]*$//' $OUTPUT > $FIXED_OUTPUT
68sed -e's/[[:space:]]*$//' $EXPECTED > $FIXED_EXPECTED
69diff $FIXED_OUTPUT $FIXED_EXPECTED
70 
71# Clean up running workerd
72kill -9 $KILL
73 
74# Clean up temp files
75rm -f $CAPNP_BINARY
76rm -f $PORT_FILE
77rm -f $OUTPUT
78rm -f $FIXED_OUTPUT
79rm -f $FIXED_EXPECTED