use javascript::*;
#[ctor::ctor(unsafe)]
fn __init_test_logger() {
let _ = env_logger::Builder::from_env(env_logger::Env::default()).is_test(true).try_init();
}
#[cfg(test)]
mod os_tests {
use super::*;
#[test]
#[cfg(feature = "os")]
fn test_os_open_close() {
let script = r#"
import * as os from "os";
let out = -1;
let fd = os.open("test.txt", 578);
if (fd >= 0) {
let result = os.close(fd);
out = result;
}
out;
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert_eq!(result, "0");
std::fs::remove_file("test.txt").ok();
}
#[test]
#[cfg(feature = "os")]
fn test_os_write_read() {
let script = r#"
import * as os from "os";
let out = "";
let fd = os.open("test_write.txt", 578);
if (fd >= 0) {
let written = os.write(fd, "Hello World");
os.seek(fd, 0, 0);
let data = os.read(fd, 11);
os.close(fd);
out = data;
}
out;
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert_eq!(result, "\"Hello World\"");
std::fs::remove_file("test_write.txt").ok();
}
#[test]
#[cfg(feature = "os")]
fn test_os_getcwd() {
let script = r#"
import * as os from "os";
os.getcwd();
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
let expected_cwd = std::env::current_dir().unwrap().to_str().unwrap().to_string();
assert_eq!(result, serde_json::to_string(&expected_cwd).unwrap());
}
#[test]
#[cfg(feature = "os")]
fn test_os_getpid() {
let script = r#"
import * as os from "os";
os.getpid();
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert!(result.parse::<i32>().unwrap() > 0);
}
#[test]
#[cfg(feature = "os")]
fn test_os_path_join() {
let script = r#"
import * as os from "os";
os.path.join("a", "b", "c");
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
let expected = format!("a{}b{}c", std::path::MAIN_SEPARATOR, std::path::MAIN_SEPARATOR);
assert_eq!(result, serde_json::to_string(&expected).unwrap());
}
#[test]
#[cfg(feature = "os")]
fn test_os_path_basename() {
let script = r#"
import * as os from "os";
os.path.basename("path/to/file.txt");
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert_eq!(result, "\"file.txt\"");
}
#[test]
#[cfg(feature = "os")]
fn test_os_path_extname() {
let script = r#"
import * as os from "os";
os.path.extname("file.txt");
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert_eq!(result, "\".txt\"");
}
#[test]
#[cfg(feature = "os")]
fn test_os_getppid() {
let script = r#"
import * as os from "os";
os.getppid();
"#;
let result = evaluate_script(script, true, None::<&std::path::Path>).unwrap();
assert!(result.parse::<i32>().unwrap() > 0);
}
}