-
Notifications
You must be signed in to change notification settings - Fork 36
/
tsc
executable file
·304 lines (266 loc) · 8.38 KB
/
tsc
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
#!/usr/bin/env lua
local telescope = require 'telescope'
pcall(require, "luarocks.require")
pcall(require, "shake")
package.path = "./?.lua;" .. package.path
local function luacov_report()
local luacov = require("luacov.stats")
local data = luacov.load_stats()
if not data then
print("Could not load stats file "..luacov.statsfile..".")
print("Run your Lua program with -lluacov and then rerun luacov.")
os.exit(1)
end
local report = io.open("coverage.html", "w")
report:write('<!DOCTYPE html>', "\n")
report:write([[
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Luacov Coverage Report
</title>
<style type="text/css">
body { text-align: center; }
#wrapper { width: 800px; margin: auto; text-align: left; }
pre, ul, li { margin: 0; padding: 0 }
li { list-style-type: none; font-size: 11px}
.covered { background-color: #98FB98 }
.uncovered { background-color: #FFC0CB }
.file { width: 800px;
background-color: #c0c0c0;
padding: 3px;
overflow: hidden;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px; }
</style>
</head>
<body>
<div id="wrapper">
<h1>Luacov Code Coverage Report</h1>
]])
report:write("<p>Generated on ", os.date(), "</p>\n")
local names = {}
for filename, _ in pairs(data) do
table.insert(names, filename)
end
local escapes = {
[">"] = ">",
["<"] = "<"
}
local function escape_html(str)
return str:gsub("[<>]", function(a) return escapes[a] end)
end
table.sort(names)
for _, filename in ipairs(names) do
if string.match(filename, "/luacov/") or
string.match(filename, "/luarocks/") or
string.match(filename, "/tsc$")
then
break
end
local filedata = data[filename]
filename = string.gsub(filename, "^%./", "")
local file = io.open(filename, "r")
if file then
report:write("<h2>", filename, "</h2>", "\n")
report:write("<div class='file'>")
report:write("<ul>", "\n")
local line_nr = 1
while true do
local line = file:read("*l")
if not line then break end
if line:match("^%s*%-%-") then -- Comment line
elseif line:match("^%s*$") -- Empty line
or line:match("^%s*end,?%s*$") -- Single "end"
or line:match("^%s*else%s*$") -- Single "else"
or line:match("^%s*{%s*$") -- Single opening brace
or line:match("^%s*}%s*$") -- Single closing brace
or line:match("^#!") -- Unix hash-bang magic line
then
report:write("<li><pre>", string.format("%-4d", line_nr), " ", escape_html(line), "</pre></li>", "\n")
else
local hits = filedata[line_nr]
local class = "uncovered"
if not hits then hits = 0 end
if hits > 0 then class = "covered" end
report:write("<li>", " <pre ", "class='", class, "'>", string.format("%-4d", line_nr), string.format("%-4d", hits), " ", escape_html(line), "</pre></li>", "\n")
end
line_nr = line_nr + 1
end
end
report:write("</ul>", "\n")
report:write("</div>", "\n")
end
report:write([[
</div>
</body>
</html>
]])
end
local function getopt(arg, options)
local tab = {}
for k, v in ipairs(arg) do
if string.sub(v, 1, 2) == "--" then
local x = string.find(v, "=", 1, true)
if x then tab[string.sub(v, 3, x - 1)] = string.sub(v, x + 1)
else tab[string.sub(v, 3)] = true
end
elseif string.sub(v, 1, 1) == "-" then
local y = 2
local l = string.len(v)
local jopt
while (y <= l) do
jopt = string.sub(v, y, y)
if string.find(options, jopt, 1, true) then
if y < l then
tab[jopt] = string.sub(v, y + 1)
y = l
else
tab[jopt] = arg[k + 1]
end
else
tab[jopt] = true
end
y = y + 1
end
end
end
return tab
end
local callbacks = {}
local function progress_meter(t)
io.stdout:write(t.status_label)
end
local function show_usage()
local text = [[
Telescope
Usage: tsc [options] [files]
Description:
Telescope is a test framework for Lua that allows you to write tests
and specs in a TDD or BDD style.
Options:
-f, --full Show full report
-q, --quiet Show don't show any stack traces
-s --silent Don't show any output
-h,-? --help Show this text
-v --version Show version
-c --luacov Output a coverage file using Luacov (http://luacov.luaforge.net/)
--load=<file> Load a Lua file before executing command
--name=<pattern> Only run tests whose name matches a Lua string pattern
--shake Use shake as the front-end for tests
Callback options:
--after=<function> Run function given after each test
--before=<function> Run function before each test
--err=<function> Run function after each test that produces an error
--fail<function> Run function after each failing test
--pass=<function> Run function after each passing test
--pending=<function> Run function after each pending test
--unassertive=<function> Run function after each unassertive test
An example callback:
tsc --after="function(t) print(t.status_label, t.name, t.context) end" example.lua
An example test:
context("A context", function()
before(function() end)
after(function() end)
context("A nested context", function()
test("A test", function()
assert_not_equal("ham", "cheese")
end)
context("Another nested context", function()
test("Another test", function()
assert_greater_than(2, 1)
end)
end)
end)
test("A test in the top-level context", function()
assert_equal(1, 1)
end)
end)
Project home:
http://telescope.luaforge.net/
License:
MIT/X11 (Same as Lua)
Author:
Norman Clarke <[email protected]>. Please feel free to email bug
reports, feedback and feature requests.
]]
print(text)
end
local function add_callback(callback, func)
if callbacks[callback] then
if type(callbacks[callback]) ~= "table" then
callbacks[callback] = {callbacks[callback]}
end
table.insert(callbacks[callback], func)
else
callbacks[callback] = func
end
end
local function process_args()
local files = {}
local opts = getopt(arg, "")
local i = 1
for _, _ in pairs(opts) do i = i+1 end
while i <= #arg do table.insert(files, arg[i]) ; i = i + 1 end
return opts, files
end
local opts, files = process_args()
if opts["h"] or opts["?"] or opts["help"] or not (next(opts) or next(files)) then
show_usage()
os.exit()
end
if opts.v or opts.version then
print(telescope.version)
os.exit(0)
end
if opts.c or opts.luacov then
require "luacov.tick"
end
-- load a file with custom functionality if desired
if opts["load"] then dofile(opts["load"]) end
local test_pattern
if opts["name"] then
test_pattern = function(t) return t.name:match(opts["name"]) end
end
-- set callbacks passed on command line
local callback_args = { "after", "before", "err", "fail", "pass",
"pending", "unassertive" }
for _, callback in ipairs(callback_args) do
if opts[callback] then
add_callback(callback, loadstring(opts[callback])())
end
end
local contexts = {}
if opts["shake"] then
for _, file in ipairs(files) do shake.load_contexts(file, contexts) end
else
for _, file in ipairs(files) do telescope.load_contexts(file, contexts) end
end
local buffer = {}
local results = telescope.run(contexts, callbacks, test_pattern)
local summary, data = telescope.summary_report(contexts, results)
if opts.f or opts.full then
table.insert(buffer, telescope.test_report(contexts, results))
end
if not opts.s and not opts.silent then
table.insert(buffer, summary)
if not opts.q and not opts.quiet then
local report = telescope.error_report(contexts, results)
if report then
table.insert(buffer, "")
table.insert(buffer, report)
end
end
end
if #buffer > 0 then print(table.concat(buffer, "\n")) end
if opts.c or opts.coverage then
luacov_report()
os.remove("luacov.stats.out")
end
for _, v in pairs(results) do
if v.status_code == telescope.status_codes.err or
v.status_code == telescope.status_codes.fail then
os.exit(1)
end
end