From 27047ddfdaaae3c07ebd102efe07432ee9b00b54 Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Sun, 29 Mar 2020 17:27:01 +0200 Subject: [PATCH] Test notebooks do not contain any impure output Unfortunately nbconvert is broken... --- default.nix | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/default.nix b/default.nix index ada41c6..9dbaf4a 100644 --- a/default.nix +++ b/default.nix @@ -76,6 +76,45 @@ let NIX_CONF_DIR="${env}/etc"; }; + # Utility that given a filename cleans the notebook of its output. + # TODO: broken because of https://github.com/jupyter/nbconvert/issues/822 + clean-notebook = pkgs.writeShellScriptBin "clean-notebook" '' + ${python.pkgs.nbconvert}/bin/jupyter-nbconvert --ClearMetadataPreprocessor.enabled=True --ClearOutput.enabled=True --to=notebook $1 + ''; + + # Validate the notebook purely. In this case, that means testing it does not have any output. + # TODO: broken because of https://github.com/jupyter/nbconvert/issues/822 + test-notebook = pkgs.writeShellScriptBin "test-notebook" '' + if [ ! -f "$1" ]; then + echo "Error: file $1 does not exist" + exit 1 + fi + bname=$(basename -- "$1") + fname="''${bname%.*}" + dname=$(dirname "$1") + echo "Testing notebook $1" + + ${python.pkgs.nbconvert}/bin/jupyter-nbconvert --ClearMetadataPreprocessor.enabled=True --ClearOutput.enabled=True --to=notebook --output="$fname-cleaned.ipynb" "$1" + diff "$1" "$dname/$fname-cleaned.ipynb" + rm "$dname/$fname-cleaned.ipynb" + ''; + + # Test all notebooks purely + testsPure = pkgs.stdenv.mkDerivation { + name = "nix-tutorials-pure-tests"; + src = fetchGit ./.; + + buildPhase = '' + shopt -s globstar + for tutorial in tutorials/**/*.ipynb; do + ${test-notebook}/bin/test-notebook "$tutorial" + done + ''; + installPhase = '' + echo success > $out + ''; + }; + in { - inherit devDeps runDeps devEnv runEnv pkgs; + inherit devDeps runDeps devEnv runEnv clean-notebook test-notebook testsPure pkgs; }