summaryrefslogtreecommitdiff
path: root/tests/package_shell_test.lua
blob: 3982d42d14245fd8f66a3a30ae8bc5dc07a80958 (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
local package_dir = "yazi-enter.yazi"
local entry_name = "main.lua"
local entry_path = package_dir .. "/" .. entry_name

assert(entry_path == "yazi-enter.yazi/main.lua", "unexpected plugin entry path")

local file = assert(io.open(entry_path, "rb"))
local source = assert(file:read("*a"))
file:close()

local first_line = source:match("^([^\r\n]*)")
assert(first_line == "--- @since 26.9.1", "invalid compatibility annotation")
assert(not source:find("--- @sync entry", 1, true), "entry must use the default async context")

local external_dependency_patterns = {
	"%f[%a]require%s*%(",
	"%f[%a]dofile%s*%(",
	"%f[%a]loadfile%s*%(",
	"package%.loadlib%s*%(",
	"io%.popen%s*%(",
	"os%.execute%s*%(",
	"%f[%a]Command%s*%(",
}

for _, pattern in ipairs(external_dependency_patterns) do
	assert(not source:find(pattern), "plugin source references an external runtime dependency")
end

local sync_calls = 0
local sync_callbacks = {}
local environment = {
	cx = { active = { finder = nil, current = {} } },
	error = error,
	getmetatable = getmetatable,
	pairs = pairs,
	type = type,
	ya = {
		sync = function(callback)
			sync_calls = sync_calls + 1
			sync_callbacks[sync_calls] = callback
			assert(type(callback) == "function", "ya.sync requires a function")
			return callback
		end,
		input = function()
			return { recv = function() return nil, 2 end }
		end,
	},
}

local chunk = assert(load(source, "@" .. entry_path, "t", environment))
local plugin = chunk()

assert(sync_calls == 1, "expected one top-level synchronous transaction")
assert(type(plugin) == "table", "plugin must export a table")
assert(type(plugin.entry) == "function", "plugin must export an entry method")

local result = plugin:entry({ args = { "directory" } })
assert(result == 0, "entry shell must load with the bundled runtime")
assert(sync_callbacks[1](nil, "directory", "query") == 0, "commit must guard absent finder state")

print("package shell checks passed")