From 6f592f6e8d932bfa68352453cda5966ff1a1e41f Mon Sep 17 00:00:00 2001 From: Benton Edmondson Date: Sun, 5 Jun 2022 21:20:38 -0400 Subject: [PATCH] tests run hermetically via nix --- flake.nix | 17 +++++++++++++++++ readme.md | 2 +- tests/{test.py => tests.py} | 22 +++++++++++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) rename tests/{test.py => tests.py} (68%) mode change 100755 => 100644 diff --git a/flake.nix b/flake.nix index 519aa3e..92eddaa 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,7 @@ outputs = flakes: let self = flakes.self.packages.x86_64-linux; nixpkgs = flakes.nixpkgs.legacyPackages.x86_64-linux.pkgsStatic; + nixpkgs-dyn = flakes.nixpkgs.legacyPackages.x86_64-linux; gourou-src = flakes.gourou-src; updfparser-src = flakes.updfparser-src; base64-src = flakes.base64-src; @@ -146,5 +147,21 @@ '' ]; }; defaultPackage.x86_64-linux = self.knock; + packages.x86_64-linux.tests = nixpkgs-dyn.stdenv.mkDerivation { + name = "tests"; + src = ./tests; + buildInputs = [ (nixpkgs-dyn.python3.withPackages(p: [ + p.beautifulsoup4 + p.requests + ])) ]; + patchPhase = '' + substituteInPlace tests.py --replace "./result/bin/knock" "${self.knock}/bin/knock" + ''; + installPhase = '' + mkdir -p $out/bin + cp tests.py $out/bin/tests + chmod +x $out/bin/tests + ''; + }; }; } \ No newline at end of file diff --git a/readme.md b/readme.md index 6844151..a1bbc7b 100644 --- a/readme.md +++ b/readme.md @@ -52,7 +52,7 @@ nix flake update ### Testing ``` -./tests/test.py +nix run .#tests -- ./tests/workspace ``` Test books can be found [here](https://www.adobe.com/solutions/ebook/digital-editions/sample-ebook-library.html). diff --git a/tests/test.py b/tests/tests.py old mode 100755 new mode 100644 similarity index 68% rename from tests/test.py rename to tests/tests.py index a45ab62..fd3e710 --- a/tests/test.py +++ b/tests/tests.py @@ -1,13 +1,25 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i python3 -p python3 python310Packages.beautifulsoup4 python310Packages.requests +#!/usr/bin/env python3 from bs4 import BeautifulSoup from urllib.parse import urlparse from pathlib import Path import requests, sys, subprocess, shutil, os -knock_path = Path(__file__).parent.parent.joinpath("result/bin/knock") -workspace = Path(__file__).parent.joinpath("workspace") +knock = Path("./result/bin/knock") + +if not knock.exists(): + print("error: " + str(knock) + " does not exist", file=sys.stderr) + sys.exit() + +if len(sys.argv) != 2: + print( + "error: missing required argument: directory in which to perform the tests", + file=sys.stderr + ) + sys.exit() + +print("Testing " + str(knock)) +workspace = Path(sys.argv[1]) if workspace.exists(): shutil.rmtree(workspace) @@ -35,7 +47,7 @@ for i, link in enumerate(links): r = requests.get(link) open(file, "wb").write(r.content) - result = subprocess.run([knock_path, file]) + result = subprocess.run([knock, file]) if result.returncode != 0: print("Failed")