Luce packages
LuceLuce BaseLuciaOS

dymokomi/luc

Source of release 0.24.1

0.24.1 / src / package_tests.lucb

## `luc test`: the `test` blocks of every module of the package, each run by the compiler of
## its language, and then the package's test programs (test_programs). The Luce modules go
## to `luce test --package` and the Base modules to `luce-base test --package`, whatever the
## package's language, so a mixed package runs both halves. Each run starts from the
## package's entry when it is in that language and otherwise from its first module, since
## `--package` takes every module under the source root either way. A package with no test
## at all is refused: every package has tests.
import c
import io
import memory
import strings
from luce_std import files, paths, process
import test_programs

## What `luc test` was asked to test, as main loaded it.
pub struct Request:
    ## The project's root directory.
    pub var root: str
    ## The source root under it, `src` unless the manifest says otherwise.
    pub var source: str
    ## The entry's path, or empty for a library with no starter module.
    pub var entry: str
    ## The two compilers.
    pub var luce: str
    pub var base: str
    ## `--diagnostic`: the diagnostic profile, which builds the tests.
    pub var diagnostic: bool
    ## What followed `--`, given to every compiler run.
    pub var passthrough: str[]
    ## `--list`: name the tests instead of running them.
    pub var list: bool

## The modules a run can start from and the tests the sources declare: the first Luce
## module and the first Base module under the source root, a `.lucb` file or a directory
## module (one with an ORDER), as full paths the caller releases; empty when there is none.
struct Sources:
    var luce: str
    var base: str
    var declared: u64

    func release():
        if self.luce.length > 0: strings.release(self.luce)
        if self.base.length > 0: strings.release(self.base)

## One compiler's run: its status and the counts its report ended with.
struct Outcome:
    var status: i32
    var passed: u64
    var failed: u64

## Test the package and answer the exit status: the worst of the runs; 1 when the sources
## declare tests and no run reported any, or when the package has no test at all.
pub func run(request: Request) -> i32!:
    if request.list: return try list(request)
    var sources = try scan(request.root, request.source)
    defer sources.release()
    # each compiler starts from the entry when it is in its language, else its first module
    let luce_start = request.entry if strings.ends_with(request.entry, ".luc") else sources.luce
    let base_start = request.entry if strings.ends_with(request.entry, ".lucb") else sources.base
    var total = Outcome(status = 0, passed = 0, failed = 0)
    var runs: usize = 0
    for start in [luce_start, base_start]:
        if start.length == 0: continue
        let compiler = request.luce if strings.ends_with(start, ".luc") else request.base
        let outcome = try test_with(compiler, start, request)
        if outcome.status != 0 and total.status == 0: total.status = outcome.status
        total.passed += outcome.passed
        total.failed += outcome.failed
        runs += 1
    # a report of no tests from sources that declare some means a run missed them
    if total.status == 0 and total.passed + total.failed == 0 and sources.declared > 0:
        try io.stderr().write(f"luc test: the sources declare {sources.declared} test(s) but none ran\n")
        return 1
    let blocks = total.passed + total.failed
    let programs = try test_programs.run(test_programs.Settings(root = request.root,
        luce = request.luce, base = request.base, diagnostic = request.diagnostic))
    let program_count = programs.passed + programs.failed + programs.skipped
    if programs.failed > 0 and total.status == 0: total.status = 1
    total.passed += programs.passed
    total.failed += programs.failed
    if blocks + program_count == 0 and total.status == 0:
        try io.stderr().write("luc test: no tests found: add a `test` block or a tests/<name>/main program\n")
        return 1
    var skipped_text: u8[48]
    let skipped = try strings.format(skipped_text, f", {programs.skipped} skipped") if programs.skipped > 0 else ""
    # one total when more than one run contributed
    if runs > 1 or (runs > 0 and program_count > 0):
        let block_word = "test block" if blocks == 1 else "test blocks"
        let program_word = "program" if program_count == 1 else "programs"
        try io.stdout().write(f"total: {total.passed} passed, {total.failed} failed{skipped} ({blocks} {block_word}, {program_count} {program_word})\n")
    elif program_count > 0:
        try io.stdout().write(f"{programs.passed} passed\n")
        if programs.failed > 0: try io.stdout().write(f"{programs.failed} failed\n")
        if programs.skipped > 0: try io.stdout().write(f"{programs.skipped} skipped\n")
    return total.status

## `luc test --list`: the test programs, then the test blocks the sources declare, one per
## line, without building anything.
func list(request: Request) -> i32!:
    try test_programs.list(request.root)
    let joined = try paths.join(request.root, request.source)
    defer joined.release()
    if not files.exists((c.str)joined.value): return 0
    let listing = try files.walk((c.str)joined.value)
    defer listing.release()
    for relative in listing.value:
        if hidden(relative) or try in_nested_package(joined.value, relative): continue
        if not strings.ends_with(relative, ".luc") and not strings.ends_with(relative, ".lucb"): continue
        let path = try paths.join(joined.value, relative)
        defer path.release()
        _ = try visit_tests(path.value, true)
    return 0

## Run `compiler test start --package` with the request's profile and arguments, show what
## it wrote, and read the counts from its report.
func test_with(compiler: str, start: str, request: Request) -> Outcome!:
    var words: str[8] = ---
    var count: usize = 0
    for word in ["test", start, "--package"]:
        words[count] = word
        count += 1
    if request.diagnostic:
        words[count] = "--profile"
        words[count + 1] = "diagnostic"
        count += 2
    let argv = try new c.str[count + request.passthrough.length] ---
    var made: usize = 0
    defer release_arguments(argv, made)
    for i in 0..<count:
        argv[made] = (c.str)try strings.copy(words[i])
        made += 1
    for word in request.passthrough:
        argv[made] = (c.str)try strings.copy(word)
        made += 1
    let program = try strings.copy(compiler)
    defer strings.release(program)
    let ran = try process.run((c.str)program, argv)
    defer ran.release()
    var outcome = Outcome(status = ran.value.exit_code, passed = 0, failed = 0)
    read_counts(ran.value.output, &outcome)
    # a run that found no test block (the Base half of a Luce package with none, say) has
    # nothing to show but its `0 passed`: luc's total speaks for it
    let empty = outcome.status == 0 and outcome.passed + outcome.failed == 0
    if ran.value.output.length > 0 and not empty: try io.stdout().write(ran.value.output)
    if ran.value.error_output.length > 0: try io.stderr().write(ran.value.error_output)
    return outcome

func release_arguments(arguments: c.str[], count: usize):
    for i in 0..<count: strings.release((str)arguments[i])
    if arguments.length > 0: free(arguments) in memory.heap

## The report's closing counts, the lines `N passed` and `N failed`; the last of each wins.
func read_counts(output: str, outcome: Outcome*):
    var start: usize = 0
    while start < output.length:
        let end = strings.find_from(output, "\n", start) else output.length
        let line = (str)output.bytes[start..<end]
        start = end + 1
        let (number, word) = strings.split_once(line, " ") else continue
        let value = strings.parse_u64(number) else continue
        if word == "passed": outcome.passed = value
        elif word == "failed": outcome.failed = value

# mark: The sources ============================================================================

## The package's modules under `root`/`source`, in the order of their paths, and how many
## tests they declare. A hidden directory and one holding a package of its own are left out,
## as the compilers leave them out of `--package`.
func scan(root: str, source: str) -> Sources!:
    var sources = Sources(luce = "", base = "", declared = 0)
    errdefer sources.release()
    let joined = try paths.join(root, source)
    defer joined.release()
    let directory = try strings.copy(joined.value)
    defer strings.release(directory)
    if not files.exists((c.str)directory): return sources
    let listing = try files.walk((c.str)directory)
    defer listing.release()
    for relative in listing.value:
        if hidden(relative) or try in_nested_package(directory, relative): continue
        let path = try paths.join(directory, relative)
        defer path.release()
        let luce_module = strings.ends_with(relative, ".luc")
        let base_file = strings.ends_with(relative, ".lucb")
        if luce_module or base_file:
            sources.declared += try declared_tests(path.value)
        if luce_module and sources.luce.length == 0:
            sources.luce = try strings.copy(path.value)
        # the walk lists a directory before its files, so a directory module comes first
        let base_module = base_file or try has_file(path.value, "ORDER")
        if base_module and sources.base.length == 0 and not try in_ancestor(directory, relative, "ORDER"):
            sources.base = try strings.copy(path.value)
    return sources

## Whether a part of `relative` starts with a dot.
func hidden(relative: str) -> bool:
    if relative.length > 0 and relative.bytes[0] == '.': return true
    return strings.contains(relative, "/.")

## Whether a directory between `directory` and the entry `relative` has a package.prisma:
## that package's modules are its own.
func in_nested_package(directory: str, relative: str) -> bool!:
    return try in_ancestor(directory, relative, "package.prisma")

## Whether a directory between `directory` and the entry `relative`, not counting `relative`
## itself, holds a file `name`: an ORDER makes everything below it part of one module.
func in_ancestor(directory: str, relative: str, name: str) -> bool!:
    for i in 0..<relative.length:
        if relative.bytes[i] != '/': continue
        let below = try paths.join(directory, (str)relative.bytes[..<i])
        defer below.release()
        if try has_file(below.value, name): return true
    return false

func has_file(directory: str, name: str) -> bool!:
    let joined = try paths.join(directory, name)
    defer joined.release()
    let terminated = try strings.copy(joined.value)
    defer strings.release(terminated)
    return files.exists((c.str)terminated)

## How many top-level `test "..."` declarations the source at `path` holds.
func declared_tests(path: str) -> u64!:
    return try visit_tests(path, false)

## The top-level `test "..."` declarations of the source at `path`, counted, and with
## `show` written out by name, one per line.
func visit_tests(path: str, show: bool) -> u64!:
    let terminated = try strings.copy(path)
    defer strings.release(terminated)
    let contents = try files.read((c.str)terminated)
    defer contents.release()
    let text = (str)contents.value
    var count: u64 = 0
    var start: usize = 0
    while start < text.length:
        let end = strings.find_from(text, "\n", start) else text.length
        let line = (str)text.bytes[start..<end]
        if strings.starts_with(line, "test \""):
            count += 1
            if show:
                let named = (str)line.bytes[6..]
                let close = strings.find_last(named, "\"") else named.length
                try io.stdout().write(f"{(str)named.bytes[..<close]}\n")
        start = end + 1
    return count

test "the last counts of a report win":
    var outcome = Outcome(status = 0, passed = 0, failed = 0)
    read_counts("ok    a\n1 passed\nFAIL  b\n2 passed\n1 failed\n", &outcome)
    assert(outcome.passed == 2 and outcome.failed == 1)

test "a hidden path has a part starting with a dot":
    assert(hidden(".git/x.lucb") and hidden("a/.cache/b.lucb") and not hidden("a/b.lucb"))