aboutsummaryrefslogtreecommitdiff
path: root/scripts/test.lua
blob: df1218ce88ff7bbea0b4ae96f2ae972914b7dca5 (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
-- Copyright Epic Games, Inc. All Rights Reserved.

function main()
    import("core.base.option")
    import("core.project.config")
    import("core.project.project")

    config.load()

    -- Override table: target name -> short name (for targets that don't follow convention)
    local short_name_overrides = {
        ["zenserver-test"] = "integration",
    }

    -- Build test list from targets in the "tests" group
    local available_tests = {}
    for name, target in pairs(project.targets()) do
        if target:get("group") == "tests" and name:endswith("-test") then
            local short = short_name_overrides[name]
            if not short then
                -- Derive short name: "zencore-test" -> "core"
                short = name
                if short:startswith("zen") then short = short:sub(4) end
                if short:endswith("-test") then short = short:sub(1, -6) end
            end
            table.insert(available_tests, {short, name})
        end
    end

    -- Add non-test-group entries that have a test subcommand
    table.insert(available_tests, {"server", "zenserver"})

    table.sort(available_tests, function(a, b) return a[1] < b[1] end)

    -- Handle --list: print discovered test names and exit
    if option.get("list") then
        printf("Available tests:\n")
        for _, entry in ipairs(available_tests) do
            printf("  %-16s -> %s\n", entry[1], entry[2])
        end
        return
    end

    local testname = option.get("run")

    -- Parse comma-separated test names into a set
    local requested = {}
    for token in testname:gmatch("[^,]+") do
        requested[token:match("^%s*(.-)%s*$")] = true
    end

    -- Filter to requested test(s)
    local tests = {}
    local matched = {}

    for _, entry in ipairs(available_tests) do
        local name, target = entry[1], entry[2]
        if requested["all"] or requested[name] then
            table.insert(tests, {name = name, target = target})
            matched[name] = true
        end
    end

    -- Check for unknown test names
    if not requested["all"] then
        for name, _ in pairs(requested) do
            if not matched[name] then
                raise("no tests match specification: '%s'", name)
            end
        end
    end

    if #tests == 0 then
        raise("no tests match specification: '%s'", testname)
    end

    local plat, arch
    if is_host("windows") then
        plat = "windows"
        arch = "x64"
    elseif is_host("macosx") then
        plat = "macosx"
        arch = is_arch("arm64") and "arm64" or "x86_64"
    else
        plat = "linux"
        arch = "x86_64"
    end

    -- Only reconfigure if current config doesn't already match
    if config.get("mode") ~= "debug" or config.get("plat") ~= plat or config.get("arch") ~= arch then
        local toolchain_flag = config.get("toolchain") and ("--toolchain=" .. config.get("toolchain")) or ""
        local sdk_flag = config.get("sdk") and ("--sdk=" .. config.get("sdk")) or ""
        os.exec("xmake config -y -c -m debug -p %s -a %s %s %s", plat, arch, toolchain_flag, sdk_flag)
    end

    -- Build targets we're going to run
    if requested["all"] then
        os.exec("xmake build -y")
    else
        for _, entry in ipairs(tests) do
            os.exec("xmake build -y %s", entry.target)
        end
    end

    local use_junit_reporting = option.get("junit")
    local use_noskip = option.get("noskip")
    local use_verbose = option.get("verbose")
    local repeat_count = tonumber(option.get("repeat")) or 1
    local extra_args = option.get("arguments") or {}
    local junit_report_files = {}

    local junit_report_dir
    if use_junit_reporting then
        junit_report_dir = path.join(os.projectdir(), config.get("buildir"), "reports")
        os.mkdir(junit_report_dir)
    end

    -- Results collection for summary table
    local results = {}
    local any_failed = false

    -- Format a number with thousands separators (e.g. 31103 -> "31,103")
    local function format_number(n)
        local s = tostring(n)
        local pos = #s % 3
        if pos == 0 then pos = 3 end
        local result = s:sub(1, pos)
        for i = pos + 1, #s, 3 do
            result = result .. "," .. s:sub(i, i + 2)
        end
        return result
    end

    -- Center a string within a given width
    local function center_str(s, width)
        local pad = width - #s
        local lpad = math.floor(pad / 2)
        local rpad = pad - lpad
        return string.rep(" ", lpad) .. s .. string.rep(" ", rpad)
    end

    -- Left-align a string within a given width (with 1-space left margin)
    local function left_align_str(s, width)
        return " " .. s .. string.rep(" ", width - #s - 1)
    end

    -- Right-align a string within a given width (with 1-space right margin)
    local function right_align_str(s, width)
        return string.rep(" ", width - #s - 1) .. s .. " "
    end

    -- Format elapsed seconds as a human-readable string
    local function format_time(seconds)
        if seconds >= 60 then
            local mins = math.floor(seconds / 60)
            local secs = seconds - mins * 60
            return string.format("%dm %04.1fs", mins, secs)
        else
            return string.format("%.1fs", seconds)
        end
    end

    -- Parse test summary file written by TestListener
    local function parse_summary_file(filepath)
        if not os.isfile(filepath) then return nil end
        local content = io.readfile(filepath)
        if not content then return nil end
        local ct = content:match("cases_total=(%d+)")
        local cp = content:match("cases_passed=(%d+)")
        local at = content:match("assertions_total=(%d+)")
        local ap = content:match("assertions_passed=(%d+)")
        if ct then
            local failures = {}
            for name, file, line in content:gmatch("failed=([^|\n]+)|([^|\n]+)|(%d+)") do
                table.insert(failures, {name = name, file = file, line = tonumber(line)})
            end
            local es = content:match("elapsed_seconds=([%d%.]+)")
            return {
                cases_total = tonumber(ct),
                cases_passed = tonumber(cp) or 0,
                asserts_total = tonumber(at) or 0,
                asserts_passed = tonumber(ap) or 0,
                elapsed_seconds = tonumber(es) or 0,
                failures = failures
            }
        end
        return nil
    end

    -- Temp directory for summary files
    local summary_dir = path.join(os.tmpdir(), "zen-test-summary")
    os.mkdir(summary_dir)

    -- Run each test suite and collect results
    for iteration = 1, repeat_count do
        if repeat_count > 1 then
            printf("\n*** Iteration %d/%d ***\n", iteration, repeat_count)
        end

        for _, entry in ipairs(tests) do
            local name, target = entry.name, entry.target
            printf("=== %s ===\n", target)

            local suite_name = target
            if name == "server" then
                suite_name = "zenserver (test)"
            end

            local cmd = string.format("xmake run %s", target)
            if name == "server" then
                cmd = string.format("xmake run %s test", target)
            end
            cmd = string.format("%s --duration=true", cmd)

            if use_junit_reporting then
                local junit_report_file = path.join(junit_report_dir, string.format("junit-%s-%s-%s.xml", config.plat(), arch, target))
                junit_report_files[target] = junit_report_file
                cmd = string.format("%s --reporters=junit --out=%s", cmd, junit_report_file)
            end
            if use_noskip then
                cmd = string.format("%s --no-skip", cmd)
            end
            if use_verbose and name == "integration" then
                cmd = string.format("%s --verbose", cmd)
            end
            for _, arg in ipairs(extra_args) do
                cmd = string.format("%s %s", cmd, arg)
            end

            -- Tell TestListener where to write the summary
            local summary_file = path.join(summary_dir, target .. ".txt")
            os.setenv("ZEN_TEST_SUMMARY_FILE", summary_file)

            -- Run test with real-time streaming output
            local test_ok = true
            try {
                function()
                    os.exec(cmd)
                end,
                catch {
                    function(errors)
                        test_ok = false
                    end
                }
            }

            -- Read summary written by TestListener
            local summary = parse_summary_file(summary_file)
            os.tryrm(summary_file)

            if not test_ok then
                any_failed = true
            end

            table.insert(results, {
                suite = suite_name,
                cases_passed = summary and summary.cases_passed or 0,
                cases_total = summary and summary.cases_total or 0,
                asserts_passed = summary and summary.asserts_passed or 0,
                asserts_total = summary and summary.asserts_total or 0,
                elapsed_seconds = summary and summary.elapsed_seconds or 0,
                failures = summary and summary.failures or {},
                passed = test_ok
            })
        end

        if any_failed then
            if repeat_count > 1 then
                printf("\n*** Failure detected on iteration %d, stopping ***\n", iteration)
            end
            break
        end
    end

    -- Clean up
    os.setenv("ZEN_TEST_SUMMARY_FILE", "")
    os.tryrm(summary_dir)

    -- Print JUnit reports if requested
    for test, junit_report_file in pairs(junit_report_files) do
        printf("=== report - %s ===\n", test)
        if os.isfile(junit_report_file) then
            local data = io.readfile(junit_report_file)
            if data then
                print(data)
            end
        end
    end

    -- Print summary table
    if #results > 0 then
        -- Calculate column widths based on content
        local col_suite = #("Suite")
        local col_cases = #("Cases")
        local col_asserts = #("Assertions")
        local col_time = #("Time")
        local col_status = #("Status")

        -- Compute totals
        local total_cases_passed = 0
        local total_cases_total = 0
        local total_asserts_passed = 0
        local total_asserts_total = 0
        local total_elapsed = 0

        for _, r in ipairs(results) do
            col_suite = math.max(col_suite, #r.suite)
            local cases_str = format_number(r.cases_passed) .. "/" .. format_number(r.cases_total)
            col_cases = math.max(col_cases, #cases_str)
            local asserts_str = format_number(r.asserts_passed) .. "/" .. format_number(r.asserts_total)
            col_asserts = math.max(col_asserts, #asserts_str)
            col_time = math.max(col_time, #format_time(r.elapsed_seconds))
            local status_str = r.passed and "SUCCESS" or "FAILED"
            col_status = math.max(col_status, #status_str)

            total_cases_passed = total_cases_passed + r.cases_passed
            total_cases_total = total_cases_total + r.cases_total
            total_asserts_passed = total_asserts_passed + r.asserts_passed
            total_asserts_total = total_asserts_total + r.asserts_total
            total_elapsed = total_elapsed + r.elapsed_seconds
        end

        -- Account for totals row in column widths
        col_suite = math.max(col_suite, #("Total"))
        col_cases = math.max(col_cases, #(format_number(total_cases_passed) .. "/" .. format_number(total_cases_total)))
        col_asserts = math.max(col_asserts, #(format_number(total_asserts_passed) .. "/" .. format_number(total_asserts_total)))
        col_time = math.max(col_time, #format_time(total_elapsed))

        -- Add padding (1 space each side)
        col_suite = col_suite + 2
        col_cases = col_cases + 2
        col_asserts = col_asserts + 2
        col_time = col_time + 2
        col_status = col_status + 2

        -- Build horizontal border segments
        local h_suite = string.rep("-", col_suite)
        local h_cases = string.rep("-", col_cases)
        local h_asserts = string.rep("-", col_asserts)
        local h_time = string.rep("-", col_time)
        local h_status = string.rep("-", col_status)

        local top    = "+" .. h_suite .. "+" .. h_cases .. "+" .. h_asserts .. "+" .. h_time .. "+" .. h_status .. "+"
        local mid    = "+" .. h_suite .. "+" .. h_cases .. "+" .. h_asserts .. "+" .. h_time .. "+" .. h_status .. "+"
        local bottom = "+" .. h_suite .. "+" .. h_cases .. "+" .. h_asserts .. "+" .. h_time .. "+" .. h_status .. "+"
        local vbar   = "|"

        local header_msg = any_failed and "Some tests failed:" or "All tests passed:"
        printf("\n* %s\n", header_msg)
        printf("  %s\n", top)
        printf("  %s%s%s%s%s%s%s%s%s%s%s\n", vbar, center_str("Suite", col_suite), vbar, center_str("Cases", col_cases), vbar, center_str("Assertions", col_asserts), vbar, center_str("Time", col_time), vbar, center_str("Status", col_status), vbar)

        for _, r in ipairs(results) do
            printf("  %s\n", mid)
            local cases_str = format_number(r.cases_passed) .. "/" .. format_number(r.cases_total)
            local asserts_str = format_number(r.asserts_passed) .. "/" .. format_number(r.asserts_total)
            local time_str = format_time(r.elapsed_seconds)
            local status_str = r.passed and "SUCCESS" or "FAILED"
            printf("  %s%s%s%s%s%s%s%s%s%s%s\n", vbar, left_align_str(r.suite, col_suite), vbar, right_align_str(cases_str, col_cases), vbar, right_align_str(asserts_str, col_asserts), vbar, right_align_str(time_str, col_time), vbar, right_align_str(status_str, col_status), vbar)
        end

        -- Totals row
        if #results > 1 then
            local h_suite_eq = string.rep("=", col_suite)
            local h_cases_eq = string.rep("=", col_cases)
            local h_asserts_eq = string.rep("=", col_asserts)
            local h_time_eq = string.rep("=", col_time)
            local h_status_eq = string.rep("=", col_status)
            local totals_sep = "+" .. h_suite_eq .. "+" .. h_cases_eq .. "+" .. h_asserts_eq .. "+" .. h_time_eq .. "+" .. h_status_eq .. "+"
            printf("  %s\n", totals_sep)

            local total_cases_str = format_number(total_cases_passed) .. "/" .. format_number(total_cases_total)
            local total_asserts_str = format_number(total_asserts_passed) .. "/" .. format_number(total_asserts_total)
            local total_time_str = format_time(total_elapsed)
            local total_status_str = any_failed and "FAILED" or "SUCCESS"
            printf("  %s%s%s%s%s%s%s%s%s%s%s\n", vbar, left_align_str("Total", col_suite), vbar, right_align_str(total_cases_str, col_cases), vbar, right_align_str(total_asserts_str, col_asserts), vbar, right_align_str(total_time_str, col_time), vbar, right_align_str(total_status_str, col_status), vbar)
        end

        printf("  %s\n", bottom)
    end

    -- Print list of individual failing tests
    if any_failed then
        printf("\n  Failures:\n")
        for _, r in ipairs(results) do
            if #r.failures > 0 then
                printf("  -- %s --\n", r.suite)
                for _, f in ipairs(r.failures) do
                    printf("    FAILED: %s (%s:%d)\n", f.name, f.file, f.line)
                end
            elseif not r.passed then
                printf("  -- %s --\n", r.suite)
                printf("    (test binary exited with error, no failure details available)\n")
            end
        end
    end

    if any_failed then
        raise("one or more test suites failed")
    end
end