tests run hermetically via nix

This commit is contained in:
Benton Edmondson 2022-06-05 21:20:38 -04:00
parent 4431f7820a
commit 6f592f6e8d
3 changed files with 35 additions and 6 deletions

View file

@ -26,6 +26,7 @@
outputs = flakes: let outputs = flakes: let
self = flakes.self.packages.x86_64-linux; self = flakes.self.packages.x86_64-linux;
nixpkgs = flakes.nixpkgs.legacyPackages.x86_64-linux.pkgsStatic; nixpkgs = flakes.nixpkgs.legacyPackages.x86_64-linux.pkgsStatic;
nixpkgs-dyn = flakes.nixpkgs.legacyPackages.x86_64-linux;
gourou-src = flakes.gourou-src; gourou-src = flakes.gourou-src;
updfparser-src = flakes.updfparser-src; updfparser-src = flakes.updfparser-src;
base64-src = flakes.base64-src; base64-src = flakes.base64-src;
@ -146,5 +147,21 @@
'' ]; '' ];
}; };
defaultPackage.x86_64-linux = self.knock; 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
'';
};
}; };
} }

View file

@ -52,7 +52,7 @@ nix flake update
### Testing ### 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). Test books can be found [here](https://www.adobe.com/solutions/ebook/digital-editions/sample-ebook-library.html).

22
tests/test.py → tests/tests.py Executable file → Normal file
View file

@ -1,13 +1,25 @@
#! /usr/bin/env nix-shell #!/usr/bin/env python3
#! nix-shell -i python3 -p python3 python310Packages.beautifulsoup4 python310Packages.requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from urllib.parse import urlparse from urllib.parse import urlparse
from pathlib import Path from pathlib import Path
import requests, sys, subprocess, shutil, os import requests, sys, subprocess, shutil, os
knock_path = Path(__file__).parent.parent.joinpath("result/bin/knock") knock = Path("./result/bin/knock")
workspace = Path(__file__).parent.joinpath("workspace")
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(): if workspace.exists():
shutil.rmtree(workspace) shutil.rmtree(workspace)
@ -35,7 +47,7 @@ for i, link in enumerate(links):
r = requests.get(link) r = requests.get(link)
open(file, "wb").write(r.content) open(file, "wb").write(r.content)
result = subprocess.run([knock_path, file]) result = subprocess.run([knock, file])
if result.returncode != 0: if result.returncode != 0:
print("Failed") print("Failed")