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
|
local entry_path = "yazi-enter.yazi/main.lua"
local file = assert(io.open(entry_path, "rb"))
local source = assert(file:read("*a"))
file:close()
local capture = [[
capture_find_update {
apply = apply_find,
success = UPDATE_SUCCESS,
failure = UPDATE_FAILURE,
clear_error = ERROR_STATUS_CLEAR,
}
]]
local instrumented_source, replacement_count = source:gsub(
"\nreturn {\n\tentry = function",
"\n" .. capture .. "\nreturn {\n\tentry = function",
1
)
assert(replacement_count == 1, "Find updater capture point must occur once")
local model
local exec_handler
local plugin_error_reports = 0
local environment = {
error = error,
getmetatable = getmetatable,
pairs = pairs,
pcall = pcall,
type = type,
capture_find_update = function(value)
model = value
end,
ya = {
sync = function(callback)
return callback
end,
exec = function(...)
return exec_handler(...)
end,
err = function()
plugin_error_reports = plugin_error_reports + 1
end,
},
}
local chunk = assert(load(instrumented_source, "@" .. entry_path, "t", environment))
chunk()
assert(type(model) == "table", "Find updater was not captured")
assert(type(model.apply) == "function", "Find updater must be a function")
local function assert_equal(actual, expected, label)
assert(actual == expected, ("%s: expected %s, got %s"):format(label, tostring(expected), tostring(actual)))
end
local function assert_request(request, query, policy, label)
assert(type(request) == "table", label .. " request must be a table")
assert(request ~= policy, label .. " request must be separate from the search policy")
assert_equal(request[1], query, label .. " positional query")
assert_equal(request[2], nil, label .. " second positional argument")
assert_equal(request.previous, policy.previous, label .. " previous option")
assert_equal(request.smart, policy.smart, label .. " smart option")
assert_equal(request.insensitive, policy.insensitive, label .. " insensitive option")
assert_equal(type(request.previous), "boolean", label .. " previous type")
assert_equal(type(request.smart), "boolean", label .. " smart type")
assert_equal(type(request.insensitive), "boolean", label .. " insensitive type")
local expected_keys = {
[1] = true,
previous = true,
smart = true,
insensitive = true,
}
local count = 0
for key in pairs(request) do
assert(expected_keys[key], label .. " unexpected request key " .. tostring(key))
count = count + 1
end
assert_equal(count, 4, label .. " request field count")
end
local complete_query = "literal ; $(touch ignored)\0tail.*"
local policy = {
previous = true,
smart = false,
insensitive = true,
}
local calls = {}
exec_handler = function(action, request, ...)
assert_equal(select("#", ...), 0, "native Find extra argument count")
calls[#calls + 1] = { action = action, request = request }
return "completed reply"
end
local result, error_status = model.apply(complete_query, policy)
assert_equal(result, model.success, "completed Find result")
assert_equal(error_status, model.clear_error, "completed Find error status")
assert_equal(#calls, 1, "completed Find call count")
assert_equal(calls[1].action, "find_do", "native Find action")
assert_request(calls[1].request, complete_query, policy, "completed Find")
assert_equal(policy.previous, true, "preserved policy previous")
assert_equal(policy.smart, false, "preserved policy smart")
assert_equal(policy.insensitive, true, "preserved policy insensitive")
assert(not calls[1].action:find(complete_query, 1, true), "query must stay separate from action text")
local reply_completed = false
local awaited_call
exec_handler = function(action, request)
awaited_call = { action = action, request = request }
local reply = coroutine.yield("manager reply pending")
reply_completed = true
return reply
end
local awaited_policy = {
previous = false,
smart = true,
insensitive = false,
}
local thread = coroutine.create(function()
return model.apply("awaited query", awaited_policy)
end)
local resumed, pending = coroutine.resume(thread)
assert_equal(resumed, true, "awaited Find first resume")
assert_equal(pending, "manager reply pending", "awaited Find pending marker")
assert_equal(coroutine.status(thread), "suspended", "Find updater while reply is pending")
assert_equal(reply_completed, false, "Find completion before manager reply")
assert_equal(awaited_call.action, "find_do", "awaited native Find action")
assert_request(awaited_call.request, "awaited query", awaited_policy, "awaited Find")
resumed, result, error_status = coroutine.resume(thread, "manager completed")
assert_equal(resumed, true, "awaited Find second resume")
assert_equal(coroutine.status(thread), "dead", "Find updater after reply")
assert_equal(reply_completed, true, "Find completion after manager reply")
assert_equal(result, model.success, "awaited Find result")
assert_equal(error_status, model.clear_error, "awaited Find error status")
local dispatcher_reported = false
local failed_call
exec_handler = function(action, request)
failed_call = { action = action, request = request }
coroutine.yield("manager error pending")
dispatcher_reported = true
error("native malformed regular expression", 0)
end
local failed_policy = {
previous = false,
smart = false,
insensitive = false,
}
thread = coroutine.create(function()
return model.apply("[", failed_policy)
end)
resumed, pending = coroutine.resume(thread)
assert_equal(resumed, true, "failed Find first resume")
assert_equal(pending, "manager error pending", "failed Find pending marker")
assert_equal(coroutine.status(thread), "suspended", "failed Find before manager error")
assert_equal(dispatcher_reported, false, "dispatcher report before failed reply")
assert_equal(failed_call.action, "find_do", "failed native Find action")
assert_request(failed_call.request, "[", failed_policy, "failed Find")
resumed, result, error_status = coroutine.resume(thread)
assert_equal(resumed, true, "caught Find failure resume")
assert_equal(coroutine.status(thread), "dead", "Find updater after caught failure")
assert_equal(dispatcher_reported, true, "standard dispatcher error report")
assert_equal(result, model.failure, "caught Find result")
assert_equal(error_status, "native malformed regular expression", "caught Find error status")
assert_equal(plugin_error_reports, 0, "duplicate plugin error reports")
exec_handler = function()
return nil
end
result, error_status = model.apply("corrected", failed_policy)
assert_equal(result, model.success, "Find result after a caught failure")
assert_equal(error_status, model.clear_error, "error status after a caught failure")
print("Find update checks passed")
|