Skip to content

Commit 8c3a34a

Browse files
committed
unit tests
1 parent 3776ab9 commit 8c3a34a

File tree

12 files changed

+288
-180
lines changed

12 files changed

+288
-180
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
@@ -62,12 +62,18 @@ GitHubOS getGHOS(string os)
6262
return os in osMap ? osMap[os] : GitHubOS.selfHosted;
6363
}
6464

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

7379
immutable SupportedSystem[string] systemMap;
@@ -92,13 +98,19 @@ SupportedSystem getSystem(string system)
9298
return system in systemMap ? systemMap[system] : SupportedSystem.x86_64_linux;
9399
}
94100

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

104116
struct Package
@@ -229,12 +241,18 @@ struct Params
229241
GitHubOS systemToGHPlatform(SupportedSystem os) =>
230242
os == SupportedSystem.x86_64_linux ? GitHubOS.selfHosted : GitHubOS.macos14;
231243

244+
void assertGHPlatform(SupportedSystem system, GitHubOS expected)
245+
{
246+
auto actual = systemToGHPlatform(system);
247+
assert(actual == expected, fmt("`systemToGHPlatform(%s)` should return `%s`, but returned `%s`", system, expected, actual));
248+
}
249+
232250
@("systemToGHPlatform")
233251
unittest
234252
{
235-
assert(systemToGHPlatform(SupportedSystem.x86_64_linux) == GitHubOS.selfHosted);
236-
assert(systemToGHPlatform(SupportedSystem.x86_64_darwin) == GitHubOS.macos14);
237-
assert(systemToGHPlatform(SupportedSystem.aarch64_darwin) == GitHubOS.macos14);
253+
assertGHPlatform(SupportedSystem.x86_64_linux, GitHubOS.selfHosted);
254+
assertGHPlatform(SupportedSystem.x86_64_darwin, GitHubOS.macos14);
255+
assertGHPlatform(SupportedSystem.aarch64_darwin, GitHubOS.macos14);
238256
}
239257

240258
static immutable string[] uselessWarnings =
@@ -371,7 +389,9 @@ int getNixEvalWorkerCount()
371389
@("getNixEvalWorkerCount")
372390
unittest
373391
{
374-
assert(getNixEvalWorkerCount() == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS));
392+
auto actual = getNixEvalWorkerCount();
393+
assert(actual == (threadsPerCPU() < MAX_WORKERS ? threadsPerCPU() : MAX_WORKERS),
394+
"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));
375395
}
376396

377397
string[] meminfo;
@@ -403,11 +423,13 @@ unittest
403423
{
404424
// Test when params.maxMemory is 0
405425
params.maxMemory = 0;
406-
assert(getAvailableMemoryMB() > 0);
426+
auto actual = getAvailableMemoryMB();
427+
assert(actual > 0, "getAvailableMemoryMB() should return a value greater than 0, but returned %s".fmt(actual));
407428

408429
// Test when params.maxMemory is not 0
409430
params.maxMemory = 1024;
410-
assert(getAvailableMemoryMB() == 1024);
431+
actual = getAvailableMemoryMB();
432+
assert(actual == 1024, "getAvailableMemoryMB() should return 1024, but returned %s".fmt(actual));
411433
}
412434

413435
void saveCachixDeploySpec(Package[] packages)
@@ -430,8 +452,14 @@ unittest
430452
createResultDirs();
431453
saveCachixDeploySpec(cast(Package[]) testPackageArray);
432454
JSONValue deploySpec = parseJSON(resultDir.buildPath("cachix-deploy-spec.json").readText);
433-
assert(testPackageArray[1].name == deploySpec[0]["package"].str);
434-
assert(testPackageArray[1].output == deploySpec[0]["out"].str);
455+
string testPackageName = testPackageArray[1].name;
456+
string deploySpecName = deploySpec[0]["package"].str;
457+
string testPackageOutput = testPackageArray[1].output;
458+
string deploySpecOutput = deploySpec[0]["out"].str;
459+
assert(testPackageName == deploySpecName,
460+
"The name of the package should be %s, but was %s".fmt(testPackageName, deploySpecName));
461+
assert(testPackageOutput == deploySpecOutput,
462+
"The output of the package should be %s, but was %s".fmt(testPackageOutput, deploySpecOutput));
435463
}
436464

437465
void saveGHCIMatrix(Package[] packages)
@@ -466,7 +494,10 @@ unittest
466494
.parseJSON;
467495
foreach (i, pkg; testPackageArray)
468496
{
469-
assert(pkg.name == matrix["include"][i]["name"].str);
497+
string pkgName = pkg.name;
498+
string matrixName = matrix["include"][i]["name"].str;
499+
assert(pkgName == matrixName,
500+
"The name of the package should be %s, but was %s".fmt(pkgName, matrixName));
470501
}
471502
}
472503

@@ -507,10 +538,14 @@ unittest
507538
string comment = rootDir.buildPath("comment.md").readText;
508539
foreach (pkg; testSummaryTableEntryArray)
509540
{
510-
assert(comment.indexOf(pkg.name) != -1);
511-
assert(comment.indexOf(pkg.x86_64.linux) != -1);
512-
assert(comment.indexOf(pkg.x86_64.darwin) != -1);
513-
assert(comment.indexOf(pkg.aarch64.darwin) != -1);
541+
assert(comment.indexOf(pkg.name) != -1,
542+
"The comment should contain the package name %s, the comment is:\n%s".fmt(pkg.name, comment));
543+
assert(comment.indexOf(pkg.x86_64.linux) != -1,
544+
"The comment should contain the x86_64 linux status %s, the comment is:\n%s".fmt(pkg.x86_64.linux, comment));
545+
assert(comment.indexOf(pkg.x86_64.darwin) != -1,
546+
"The comment should contain the x86_64 darwin status %s, the comment is:\n%s".fmt(pkg.x86_64.darwin, comment));
547+
assert(comment.indexOf(pkg.aarch64.darwin) != -1,
548+
"The comment should contain the aarch64 darwin status %s, the comment is:\n%s".fmt(pkg.aarch64.darwin, comment));
514549
}
515550
}
516551

@@ -564,30 +599,35 @@ SummaryTableEntry createSummaryTableEntry(Package[] group)
564599
return entry;
565600
}
566601

602+
void assertNixTable(SummaryTableEntry[] tableSummary, immutable(Package[]) testPackageArray, int index,
603+
string expectedLinuxStatus = "[" ~ Status.cached ~ "](https://testPackage.com)", string expectedDarwinStatus = Status.notSupported, string expectedAarch64Status = Status.notSupported)
604+
{
605+
string actualName = tableSummary[index].name;
606+
string expectedName = testPackageArray[index].name;
607+
assert(actualName == expectedName, fmt("Expected name to be %s, but got %s", expectedName, actualName));
608+
609+
string actualLinuxStatus = tableSummary[index].x86_64.linux;
610+
assert(actualLinuxStatus == expectedLinuxStatus, fmt("Expected Linux status to be %s, but got %s", expectedLinuxStatus, actualLinuxStatus));
611+
612+
string actualDarwinStatus = tableSummary[index].x86_64.darwin;
613+
assert(actualDarwinStatus == expectedDarwinStatus, fmt("Expected Darwin status to be %s, but got %s", expectedDarwinStatus, actualDarwinStatus));
614+
615+
string actualAarch64Status = tableSummary[index].aarch64.darwin;
616+
assert(actualAarch64Status == expectedAarch64Status, fmt("Expected Aarch64 Darwin status to be %s, but got %s", expectedAarch64Status, actualAarch64Status));
617+
}
618+
567619
@("convertNixEvalToTableSummary/getStatus")
568620
unittest
569621
{
570622
auto tableSummary = convertNixEvalToTableSummary(cast(Package[]) testPackageArray);
571-
assert(tableSummary[0].name == testPackageArray[0].name);
572-
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
573-
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
574-
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
575-
assert(tableSummary[1].name == testPackageArray[1].name);
576-
assert(tableSummary[1].x86_64.linux == Status.notSupported);
577-
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
578-
assert(tableSummary[1].aarch64.darwin == Status.buildFailed);
623+
assertNixTable(tableSummary, testPackageArray, 0);
624+
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.buildFailed);
579625

580626
params.isInitial = true;
581627
tableSummary = convertNixEvalToTableSummary(cast(Package[]) testPackageArray);
582628
params.isInitial = false;
583-
assert(tableSummary[0].name == testPackageArray[0].name);
584-
assert(tableSummary[0].x86_64.linux == "[" ~ Status.cached ~ "](https://testPackage.com)");
585-
assert(tableSummary[0].x86_64.darwin == Status.notSupported);
586-
assert(tableSummary[0].aarch64.darwin == Status.notSupported);
587-
assert(tableSummary[1].name == testPackageArray[1].name);
588-
assert(tableSummary[1].x86_64.linux == Status.notSupported);
589-
assert(tableSummary[1].x86_64.darwin == Status.notSupported);
590-
assert(tableSummary[1].aarch64.darwin == Status.building);
629+
assertNixTable(tableSummary, testPackageArray, 0);
630+
assertNixTable(tableSummary, testPackageArray, 1, Status.notSupported, Status.notSupported, Status.building);
591631
}
592632

593633
void printTableForCacheStatus(Package[] packages)
@@ -640,12 +680,11 @@ unittest
640680
cacheUrl: nixosCacheEndpoint ~ storePathHash ~ ".narinfo",
641681
};
642682

643-
assert(!testPackage.isCached);
644-
assert(checkPackage(testPackage).isCached);
683+
assert(checkPackage(testPackage).isCached, "Package %s should be cached".fmt(testPackage.cacheUrl));
645684

646685
testPackage.cacheUrl = nixosCacheEndpoint ~ "nonexistent.narinfo";
647686

648-
assert(!checkPackage(testPackage).isCached);
687+
assert(!checkPackage(testPackage).isCached, "Package %s should not be cached".fmt(testPackage.cacheUrl));
649688
}
650689

651690
Package[] getPrecalcMatrix()
@@ -677,13 +716,23 @@ unittest
677716
string precalcMatrixStr = "{\"include\": [{\"name\": \"test\", \"allowedToFail\": false, \"attrPath\": \"test\", \"cacheUrl\": \"url\", \"isCached\": true, \"os\": \"linux\", \"system\": \"x86_64-linux\", \"output\": \"output\"}]}";
678717
params.precalcMatrix = precalcMatrixStr;
679718
auto packages = getPrecalcMatrix();
719+
Package testPackage = {
720+
name: "test",
721+
allowedToFail: false,
722+
attrPath: "test",
723+
cacheUrl: "url",
724+
isCached: true,
725+
os: GitHubOS.selfHosted,
726+
system: SupportedSystem.x86_64_linux,
727+
output: "output"
728+
};
680729
assert(packages.length == 1);
681-
assert(packages[0].name == "test");
682-
assert(!packages[0].allowedToFail);
683-
assert(packages[0].attrPath == "test");
684-
assert(packages[0].cacheUrl == "url");
730+
assert(packages[0].name == testPackage.name, "Expected %s, got %s".fmt(testPackage.name, packages[0].name));
731+
assert(!packages[0].allowedToFail, "Expected %s, got %s".fmt(testPackage.allowedToFail, packages[0].allowedToFail));
732+
assert(packages[0].attrPath == testPackage.attrPath, "Expected %s, got %s".fmt(testPackage.attrPath, packages[0].attrPath));
733+
assert(packages[0].cacheUrl == testPackage.cacheUrl, "Expected %s, got %s".fmt(testPackage.cacheUrl, packages[0].cacheUrl));
685734
assert(packages[0].isCached);
686-
assert(packages[0].os == GitHubOS.selfHosted);
687-
assert(packages[0].system == SupportedSystem.x86_64_linux);
688-
assert(packages[0].output == "output");
735+
assert(packages[0].os == testPackage.os, "Expected %s, got %s".fmt(testPackage.os, packages[0].os));
736+
assert(packages[0].system == testPackage.system, "Expected %s, got %s".fmt(testPackage.system, packages[0].system));
737+
assert(packages[0].output == testPackage.output, "Expected %s, got %s".fmt(testPackage.output, packages[0].output));
689738
}

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;
@@ -91,22 +95,28 @@ unittest
9195

9296
auto shards = generateShardMatrix(flakeRef);
9397
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);
98+
foreach(i, shard; shards.include.parallel)
99+
assertShard(shard, i.to!int);
100+
}
101+
102+
void assertShard(Shard shard, int index) {
103+
string expectedPrefix = index == -1 ? "" : "legacyPackages";
104+
string expectedPostfix = index == -1 ? "" : ("shards." ~ index.to!string);
105+
assert(shard.prefix == expectedPrefix, "Expected shard %s to have prefix '%s', but got %s".format(index, expectedPrefix, shard.prefix));
106+
assert(shard.postfix == expectedPostfix, "Expected shard %s to have postfix '%s', but got %s".format(index, expectedPostfix, shard.postfix));
107+
assert(shard.digit == index, "Expected shard %s to have digit %s, but got %s".format(index, index, shard.digit));
97108
}
98109

110+
99111
@("generateShardMatrix.fail")
100112
unittest
101113
{
102114
import mcl.utils.path : rootDir;
103115
auto flakeRef = rootDir.buildPath("packages/mcl/src/src/mcl/utils/test/nix/shard-matrix-no-shards");
104116

105117
auto shards = generateShardMatrix(flakeRef);
106-
assert(shards.include.length == 1);
107-
assert(shards.include[0].prefix == "");
108-
assert(shards.include[0].postfix == "");
109-
assert(shards.include[0].digit == -1);
118+
assert(shards.include.length == 1, "generateShardMatrix should return 1 shard, but got %s".format(shards.include.length));
119+
assertShard(shards.include[0], -1);
110120
}
111121

112122
ShardMatrix splitToShards(int shardCount)
@@ -124,16 +134,9 @@ ShardMatrix splitToShards(int shardCount)
124134
unittest
125135
{
126136
auto shards = splitToShards(3);
127-
assert(shards.include.length == 3);
128-
assert(shards.include[0].prefix == "legacyPackages");
129-
assert(shards.include[0].postfix == "shards.0");
130-
assert(shards.include[0].digit == 0);
131-
assert(shards.include[1].prefix == "legacyPackages");
132-
assert(shards.include[1].postfix == "shards.1");
133-
assert(shards.include[1].digit == 1);
134-
assert(shards.include[2].prefix == "legacyPackages");
135-
assert(shards.include[2].postfix == "shards.2");
136-
assert(shards.include[2].digit == 2);
137+
assert(shards.include.length == 3, "Expectes splitToShards(3) to return 3 shards, but got %s".format(shards.include.length));
138+
foreach(i, shard; shards.include.parallel)
139+
assertShard(shard, i.to!int);
137140

138141
}
139142

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
@@ -12,7 +12,9 @@ in (workspace && machine && deploymentId) =>
1212
unittest
1313
{
1414
assert(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123) ==
15-
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123");
15+
"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123",
16+
"getCachixDeploymentApiUrl(\"my-workspace\", \"my-machine\", 123) should return \"https://app.cachix.org/api/v1/deploy/deployment/my-workspace/my-machine/123\", but returned %s"
17+
.fmt(getCachixDeploymentApiUrl("my-workspace", "my-machine", 123)));
1618

1719
}
1820

0 commit comments

Comments
 (0)