WPT.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/usr/bin/env bash
  2. set -e
  3. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  4. # shellcheck source=/dev/null
  5. . "${DIR}/shell_include.sh"
  6. ensure_ladybird_source_dir
  7. WPT_SOURCE_DIR=${WPT_SOURCE_DIR:-"${LADYBIRD_SOURCE_DIR}/Tests/LibWeb/WPT/wpt"}
  8. WPT_REPOSITORY_URL=${WPT_REPOSITORY_URL:-"https://github.com/web-platform-tests/wpt.git"}
  9. BUILD_PRESET=${BUILD_PRESET:-default}
  10. BUILD_DIR=$(get_build_dir "$BUILD_PRESET")
  11. default_binary_path() {
  12. if [ "$(uname -s)" = "Darwin" ]; then
  13. echo "${BUILD_DIR}/bin/Ladybird.app/Contents/MacOS"
  14. else
  15. echo "${BUILD_DIR}/bin"
  16. fi
  17. }
  18. ladybird_git_hash() {
  19. pushd "${LADYBIRD_SOURCE_DIR}" > /dev/null
  20. git rev-parse --short HEAD
  21. popd > /dev/null
  22. }
  23. LADYBIRD_BINARY=${LADYBIRD_BINARY:-"$(default_binary_path)/Ladybird"}
  24. WEBDRIVER_BINARY=${WEBDRIVER_BINARY:-"$(default_binary_path)/WebDriver"}
  25. HEADLESS_BROWSER_BINARY=${HEADLESS_BROWSER_BINARY:-"$(default_binary_path)/headless-browser"}
  26. WPT_PROCESSES=${WPT_PROCESSES:-$(get_number_of_processing_units)}
  27. WPT_CERTIFICATES=(
  28. "tools/certs/cacert.pem"
  29. "${BUILD_DIR}/Lagom/cacert.pem"
  30. )
  31. WPT_ARGS=( "--webdriver-binary=${WEBDRIVER_BINARY}"
  32. "--install-webdriver"
  33. "--processes=${WPT_PROCESSES}"
  34. "--webdriver-arg=--force-cpu-painting"
  35. "--no-pause-after-test"
  36. "-f"
  37. "${EXTRA_WPT_ARGS[@]}"
  38. )
  39. ARG0=$0
  40. print_help() {
  41. NAME=$(basename "$ARG0")
  42. cat <<EOF
  43. Usage: $NAME COMMAND [OPTIONS..] [TESTS...]
  44. Supported COMMANDs:
  45. update: Update the Web Platform Tests repository.
  46. run: $NAME run [OPTIONS...] [TESTS...]
  47. Run the Web Platform Tests.
  48. compare: $NAME compare [OPTIONS...] LOG_FILE [TESTS...]
  49. Run the Web Platform Tests comparing the results to the expectations in LOG_FILE.
  50. import: $NAME import [PATHS...]
  51. Fetch the given test file(s) from https://wpt.live/ and create an in-tree test and expectation files.
  52. list-tests: $NAME list-tests [PATHS..]
  53. List the tests in the given PATHS.
  54. Examples:
  55. $NAME update
  56. Updates the Web Platform Tests repository.
  57. $NAME run
  58. Run all of the Web Platform Tests.
  59. $NAME run --log expectations.log css dom
  60. Run the Web Platform Tests in the 'css' and 'dom' directories and save the output to expectations.log.
  61. $NAME run --log-wptreport expectations.json --log-wptscreenshot expectations.db css dom
  62. Run the Web Platform Tests in the 'css' and 'dom' directories; save the output in wptreport format to expectations.json and save screenshots to expectations.db.
  63. $NAME run --debug-process WebContent http://wpt.live/dom/historical.html
  64. Run the 'dom/historical.html' test, attaching the debugger to the WebContent process when the browser is launched.
  65. $NAME compare expectations.log
  66. Run all of the Web Platform Tests comparing the results to the expectations in before.log.
  67. $NAME compare --log results.log expectations.log css/CSS2
  68. Run the Web Platform Tests in the 'css/CSS2' directory, comparing the results to the expectations in expectations.log; output the results to results.log.
  69. $NAME import html/dom/aria-attribute-reflection.html
  70. Import the test from https://wpt.live/html/dom/aria-attribute-reflection.html into the Ladybird test suite.
  71. $NAME list-tests css/CSS2 dom
  72. Show a list of all tests in the 'css/CSS2' and 'dom' directories.
  73. EOF
  74. }
  75. usage() {
  76. >&2 print_help
  77. exit 1
  78. }
  79. CMD=$1
  80. [ -n "$CMD" ] || usage
  81. shift
  82. if [ "$CMD" = "--help" ] || [ "$CMD" = "help" ]; then
  83. print_help
  84. exit 0
  85. fi
  86. set_logging_flags()
  87. {
  88. [ -n "${1}" ] || usage;
  89. [ -n "${2}" ] || usage;
  90. log_type="${1}"
  91. log_name="$(absolutize_path "${2}")"
  92. WPT_ARGS+=( "${log_type}=${log_name}" )
  93. }
  94. headless=1
  95. ARG=$1
  96. while [[ "$ARG" =~ ^(--show-window|--debug-process|(--log(-(raw|unittest|xunit|html|mach|tbpl|grouped|chromium|wptreport|wptscreenshot))?))$ ]]; do
  97. case "$ARG" in
  98. --show-window)
  99. headless=0
  100. ;;
  101. --debug-process)
  102. process_name="${2}"
  103. shift
  104. WPT_ARGS+=( "--webdriver-arg=--debug-process=${process_name}" )
  105. ;;
  106. --log)
  107. set_logging_flags "--log-raw" "${2}"
  108. shift
  109. ;;
  110. *)
  111. set_logging_flags "${ARG}" "${2}"
  112. shift
  113. ;;
  114. esac
  115. shift
  116. ARG=$1
  117. done
  118. if [ $headless -eq 1 ]; then
  119. WPT_ARGS+=( "--binary=${HEADLESS_BROWSER_BINARY}" )
  120. WPT_ARGS+=( "--webdriver-arg=--headless" )
  121. else
  122. WPT_ARGS+=( "--binary=${LADYBIRD_BINARY}" )
  123. fi
  124. exit_if_running_as_root "Do not run WPT.sh as root"
  125. construct_test_list() {
  126. TEST_LIST=( "$@" )
  127. for i in "${!TEST_LIST[@]}"; do
  128. item="${TEST_LIST[i]}"
  129. item="${item#"$WPT_SOURCE_DIR"/}"
  130. item="${item#*Tests/LibWeb/WPT/wpt/}"
  131. item="${item#http://wpt.live/}"
  132. item="${item#https://wpt.live/}"
  133. TEST_LIST[i]="$item"
  134. done
  135. }
  136. ensure_wpt_repository() {
  137. mkdir -p "${WPT_SOURCE_DIR}"
  138. pushd "${WPT_SOURCE_DIR}" > /dev/null
  139. if [ ! -d .git ]; then
  140. git clone --depth 1 "${WPT_REPOSITORY_URL}" "${WPT_SOURCE_DIR}"
  141. fi
  142. # Update hosts file if needed
  143. if [ "$(comm -13 <(sort -u /etc/hosts) <(./wpt make-hosts-file | sort -u) | wc -l)" -gt 0 ]; then
  144. echo "Enter superuser password to append wpt hosts to /etc/hosts"
  145. ./wpt make-hosts-file | sudo tee -a /etc/hosts
  146. fi
  147. popd > /dev/null
  148. }
  149. build_ladybird_and_webdriver() {
  150. "${DIR}"/ladybird.sh build WebDriver
  151. }
  152. update_wpt() {
  153. ensure_wpt_repository
  154. pushd "${WPT_SOURCE_DIR}" > /dev/null
  155. git pull
  156. popd > /dev/null
  157. }
  158. execute_wpt() {
  159. # Ensure open files limit is at least 1024, so the WPT runner does not run out of descriptors
  160. if [ "$(ulimit -n)" -lt 1024 ]; then
  161. ulimit -S -n 1024
  162. fi
  163. pushd "${WPT_SOURCE_DIR}" > /dev/null
  164. for certificate_path in "${WPT_CERTIFICATES[@]}"; do
  165. if [ ! -f "${certificate_path}" ]; then
  166. echo "Certificate not found: \"${certificate_path}\""
  167. exit 1
  168. fi
  169. WPT_ARGS+=( "--webdriver-arg=--certificate=${certificate_path}" )
  170. done
  171. construct_test_list "${@}"
  172. echo LADYBIRD_GIT_VERSION="$(ladybird_git_hash)" ./wpt run "${WPT_ARGS[@]}" ladybird "${TEST_LIST[@]}"
  173. LADYBIRD_GIT_VERSION="$(ladybird_git_hash)" ./wpt run "${WPT_ARGS[@]}" ladybird "${TEST_LIST[@]}"
  174. popd > /dev/null
  175. }
  176. run_wpt() {
  177. ensure_wpt_repository
  178. build_ladybird_and_webdriver
  179. execute_wpt "${@}"
  180. }
  181. serve_wpt()
  182. {
  183. ensure_wpt_repository
  184. pushd "${WPT_SOURCE_DIR}" > /dev/null
  185. ./wpt serve
  186. popd > /dev/null
  187. }
  188. list_tests_wpt()
  189. {
  190. ensure_wpt_repository
  191. construct_test_list "${@}"
  192. pushd "${WPT_SOURCE_DIR}" > /dev/null
  193. ./wpt run --list-tests ladybird "${TEST_LIST[@]}"
  194. popd > /dev/null
  195. }
  196. import_wpt()
  197. {
  198. for i in "${!INPUT_PATHS[@]}"; do
  199. item="${INPUT_PATHS[i]}"
  200. item="${item#http://wpt.live/}"
  201. item="${item#https://wpt.live/}"
  202. INPUT_PATHS[i]="$item"
  203. done
  204. RAW_TESTS=()
  205. while IFS= read -r test_file; do
  206. RAW_TESTS+=("${test_file%%\?*}")
  207. done < <(
  208. "${ARG0}" list-tests "${INPUT_PATHS[@]}"
  209. )
  210. if [ "${#RAW_TESTS[@]}" -eq 0 ]; then
  211. echo "No tests found for the given paths"
  212. exit 1
  213. fi
  214. TESTS=()
  215. while IFS= read -r test_file; do
  216. TESTS+=("$test_file")
  217. done < <(printf "%s\n" "${RAW_TESTS[@]}" | sort -u)
  218. pushd "${LADYBIRD_SOURCE_DIR}" > /dev/null
  219. ./Meta/ladybird.sh build headless-browser
  220. set +e
  221. for path in "${TESTS[@]}"; do
  222. echo "Importing test from ${path}"
  223. if ! ./Meta/import-wpt-test.py https://wpt.live/"${path}"; then
  224. continue
  225. fi
  226. "${HEADLESS_BROWSER_BINARY}" --run-tests ./Tests/LibWeb --rebaseline -f "$path"
  227. done
  228. set -e
  229. popd > /dev/null
  230. }
  231. compare_wpt() {
  232. ensure_wpt_repository
  233. METADATA_DIR=$(mktemp -d)
  234. pushd "${WPT_SOURCE_DIR}" > /dev/null
  235. ./wpt update-expectations --product ladybird --full --metadata="${METADATA_DIR}" "${INPUT_LOG_NAME}"
  236. popd > /dev/null
  237. WPT_ARGS+=( "--metadata=${METADATA_DIR}" )
  238. build_ladybird_and_webdriver
  239. execute_wpt "${@}"
  240. rm -rf "${METADATA_DIR}"
  241. }
  242. if [[ "$CMD" =~ ^(update|run|serve|compare|import|list-tests)$ ]]; then
  243. case "$CMD" in
  244. update)
  245. update_wpt
  246. ;;
  247. run)
  248. run_wpt "${@}"
  249. ;;
  250. serve)
  251. serve_wpt
  252. ;;
  253. import)
  254. if [ $# -eq 0 ]; then
  255. usage
  256. fi
  257. INPUT_PATHS=( "$@" )
  258. import_wpt
  259. ;;
  260. compare)
  261. INPUT_LOG_NAME="$(pwd -P)/$1"
  262. if [ ! -f "$INPUT_LOG_NAME" ]; then
  263. echo "Log file not found: \"${INPUT_LOG_NAME}\""
  264. usage;
  265. fi
  266. shift
  267. compare_wpt "${@}"
  268. ;;
  269. list-tests)
  270. list_tests_wpt "${@}"
  271. ;;
  272. esac
  273. else
  274. >&2 echo "Unknown command: $CMD"
  275. usage
  276. fi