Skip to content

Commit 3dc41b3

Browse files
committed
test(plugin): add test for missing required hook in Lua plugin
Add a new test case to verify that the Lua plugin correctly handles the absence of a required hook. This ensures that the plugin validation logic works as expected and provides meaningful error messages.
1 parent 2603386 commit 3dc41b3

2 files changed

Lines changed: 198 additions & 0 deletions

File tree

internal/plugin/plugin_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ func TestInvalidPluginName(t *testing.T) {
143143
}
144144
}
145145

146+
func TestMissingRequiredHook(t *testing.T) {
147+
teardownSuite := setupSuite(t)
148+
defer teardownSuite(t)
149+
_, err := plugin.NewLuaPlugin("testdata/plugins/missing_required_hook", internal.NewSdkManager().Config, internal.RuntimeVersion)
150+
if err == nil {
151+
t.Fatal("expected error, got nil")
152+
}
153+
t.Logf("error: %s", err.Error())
154+
if !strings.Contains(err.Error(), "[EnvKeys] function not found") {
155+
t.Errorf("expected error to contain '[EnvKeys] function not found', got '%s'", err.Error())
156+
}
157+
}
158+
146159
func testHookFunc(t *testing.T, factory func() (*internal.Manager, *plugin.PluginWrapper, error)) {
147160
t.Helper()
148161

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
--- Common libraries provided by VersionFox (optional)
2+
local http = require("http")
3+
local json = require("json")
4+
local html = require("html")
5+
6+
--- The following two parameters are injected by VersionFox at runtime
7+
--- Operating system type at runtime (Windows, Linux, Darwin)
8+
RUNTIME = {
9+
--- Operating system type at runtime (Windows, Linux, Darwin)
10+
osType = "",
11+
--- Operating system architecture at runtime (amd64, arm64, etc.)
12+
archType = "",
13+
--- vfox runtime version
14+
version = "",
15+
}
16+
17+
PLUGIN = {
18+
--- Plugin name
19+
name = "missing_required_hook",
20+
--- Plugin author
21+
author = "Lihan",
22+
--- Plugin version
23+
version = "0.0.1",
24+
--- Plugin description
25+
description = "xxx",
26+
-- Update URL
27+
updateUrl = "{URL}/sdk.lua",
28+
-- minimum compatible vfox version
29+
minRuntimeVersion = "0.2.2",
30+
legacyFilenames = {
31+
".node-version",
32+
".nvmrc"
33+
}
34+
}
35+
36+
--- Returns some pre-installed information, such as version number, download address, local files, etc.
37+
--- If checksum is provided, vfox will automatically check it for you.
38+
--- @param ctx table
39+
--- @field ctx.version string User-input version
40+
--- @return table Version information
41+
function PLUGIN:PreInstall(ctx)
42+
print(json.encode(RUNTIME))
43+
local version = ctx.version
44+
return {
45+
--- Version number
46+
version = "version",
47+
--- remote URL or local file path [optional]
48+
url = "xxx",
49+
--- SHA256 checksum [optional]
50+
sha256 = "xxx",
51+
--- md5 checksum [optional]
52+
md5 = "xxx",
53+
--- sha1 checksum [optional]
54+
sha1 = "xxx",
55+
--- sha512 checksum [optional]
56+
sha512 = "xx",
57+
--- additional need files [optional]
58+
addition = {
59+
{
60+
--- additional file name !
61+
name = "xxx",
62+
--- remote URL or local file path [optional]
63+
url = "xxx",
64+
--- SHA256 checksum [optional]
65+
sha256 = "xxx",
66+
--- md5 checksum [optional]
67+
md5 = "xxx",
68+
--- sha1 checksum [optional]
69+
sha1 = "xxx",
70+
--- sha512 checksum [optional]
71+
sha512 = "xx",
72+
}
73+
}
74+
}
75+
end
76+
77+
--- Extension point, called after PreInstall, can perform additional operations,
78+
--- such as file operations for the SDK installation directory or compile source code
79+
--- Currently can be left unimplemented!
80+
function PLUGIN:PostInstall(ctx)
81+
--- ctx.rootPath SDK installation directory
82+
local rootPath = ctx.rootPath
83+
local sdkInfo = ctx.sdkInfo['sdk-name']
84+
local path = sdkInfo.path
85+
local version = sdkInfo.version
86+
local name = sdkInfo.name
87+
end
88+
89+
--- Return all available versions provided by this plugin
90+
--- @param ctx table Empty table used as context, for future extension
91+
--- @return table Descriptions of available versions and accompanying tool descriptions
92+
function PLUGIN:Available(ctx)
93+
printTable(ctx.args)
94+
95+
return {
96+
{
97+
version = "xxxx",
98+
note = os.time(),
99+
addition = {
100+
{
101+
name = "npm",
102+
version = "8.8.8",
103+
}
104+
}
105+
}
106+
}
107+
end
108+
109+
--- When user invoke `use` command, this function will be called to get the
110+
--- valid version information.
111+
--- @param ctx table Context information
112+
function PLUGIN:PreUse(ctx)
113+
--- user input version
114+
local version = ctx.version
115+
--- installed sdks
116+
local sdkInfo = ctx.installedSdks['xxxx']
117+
local path = sdkInfo.path
118+
local name = sdkInfo.name
119+
local sdkVersion = sdkInfo.version
120+
121+
--- working directory
122+
local cwd = ctx.cwd
123+
124+
printTable(ctx)
125+
126+
--- user input scope
127+
local scope = ctx.scope
128+
129+
if (scope == "global") then
130+
print("return 9.9.9")
131+
return {
132+
version = "9.9.9",
133+
}
134+
end
135+
136+
if (scope == "project") then
137+
print("return 10.0.0")
138+
return {
139+
version = "10.0.0",
140+
}
141+
end
142+
143+
print("return 1.0.0")
144+
145+
return {
146+
version = "1.0.0"
147+
}
148+
end
149+
150+
function PLUGIN:ParseLegacyFile(ctx)
151+
printTable(ctx)
152+
local filename = ctx.filename
153+
local filepath = ctx.filepath
154+
155+
installed = ctx.getInstalledVersions()
156+
if #installed > 0 then
157+
print("Installed: " .. installed[1])
158+
return {
159+
version = "check-installed"
160+
}
161+
end
162+
163+
if filename == ".node-version" then
164+
return {
165+
version = "14.17.0"
166+
}
167+
else
168+
return {
169+
version = "0.0.1"
170+
}
171+
end
172+
173+
end
174+
175+
function PLUGIN:PreUninstall(ctx)
176+
printTable(ctx)
177+
local mainSdkInfo = ctx.main
178+
local mpath = mainSdkInfo.path
179+
local mversion = mainSdkInfo.version
180+
local mname = mainSdkInfo.name
181+
local sdkInfo = ctx.sdkInfo['sdk-name']
182+
local path = sdkInfo.path
183+
local version = sdkInfo.version
184+
local name = sdkInfo.name
185+
end

0 commit comments

Comments
 (0)