Binary not available in the check phase?

I’ve noticed the black Python package is lagging a few versions behind and I thought I’d submit a PR to update it.

When building it, the check phase fails with some errors related to the black binary not being found (apparently some black tests try to execute the black binary). Here’s the error message:

black_primer.cli:cli.py:54 Can not find 'black' executable in PATH. No point in running

Here’s the code of the derivation I’m trying to build:

{ stdenv, buildPythonPackage, fetchPypi, pythonOlder
, attrs, click, toml, appdirs, aiohttp, aiohttp-cors
, glibcLocales, typed-ast, pathspec, regex
, setuptools_scm, pytest, mypy-extensions }:

buildPythonPackage rec {
  pname = "black";
  version = "20.8b1";

  disabled = pythonOlder "3.6";

  src = fetchPypi {
    inherit pname version;
    sha256 = "1spv6sldp3mcxr740dh3ywp25lly9s8qlvs946fin44rl1x5a0hw";
  };

  nativeBuildInputs = [ setuptools_scm ];
  checkInputs =  [ pytest glibcLocales ];

  # Necessary for the tests to pass on Darwin with sandbox enabled.
  # Black starts a local server and needs to bind a local address.
  __darwinAllowLocalNetworking = true;

  # Don't know why these tests fails
  # Disable test_expression_diff, because it fails on darwin
  checkPhase = ''
    LC_ALL="en_US.UTF-8" pytest \
      --deselect tests/test_black.py::BlackTestCase::test_expression_diff \
      --deselect tests/test_black.py::BlackTestCase::test_cache_multiple_files \
      --deselect tests/test_black.py::BlackTestCase::test_failed_formatting_does_not_get_cached
  '';

  propagatedBuildInputs = [ attrs appdirs click toml aiohttp aiohttp-cors pathspec regex typed-ast mypy-extensions ];

  meta = with stdenv.lib; {
    description = "The uncompromising Python code formatter";
    homepage    = "https://github.com/psf/black";
    license     = licenses.mit;
    maintainers = with maintainers; [ sveitser ];
  };
}

I don’t know if there’s a way to make these tests pass (according to the docs, the checks are supposed to be run after the install phase, right?), or if I should just ignore the failing tests (by adding them to --deselect)?

Also is there a recommended way to build a Python derivation? At the moment I’m using nix-build -E 'with import <nixpkgs> {}; with python37Packages; callPackage ./default.nix { }' to test, but maybe there’s a better way?

  preCheck = ''
    PATH=$PATH:$out/bin
  '';

Thanks! It helped me getting some tests to pass. There are 2 failing ones left:

  1. A test tries to execute a hardcoded path /bin/false, which obviously fails. I guess there’s no way of fixing it?
  2. Another test refers to a file that’s not included in the package once built (I don’t know why this file is in the src directory since it’s only used in tests). I’m not sure if this one is fixable, since buildPythonPackage maps checkPhase to installCheckPhase?

You can of course use the patchPhase to patch the test.

you would have to patch or disable them

I would just disable the test, that’s odd behavior.

Thanks for your help folks! I got it working and submitted a PR.