From f39cb92c2995103f5eb4d65e9ad56fcdb206c39f Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Mon, 7 Oct 2019 13:24:31 -0400
Subject: [PATCH] A first stab at testing with Nix instead

---
 .circleci/config.yml | 17 ++++++-----------
 default.nix          |  4 ++--
 zkapauthorizer.nix   | 15 +++++++++++----
 3 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index 3fe7c76..b8beebc 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -49,30 +49,25 @@ jobs:
 
   tests:
     docker:
-      - image: "circleci/python:2.7"
+      # Run in a highly Nix-capable environment.
+      - image: "nixorg/nix:circleci"
 
     environment:
-      PIP_REQUIREMENTS: "-r requirements.txt -r requirements-tests.txt"
-      ZKAPAUTHORIZER_HYPOTHESIS_PROFILE: "ci"
+      CODECOV_TOKEN: "cc6e4697-4337-4506-88af-92b8f8ca6b22"
 
     steps:
       - "checkout"
 
-      - <<: *PREPARE_VIRTUALENV
-
       - run:
           name: "Run Test Suite"
           command: |
-            . venv/bin/activate
-            pip install .
-            coverage run -m twisted.trial _zkapauthorizer
+            nix-build --argstr hypothesisProfile ci --arg collectCoverage true --attr doc
 
       - run:
           name: "Report Coverage"
           command: |
-            . venv/bin/activate
-            CODECOV_TOKEN="cc6e4697-4337-4506-88af-92b8f8ca6b22" codecov
-
+            nix-shell -p 'python.withPackages (ps: [ ps.codecov ])' --keep CODECOV_TOKEN --run \
+              codecov ./result-doc/share/doc/*/.coverage
 
 workflows:
   version: 2
diff --git a/default.nix b/default.nix
index f1c5e37..04af35d 100644
--- a/default.nix
+++ b/default.nix
@@ -1,2 +1,2 @@
-{ pkgs ? import ./nixpkgs.nix { } }:
-pkgs.python27Packages.callPackage ./zkapauthorizer.nix { }
+{ pkgs ? import ./nixpkgs.nix { }, hypothesisProfile ? null, collectCoverage ? false }:
+pkgs.python27Packages.callPackage ./zkapauthorizer.nix { inherit hypothesisProfile collectCoverage; }
diff --git a/zkapauthorizer.nix b/zkapauthorizer.nix
index 0b311a4..ae74536 100644
--- a/zkapauthorizer.nix
+++ b/zkapauthorizer.nix
@@ -1,6 +1,8 @@
 { buildPythonPackage, sphinx, circleci-cli
 , attrs, zope_interface, twisted, tahoe-lafs, privacypass
 , fixtures, testtools, hypothesis, pyflakes, treq, coverage
+, hypothesisProfile ? null
+, collectCoverage ? false
 }:
 buildPythonPackage rec {
   version = "0.0";
@@ -8,7 +10,7 @@ buildPythonPackage rec {
   name = "${pname}-${version}";
   src = ./.;
 
-  outputs = [ "out" "doc" ];
+  outputs = [ "out" ] ++ (if collectCoverage then [ "doc" ] else [ ]);
 
   depsBuildBuild = [
     sphinx
@@ -35,13 +37,18 @@ buildPythonPackage rec {
   checkPhase = ''
     runHook preCheck
     "${pyflakes}/bin/pyflakes" src/_zkapauthorizer
-    python -m coverage run --branch --source _zkapauthorizer,twisted.plugins.zkapauthorizer --module twisted.trial _zkapauthorizer
+    python -m ${if collectCoverage
+      then "coverage run --branch --source _zkapauthorizer,twisted.plugins.zkapauthorizer --module"
+      else ""
+    } twisted.trial _zkapauthorizer
     runHook postCheck
   '';
 
-  postCheck = ''
+  postCheck = if collectCoverage
+    then ''
     python -m coverage html
     mkdir -p "$doc/share/doc/${name}"
     cp -vr .coverage htmlcov "$doc/share/doc/${name}"
-  '';
+    ''
+    else "";
 }
-- 
GitLab