summaryrefslogtreecommitdiff
path: root/mbld/test.myr
blob: ae603a0f35ec34d984b0cf3cd49699ea51266b43 (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
use std

use "build.use"
use "clean.use"
use "deps.use"
use "opts.use"
use "parse.use"
use "types.use"
use "util.use"

use "config.use"

pkg bld =
	const test	: (b : build# -> void)
;;

const test = {b
	var ok, bin, hastest

	/* no implicit tests to run */
	ok = true
	hastest = false
	if std.fexists("test")
		hastest = true
		for tn in b.all
			match gettarg(b.targs, tn)
			| `Bin bt:
				if !dotest(b, bt)
					ok = false
				;;
			| `Lib lt:
				if !dotest(b, lt)
					ok = false
				;;
			| _:	/* ignore */
			;;
		;;
	;;
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Test t:
			hastest = true
			if t.incpath.len == 0 || !std.sleq(t.incpath[0], ".")
				t.incpath = std.slput(t.incpath, 0, std.sldup("."))
			;;
			buildbin(b, t, false)
			bin = std.strcat("./", t.name)
			if !runtest(bin)
				ok = false
			;;
			std.slfree(bin)
		| _:
			/* skip */
		;;
	;;
	if hastest
		if ok
			std.put("TESTS PASSED\n")
		else
			std.put("TESTS FAILED\n")
			std.exit(1)
		;;
	;;
}

const dotest = {b, targ
	var tt, bin ,path, tests, ok

	tests = [][:]
	setdir(b, targ.dir)
	for s in targ.inputs
		path = std.pathcat("./test", s)
		if std.fexists(path)
			bin = srcswapsuffix(path, "")
			tt = [
				.name = bin,
				.dir = targ.dir,
				.inputs = [path, s][:],
				.install = false,
				.libdeps = targ.libdeps,
				.incpath = targ.incpath,
			]

			cleantest(b, path)
			buildbin(b, &tt, true)
			tests = std.slpush(tests, bin)
		;;
		std.slfree(path)
	;;

	ok = true
	for t in tests
		if !runtest(t)
			ok = false
		;;
		std.slfree(t)
	;;
	std.slfree(tests)
	-> ok
}

const cleantest = {b, src
	var obj, bin, log, usef

	obj = srcswapsuffix(src, config.Objsuffix)
	log = srcswapsuffix(src, ".log")
	usef = srcswapsuffix(src, ".use")
	bin = srcswapsuffix(src, "")

	std.remove(obj)
	std.remove(usef)
	std.remove(log)
	std.remove(bin)

	std.slfree(obj)
	std.slfree(usef)
	std.slfree(log)
	std.slfree(bin)
}

const runtest = {bin
	var r, log

	std.put("run %s:\t", bin)
	log = std.strcat(bin, ".log")
	match std.spork([bin][:])
	| `std.Fail m:
		std.fatal("unable to run test: %s\n", m)
	| `std.Ok (pid, infd, outfd):
		match std.fslurp(outfd)
		| `std.Ok "":	/* empty output; nothing to log */
		| `std.Ok buf:
			std.blat(log, buf, 0o644)
		| `std.Fail m:
		;;
		std.slfree(log)

		r = false
		match std.wait(pid)
		| `std.Wfailure:	std.put("FAIL\n")
		| `std.Wsignalled:	std.put("CRASH\n")
		| `std.Wsuccess:
			std.put("PASS\n")
			r = true
		| _:	std.put("???\n")
		;;
	;;
	-> r
}