From 75055391f24669c45600a91904e44e937004cc34 Mon Sep 17 00:00:00 2001 From: Richard Bradfield Date: Thu, 25 Aug 2022 12:41:42 +0100 Subject: [PATCH] Add extension testing for SQlite Extends x.py to download an appropriate shared object file for supported operating systems, and uses wget to fetch one into the GitHub Actions context for use by CI. Overriding LD_LIBRARY_PATH for only this specific DB minimises the impact on the rest of the suite. --- .github/workflows/sqlx.yml | 6 +++++- tests/sqlite/sqlite.rs | 15 +++++++++++++++ tests/x.py | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sqlx.yml b/.github/workflows/sqlx.yml index a9a805002f..2083eebc1c 100644 --- a/.github/workflows/sqlx.yml +++ b/.github/workflows/sqlx.yml @@ -46,7 +46,7 @@ jobs: - uses: Swatinem/rust-cache@v1 with: key: ${{ runner.os }}-check-${{ matrix.runtime }}-${{ matrix.tls }} - + - uses: actions-rs/cargo@v1 with: command: check @@ -144,6 +144,8 @@ jobs: steps: - uses: actions/checkout@v2 + - run: mkdir /tmp/sqlite3-lib && wget -O /tmp/sqlite3-lib/ipaddr.so https://github.com/nalgeon/sqlean/releases/download/0.15.2/ipaddr.so + - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -164,6 +166,8 @@ jobs: --test-threads=1 env: DATABASE_URL: sqlite://tests/sqlite/sqlite.db + RUSTFLAGS: --cfg sqlite_ipaddr + LD_LIBRARY_PATH: /tmp/sqlite3-lib postgres: name: Postgres diff --git a/tests/sqlite/sqlite.rs b/tests/sqlite/sqlite.rs index cdcff0508c..914d4ff88a 100644 --- a/tests/sqlite/sqlite.rs +++ b/tests/sqlite/sqlite.rs @@ -204,6 +204,21 @@ async fn it_executes_with_pool() -> anyhow::Result<()> { Ok(()) } +#[cfg(sqlite_ipaddr)] +#[sqlx_macros::test] +async fn it_opens_with_extension() -> anyhow::Result<()> { + use std::str::FromStr; + + let opts = SqliteConnectOptions::from_str(&dotenvy::var("DATABASE_URL")?)?.extension("ipaddr"); + + let mut conn = SqliteConnection::connect_with(&opts).await?; + conn.execute("SELECT ipmasklen('192.168.16.12/24');") + .await?; + conn.close().await?; + + Ok(()) +} + #[sqlx_macros::test] async fn it_opens_in_memory() -> anyhow::Result<()> { // If the filename is ":memory:", then a private, temporary in-memory database diff --git a/tests/x.py b/tests/x.py index 6b8785d83f..75791b5392 100755 --- a/tests/x.py +++ b/tests/x.py @@ -5,6 +5,8 @@ import sys import time import argparse +import platform +import urllib.request from glob import glob from docker import start_database @@ -23,6 +25,36 @@ dir_tests = os.path.join(dir_workspace, "tests") +def maybe_fetch_sqlite_extension(): + """ + For supported platforms, if we're testing SQLite and the file isn't + already present, grab a simple extension for testing. + + Returns the extension name if it was downloaded successfully or `None` if not. + """ + BASE_URL = "https://github.com/nalgeon/sqlean/releases/download/0.15.2/" + if platform.system() == "Darwin": + if platform.machine() == "arm64": + download_url = BASE_URL + "/ipaddr.arm64.dylib" + filename = "ipaddr.dylib" + else: + download_url = BASE_URL + "/ipaddr.dylib" + filename = "ipaddr.dylib" + elif platform.system() == "Linux": + download_url = BASE_URL + "/ipaddr.so" + filename = "ipaddr.so" + else: + # Unsupported OS + return None + + if not os.path.exists(filename): + content = urllib.request.urlopen(download_url).read() + with open(filename, "wb") as fd: + fd.write(content) + + return filename.split(".")[0] + + def run(command, comment=None, env=None, service=None, tag=None, args=None, database_url_args=None): if argv.list_targets: if tag: @@ -41,6 +73,13 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None, data environ = env or {} + if service == "sqlite": + if maybe_fetch_sqlite_extension() is not None: + if environ.get("RUSTFLAGS"): + environ["RUSTFLAGS"] += " --cfg sqlite_ipaddr" + else: + environ["RUSTFLAGS"] = "--cfg sqlite_ipaddr" + if service is not None: database_url = start_database(service, database="sqlite/sqlite.db" if service == "sqlite" else "sqlx", cwd=dir_tests)