Commit 5a51f54e authored by Robert Hensing's avatar Robert Hensing Committed by github-actions[bot]
Browse files

testers.lycheeLinkCheck: Fix plain derivation remap, showcase emptyDirectory

(cherry picked from commit 60536f85a3b967b0ee866b2fe5264d1faa774030)
parent 20fccd7b
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -93,7 +93,14 @@ It has two modes:

  Before checking the existence of a URL, the regular expressions are matched and replaced by their corresponding values.

  Example: `{ "https://blog\\.example\\.com" = site; }`
  Example:

  ```nix
  {
    "https://nix\\.dev/manual/nix/[a-z0-9.-]*" = "${nix.doc}/share/doc/nix/manual";
    "https://nixos\\.org/manual/nix/(un)?stable" = "${emptyDirectory}/placeholder-to-disallow-old-nix-docs-urls";
  }
  ```

  Store path in the attribute values are automatically prefixed with `file://`, because lychee requires this for paths in the file system.
  If this is a problem, or if you need to control the order in which replacements are performed, use `extraConfig.remap` instead.
+4 −4
Original line number Diff line number Diff line
@@ -4,11 +4,11 @@ let
  inherit (lib.strings) hasPrefix;

  toURL = v:
    if builtins.isString v && hasPrefix builtins.storeDir v
      || isPath v
    let s = "${v}";
    in if hasPrefix builtins.storeDir s
    then # lychee requires that paths on the file system are prefixed with file://
      "file://${v}"
    else "${v}";
      "file://${s}"
    else s;

  # See https://nixos.org/manual/nixpkgs/unstable/#tester-lycheeLinkCheck
  # or doc/builders/testers.chapter.md
+1 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ rustPlatform.buildRustPackage rec {
    #       Which is true most of the time, but not necessarily after overriding.
    ok = callPackage ./tests/ok.nix { };
    fail = callPackage ./tests/fail.nix { };
    fail-emptyDirectory = callPackage ./tests/fail-emptyDirectory.nix { };
    network = testers.runNixOSTest ./tests/network.nix;
  };

+28 −0
Original line number Diff line number Diff line
{ runCommand, testers, emptyDirectory }:
let
  sitePkg = runCommand "site" { } ''
    dist=$out/dist
    mkdir -p $dist
    echo "<html><body><a href=\"https://example.com/foo\">foo</a></body></html>" > $dist/index.html
    echo "<html><body></body></html>" > $dist/foo.html
  '';
  check = testers.lycheeLinkCheck {
    site = sitePkg + "/dist";
    remap = {
      # Normally would recommend to append a subpath that hints why it's forbidden; see example in docs.
      # However, we also want to test that a package is converted to a string *before*
      # it's tested whether it's a store path. Mistake made during development caused:
      # cannot check URI: InvalidUrlRemap("The remapping pattern must produce a valid URL, but it is not: /nix/store/4d0ix...empty-directory/foo
      "https://example.com" = emptyDirectory;
    };
  };

  failure = testers.testBuildFailure check;
in
  runCommand "link-check-fail" { inherit failure; } ''
    # The details of the message might change, but we have to make sure the
    # correct error is reported, so that we know it's not something else that
    # went wrong.
    grep 'empty-directory/foo.*Cannot find file' $failure/testBuildFailure.log >/dev/null
    touch $out
  ''