summaryrefslogtreecommitdiff
path: root/tests/modules/stdin/test.lua
blob: 6b1b4026b8ddf5ce3915f92c009f72cd1ae1ba3a (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function resolve_path(p)
    if path.is_absolute(p) then return p end
    local root = path.join(os.scriptdir(), "../../..")
    local p_root = path.join(root, p)
    if os.isfile(p_root) then return path.absolute(p_root) end
    if os.isfile(p) then return path.absolute(p) end
    return p
end

-- Fix /usr/bin/ape loader mode on Linux
function fix_ape_programfile(xmake)
    if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then
        local content = io.readfile("/proc/self/cmdline")
        if content then
            local args = {}
            for arg in content:gmatch("[^\0]+") do
                table.insert(args, arg)
            end
            if #args >= 2 and path.unix(args[1]) == xmake then
                xmake = path.unix(args[2])
                xmake = resolve_path(xmake)
            end
        end
    end
    return xmake
end

function main(t)
    local xmake = path.unix(os.programfile())
    if is_host("windows") then
        xmake = xmake:gsub("/", "\\")
    end
    xmake = resolve_path(xmake)

    -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux
    xmake = fix_ape_programfile(xmake)

    local is_ape = path.filename(xmake):find("ape-", 1, true) ~= nil

    local run_stdin = string.format('env "%s" l --stdin', xmake)
     -- Fix pwsh and cosmocc "exec format error" for MacOS
     if is_ape and not is_host("windows") then
        run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake)
    end

    local function test_shell(name, cmd, expect)
        local outfile = os.tmpfile()
        local errfile = os.tmpfile()
        local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile)
        local ret = -1
        try({
            function()
                if not is_host("windows") then
                    ret = os.execv("sh", { "-c", full_cmd })
                else
                    ret = os.exec(full_cmd)
                end
            end,
        })
        local out = ""
        if os.isfile(outfile) then
            out = io.readfile(outfile)
            if out and out:find("\0", 1, true) then
                out = out:gsub("\0", "")
            end
        end
        local err = ""
        if os.isfile(errfile) then
            err = io.readfile(errfile)
        end
        local passed = out:find(expect)
        if passed then
            print("  -> passed")
        else
            print("  -> failed")
        end
        print("     out: " .. (out or ""))
        print("     err: " .. (err or ""))
        if not passed then
            raise("[test_stdin]: Test failed! Expect: ", expect)
        end
        os.tryrm(outfile)
        os.tryrm(errfile)
    end

    local pwsh = ""
    if is_host("windows") then
        -- Test cmd
        test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd")
        test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2")
        test_shell(
            "cmd_multi",
            string.format("cmd /c echo \"print('line1')\\nprint('line2')\" | %s l --stdin", xmake),
            "line1[\r\n]+line2"
        )
        -- Test powershell (if available)
        local pwsh = "powershell"
        try({
            function()
                if os.exec("pwsh -v") == 0 then
                    pwsh = "pwsh"
                end
            end,
        })
        test_shell(
            "pwsh_single",
            string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake),
            "hello_pwsh"
        )
        test_shell(
            "pwsh_calc",
            string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake),
            "2"
        )
        test_shell(
            "pwsh_multi",
            string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake),
            "pline1[\r\n]+pline2"
        )
    else
        -- Linux/MacOS
        local pwsh = ""
        try({
            function()
                os.iorun("pwsh -v")
                pwsh = "pwsh"
            end,
        })
        if pwsh == "" then
            try({
                function()
                    os.iorun("powershell -v")
                    pwsh = "powershell"
                end,
            })
        end

        if pwsh ~= "" then
            test_shell(
                "pwsh_single",
                string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin),
                "hello_pwsh"
            )
            test_shell(
                "pwsh_calc",
                string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin),
                "2"
            )
            test_shell(
                "pwsh_main",
                string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin),
                "in_pwsh_main"
            )
            test_shell(
                "pwsh_multi",
                string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin),
                "pline1[\r\n]+pline2"
            )
        end

        test_shell("sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh")
        test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2")
        test_shell(
            "sh_main",
            string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake),
            "in_sh_main"
        )
        test_shell(
            "sh_multi",
            string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake),
            "shell_line1[\r\n]+shell_line2"
        )
    end
end