Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
181 lines (165 loc) · 8.08 KB
/
Copy pathinit.lua
File metadata and controls
181 lines (165 loc) · 8.08 KB
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
codeblock = {modpath = core.get_modpath('codeblock')}
dofile(codeblock.modpath .. "/lib/intl.lua")
dofile(codeblock.modpath .. "/lib/palette.lua")
dofile(codeblock.modpath .. "/lib/config.lua")
dofile(codeblock.modpath .. "/lib/nodes.lua")
dofile(codeblock.modpath .. "/lib/api.lua")
dofile(codeblock.modpath .. "/lib/blocks.lua")
dofile(codeblock.modpath .. "/lib/pathjoin.lua")
dofile(codeblock.modpath .. "/lib/filesystem.lua")
dofile(codeblock.modpath .. "/lib/examples.lua")
--
-- codeblock.examples.load_examples() -- read at init time
--
dofile(codeblock.modpath .. "/lib/limits.lua")
dofile(codeblock.modpath .. "/lib/shapes.lua")
dofile(codeblock.modpath .. "/lib/cost.lua")
dofile(codeblock.modpath .. "/lib/commands.lua")
dofile(codeblock.modpath .. "/lib/preprocess.lua")
dofile(codeblock.modpath .. "/lib/env.lua")
dofile(codeblock.modpath .. "/lib/strguard.lua")
dofile(codeblock.modpath .. "/lib/sandbox.lua")
dofile(codeblock.modpath .. "/lib/forms.lua")
dofile(codeblock.modpath .. "/lib/stepper.lua")
dofile(codeblock.modpath .. "/lib/drone.lua")
dofile(codeblock.modpath .. "/lib/drone_entity.lua")
dofile(codeblock.modpath .. "/lib/hud.lua")
dofile(codeblock.modpath .. "/lib/formspecs.lua")
dofile(codeblock.modpath .. "/lib/register.lua")
-- Bound the string methods that can allocate more than they are given.
-- Inert until a player program is actually running; see lib/strguard.lua.
local ok, why = codeblock.strguard.install()
if not ok then
core.log("warning", "[codeblock] string guards not installed: " ..
tostring(why))
end
-- Name an installed vector3 that hands its method table back from every vector,
-- because a player program can then replace a vector3 method for every mod on
-- the server (S9). Fixed upstream in 2.0.2, which carries __index on a separate
-- metatable; mod.conf cannot ask for a version, Luanti having no version
-- constraint, so saying so is all this mod can do. Silent on a library without
-- the hole: a line on every start that reports nothing is noise.
--
-- Read-only detection, deliberately. Probing by writing to a constant succeeds
-- on 1.5 and changes that constant for the whole server, which would be causing
-- the other defect in order to test for this one (S8). Which version it is
-- follows from the same two reads: on 1.5 the constants are plain vectors, on
-- 2.0.1 they are frozen and iterate as empty. Not translated - this is a
-- debug.txt line for whoever runs the server, and engine logs are English.
if vector3(1, 1, 1).__index ~= nil then
local guess = (type(vector3.one) == 'table' and next(vector3.one)) and
'v1.5' or 'v2.0.1'
core.log('warning',
'[codeblock] the installed vector3 (looks like ' .. guess ..
') exposes its method table on every vector, so a player ' ..
'program can replace a vector3 method for every mod on this ' ..
'server. Install vector3 2.0.2 or newer.')
end
if not core.mkdir(codeblock.filesystem.data_path) then
error("[editor] failed to create directory!")
end
-- Regenerate the API reference from lib/api.lua when asked.
-- scripts/gen_docs.lua does the same thing under a bare interpreter and writes
-- doc/api.md in place; this path exists so the docs can be regenerated with
-- nothing installed but the game itself.
--
-- It writes into the world directory rather than the mod directory, because
-- Luanti's mod security blocks writes to a mod's own files - reads are fine,
-- writes are not. Copy the result over doc/api.md. Slightly manual, but it
-- beats requiring a Lua toolchain, and CI's `gen_docs.lua --check` catches it if
-- the copy is forgotten.
if core.settings:get_bool("codeblock_gen_docs") then
local source = codeblock.modpath .. "/doc/api.md"
local path = core.get_worldpath() .. "/api.md"
local current = ""
local f = io.open(source, "rb")
if f then
current = f:read("*a")
f:close()
end
-- The block lists come out in palette order, not sorted: that order is the
-- rainbow a ramp maps onto, so a list in it reads as one. config.lua owns
-- it, and both this and scripts/gen_docs.lua hand the same table to the
-- same renderer, which is what keeps their answers identical.
local wanted, err = codeblock.api.compose_markdown(current,
codeblock.config
.allowed_blocks)
if not wanted then
print("[codeblock] doc/api.md: " .. tostring(err))
elseif wanted == current then
print("[codeblock] doc/api.md is already up to date")
else
local out = io.open(path, "wb")
if out then
out:write(wanted)
out:close()
print("[codeblock] wrote " .. path)
print("[codeblock] copy it over doc/api.md to update the reference")
else
print("[codeblock] cannot write " .. path)
end
end
end
-- Run the test suite in-engine when asked. Mirrors worldedit_run_tests, and
-- means the specs are runnable without a Lua toolchain installed.
--
-- Nothing is assumed to be there: `tests export-ignore` in .gitattributes keeps
-- the specs out of the ContentDB archive, so in a release build this setting
-- would take the whole mod down on a missing file. It says so instead. (C16)
if core.settings:get_bool("codeblock_run_tests") then
local specs = {
'api', 'preprocess', 'env', 'shapes', 'strguard', 'limits', 'palette',
'forms', 'stepper', 'integration'
}
local first = io.open(codeblock.modpath .. '/tests/api_spec.lua', 'r')
if not first then
core.log('warning', '[codeblock] codeblock_run_tests is set, but this ' ..
'build ships no tests/ directory')
else
first:close()
-- One verdict line, so a reader and a CI job apply the same pass
-- criteria instead of each restating them. Every spec returns its own
-- counts, and a spec that returned nothing is missing from the tally -
-- which is what catches a spec that did not run at all, the failure a
-- per-spec eyeball misses. It carries the word `passed`, so
-- run_tests.ps1's report filter keeps it.
--
-- `skipped` is counted apart from `ran`, and both are reported. The
-- three in-engine specs answer their can't-run branch with a table as
-- well, so counting every table as a spec that ran let one of them
-- stop asserting entirely and still report 9/9: breaking the guard
-- forms_spec exists to hold lost 66 assertions and stayed green.
local ran, skipped = 0, 0
local pass, fail, xfail, xpass = 0, 0, 0, 0
for _, spec in ipairs(specs) do
local r = dofile(codeblock.modpath .. '/tests/' .. spec ..
'_spec.lua')
if type(r) == 'table' then
if r.skipped then
skipped = skipped + 1
else
ran = ran + 1
pass = pass + (r.passed or 0)
fail = fail + (r.failed or 0)
xfail = xfail + (r.xfail or 0)
xpass = xpass + (r.xpass or 0)
end
end
end
local verdict =
' suite: %d/%d specs %d passed %d failed %d xfail ' ..
'%d xpass %d skipped'
print(verdict:format(ran, #specs, pass, fail, xfail, xpass, skipped))
-- Let the run end itself, so no caller has to guess how long the suite
-- takes: the local script waited a fixed 25 seconds for a suite that
-- finishes in one. Its own setting, because the fixture game enables
-- the suite for every boot of it (tests/game/minetest.conf) and
-- booting that game to look around has to stay possible.
--
-- core.after and not a call here: the shutdown belongs on the first
-- step, not in the middle of mod load.
if core.settings:get_bool('codeblock_run_tests_exit') then
core.after(0, function() core.request_shutdown('', false, 0) end)
end
end
end