Unverified Commit 33daa3f4 authored by Johannes Kirschbauer's avatar Johannes Kirschbauer
Browse files

lib.modules: init test for lib.mkDefinition

parent c906064a
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -1177,8 +1177,6 @@ let
      map (mapAttrs (n: v: mkIf cfg.condition v)) (pushDownProperties cfg.content)
    else if cfg._type or "" == "override" then
      map (mapAttrs (n: v: mkOverride cfg.priority v)) (pushDownProperties cfg.content)
    # else if cfg._type or "" == "definition" then
    #   map (mapAttrs (n: v: mkDefinition v)) (pushDownProperties cfg.content)
    # FIXME: handle mkOrder?
    else
      [ cfg ];
+8 −0
Original line number Diff line number Diff line
@@ -673,6 +673,14 @@ checkConfigError 'The option .conflictingPathOptionType. in .*/pathWith.nix. is
# types.pathWith { inStore = true; absolute = false; }
checkConfigError 'In pathWith, inStore means the path must be absolute' config.impossiblePathOptionType ./pathWith.nix

# mkDefinition
# check that mkDefinition 'file' is printed in the error message
checkConfigError 'Cannot merge definitions.*\n\s*- In .file.*\n\s*- In .other.*' config.conflict ./mkDefinition.nix
checkConfigError 'A definition for option .viaOptionDefault. is not of type .boolean.*' config.viaOptionDefault ./mkDefinition.nix
checkConfigOutput '^true$' config.viaConfig ./mkDefinition.nix
checkConfigOutput '^true$' config.mkMerge ./mkDefinition.nix
checkConfigOutput '^true$' config.mkForce ./mkDefinition.nix

cat <<EOF
====== module tests ======
$pass Pass
+71 −0
Original line number Diff line number Diff line
{ lib, ... }:
let
  inherit (lib)
    mkOption
    mkDefinition
    mkOptionDefault
    ;
in
{
  imports = [
    {
      _file = "file";
      options.conflict = mkOption {
        default = 1;
      };
      config.conflict = mkDefinition {
        file = "other";
        value = mkOptionDefault 42;
      };
    }
    {
      # Check that mkDefinition works within 'config'
      options.viaConfig = mkOption { };
      config.viaConfig = mkDefinition {
        file = "other";
        value = true;
      };
    }
    {
      # Check mkMerge can wrap mkDefinitions
      # Not the other way around
      options.mkMerge = mkOption {
        type = lib.types.bool;
      };
      config.mkMerge = lib.mkMerge [
        (mkDefinition {
          file = "a.nix";
          value = true;
        })
        (mkDefinition {
          file = "b.nix";
          value = true;
        })
      ];
    }
    {
      # Check mkDefinition can use mkForce on the value
      # Not the other way around
      options.mkForce = mkOption {
        type = lib.types.bool;
        default = false;
      };
      config.mkForce = mkDefinition {
        file = "other";
        value = lib.mkForce true;
      };
    }
    {
      # Currently expects an error
      # mkDefinition doesn't work on option default
      # This is a limitation and might be resolved in the future
      options.viaOptionDefault = mkOption {
        type = lib.types.bool;
        default = mkDefinition {
          file = "other";
          value = true;
        };
      };
    }
  ];
}
+19 −1
Original line number Diff line number Diff line
@@ -139,7 +139,25 @@ A free-floating definition is created with `mkDefinition { file = ...; value = .

Preserving the file location creates better error messages, for example when copying definitions from one option to another.

Other properties like `mkOverride` `mkMerge` `mkAfter` can be used in the `value` attribute but not the other way around.
Other properties like `mkOverride` `mkMerge` `mkAfter` can be used in the `value` attribute but not on the entire definition.

This is what would work

```nix
mkDefinition {
   value = mkForce 42;
   file = "somefile.nix";
}
```

While this would NOT work.

```nix
mkForce (mkDefinition {
   value = 42;
   file = "somefile.nix";
})
```

The following shows an example configuration that yields an error with the custom position information: