Skip to content

Commit bbaee5f

Browse files
committed
fix(mcl.commands.shard_matrix): Fix implementation, add flakeRef param and add tests
1 parent 2e84cef commit bbaee5f

File tree

5 files changed

+1232
-21
lines changed

5 files changed

+1232
-21
lines changed

packages/mcl/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"(nix\\.(build|run))"
2828
"fetchJson|(coda\.)"
2929
"checkPackage"
30+
"generateShardMatrix"
3031
]
3132
);
3233
in

packages/mcl/src/src/mcl/commands/shard_matrix.d

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,67 @@ struct ShardMatrix
4646
Shard[] include;
4747
}
4848

49-
ShardMatrix generateShardMatrix()
49+
ShardMatrix generateShardMatrix(string flakeRef = ".")
5050
{
51-
try
51+
import std.path : isValidPath, absolutePath, buildNormalizedPath;
52+
53+
if (flakeRef.isValidPath) {
54+
flakeRef = flakeRef.absolutePath.buildNormalizedPath;
55+
}
56+
57+
const shardCountOutput = nix.eval("", [
58+
"--impure",
59+
"--expr",
60+
`(builtins.getFlake "` ~ flakeRef ~ `").outputs.legacyPackages.x86_64-linux.mcl.matrix.shardCount or 0`
61+
]);
62+
63+
infof("shardCount: '%s'", shardCountOutput);
64+
65+
const shardCount = shardCountOutput
66+
.strip()
67+
.to!uint;
68+
69+
if (shardCount == 0)
5270
{
53-
const shardCount = nix.eval(".#legacyPackages.x86_64-linux.shardCount")
54-
.strip()
55-
.to!int;
71+
errorf("No shards found, exiting");
72+
return ShardMatrix([Shard("", "", -1)]);
73+
}
5674

57-
infof("shardCount: %s", shardCount);
75+
return splitToShards(shardCount);
76+
}
5877

59-
return splitToShards(shardCount);
78+
@("generateShardMatrix.ok")
79+
unittest
80+
{
81+
version (none)
82+
{
83+
// See: https://github.com/metacraft-labs/nixos-modules/blob/b70f5bf556a0afc25d45ff5abd9d4eeae58d2647/flake.nix
84+
auto flakeRef = "github:metacraft-labs/nixos-modules?rev=b70f5bf556a0afc25d45ff5abd9d4eeae58d2647";
6085
}
61-
catch (Exception e)
86+
else
6287
{
63-
version (unittest)
64-
{
65-
}
66-
else
67-
{
68-
errorf("Error: %s", e.msg);
69-
errorf("No shards found, exiting");
70-
}
71-
return ShardMatrix([Shard("", "", -1)]);
88+
import mcl.utils.path : rootDir;
89+
auto flakeRef = rootDir.buildPath("packages/mcl/src/src/mcl/utils/test/nix/shard-matrix-ok");
7290
}
7391

92+
auto shards = generateShardMatrix(flakeRef);
93+
assert(shards.include.length == 11);
94+
assert(shards.include[0].prefix == "legacyPackages");
95+
assert(shards.include[0].postfix == "shards.0");
96+
assert(shards.include[0].digit == 0);
7497
}
7598

76-
@("generateShardMatrix")
99+
@("generateShardMatrix.fail")
77100
unittest
78101
{
79-
auto shards = generateShardMatrix();
80-
//this repo doesn't include shards, so we should get the error message
102+
import mcl.utils.path : rootDir;
103+
auto flakeRef = rootDir.buildPath("packages/mcl/src/src/mcl/utils/test/nix/shard-matrix-no-shards");
104+
105+
auto shards = generateShardMatrix(flakeRef);
81106
assert(shards.include.length == 1);
82107
assert(shards.include[0].prefix == "");
83108
assert(shards.include[0].postfix == "");
84109
assert(shards.include[0].digit == -1);
85-
86110
}
87111

88112
ShardMatrix splitToShards(int shardCount)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
outputs =
3+
{ ... }:
4+
{
5+
6+
};
7+
}

0 commit comments

Comments
 (0)