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

use "build"
use "clean"
use "deps"
use "opts"
use "parse"
use "types"
use "util"
use "syssel"
use "subtest"

use "config"

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

const test = {b
	var tests : (byte[:][:], byte[:])[:]
	var failed, ok
	var bincmd

	/* build with the test tag */
	addsysattrs(b, ["test"][:])
	/* no implicit tests to run */
	tests = [][:]
	buildall(b)
	setdir(b, "")
        for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:      std.sljoin(&tests, buildtests(b, bt))
		| `Lib lt:	std.sljoin(&tests, buildtests(b, lt))
		| _:	/* nothing */
		;;
        ;;
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin t:
			if !t.istest
				continue
			;;
			if t.incpath.len == 0 || !std.sleq(t.incpath[0], ".")
				std.slput(&t.incpath, 0, std.sldup("."))
			;;
			buildbin(b, t, false)
			bincmd = std.sldup([std.strcat("./", t.name)][:])
			std.slpush(&tests, (bincmd, std.sldup(t.dir)))
		| `Cmd t:
			if !t.istest
				continue
			;;
			std.slpush(&tests, (dupcmd(t.cmd), std.sldup(t.dir)))
		| _:
			/* skip */
		;;
	;;

	ok = true
	failed = [][:]
	for (c, dir) in tests
		setdir(b, dir)
		if !runtest(b, c, &failed)
			ok = false
		;;

	;;
	for (bin, dir) in tests
		freecmd(bin)
		std.slfree(dir)
	;;
	if tests.len == 0
		-> true
	;;

	printfailed(failed)
	for f in failed
		std.slfree(f)
	;;
	if ok
		mbldput("TESTS PASSED\n")
	else
		mbldput("TESTS FAILED\n")
	;;
	std.slfree(failed)
	std.slfree(tests)
	-> ok
}

const printfailed = {failed
	if failed.len > 0
		mbldput("FAILURES: {}\n", failed.len)
		for t in failed
			mbldput("\t{}\n", t)
		;;
	;;
}

const dupcmd = {cmd
	var ret

	ret = [][:]
	for c in cmd
		std.slpush(&ret, std.sldup(c))
	;;
	-> ret
}

const freecmd = {cmd
	for c in cmd
		std.slfree(c)
	;;
	std.slfree(cmd)
}

const buildtests = {b, targ
	var tt, bin, tests, incpath, libdeps
	var cmd

	tests = [][:]
	setdir(b, targ.dir)
	for s in targ.inputs
		match testpath(s)
		| `std.None: /* nothing to do */
		| `std.Some path:
			bin = srcswapsuffix(path, "")
			incpath = std.sldup(targ.incpath)
			libdeps = std.sldup(targ.libdeps)
			std.sljoin(&libdeps, targ.tstdeps)
			tt = [
				.name = bin,
				.dir = targ.dir,
				.inputs = [path][:],
				.install = false,
				.libdeps = libdeps,
				.incpath = std.slput(&incpath, 0, "."),
			]

			buildbin(b, &tt, true)
			cmd = std.sldup([std.strcat("./", bin)][:])
			std.slpush(&tests, (cmd, std.sldup(targ.dir)))
			std.slfree(tt.libdeps)
			std.slfree(tt.incpath)
			std.slfree(path)
		;;
	;;
	-> tests
}

const runtest = {b, cmd, failed
	var res, log, logfd, p
	var sub

	mbldput("run")
	for c in cmd
		p = std.pathcat(b.curdir, c)
		mbldput(" {}", p)
		std.slfree(p)
	;;
	mbldput(":\t")
	match std.spork(cmd)
	| `std.Err m:
		std.fatal("\nunable to run test: {}\n", m)
	| `std.Ok (pid, infd, outfd):
		log = std.strcat(cmd[0], ".log")
		logfd = std.try(std.openmode(log, std.Owronly | std.Ocreat, 0o644))
		sub = showsub(b, cmd[0], outfd, logfd, failed)
		std.slfree(log)
		std.close(infd)
		std.close(outfd)

		res = false
		match std.wait(pid)
		| `std.Wfailure:	mbldput("FAIL\n")
		| `std.Wsignalled:	mbldput("CRASH\n")
		| `std.Wsuccess:
			res = true
			/* if we have subtests, we've already printed the output */
			match sub
			| `std.Some r:	res = r
			| `std.None:	mbldput("PASS\n")
			;;
		| `std.Waiterror:	mbldput("failed waiting for pid {}\n", pid)
		;;
		if !res
			p = std.pathcat(b.curdir, cmd[0])
			std.slpush(failed, p)
		;;
	;;
	-> res
}

const testpath = {s
	var path, file

	path = std.pathcat("./test", s)
	if std.fexists(path)
		-> `std.Some path
	;;
	match std.strfind(s, "+")
	| `std.None:
	| `std.Some idx:
		file = std.strcat(s[:idx], ".myr")
		path = std.pathcat("./test", file)
		std.slfree(file)
		if std.fexists(path)
			-> `std.Some path
		;;
	;;
	-> `std.None
}