From 2e39ef952b44b8bc108f9e12de7a3ab165d21315 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 27 Jul 2024 20:45:17 +0200 Subject: [PATCH] Add testing infrastructure --- .forgejo/workflows/build.yaml | 2 +- .gitignore | 1 + .tarpaulin.toml | 16 ++++++++ flake.nix | 74 ++++++++++++++++++++++++++++------- 4 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 .tarpaulin.toml diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml index e8a7d12..a086796 100644 --- a/.forgejo/workflows/build.yaml +++ b/.forgejo/workflows/build.yaml @@ -20,7 +20,7 @@ jobs: git config --unset "http.${GITHUB_SERVER_URL}/.extraHeader" git lfs install --local git lfs pull - - name: Build + - name: Build and test run: nix build -L .#li7y .#li7y-oci - name: Push OCI image if: github.ref == 'refs/heads/master' diff --git a/.gitignore b/.gitignore index 2ac9df7..f885903 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-or-later /target +/.tarpaulin_target result* .pre-commit-config.yaml diff --git a/.tarpaulin.toml b/.tarpaulin.toml new file mode 100644 index 0000000..9d116e5 --- /dev/null +++ b/.tarpaulin.toml @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: 2024 Simon Bruder +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +[default] +# Tarpaulin uses custom options +# that are incompatible with the default options. +# This sets a different target directory, +# so the files from tarpaulin do not interfere with the regular outputs. +target-dir = ".tarpaulin_target" +# Do not recompile everything on every run +skip-clean = true + +[report] +out = ["Html"] +output-dir = "target/tarpaulin" diff --git a/flake.nix b/flake.nix index b7a2d7f..60b6756 100644 --- a/flake.nix +++ b/flake.nix @@ -74,31 +74,77 @@ }; }; - packages = rec { - li7y = naersk'.buildPackage { - src = self; - }; - default = li7y; + packages = + let + # naersk does not easily allow overrideAttrs + commonNaerskConfigurarion = { + src = self; - li7y-oci = pkgs.dockerTools.buildLayeredImage { - name = "li7y"; - tag = "latest"; + checkInputs = with pkgs; [ + postgresql + postgresqlTestHook + ]; - contents = [ - li7y - ]; + # otherwise the deps derivation is also tested (which just wastes time as there are no tests) + overrideMain = (o: o // { + doCheck = true; + }); - config = { - Cmd = [ "${li7y}/bin/li7y" ]; + # tests need to be able to create and drop databases + postgresqlTestUserOptions = "LOGIN SUPERUSER"; + + postgresqlTestSetupPost = '' + export DATABASE_URL="postgres://''${PGUSER}/''${PGDATABASE}?port=5432&host=''${PGHOST}" + ''; + + # Otherwise SQLx tries to infer the databse schema from an empty database + # (as it can only run the migrations once the test binary is built). + # Also, this enforces that the full query cache is included in the repository. + SQLX_OFFLINE = true; + }; + in + rec { + li7y = naersk'.buildPackage commonNaerskConfigurarion; + default = li7y; + + li7y-tarpaulin = naersk'.buildPackage (commonNaerskConfigurarion // { + name = "li7y-tarpaulin"; + + checkInputs = commonNaerskConfigurarion.checkInputs ++ (with pkgs; [ + cargo-tarpaulin + ]); + + dontBuild = true; + singleStep = true; # tarpaulin uses different options anyway + + cargoTestCommands = _: [ "cargo tarpaulin" ]; + + postInstall = '' + rm -r $out + cp -r target/tarpaulin $out + ''; + }); + + li7y-oci = pkgs.dockerTools.buildLayeredImage { + name = "li7y"; + tag = "latest"; + + contents = [ + li7y + ]; + + config = { + Cmd = [ "${li7y}/bin/li7y" ]; + }; }; }; - }; devShells.default = pkgs.mkShell { buildInputs = [ rustPackageDev ] ++ (with pkgs; [ cargo-deny + cargo-tarpaulin cargo-watch clippy graphviz