Skip to content

Commit 227aa17

Browse files
committed
test(mcl): Refactor tests and add fail messages
1 parent e23c92b commit 227aa17

File tree

12 files changed

+289
-177
lines changed

12 files changed

+289
-177
lines changed

packages/mcl/src/src/mcl/commands/ci_matrix.d

Lines changed: 95 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ GitHubOS getGHOS(string os)
6363
return os in osMap ? osMap[os] : GitHubOS.selfHosted;
6464
}
6565

66+
void assertGHOS(string input, GitHubOS expected)
67+
{
68+
auto actual = getGHOS(input);
69+
assert(actual == expected, fmt("getGHOS(\"%s\") should return %s, but returned %s", input, expected, actual));
70+
}
71+
6672
@("getGHOS")
6773
unittest
6874
{
69-
assert(getGHOS("ubuntu-latest") == GitHubOS.ubuntuLatest);
70-
assert(getGHOS("macos-14") == GitHubOS.macos14);
71-
assert(getGHOS("crazyos-inator-2000") == GitHubOS.selfHosted);
75+
assertGHOS("ubuntu-latest", GitHubOS.ubuntuLatest);
76+
assertGHOS("macos-14", GitHubOS.macos14);
77+
assertGHOS("crazyos-inator-2000", GitHubOS.selfHosted);
7278
}
7379

7480
immutable SupportedSystem[string] systemMap;
@@ -93,13 +99,19 @@ SupportedSystem getSystem(string system)
9399
return system in systemMap ? systemMap[system] : SupportedSystem.x86_64_linux;
94100
}
95101

102+
void assertSystem(string input, SupportedSystem expected)
103+
{
104+
auto actual = getSystem(input);
105+
assert(actual == expected, fmt("getSystem(\"%s\") should return %s, but returned %s", input, expected, actual));
106+
}
107+
96108
@("getSystem")
97109
unittest
98110
{
99-
assert(getSystem("x86_64-linux") == SupportedSystem.x86_64_linux);
100-
assert(getSystem("x86_64-darwin") == SupportedSystem.x86_64_darwin);
101-
assert(getSystem("aarch64-darwin") == SupportedSystem.aarch64_darwin);
102-
assert(getSystem("bender-bending-rodriguez-os") == SupportedSystem.x86_64_linux);
111+
assertSystem("x86_64-linux", SupportedSystem.x86_64_linux);
112+
assertSystem("x86_64-darwin", SupportedSystem.x86_64_darwin);
113+
assertSystem("aarch64-darwin", SupportedSystem.aarch64_darwin);
114+
assertSystem("bender-bending-rodriguez-os", SupportedSystem.x86_64_linux);
103115
}
104116

105117
struct Package
@@ -246,12 +258,18 @@ struct Params
246258
GitHubOS systemToGHPlatform(SupportedSystem os) =>
247259
os == SupportedSystem.x86_64_linux ? GitHubOS.selfHosted : GitHubOS.macos14;
248260

261+
void assertGHPlatform(SupportedSystem system, GitHubOS expected)
262+
{
263+
auto actual = systemToGHPlatform(system);
264+
assert(actual == expected, fmt("`systemToGHPlatform(%s)` should return `%s`, but returned `%s`", system, expected, actual));
265+
}
266+
249267
@("systemToGHPlatform")
250268
unittest
251269
{
252-
assert(systemToGHPlatform(SupportedSystem.x86_64_linux) == GitHubOS.selfHosted);
253-
assert(systemToGHPlatform(SupportedSystem.x86_64_darwin) == GitHubOS.macos14);
254-
assert(systemToGHPlatform(SupportedSystem.aarch64_darwin) == GitHubOS.macos14);
270+
assertGHPlatform(SupportedSystem.x86_64_linux, GitHubOS.selfHosted);
271+
assertGHPlatform(SupportedSystem.x86_64_darwin, GitHubOS.macos14);
272+
assertGHPlatform(SupportedSystem.aarch64_darwin, GitHubOS.macos14);
255273
}
256274

257275
static immutable string[] uselessWarnings =
@@ -453,7 +471,9 @@ int getNixEvalWorkerCount()
453471
@("getNixEvalWorkerCount")
454472
unittest
455473
{
456-
assert(getNixEvalWorkerCount() == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS));
474+
auto actual = getNixEvalWorkerCount();
475+
assert(actual == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS),
476+
"getNixEvalWorkerCount() should return the number of threads per CPU if it is less than MAX_WORKERS, otherwise it should return MAX_WORKERS, but returned %s".fmt(actual));
457477
}
458478

459479
string[] meminfo;
@@ -485,11 +505,13 @@ unittest
485505
{
486506
// Test when params.maxMemory is 0
487507
params.maxMemory = 0;
488-
assert(getAvailableMemoryMB() > 0);
508+
auto actual = getAvailableMemoryMB();
509+
assert(actual > 0, "getAvailableMemoryMB() should return a value greater than 0, but returned %s".fmt(actual));
489510

490511
// Test when params.maxMemory is not 0
491512
params.maxMemory = 1024;
492-
assert(getAvailableMemoryMB() == 1024);
513+
actual = getAvailableMemoryMB();
514+
assert(actual == 1024, "getAvailableMemoryMB() should return 1024, but returned %s".fmt(actual));
493515
}
494516

495517
void saveCachixDeploySpec(Package[] packages)
@@ -512,8 +534,14 @@ unittest
512534
createResultDirs();
513535
saveCachixDeploySpec(cast(Package[]) testPackageArray);
514536
JSONValue deploySpec = parseJSON(resultDir.buildPath("cachix-deploy-spec.json").readText);
515-
assert(testPackageArray[1].name == deploySpec[0]["package"].str);
516-
assert(testPackageArray[1].output == deploySpec[0]["out"].str);
537+
string testPackageName = testPackageArray[1].name;
538+
string deploySpecName = deploySpec[0]["package"].str;
539+
string testPackageOutput = testPackageArray[1].output;
540+
string deploySpecOutput = deploySpec[0]["out"].str;
541+
assert(testPackageName == deploySpecName,
542+
"The name of the package should be %s, but was %s".fmt(testPackageName, deploySpecName));
543+
assert(testPackageOutput == deploySpecOutput,
544+
"The output of the package should be %s, but was %s".fmt(testPackageOutput, deploySpecOutput));
517545
}
518546

519547
void saveGHCIMatrix(Package[] packages)
@@ -548,7 +576,10 @@ unittest
548576
.parseJSON;
549577
foreach (i, pkg; testPackageArray)
550578
{
551-
assert(pkg.name == matrix["include"][i]["name"].str);
579+
string pkgName = pkg.name;
580+
string matrixName = matrix["include"][i]["name"].str;
581+
assert(pkgName == matrixName,
582+
"The name of the package should be %s, but was %s".fmt(pkgName, matrixName));
552583
}
553584
}
554585

@@ -589,10 +620,14 @@ unittest
589620
string comment = rootDir.buildPath("comment.md").readText;
590621
foreach (pkg; testSummaryTableEntryArray)
591622
{
592-
assert(comment.indexOf(pkg.name) != -1);
593-
assert(comment.indexOf(pkg.x86_64.linux) != -1);
594-
assert(comment.indexOf(pkg.x86_64.darwin) != -1);
595-
assert(comment.indexOf(pkg.aarch64.darwin) != -1);
623+
assert(comment.indexOf(pkg.name) != -1,
624+
"The comment should contain the package name %s, the comment is:\n%s".fmt(pkg.name, comment));
625+
assert(comment.indexOf(pkg.x86_64.linux) != -1,
626+
"The comment should contain the x86_64 linux status %s, the comment is:\n%s".fmt(pkg.x86_64.linux, comment));
627+
assert(comment.indexOf(pkg.x86_64.darwin) != -1,
628+
"The comment should contain the x86_64 darwin status %s, the comment is:\n%s".fmt(pkg.x86_64.darwin, comment));
629+
assert(comment.indexOf(pkg.aarch64.darwin) != -1,
630+
"The comment should contain the aarch64 darwin status %s, the comment is:\n%s".fmt(pkg.aarch64.darwin, comment));
596631
}
597632
}
598633

@@ -650,34 +685,39 @@ SummaryTableEntry createSummaryTableEntry(const(Package)[] group, bool isInitial
650685
return entry;
651686
}
652687

688+
void assertNixTable(SummaryTableEntry[] tableSummary, immutable(Package[]) testPackageArray, int index,
689+
string expectedLinuxStatus = "[" ~ Status.cached ~ "](https://testPackage.com)", string expectedDarwinStatus = Status.notSupported, string expectedAarch64Status = Status.notSupported)
690+
{
691+
string actualName = tableSummary[index].name;
692+
string expectedName = testPackageArray[index].name;
693+
assert(actualName == expectedName, fmt("Expected name to be %s, but got %s", expectedName, actualName));
694+
695+
string actualLinuxStatus = tableSummary[index].x86_64.linux;
696+
assert(actualLinuxStatus == expectedLinuxStatus, fmt("Expected Linux status to be %s, but got %s", expectedLinuxStatus, actualLinuxStatus));
697+
698+
string actualDarwinStatus = tableSummary[index].x86_64.darwin;
699+
assert(actualDarwinStatus == expectedDarwinStatus, fmt("Expected Darwin status to be %s, but got %s", expectedDarwinStatus, actualDarwinStatus));
700+
701+
string actualAarch64Status = tableSummary[index].aarch64.darwin;
702+
assert(actualAarch64Status == expectedAarch64Status, fmt("Expected Aarch64 Darwin status to be %s, but got %s", expectedAarch64Status, actualAarch64Status));
703+
}
704+
653705
@("convertNixEvalToTableSummary/getStatus")
654706
unittest
655707
{
656708
auto tableSummary = convertNixEvalToTableSummary(
657709
testPackageArray,
658710
isInitial: false
659711
);
660-
assert(tableSummary[0].name == testPackageArray[0].name);
661-
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
662-
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
663-
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
664-
assert(tableSummary[1].name == testPackageArray[1].name);
665-
assert(tableSummary[1].x86_64.linux == Status.notSupported);
666-
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
667-
assert(tableSummary[1].aarch64.darwin == Status.buildFailed);
712+
assertNixTable(tableSummary, testPackageArray, 0);
713+
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.buildFailed);
668714

669715
tableSummary = convertNixEvalToTableSummary(
670716
testPackageArray,
671717
isInitial: true
672718
);
673-
assert(tableSummary[0].name == testPackageArray[0].name);
674-
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
675-
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
676-
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
677-
assert(tableSummary[1].name == testPackageArray[1].name);
678-
assert(tableSummary[1].x86_64.linux == Status.notSupported);
679-
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
680-
assert(tableSummary[1].aarch64.darwin == Status.building);
719+
assertNixTable(tableSummary, testPackageArray, 0);
720+
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.building);
681721
}
682722

683723
void printTableForCacheStatus(Package[] packages)
@@ -733,12 +773,11 @@ unittest
733773
cacheUrl: nixosCacheEndpoint ~ storePathHash ~ ".narinfo",
734774
};
735775

736-
assert(!testPackage.isCached);
737-
assert(checkPackage(testPackage).isCached);
776+
assert(checkPackage(testPackage).isCached, "Package %s should be cached".fmt(testPackage.cacheUrl));
738777

739778
testPackage.cacheUrl = nixosCacheEndpoint ~ "nonexistent.narinfo";
740779

741-
assert(!checkPackage(testPackage).isCached);
780+
assert(!checkPackage(testPackage).isCached, "Package %s should not be cached".fmt(testPackage.cacheUrl));
742781
}
743782

744783
Package[] getPrecalcMatrix()
@@ -770,13 +809,23 @@ unittest
770809
string precalcMatrixStr = "{\"include\": [{\"name\": \"test\", \"allowedToFail\": false, \"attrPath\": \"test\", \"cacheUrl\": \"url\", \"isCached\": true, \"os\": \"linux\", \"system\": \"x86_64-linux\", \"output\": \"output\"}]}";
771810
params.precalcMatrix = precalcMatrixStr;
772811
auto packages = getPrecalcMatrix();
812+
Package testPackage = {
813+
name: "test",
814+
allowedToFail: false,
815+
attrPath: "test",
816+
cacheUrl: "url",
817+
isCached: true,
818+
os: GitHubOS.selfHosted,
819+
system: SupportedSystem.x86_64_linux,
820+
output: "output"
821+
};
773822
assert(packages.length == 1);
774-
assert(packages[0].name == "test");
775-
assert(!packages[0].allowedToFail);
776-
assert(packages[0].attrPath == "test");
777-
assert(packages[0].cacheUrl == "url");
823+
assert(packages[0].name == testPackage.name, "Expected %s, got %s".fmt(testPackage.name, packages[0].name));
824+
assert(!packages[0].allowedToFail, "Expected %s, got %s".fmt(testPackage.allowedToFail, packages[0].allowedToFail));
825+
assert(packages[0].attrPath == testPackage.attrPath, "Expected %s, got %s".fmt(testPackage.attrPath, packages[0].attrPath));
826+
assert(packages[0].cacheUrl == testPackage.cacheUrl, "Expected %s, got %s".fmt(testPackage.cacheUrl, packages[0].cacheUrl));
778827
assert(packages[0].isCached);
779-
assert(packages[0].os == GitHubOS.selfHosted);
780-
assert(packages[0].system == SupportedSystem.x86_64_linux);
781-
assert(packages[0].output == "output");
828+
assert(packages[0].os == testPackage.os, "Expected %s, got %s".fmt(testPackage.os, packages[0].os));
829+
assert(packages[0].system == testPackage.system, "Expected %s, got %s".fmt(testPackage.system, packages[0].system));
830+
assert(packages[0].output == testPackage.output, "Expected %s, got %s".fmt(testPackage.output, packages[0].output));
782831
}

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import std.range : iota;
1111
import std.regex : matchFirst, regex;
1212
import std.stdio : writeln;
1313
import std.string : strip;
14+
import std.regex : matchFirst, regex;
15+
import std.format : format;
16+
import std.algorithm : each;
17+
import std.parallelism : parallel;
1418

1519
import mcl.utils.env : parseEnv, optional;
1620
import mcl.utils.json : toJSON;
@@ -94,11 +98,19 @@ unittest
9498

9599
auto shards = generateShardMatrix(flakeRef);
96100
assert(shards.include.length == 11);
97-
assert(shards.include[0].prefix == "legacyPackages");
98-
assert(shards.include[0].postfix == "mcl.matrix.shards.0");
99-
assert(shards.include[0].digit == 0);
101+
foreach(i, shard; shards.include.parallel)
102+
assertShard(shard, i.to!int);
103+
}
104+
105+
void assertShard(Shard shard, int index) {
106+
string expectedPrefix = index == -1 ? "" : "legacyPackages";
107+
string expectedPostfix = index == -1 ? "" : ("mcl.matrix.shards." ~ index.to!string);
108+
assert(shard.prefix == expectedPrefix, "Expected shard %s to have prefix '%s', but got %s".format(index, expectedPrefix, shard.prefix));
109+
assert(shard.postfix == expectedPostfix, "Expected shard %s to have postfix '%s', but got %s".format(index, expectedPostfix, shard.postfix));
110+
assert(shard.digit == index, "Expected shard %s to have digit %s, but got %s".format(index, index, shard.digit));
100111
}
101112

113+
102114
@("generateShardMatrix.fail")
103115
unittest
104116
{
@@ -108,10 +120,8 @@ unittest
108120
"packages/mcl/src/src/mcl/utils/test/nix/shard-matrix-no-shards");
109121

110122
auto shards = generateShardMatrix(flakeRef);
111-
assert(shards.include.length == 1);
112-
assert(shards.include[0].prefix == "");
113-
assert(shards.include[0].postfix == "");
114-
assert(shards.include[0].digit == -1);
123+
assert(shards.include.length == 1, "generateShardMatrix should return 1 shard, but got %s".format(shards.include.length));
124+
assertShard(shards.include[0], -1);
115125
}
116126

117127
ShardMatrix splitToShards(int shardCount)
@@ -129,16 +139,9 @@ ShardMatrix splitToShards(int shardCount)
129139
unittest
130140
{
131141
auto shards = splitToShards(3);
132-
assert(shards.include.length == 3);
133-
assert(shards.include[0].prefix == "legacyPackages");
134-
assert(shards.include[0].postfix == "mcl.matrix.shards.0");
135-
assert(shards.include[0].digit == 0);
136-
assert(shards.include[1].prefix == "legacyPackages");
137-
assert(shards.include[1].postfix == "mcl.matrix.shards.1");
138-
assert(shards.include[1].digit == 1);
139-
assert(shards.include[2].prefix == "legacyPackages");
140-
assert(shards.include[2].postfix == "mcl.matrix.shards.2");
141-
assert(shards.include[2].digit == 2);
142+
assert(shards.include.length == 3, "Expected splitToShards(3) to return 3 shards, but got %s".format(shards.include.length));
143+
foreach(i, shard; shards.include.parallel)
144+
assertShard(shard, i.to!int);
142145

143146
}
144147

packages/mcl/src/src/mcl/utils/array.d

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ T[] uniqIfSame(T)(T[] arr)
1919
@("uniqIfSame")
2020
unittest
2121
{
22-
assert(uniqIfSame([1, 1, 1, 1]) == [1]);
23-
assert(uniqIfSame([1, 2, 3, 4]) == [1, 2, 3, 4]);
24-
assert(uniqIfSame(["a", "a", "a", "a"]) == ["a"]);
25-
assert(uniqIfSame(["a", "b", "c", "d"]) == ["a", "b", "c", "d"]);
22+
assert(uniqIfSame([1, 1, 1, 1]) == [1],
23+
"uniqIfSame should return [1] for [1, 1, 1, 1], but got " ~ uniqIfSame([1, 1, 1, 1]).to!string);
24+
assert(uniqIfSame([1, 2, 3, 4]) == [1, 2, 3, 4],
25+
"uniqIfSame should return [1, 2, 3, 4] for [1, 2, 3, 4], but got " ~ uniqIfSame([1, 2, 3, 4]).to!string);
26+
assert(uniqIfSame(["a", "a", "a", "a"]) == ["a"],
27+
"uniqIfSame should return [\"a\"] for [\"a\", \"a\", \"a\", \"a\"], but got " ~ uniqIfSame(["a", "a", "a", "a"]).to!string);
28+
assert(uniqIfSame(["a", "b", "c", "d"]) == ["a", "b", "c", "d"],
29+
"uniqIfSame should return [\"a\", \"b\", \"c\", \"d\"] for [\"a\", \"b\", \"c\", \"d\"], but got " ~ uniqIfSame(["a", "b", "c", "d"]).to!string);
2630
}
2731

2832
T uniqArrays(T)(T s)
@@ -39,16 +43,17 @@ T uniqArrays(T)(T s)
3943
@("uniqArrays")
4044
unittest
4145
{
42-
assert(uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]) == [1, 2, 3, 4]);
43-
assert(uniqArrays("aabbccdd") == "aabbccdd");
44-
assert(uniqArrays(5) == 5);
46+
assert(uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]) == [1, 2, 3, 4],
47+
"uniqArrays should return [1, 2, 3, 4] for [1, 2, 3, 4, 1, 2, 3, 4], but got " ~ uniqArrays([1, 2, 3, 4, 1, 2, 3, 4]).to!string);
48+
assert(uniqArrays("aabbccdd") == "aabbccdd",
49+
"uniqArrays should return \"aabbccdd\" for \"aabbccdd\", but got " ~ uniqArrays("aabbccdd").to!string);
50+
assert(uniqArrays(5) == 5, "uniqArrays should return 5 for 5, but got " ~ uniqArrays(5).to!string);
4551
struct TestStruct
4652
{
4753
int[] a;
4854
string b;
4955
}
5056

51-
assert(uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")) == TestStruct([
52-
1, 2, 3, 4
53-
], "aabbccdd"));
57+
assert(uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")) == TestStruct([1, 2, 3, 4], "aabbccdd"),
58+
"uniqArrays should return TestStruct([1, 2, 3, 4], \"aabbccdd\") for TestStruct([1, 2, 3, 4, 1, 2, 3, 4], \"aabbccdd\"), but got " ~ uniqArrays(TestStruct([1, 2, 3, 4, 1, 2, 3, 4], "aabbccdd")).to!string);
5459
}

packages/mcl/src/src/mcl/utils/cachix.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ in (workspace && machine && deploymentId) =>
1919
unittest
2020
{
2121
assert(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123) ==
22-
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123");
22+
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123",
23+
"getCachixDeploymentApiUrl(\"my-workspace\", \"my-machine\", 123) should return \"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123\", but returned %s"
24+
.fmt(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123)));
2325

2426
}
2527

0 commit comments

Comments
 (0)