From 32bac81d9142427b9827d5cd7a35cd5dafa426de Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 17 Apr 2021 12:44:22 +0200 Subject: [PATCH] nix(refactor): Move withPostgresqlVersions and withAllVersions to new withTools --- .circleci/config.yml | 2 +- default.nix | 6 ++-- nix/tests.nix | 76 +++++------------------------------------ nix/withtmpdb.nix | 21 ------------ nix/withtools.nix | 80 ++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 7 ++-- 6 files changed, 97 insertions(+), 95 deletions(-) delete mode 100644 nix/withtmpdb.nix create mode 100644 nix/withtools.nix diff --git a/.circleci/config.yml b/.circleci/config.yml index b13af72af..8f01c2940 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -172,7 +172,7 @@ jobs: cachix use postgrest - run: name: Install testing scripts - command: nix-env -f default.nix -iA tests tests.memoryTests + command: nix-env -f default.nix -iA tests tests.memoryTests withTools - run: name: Run coverage (io tests and spec tests against PostgreSQL 13) command: postgrest-coverage diff --git a/default.nix b/default.nix index 58be30fdc..51644d900 100644 --- a/default.nix +++ b/default.nix @@ -112,13 +112,13 @@ rec { nixpkgsUpgrade = pkgs.callPackage nix/nixpkgs-upgrade.nix { }; - withTmpDb = - pkgs.callPackage nix/withtmpdb.nix { }; + withTools = + pkgs.callPackage nix/withtools.nix { inherit postgresqlVersions; }; # Scripts for running tests. tests = pkgs.callPackage nix/tests.nix { - inherit postgrest postgrestProfiled postgresqlVersions devCabalOptions withTmpDb; + inherit postgrest postgrestProfiled devCabalOptions withTools; ghc = pkgs.haskell.compiler."${compiler}"; hpc-codecov = pkgs.haskell.packages."${compiler}".hpc-codecov; }; diff --git a/nix/tests.nix b/nix/tests.nix index a3aa32690..caf44b01b 100644 --- a/nix/tests.nix +++ b/nix/tests.nix @@ -10,71 +10,14 @@ , gnugrep , haskell , hpc-codecov -, lib -, postgresql -, postgresqlVersions , postgrest , postgrestProfiled , python3 , runtimeShell -, withTmpDb +, withTools , yq }: let - # Create a `withPostgresql` for each PostgreSQL version that we want to test - # against. - withPostgresqlVersions = - builtins.map - ({ name, postgresql }: - (checkedShellScript - { - name = "postgrest-with-${name}"; - docs = "Run the given command in a temporary database with ${name}"; - inRootDir = false; - } - '' - ${withTmpDb postgresql} "$@" - '' - ).bin - ) - postgresqlVersions; - - # Helper script for running a command against all PostgreSQL versions. - withAllVersions = - let - runners = - builtins.map - ({ name, postgresql }: - '' - cat << EOF - - Running against ${name}... - - EOF - - trap 'echo "Failed on ${name}"' exit - - ${withTmpDb postgresql} "$@" - - trap "" exit - - cat << EOF - - Done running against ${name}. - - EOF - '') - postgresqlVersions; - in - checkedShellScript - { - name = "postgrest-with-all"; - docs = "Run command against all supported PostgreSQL versions."; - inRootDir = true; - } - (lib.concatStringsSep "\n\n" runners); - - # Script to run the Haskell test suite testSpec = checkedShellScript { @@ -86,7 +29,7 @@ let env="$(cat ${postgrest.env})" export PATH="$env/bin:$PATH" - ${withTmpDb postgresql} ${cabal-install}/bin/cabal v2-test ${devCabalOptions} + ${withTools.latest} ${cabal-install}/bin/cabal v2-test ${devCabalOptions} ''; testSpecIdempotence = @@ -100,7 +43,7 @@ let env="$(cat ${postgrest.env})" export PATH="$env/bin:$PATH" - ${withTmpDb postgresql} ${runtimeShell} -c " \ + ${withTools.latest} ${runtimeShell} -c " \ ${cabal-install}/bin/cabal v2-test ${devCabalOptions} && \ ${cabal-install}/bin/cabal v2-test ${devCabalOptions}" ''; @@ -127,7 +70,7 @@ let export PATH="$env/bin:$PATH" ${cabal-install}/bin/cabal v2-build ${devCabalOptions} - ${cabal-install}/bin/cabal v2-exec ${withTmpDb postgresql} \ + ${cabal-install}/bin/cabal v2-exec ${withTools.latest} \ ${ioTestPython}/bin/pytest -- -v test/io-tests "$@" ''; @@ -141,7 +84,7 @@ let '' export PATH="${postgrestProfiled}/bin:${curl}/bin:$PATH" - ${withTmpDb postgresql} test/memory-tests.sh + ${withTools.latest} test/memory-tests.sh ''; dumpSchema = @@ -155,7 +98,7 @@ let env="$(cat ${postgrest.env})" export PATH="$env/bin:$PATH" - ${withTmpDb postgresql} \ + ${withTools.latest} \ ${cabal-install}/bin/cabal v2-run ${devCabalOptions} --verbose=0 -- \ postgrest --dump-schema \ | ${yq}/bin/yq -y . @@ -184,11 +127,11 @@ let # collect all tests HPCTIXFILE="$tmpdir"/io.tix \ - ${withTmpDb postgresql} ${cabal-install}/bin/cabal v2-exec ${devCabalOptions} \ + ${withTools.latest} ${cabal-install}/bin/cabal v2-exec ${devCabalOptions} \ ${ioTestPython}/bin/pytest -- -v test/io-tests HPCTIXFILE="$tmpdir"/spec.tix \ - ${withTmpDb postgresql} ${cabal-install}/bin/cabal v2-test ${devCabalOptions} + ${withTools.latest} ${cabal-install}/bin/cabal v2-test ${devCabalOptions} # collect all the tix files ${ghc}/bin/hpc sum --union --exclude=Paths_postgrest --output="$tmpdir"/tests.tix "$tmpdir"/io*.tix "$tmpdir"/spec.tix @@ -251,8 +194,7 @@ buildEnv dumpSchema.bin coverage.bin coverageDraftOverlay.bin - withAllVersions.bin - ] ++ withPostgresqlVersions; + ]; } # The memory tests have large dependencies (a profiled build of PostgREST) # and are run less often than the spec tests, so we don't include them in diff --git a/nix/withtmpdb.nix b/nix/withtmpdb.nix deleted file mode 100644 index 8964bb935..000000000 --- a/nix/withtmpdb.nix +++ /dev/null @@ -1,21 +0,0 @@ -# Wrap the `test/with_tmp_db` script with the required dependencies from Nix. -{ checkedShellScript }: - -postgresql: -checkedShellScript -{ - name = "postgrest-test-withtmpdb-${postgresql.name}"; - docs = "Run the given command in a temporary database"; - inRootDir = true; - redirectTixFiles = false; -} - '' - # avoid starting multiple layers of with_tmp_db - if test ! -v PGRST_DB_URI; then - export PATH=${postgresql}/bin:"$PATH" - - exec test/with_tmp_db "$@" - else - "$@" - fi - '' diff --git a/nix/withtools.nix b/nix/withtools.nix new file mode 100644 index 000000000..0e7288567 --- /dev/null +++ b/nix/withtools.nix @@ -0,0 +1,80 @@ +{ buildEnv +, checkedShellScript +, lib +, postgresqlVersions +}: +let + # Wrap the `test/with_tmp_db` script with the required dependencies from Nix. + withTmpDb = + { name, postgresql }: + checkedShellScript + { + name = "postgrest-with-${name}"; + docs = "Run the given command in a temporary database with ${name}"; + inRootDir = true; + redirectTixFiles = false; + } + '' + # avoid starting multiple layers of with_tmp_db + if test ! -v PGRST_DB_URI; then + export PATH=${postgresql}/bin:"$PATH" + + exec test/with_tmp_db "$@" + else + "$@" + fi + ''; + + # Helper script for running a command against all PostgreSQL versions. + withAll = + let + runners = + builtins.map + (pg: + '' + cat << EOF + + Running against ${pg.name}... + + EOF + + trap 'echo "Failed on ${pg.name}"' exit + + ${withTmpDb pg} "$@" + + trap "" exit + + cat << EOF + + Done running against ${pg.name}. + + EOF + '') + postgresqlVersions; + in + checkedShellScript + { + name = "postgrest-with-all"; + docs = "Run command against all supported PostgreSQL versions."; + inRootDir = true; + } + (lib.concatStringsSep "\n\n" runners); + + # Create a `postgrest-with-postgresql-` for each PostgreSQL version + withVersions = builtins.map withTmpDb postgresqlVersions; + +in +buildEnv + { + name = + "postgrest-with"; + + paths = + [ + withAll.bin + ] ++ (builtins.map (v: v.bin) withVersions); + } + # make withTools.latest available for other nix files + // { + latest = withTmpDb (builtins.head postgresqlVersions); +} diff --git a/shell.nix b/shell.nix index 8af0a6e95..9ea7ccc2e 100644 --- a/shell.nix +++ b/shell.nix @@ -27,11 +27,12 @@ lib.overrideDerivation postgrest.env ( pkgs.cabal-install pkgs.cabal2nix pkgs.postgresql - postgrest.nixpkgsUpgrade postgrest.devtools - postgrest.tests - postgrest.style postgrest.hsie.bin + postgrest.nixpkgsUpgrade + postgrest.style + postgrest.tests + postgrest.withTools ] ++ lib.optional memoryTests postgrest.tests.memoryTests ++ lib.optional docker postgrest.docker