Commit 33aaac17 authored by Robert Hensing's avatar Robert Hensing
Browse files

devShellTools.unstructuredDerivationInputEnv: Return attrsOf str

... and test it.
parent 43df2b50
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ rec {
      (name: value:
        let str = valueToString value;
        in if lib.elem name (drvAttrs.passAsFile or [])
        then lib.nameValuePair "${name}Path" (writeText "pass-as-text-${name}" str)
        then lib.nameValuePair "${name}Path" "${writeText "pass-as-text-${name}" str}"
        else lib.nameValuePair name str
      )
      drvAttrs;
+103 −1
Original line number Diff line number Diff line
@@ -4,9 +4,16 @@
  lib,
  stdenv,
  hello,
  writeText,
  runCommand, zlib,
}:
let
  inherit (lib) escapeShellArg;
  inherit (lib)
    concatLines
    escapeShellArg
    isString
    mapAttrsToList
    ;
in
{
  # nix-build -A tests.devShellTools.valueToString
@@ -42,4 +49,99 @@ in
        ) >log 2>&1 || { cat log; exit 1; }
      '';
    };

  # nix-build -A tests.devShellTools.valueToString
  unstructuredDerivationInputEnv =
    let
      inherit (devShellTools) unstructuredDerivationInputEnv;

      drvAttrs = {
        one = 1;
        boolTrue = true;
        boolFalse = false;
        foo = "foo";
        list = [ 1 2 3 ];
        pathDefaultNix = ./default.nix;
        stringWithDep = "Exe: ${hello}/bin/hello";
        aPackageAttrSet = hello;
        anOutPath = hello.outPath;
        anAnAlternateOutput = zlib.dev;

        passAsFile = [ "bar" ];
        bar = ''
          bar
          ${writeText "qux" "yea"}
        '';
      };
      result = unstructuredDerivationInputEnv { inherit drvAttrs; };
    in
    assert result // { barPath = "<check later>"; } == {
      one = "1";
      boolTrue = "1";
      boolFalse = "";
      foo = "foo";
      list = "1 2 3";
      pathDefaultNix = "${./default.nix}";
      stringWithDep = "Exe: ${hello}/bin/hello";
      aPackageAttrSet = "${hello}";
      anOutPath = "${hello.outPath}";
      anAnAlternateOutput = "${zlib.dev}";

      passAsFile = "bar";
      barPath = "<check later>";
    };

    # Not runCommand, because it alters `passAsFile`
    stdenv.mkDerivation ({
      name = "devShellTools-unstructuredDerivationInputEnv-built-tests";

      exampleBarPathString =
        assert isString result.barPath;
        result.barPath;

      dontUnpack = true;
      dontBuild = true;
      dontFixup = true;
      doCheck = true;

      installPhase = "touch $out";

      checkPhase = ''
        checkAttr() {
          echo checking attribute $1...
          if [[ "$2" != "$3" ]]; then
            echo "expected: $3"
            echo "actual: $2"
            exit 1
          fi
        }
        ${
          concatLines
            (mapAttrsToList
              (name: value:
                "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}"
              )
              (removeAttrs
                result
                [
                  # Nix puts it in workdir, which is not a concept for
                  # unstructuredDerivationInputEnv, so we have to put it in the
                  # store instead. This means the full path won't match.
                  "barPath"
                ])
            )
        }
        (
          set -x

          diff $exampleBarPathString $barPath

          # TODO nice to have, as `cp $barPath foo/` preserves the basename:
          #      this is usually a mistake, so not that big a deal perhaps
          # [[ "$(basename $exampleBarPathString)" = "$(basename $barPath)" ]]
        )

        touch $out
      '';
    } // drvAttrs);
}