|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// Smoke-test and run the testthat suite for the arrow R package under webR. |
| 19 | +// |
| 20 | +// This script is called by r_wasm_test.sh after it sets up the CRAN-like |
| 21 | +// repo and installs the webr npm package. |
| 22 | +// |
| 23 | +// Environment variables: |
| 24 | +// ARROW_WASM_REPO_DIR - path to the local CRAN-like repo containing |
| 25 | +// the arrow wasm binary package |
| 26 | + |
| 27 | +const { WebR } = require("webr"); |
| 28 | +const http = require("http"); |
| 29 | +const fs = require("fs"); |
| 30 | +const path = require("path"); |
| 31 | + |
| 32 | +const repoDir = process.env.ARROW_WASM_REPO_DIR; |
| 33 | +if (!repoDir) { |
| 34 | + console.error("ERROR: ARROW_WASM_REPO_DIR not set"); |
| 35 | + process.exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +async function main() { |
| 39 | + // Serve the local repo over HTTP so webR (Emscripten) can access it. |
| 40 | + // webR's R runs in an Emscripten sandbox and cannot access the host |
| 41 | + // filesystem directly — it fetches packages over HTTP instead. |
| 42 | + const server = http.createServer((req, res) => { |
| 43 | + const filePath = path.join(repoDir, decodeURIComponent(req.url)); |
| 44 | + fs.readFile(filePath, (err, data) => { |
| 45 | + if (err) { |
| 46 | + res.writeHead(404); |
| 47 | + res.end(); |
| 48 | + } else { |
| 49 | + res.writeHead(200); |
| 50 | + res.end(data); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + server.listen(8080); |
| 55 | + console.log("✓ Repo server on :8080"); |
| 56 | + |
| 57 | + const webR = new WebR({ |
| 58 | + RArgs: ["--quiet"], |
| 59 | + interactive: false, |
| 60 | + }); |
| 61 | + |
| 62 | + await webR.init(); |
| 63 | + console.log("✓ webR initialized"); |
| 64 | + |
| 65 | + // Install the arrow Wasm package, put localhost:8080 before repo.r-wasm.org |
| 66 | + // (which is used for deps) |
| 67 | + await webR.installPackages(["arrow"], { |
| 68 | + repos: ["http://localhost:8080", "https://repo.r-wasm.org"], |
| 69 | + quiet: false, |
| 70 | + mount: false, |
| 71 | + }); |
| 72 | + console.log("✓ arrow installed"); |
| 73 | + |
| 74 | + // Install test deps. TOOD: This is flakey. We could parse the DESCRIPTION |
| 75 | + // file to be more robust. |
| 76 | + await webR.installPackages( |
| 77 | + ["testthat", "tibble", "dplyr", "withr", "pillar"], |
| 78 | + { |
| 79 | + repos: ["https://repo.r-wasm.org"], |
| 80 | + quiet: false, |
| 81 | + mount: false, |
| 82 | + }, |
| 83 | + ); |
| 84 | + console.log("✓ test dependencies installed"); |
| 85 | + |
| 86 | + // Test the package loads and functions basically |
| 87 | + const loadResult = await webR.evalRString(` |
| 88 | + library(arrow) |
| 89 | + cat("arrow loaded\\n") |
| 90 | + cat("R.version$os =", R.version$os, "\\n") |
| 91 | + use_threads <- getOption("arrow.use_threads") |
| 92 | + cat("arrow.use_threads =", use_threads, "\\n") |
| 93 | + stopifnot(identical(use_threads, FALSE)) |
| 94 | + tab <- arrow::as_arrow_table(data.frame(x = 1:10, y = letters[1:10])) |
| 95 | + stopifnot(nrow(tab) == 10L) |
| 96 | + cat("Created Arrow table with", nrow(tab), "rows\\n") |
| 97 | + "PASS" |
| 98 | + `); |
| 99 | + |
| 100 | + if (loadResult !== "PASS") { |
| 101 | + console.error("Package load test FAILED"); |
| 102 | + await webR.close(); |
| 103 | + server.close(); |
| 104 | + process.exit(1); |
| 105 | + } |
| 106 | + console.log("✓ Package loads and works correctly"); |
| 107 | + |
| 108 | + // Run tests |
| 109 | + console.log("Running testthat suite under webR..."); |
| 110 | + |
| 111 | + const testResult = await webR.evalRString(` |
| 112 | + library(testthat) |
| 113 | + library(arrow) |
| 114 | + results <- testthat::test_package("arrow", reporter = "summary", stop_on_failure = FALSE) |
| 115 | + df <- as.data.frame(results) |
| 116 | + n_pass <- sum(df$passed) |
| 117 | + n_skip <- sum(df$skipped) |
| 118 | + n_fail <- sum(df$failed) |
| 119 | + n_error <- sum(df$error) |
| 120 | + cat(sprintf("Results: %d passed, %d skipped, %d failed, %d errors\\n", |
| 121 | + n_pass, n_skip, n_fail, n_error)) |
| 122 | + if (n_fail > 0 || n_error > 0) "FAIL" else "PASS" |
| 123 | + `); |
| 124 | + |
| 125 | + if (testResult !== "PASS") { |
| 126 | + console.error("testthat suite FAILED"); |
| 127 | + await webR.close(); |
| 128 | + server.close(); |
| 129 | + process.exit(1); |
| 130 | + } |
| 131 | + console.log("✓ testthat suite passed"); |
| 132 | + |
| 133 | + console.log("✓ All tests passed!"); |
| 134 | + await webR.close(); |
| 135 | + server.close(); |
| 136 | +} |
| 137 | + |
| 138 | +main().catch((e) => { |
| 139 | + console.error("FAILED:", e); |
| 140 | + process.exit(1); |
| 141 | +}); |
0 commit comments