Skip to content

Commit

Permalink
fix: added os.setenv (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Jan 9, 2025
1 parent 5fce873 commit 20da520
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/lua_mod/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use mlua::prelude::*;
use mlua::Table;

pub fn mod_env(lua: &Lua) -> LuaResult<()> {
let package: Table = lua.globals().get("package")?;
let loaded: Table = package.get("loaded")?;
let env = lua.create_table_from(vec![("setenv", lua.create_function(setenv)?)])?;
loaded.set("env", env.clone())?;
loaded.set("vfox.env", env)?;
Ok(())
}

fn setenv(_lua: &Lua, (key, val): (String, String)) -> LuaResult<()> {
std::env::set_var(key, val);
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_env() {
let lua = Lua::new();
mod_env(&lua).unwrap();
if cfg!(windows) {
lua.load(mlua::chunk! {
local env = require("env")
env.setenv("TEST_ENV", "myvar")
handle = io.popen("pwsh -Command \"echo $env:TEST_ENV\"")
result = handle:read("*a")
handle:close()
assert(result == "myvar\n")
})
.exec()
.unwrap();
} else {
lua.load(mlua::chunk! {
local env = require("env")
env.setenv("TEST_ENV", "myvar")
handle = io.popen("echo $TEST_ENV")
result = handle:read("*a")
handle:close()
assert(result == "myvar\n")
})
.exec()
.unwrap();
}
}
}
2 changes: 2 additions & 0 deletions src/lua_mod/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod archiver;
mod env;
mod file;
mod hooks;
mod html;
Expand All @@ -7,6 +8,7 @@ mod json;
mod strings;

pub use archiver::mod_archiver as archiver;
pub use env::mod_env as env;
pub use file::mod_file as file;
pub use hooks::mod_hooks as hooks;
pub use html::mod_html as html;
Expand Down
1 change: 1 addition & 0 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl Plugin {
lua_mod::http(&self.lua)?;
lua_mod::json(&self.lua)?;
lua_mod::strings(&self.lua)?;
lua_mod::env(&self.lua)?;

let metadata = self.load_metadata()?;
self.set_global("PLUGIN", metadata.clone())?;
Expand Down

0 comments on commit 20da520

Please sign in to comment.