@@ -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 " )
77100unittest
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
88112ShardMatrix splitToShards (int shardCount)
0 commit comments