Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/system: move toLosslessStringMaybe into lib/tests #239132

Merged
1 commit merged into from Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ rec {
let removeFunctions = a: lib.filterAttrs (_: v: !builtins.isFunction v) a;
in a: b: removeFunctions a == removeFunctions b;

/*
Try to convert an elaborated system back to a simple string. If not possible,
return null. So we have the property:

sys: _valid_ sys ->
sys == elaborate (toLosslessStringMaybe sys)

NOTE: This property is not guaranteed when `sys` was elaborated by a different
version of Nixpkgs.
*/
toLosslessStringMaybe = sys:
if lib.isString sys then sys
else if equals sys (elaborate sys.system) then sys.system
else null;

/* List of all Nix system doubles the nixpkgs flake will expose the package set
for. All systems listed here must be supported by nixpkgs as `localSystem`.

Expand Down
20 changes: 18 additions & 2 deletions lib/tests/systems.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ let
expr = lib.sort lib.lessThan x;
expected = lib.sort lib.lessThan y;
};

/*
Try to convert an elaborated system back to a simple string. If not possible,
return null. So we have the property:

sys: _valid_ sys ->
sys == elaborate (toLosslessStringMaybe sys)

NOTE: This property is not guaranteed when `sys` was elaborated by a different
version of Nixpkgs.
*/
toLosslessStringMaybe = sys:
if lib.isString sys then sys
else if lib.systems.equals sys (lib.systems.elaborate sys.system) then sys.system
else null;

in
lib.runTests (
# We assert that the new algorithmic way of generating these lists matches the
Expand Down Expand Up @@ -55,11 +71,11 @@ lib.runTests (
};

test_toLosslessStringMaybe_example_x86_64-linux = {
expr = lib.systems.toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux");
expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux");
expected = "x86_64-linux";
};
test_toLosslessStringMaybe_fail = {
expr = lib.systems.toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; });
expr = toLosslessStringMaybe (lib.systems.elaborate "x86_64-linux" // { something = "extra"; });
expected = null;
};
}
Expand Down