Skip to content

Commit 2603386

Browse files
committed
test: update plugin path in test to use metadata
The test was updated to use `pluginPathWithMetadata` instead of `pluginPathWithMain` to ensure the test correctly verifies the plugin initialization with metadata. This change aligns the test with the intended functionality.
1 parent d5e031c commit 2603386

2 files changed

Lines changed: 227 additions & 1 deletion

File tree

internal/plugin/plugin_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestNewLuaPluginWithMetadataAndHooks(t *testing.T) {
8181
defer teardownSuite(t)
8282
t.Run("NewLuaPlugin", func(t *testing.T) {
8383
manager := internal.NewSdkManager()
84-
plugin, err := plugin.NewLuaPlugin(pluginPathWithMain, manager.Config, internal.RuntimeVersion)
84+
plugin, err := plugin.NewLuaPlugin(pluginPathWithMetadata, manager.Config, internal.RuntimeVersion)
8585
if err != nil {
8686
t.Fatal(err)
8787
}
@@ -130,6 +130,19 @@ func TestNewLuaPluginWithMetadataAndHooks(t *testing.T) {
130130
})
131131
}
132132

133+
func TestInvalidPluginName(t *testing.T) {
134+
teardownSuite := setupSuite(t)
135+
defer teardownSuite(t)
136+
_, err := plugin.NewLuaPlugin("testdata/plugins/invalid_name", internal.NewSdkManager().Config, internal.RuntimeVersion)
137+
if err == nil {
138+
t.Fatal("expected error, got nil")
139+
}
140+
t.Logf("error: %s", err.Error())
141+
if !strings.Contains(err.Error(), "invalid plugin name") {
142+
t.Errorf("expected error to contain 'invalid plugin name', got '%s'", err.Error())
143+
}
144+
}
145+
133146
func testHookFunc(t *testing.T, factory func() (*internal.Manager, *plugin.PluginWrapper, error)) {
134147
t.Helper()
135148

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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 = "!java_with_main",
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+
--- Each SDK may have different environment variable configurations.
110+
--- This allows plugins to define custom environment variables (including PATH settings)
111+
--- Note: Be sure to distinguish between environment variable settings for different platforms!
112+
--- @param ctx table Context information
113+
--- @field ctx.path string SDK installation directory
114+
function PLUGIN:EnvKeys(ctx)
115+
--- this variable is same as ctx.sdkInfo['plugin-name'].path
116+
local mainPath = ctx.path
117+
local sdkInfo = ctx.sdkInfo['sdk-name']
118+
local path = sdkInfo.path
119+
local version = sdkInfo.version
120+
local name = sdkInfo.name
121+
return {
122+
{
123+
key = "JAVA_HOME",
124+
value = mainPath
125+
},
126+
{
127+
key = "PATH",
128+
value = mainPath .. "/bin"
129+
},
130+
{
131+
key = "PATH",
132+
value = mainPath .. "/bin2"
133+
}
134+
}
135+
end
136+
137+
--- When user invoke `use` command, this function will be called to get the
138+
--- valid version information.
139+
--- @param ctx table Context information
140+
function PLUGIN:PreUse(ctx)
141+
--- user input version
142+
local version = ctx.version
143+
--- installed sdks
144+
local sdkInfo = ctx.installedSdks['xxxx']
145+
local path = sdkInfo.path
146+
local name = sdkInfo.name
147+
local sdkVersion = sdkInfo.version
148+
149+
--- working directory
150+
local cwd = ctx.cwd
151+
152+
printTable(ctx)
153+
154+
--- user input scope
155+
local scope = ctx.scope
156+
157+
if (scope == "global") then
158+
print("return 9.9.9")
159+
return {
160+
version = "9.9.9",
161+
}
162+
end
163+
164+
if (scope == "project") then
165+
print("return 10.0.0")
166+
return {
167+
version = "10.0.0",
168+
}
169+
end
170+
171+
print("return 1.0.0")
172+
173+
return {
174+
version = "1.0.0"
175+
}
176+
end
177+
178+
function PLUGIN:ParseLegacyFile(ctx)
179+
printTable(ctx)
180+
local filename = ctx.filename
181+
local filepath = ctx.filepath
182+
183+
installed = ctx.getInstalledVersions()
184+
if #installed > 0 then
185+
print("Installed: " .. installed[1])
186+
return {
187+
version = "check-installed"
188+
}
189+
end
190+
191+
if filename == ".node-version" then
192+
return {
193+
version = "14.17.0"
194+
}
195+
else
196+
return {
197+
version = "0.0.1"
198+
}
199+
end
200+
201+
end
202+
203+
function PLUGIN:PreUninstall(ctx)
204+
printTable(ctx)
205+
local mainSdkInfo = ctx.main
206+
local mpath = mainSdkInfo.path
207+
local mversion = mainSdkInfo.version
208+
local mname = mainSdkInfo.name
209+
local sdkInfo = ctx.sdkInfo['sdk-name']
210+
local path = sdkInfo.path
211+
local version = sdkInfo.version
212+
local name = sdkInfo.name
213+
end

0 commit comments

Comments
 (0)