~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/tests.c -rw-r--r-- 2.7 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test a month ago
                                                                                
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
#include "posix.h"

#include <limits.h>
#include <string.h>

#include "args.h"
#include "buf_size.h"
#include "lang/serial.h"
#include "log.h"
#include "output/output.h"
#include "platform/filesystem.h"
#include "platform/mem.h"
#include "platform/path.h"
#include "platform/run_cmd.h"
#include "tests.h"

static enum iteration_result
run_test(struct workspace *wk, void *_ctx, uint32_t t)
{
	struct obj *test = get_obj(wk, t);

	uint32_t cmdline;
	make_obj(wk, &cmdline, obj_array);
	obj_array_push(wk, cmdline, test->dat.test.exe);

	if (test->dat.test.args) {
		uint32_t test_args;
		if (!arr_to_args(wk, test->dat.test.args, &test_args)) {
			return ir_err;
		}

		obj_array_extend(wk, cmdline, test_args);
	}

	char *argv[MAX_ARGS], *envp[MAX_ARGS] = { 0 };

	if (!join_args_argv(wk, argv, MAX_ARGS, cmdline)) {
		LOG_E("failed to prepare arguments");
		return ir_err;
	}

	if (test->dat.test.env) {
		if (!join_args_argv(wk, envp, MAX_ARGS, test->dat.test.env)) {
			LOG_E("failed to prepare environment");
			return ir_err;
		}
	}

	enum iteration_result ret = ir_err;
	struct run_cmd_ctx cmd_ctx = { 0 };

	if (!run_cmd(&cmd_ctx, wk_objstr(wk, test->dat.test.exe), argv, envp)) {
		LOG_E("test command failed: %s", cmd_ctx.err_msg);
		goto ret;
	}

	if (cmd_ctx.status && !test->dat.test.should_fail) {
		LOG_E("%s - failed (%d)", wk_objstr(wk, test->dat.test.name), cmd_ctx.status);
		LOG_E("stdout: '%s'", cmd_ctx.out);
		LOG_E("stderr: '%s'", cmd_ctx.err);
		goto ret;
	} else {
		LOG_I("%s - success (%d)", wk_objstr(wk, test->dat.test.name), cmd_ctx.status);
	}

	ret = ir_cont;
ret:
	run_cmd_ctx_destroy(&cmd_ctx);
	return ret;
}

static enum iteration_result
run_project_tests(struct workspace *wk, void *_ctx, uint32_t proj_name, uint32_t tests)
{
	LOG_I("running tests for project '%s'", wk_objstr(wk, proj_name));

	if (!obj_array_foreach(wk, tests, NULL, run_test)) {
		return ir_err;
	}

	return ir_cont;
}

bool
tests_run(const char *build_dir)
{
	bool ret = false;
	char tests_src[PATH_MAX], private[PATH_MAX], build_root[PATH_MAX];

	if (!path_make_absolute(build_root, PATH_MAX, build_dir)) {
		return false;
	} else if (!path_join(private, PATH_MAX, build_root, outpath.private_dir)) {
		return false;
	} else if (!path_join(tests_src, PATH_MAX, private, outpath.tests)) {
		return false;
	}

	FILE *f;
	if (!(f = fs_fopen(tests_src, "r"))) {
		return false;
	}

	struct workspace wk;
	workspace_init_bare(&wk);

	uint32_t tests_dict;
	if (!serial_load(&wk, &tests_dict, f)) {
		LOG_E("invalid tests file");
		goto ret;
	} else if (!fs_fclose(f)) {
		goto ret;
	} else if (!path_chdir(build_root)) {
		goto ret;
	} else if (!obj_dict_foreach(&wk, tests_dict, NULL, run_project_tests)) {
		goto ret;
	}

	ret = true;
ret:
	workspace_destroy_bare(&wk);
	return ret;
}