nixpkgs/lib/tests
Maximilian Bosch 55ea29fd8c
lib/generators/toPretty: add evaluation-limit
When having e.g. recursive attr-set, it cannot be printed which is
solved by Nix itself like this:

    $ nix-instantiate --eval -E 'let a.b = 1; a.c = a; in builtins.trace a 1'
    trace: { b = 1; c = <CYCLE>; }
    1

However, `generators.toPretty` tries to evaluate something until it's
done which can result in a spurious `stack-overflow`-error:

    $ nix-instantiate --eval -E 'with import <nixpkgs/lib>; generators.toPretty {  } (mkOption { type = types.str; })'
    error: stack overflow (possible infinite recursion)

Those attr-sets are in fact rather common, one example is shown above, a
`types.<type>`-declaration is such an example. By adding an optional
`depthLimit`-argument, `toPretty` will stop evaluating as soon as the
limit is reached:

    $ nix-instantiate --eval -E 'with import ./Projects/nixpkgs-update-int/lib; generators.toPretty { depthLimit = 2; } (mkOption { type = types.str; })' |xargs -0 echo -e
    "{
      _type = \"option\";
      type = {
        _type = \"option-type\";
        check = <function>;
        deprecationMessage = null;
        description = \"string\";
        emptyValue = { };
        functor = {
          binOp = <unevaluated>;
          name = <unevaluated>;
          payload = <unevaluated>;
          type = <unevaluated>;
          wrapped = <unevaluated>;
        };
        getSubModules = null;
        getSubOptions = <function>;
        merge = <function>;
        name = \"str\";
        nestedTypes = { };
        substSubModules = <function>;
        typeMerge = <function>;
      };
    }"

Optionally, it's also possible to let `toPretty` throw an error if the
limit is exceeded.
2021-08-25 23:18:26 +02:00
..
modules Revert "lib/tests: Add type deprecation tests" 2021-05-05 18:53:28 +02:00
check-eval.nix lib/tests: Add check-eval.nix to run simple tests. 2018-03-04 06:28:49 -05:00
maintainers.nix treewide: runCommandNoCC -> runCommand 2021-08-15 17:36:41 +02:00
misc.nix lib/generators/toPretty: add evaluation-limit 2021-08-25 23:18:26 +02:00
modules.sh Revert "lib/tests: Add type deprecation tests" 2021-05-05 18:53:28 +02:00
release.nix treewide: runCommandNoCC -> runCommand 2021-08-15 17:36:41 +02:00
sources.sh lib/tests/sources.sh: init 2021-05-29 14:32:56 +02:00
systems.nix lib.systems: add m68k-netbsd support 2021-08-01 15:27:12 +00:00