Skip to content

Commit

Permalink
Merge pull request #118 from hercules-ci/fix-herculesCI-repo-optional…
Browse files Browse the repository at this point in the history
…-attrs

Fix `herculesCI.repo` nullable attrs
  • Loading branch information
roberth authored Feb 2, 2023
2 parents 5f5f6d9 + f0ca241 commit 0cc800c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 24 deletions.
11 changes: 7 additions & 4 deletions flake-modules/herculesCI-attribute.nix
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,13 @@ in
config = {
# Filter out values which are unavailable and therefore null.
# e.g. hci effect run may not support owner and name.
# Always be careful when filtering, because it's not as lazy as you'd like.
# Shouldn't be a problem here though.
repo = lib.filterAttrs (k: v: v != null) {
inherit (primaryRepo) ref branch tag rev shortRev;
repo = {
ref = primaryRepo.ref;
branch = primaryRepo.branch or null;
tag = primaryRepo.tag or null;
rev = primaryRepo.rev;
shortRev = primaryRepo.shortRev;
} // lib.filterAttrs (k: v: v != null) {
remoteHttpUrl = primaryRepo.remoteHttpUrl or null;
remoteSshUrl = primaryRepo.remoteSshUrl or null;
webUrl = primaryRepo.webUrl or null;
Expand Down
51 changes: 48 additions & 3 deletions flake-modules/herculesCI-eval-test.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
{ inputs }:
args@
{ inputs ? hercules-ci-effects.inputs
, hercules-ci-effects ? if args?inputs then inputs.self else builtins.getFlake "git+file://${toString ./..}"
}:
let
testSupport = import ../lib/testSupport.nix args;
in
rec {
inherit (inputs) flake-parts;
inherit (testSupport) callFlakeOutputs;

emptyFlake = callFlakeOutputs (inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
../flake-module.nix
];
systems = [ "x86_64-linux" ];
}
);

fakeRepoBranch = {
branch = "main";
ref = "refs/heads/main";
rev = "deadbeef";
shortRev = "deadbe";
tag = null;
remoteHttpUrl = "https://git.forge/repo.git";
};
fakeRepoTag = {
branch = null;
ref = "refs/heads/main";
rev = "deadbeef";
shortRev = "deadbe";
tag = "1.0";
remoteHttpUrl = "https://git.forge/repo.git";
};
fakeHerculesCI = repo: {
primaryRepo = repo;
inherit (repo) branch ref tag rev shortRev remoteHttpUrl;
};

example1 =
flake-parts.lib.mkFlake { self = { }; }
callFlakeOutputs (inputs:
flake-parts.lib.mkFlake { inherit inputs; }
({ ... }: {
imports = [
../flake-module.nix
Expand All @@ -21,7 +59,8 @@ rec {
minute = 59;
};
herculesCI.onSchedule.scheduledJob3 = { };
});
})
);

tests = ok:

Expand Down Expand Up @@ -49,6 +88,12 @@ rec {
minute = null;
};

assert (emptyFlake.herculesCI (fakeHerculesCI fakeRepoBranch))._debug.repo.branch == "main";
assert (emptyFlake.herculesCI (fakeHerculesCI fakeRepoBranch))._debug.repo.tag == null;

assert (emptyFlake.herculesCI (fakeHerculesCI fakeRepoTag))._debug.repo.branch == null;
assert (emptyFlake.herculesCI (fakeHerculesCI fakeRepoTag))._debug.repo.tag == "1.0";

ok;

}
Expand Down
21 changes: 4 additions & 17 deletions lib/mkHerculesCI-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@ args@
{ inputs ? hercules-ci-effects.inputs
, hercules-ci-effects ? if args?inputs then inputs.self else builtins.getFlake "git+file://${toString ./..}"
}:
let
testSupport = import ./testSupport.nix args;
in
rec {
inherit (inputs) flake-parts;
inherit (inputs.nixpkgs.lib) attrNames;

# Approximates https://github.com/NixOS/nix/blob/7cd08ae379746749506f2e33c3baeb49b58299b8/src/libexpr/flake/call-flake.nix#L46
# s/flake.outputs/args.outputs/
callFlake = args@{ inputs, outputs, sourceInfo }:
let
outputs = args.outputs (inputs // { self = result; });
result = outputs // sourceInfo // { inherit inputs outputs sourceInfo; _type = "flake"; };
in
result;

callFlakeOutputs = outputs: callFlake {
inherit outputs;
inputs = inputs // {
inherit hercules-ci-effects;
};
sourceInfo = { };
};
inherit (testSupport) callFlake callFlakeOutputs;

fakePkg = name: {
inherit name;
Expand Down
29 changes: 29 additions & 0 deletions lib/testSupport.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
args@
{ inputs ? hercules-ci-effects.inputs
, hercules-ci-effects ? if args?inputs then inputs.self else builtins.getFlake "git+file://${toString ./..}"
}:

let
# Approximates https://github.com/NixOS/nix/blob/7cd08ae379746749506f2e33c3baeb49b58299b8/src/libexpr/flake/call-flake.nix#L46
# s/flake.outputs/args.outputs/
callFlake = args@{ inputs, outputs, sourceInfo }:
let
outputs = args.outputs (inputs // { self = result; });
result = outputs // sourceInfo // { inherit inputs outputs sourceInfo; _type = "flake"; };
in
result;

callFlakeOutputs = outputs: callFlake {
inherit outputs;
inputs = inputs // {
inherit hercules-ci-effects;
};
sourceInfo = { };
};

in {
inherit
callFlake
callFlakeOutputs
;
}

0 comments on commit 0cc800c

Please sign in to comment.