|
root / test / script_cmd_test.reef
script_cmd_test.reef Reef 60 lines 2.4 KB
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Unit tests for package.build_script_cmd — asserts exact command strings.
import core.package
import core.str

mut g_fail = 0

proc expect(label: string, got: string, want: string)
    if str.equals(got, want)
        println("PASS: " + label)
    else
        g_fail = g_fail + 1
        println("FAIL: " + label)
        println("   got:  " + got)
        println("   want: " + want)
    end if
end expect

proc main()
    println("=== build_script_cmd tests ===")

    // Normal install: root "/", no chroot -> CORAL_ROOT="/" prefix
    let normal = package.new_script_ctx("/", false)
    expect("normal .sh",
        package.build_script_cmd(normal, "/tmp/x/.SCRIPTS/post-install.sh", "post-install.sh", "\"1.0\"", true),
        "CORAL_ROOT=\"/\" zsh \"/tmp/x/.SCRIPTS/post-install.sh\" \"1.0\"")

    // Host-fallback: root "/mnt", do_chroot false -> CORAL_ROOT="/mnt" prefix, host path
    let host = package.new_script_ctx("/mnt", false)
    expect("host-fallback .sh",
        package.build_script_cmd(host, "/tmp/x/.SCRIPTS/post-install.sh", "post-install.sh", "\"1.0\"", true),
        "CORAL_ROOT=\"/mnt\" zsh \"/tmp/x/.SCRIPTS/post-install.sh\" \"1.0\"")

    // Bare executable (is_sh false): no zsh wrapper
    expect("normal bare",
        package.build_script_cmd(normal, "/tmp/x/.SCRIPTS/post-install", "post-install", "\"1.0\"", false),
        "CORAL_ROOT=\"/\" \"/tmp/x/.SCRIPTS/post-install\" \"1.0\"")

    // Empty args: no trailing space
    expect("normal no-args",
        package.build_script_cmd(normal, "/tmp/x/.SCRIPTS/pre-remove.sh", "pre-remove.sh", "", true),
        "CORAL_ROOT=\"/\" zsh \"/tmp/x/.SCRIPTS/pre-remove.sh\"")

    // Confined: root "/mnt", do_chroot true -> chroot prefix, in-root /.SCRIPTS path, CORAL_ROOT="/"
    let jail = package.new_script_ctx("/mnt", true)
    expect("chroot .sh",
        package.build_script_cmd(jail, "/tmp/x/.SCRIPTS/post-install.sh", "post-install.sh", "\"1.0\"", true),
        "chroot \"/mnt\" env CORAL_ROOT=\"/\" zsh \"/.SCRIPTS/post-install.sh\" \"1.0\"")

    expect("chroot bare",
        package.build_script_cmd(jail, "/tmp/x/.SCRIPTS/post-install", "post-install", "\"1.0\"", false),
        "chroot \"/mnt\" env CORAL_ROOT=\"/\" \"/.SCRIPTS/post-install\" \"1.0\"")

    println("")
    if g_fail == 0
        println("RESULT: ALL PASS")
    else
        println("RESULT: FAIL (" + str.from_int(g_fail) + " failures)")
    end if
end main