
LayoutTest.pudu
Pudu66 lines2.4 KB
1/** @Test.Package.LayoutTest.Suite — every shipped module stays under the package's module root */2module Package.LayoutTest34import Std.Io as Io5import Std.List as List6import Std.Result as Result7import Std.Test as Test8import Std.Text as Text91011const ROOT: Str = "PuduLangResilience"121314fn sources(directory: Str) -> Array[Str] {15 var found: Array[Str] = []16 for path in List.sortBy(&Result.unwrapOr(Io.listPaths(directory), []), fn(left: Str, right: Str) -> Bool { left < right }) {17 if path.endsWith(".pudu") {18 found = found.push(path)19 } else if !path.contains(".") {20 found = found.concat(sources(path))21 }22 }23 found24}252627fn declared(path: Str) -> Str {28 let text = Result.unwrapOr(Io.read(path), "")29 match List.find(&text.split("\n"), |line: Str| line.startsWith("module ")) {30 case Some(line) => Text.stripPrefix(line, "module ").trim()31 case None => ""32 }33}343536fn owned(name: Str) -> Bool {37 name == ROOT || name.startsWith(ROOT + ".")38}394041fn expected(path: Str) -> Str {42 Text.stripPrefix(path, "src/").replace(".pudu", "").replace("/", ".")43}444546fn main() -> Int {47 let shipped = sources("src")48 let outside = shipped.filter(|path: Str| !owned(declared(path)))49 let misnamed = shipped.filter(|path: Str| declared(path) != expected(path))50 let manifest = Result.unwrapOr(Io.read("pudu.toml"), "")51 let checks = Test.suite("Package.Layout", &[52 Test.that("the source tree is found", shipped.length() > 1),53 Test.equals("every shipped module is under the package root", &outside, &[]),54 Test.equals("every module is named after its path", &misnamed, &[]),55 Test.that("the manifest declares the same root", manifest.contains("root = \"" + ROOT + "\"")),56 Test.equals("the package root is the root module", &declared("src/PuduLangResilience.pudu"), &ROOT),57 Test.that("a name beside the root is refused", !owned("App.Context") && !owned("PuduLangResilienceExtra")),58 Test.that("the root and its children are owned", owned(ROOT) && owned(ROOT + ".App.Context"))59 ])60 let report = Test.run(&checks)61 for failure in Test.failuresOf(&report) {62 let _reported = Io.writeErrorLine(failure)63 }64 Test.report(&report)65}66