Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/vfox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tempfile = "3"

[dev-dependencies]
insta = "1"
wiremock = "0.6"
#pretty_assertions = "1.4.0"

[features]
Expand Down
10 changes: 6 additions & 4 deletions crates/vfox/src/lua_mod/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ mod tests {

#[test]
fn test_zip() {
let _ = std::fs::remove_dir_all("/tmp/test_zip_dst");
let temp_dir = tempfile::TempDir::new().unwrap();
let dst_path = temp_dir.path().join("unzip");
let dst_path_str = dst_path.to_string_lossy().to_string();
let lua = Lua::new();
mod_archiver(&lua).unwrap();
lua.load(mlua::chunk! {
local archiver = require("archiver")
archiver.decompress("test/data/foo.zip", "/tmp/test_zip_dst")
archiver.decompress("test/data/foo.zip", $dst_path_str)
})
.exec()
.unwrap();
assert_eq!(
std::fs::read_to_string("/tmp/test_zip_dst/foo/test.txt").unwrap(),
std::fs::read_to_string(dst_path.join("foo/test.txt")).unwrap(),
"yep\n"
);
std::fs::remove_dir_all("/tmp/test_zip_dst").unwrap();
// TempDir automatically cleans up when dropped
}
}
14 changes: 12 additions & 2 deletions crates/vfox/src/lua_mod/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,26 @@ mod tests {

#[test]
fn test_cmd_with_cwd() {
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_path = temp_dir.path();
// Canonicalize to resolve symlinks (e.g., /var -> /private/var on macOS)
let temp_path_canonical = temp_path
.canonicalize()
.unwrap_or_else(|_| temp_path.to_path_buf());
let temp_dir_str = temp_path_canonical.to_string_lossy().to_string();
let expected_path = temp_dir_str.trim_end_matches('/').to_string();
let lua = Lua::new();
mod_cmd(&lua).unwrap();
lua.load(mlua::chunk! {
local cmd = require("cmd")
-- Test with working directory
local result = cmd.exec("pwd", {cwd = "/tmp"})
assert(result:find("/tmp") ~= nil)
local result = cmd.exec("pwd", {cwd = $temp_dir_str})
-- Check that result contains the expected path (handles trailing slashes/newlines)
assert(result:find($expected_path) ~= nil, "Expected result to contain: " .. $expected_path .. " but got: " .. result)
})
.exec()
.unwrap();
// TempDir automatically cleans up when dropped
}

#[test]
Expand Down
25 changes: 14 additions & 11 deletions crates/vfox/src/lua_mod/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ mod tests {

#[test]
fn test_read() {
let filepath = "/tmp/vfox-lua-file-read";
fs::write(filepath, "hello world").unwrap();
let temp_dir = tempfile::TempDir::new().unwrap();
let filepath = temp_dir.path().join("file-read.txt");
let filepath_str = filepath.to_string_lossy().to_string();
fs::write(&filepath, "hello world").unwrap();
let lua = Lua::new();
mod_file(&lua).unwrap();
lua.load(mlua::chunk! {
local file = require("file")
local success, contents = pcall(file.read, "/tmp/vfox-lua-file-read")
local success, contents = pcall(file.read, $filepath_str)
if not success then
error("Failed to read: " .. contents)
end
Expand All @@ -97,24 +99,25 @@ mod tests {
})
.exec()
.unwrap();
fs::remove_file(filepath).unwrap();
// TempDir automatically cleans up when dropped
}

#[test]
fn test_symlink() {
let _ = fs::remove_file("/tmp/test_symlink_dst");
let temp_dir = tempfile::TempDir::new().unwrap();
let src_path = temp_dir.path().join("symlink_src");
let dst_path = temp_dir.path().join("symlink_dst");
let src_path_str = src_path.to_string_lossy().to_string();
let dst_path_str = dst_path.to_string_lossy().to_string();
let lua = Lua::new();
mod_file(&lua).unwrap();
lua.load(mlua::chunk! {
local file = require("file")
file.symlink("/tmp/test_symlink_src", "/tmp/test_symlink_dst")
file.symlink($src_path_str, $dst_path_str)
})
.exec()
.unwrap();
assert_eq!(
fs::read_link("/tmp/test_symlink_dst").unwrap(),
Path::new("/tmp/test_symlink_src")
);
fs::remove_file("/tmp/test_symlink_dst").unwrap();
assert_eq!(fs::read_link(&dst_path).unwrap(), src_path);
// TempDir automatically cleans up when dropped
}
}
77 changes: 67 additions & 10 deletions crates/vfox/src/lua_mod/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,34 @@ fn get_headers(lua: &Lua, headers: &reqwest::header::HeaderMap) -> Result<Table>
mod tests {
use super::*;
use std::fs;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

#[tokio::test]
async fn test_get() {
// Start a local mock server
let server = MockServer::start().await;

// Create a mock endpoint
Mock::given(method("GET"))
.and(path("/get"))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(serde_json::json!({
"message": "test response"
}))
.insert_header("content-type", "application/json"),
)
.mount(&server)
.await;

let lua = Lua::new();
mod_http(&lua).unwrap();

let url = server.uri() + "/get";
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String concatenation using + creates temporary String allocations. Consider using format! macro for better performance: format!(\"{}/get\", server.uri())

Suggested change
let url = server.uri() + "/get";
let url = format!("{}/get", server.uri());

Copilot uses AI. Check for mistakes.
lua.load(mlua::chunk! {
local http = require("http")
local resp = http.get({ url = "https://httpbin.org/get" })
local resp = http.get({ url = $url })
assert(resp.status_code == 200)
assert(type(resp.body) == "string")
})
Expand All @@ -93,15 +113,29 @@ mod tests {

#[tokio::test]
async fn test_head() {
let server = MockServer::start().await;

Mock::given(method("HEAD"))
.and(path("/get"))
.respond_with(
ResponseTemplate::new(200)
.insert_header("content-type", "application/json")
.insert_header("x-test-header", "test-value"),
)
.mount(&server)
.await;

let lua = Lua::new();
mod_http(&lua).unwrap();

let url = server.uri() + "/get";
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String concatenation using + creates temporary String allocations. Consider using format! macro for better performance: format!(\"{}/get\", server.uri())

Suggested change
let url = server.uri() + "/get";
let url = format!("{}/get", server.uri());

Copilot uses AI. Check for mistakes.
lua.load(mlua::chunk! {
local http = require("http")
local resp = http.head({ url = "https://httpbin.org/get" })
print(resp.headers)
local resp = http.head({ url = $url })
assert(resp.status_code == 200)
assert(type(resp.headers) == "table")
assert(resp.headers["content-type"] == "application/json")
assert(resp.headers["x-test-header"] == "test-value")
assert(resp.content_length == nil)
})
.exec_async()
Expand All @@ -110,24 +144,47 @@ mod tests {
}

#[tokio::test]
#[ignore] // TODO: find out why this often fails in CI
async fn test_download_file() {
let server = MockServer::start().await;

// Create test content
let test_content = r#"{"name": "vfox-nodejs", "version": "1.0.0"}"#;

Mock::given(method("GET"))
.and(path("/index.json"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(test_content)
.insert_header("content-type", "application/json"),
)
.mount(&server)
.await;

let lua = Lua::new();
mod_http(&lua).unwrap();
let path = "test/data/test_download_file.txt";

// Use isolated temp directory for test isolation
let temp_dir = tempfile::TempDir::new().unwrap();
let path = temp_dir.path().join("download_file.txt");
let path_str = path.to_string_lossy().to_string();
let url = server.uri() + "/index.json";
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String concatenation using + creates temporary String allocations. Consider using format! macro for better performance: format!(\"{}/index.json\", server.uri())

Suggested change
let url = server.uri() + "/index.json";
let url = format!("{}/index.json", server.uri());

Copilot uses AI. Check for mistakes.

lua.load(mlua::chunk! {
local http = require("http")
err = http.download_file({
url = "https://vfox-plugins.lhan.me/index.json",
url = $url,
headers = {}
}, $path)
}, $path_str)
assert(err == nil, [[must be nil]])
})
.exec_async()
.await
.unwrap();
// TODO: figure out why this fails on gha
assert!(fs::read_to_string(path).unwrap().contains("vfox-nodejs"));
tokio::fs::remove_file(path).await.unwrap();

// Verify file was downloaded correctly
let content = fs::read_to_string(&path).unwrap();
assert!(content.contains("vfox-nodejs"));

// TempDir automatically cleans up when dropped
}
}
Loading