Skip to content

Commit 81e042b

Browse files
committed
1. Better Panic Info
2. add feature conditon to tests, and manage test scripts better
1 parent 675db85 commit 81e042b

File tree

8 files changed

+84
-30
lines changed

8 files changed

+84
-30
lines changed

src/lua.rs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,11 +3194,11 @@ impl Lua {
31943194
}
31953195

31963196
/// Compile all the files to bytecode under a directory, save as `*.bin`
3197-
///
3197+
///
31983198
/// It designs for build script, so there will be no error return.
3199-
///
3199+
///
32003200
/// It will automatically print cargo:rerun-if-changed=*
3201-
///
3201+
///
32023202
/// In release mode, it may not save all the debug information, see also [`Function::dump()`]
32033203
///
32043204
/// # Examples
@@ -3216,13 +3216,21 @@ impl Lua {
32163216
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
32173217
#[track_caller]
32183218
#[inline]
3219-
pub fn compile_single(&self, path: &str) -> &Self {
3219+
pub fn compile_single(&self, path: impl AsRef<std::path::Path>) -> &Self {
32203220
use std::fs;
32213221
use std::path::PathBuf;
32223222

3223-
println!("cargo:rerun-if-changed={}", path);
3223+
let path = path.as_ref();
32243224

3225-
let bytes = fs::read(path).unwrap();
3225+
println!("cargo:rerun-if-changed={}", path.display());
3226+
3227+
let bytes = fs::read(path).unwrap_or_else(|err| {
3228+
panic!(
3229+
"Error caused while reading the source file\nAt Path: {}\nCaused by:\n\t{:?}",
3230+
path.display(),
3231+
err
3232+
)
3233+
});
32263234

32273235
let strip;
32283236
#[cfg(debug_assertions)]
@@ -3234,22 +3242,34 @@ impl Lua {
32343242
strip = true;
32353243
}
32363244

3237-
let bytecode = self.load(&bytes).into_function().unwrap().dump(strip);
3245+
let bytecode = self.load(&bytes).into_function().unwrap_or_else(|err|{
3246+
panic!(
3247+
"Error caused while converting chunk into function\nAt Path: {}\nCaused by:\n\t{:?}",
3248+
path.display(),
3249+
err
3250+
)
3251+
}).dump(strip);
32383252

32393253
let mut path = PathBuf::from(path);
32403254
path.set_extension("bin");
32413255

3242-
fs::write(path, bytecode).unwrap();
3256+
fs::write(&path, bytecode).unwrap_or_else(|err| {
3257+
panic!(
3258+
"Error caused while writing the bytecode\nAt Path: {}\nCaused by:\n\t{:?}",
3259+
path.display(),
3260+
err
3261+
)
3262+
});
32433263

32443264
self
32453265
}
32463266

32473267
/// Compile all the files with the extension `lua` to bytecode under a directory, save as `*.bin`
3248-
///
3268+
///
32493269
/// It is designed for build script, so there will be no error return.
3250-
///
3270+
///
32513271
/// It automatically print cargo:rerun-if-changed=* of each script file
3252-
///
3272+
///
32533273
/// It calls [`Lua::compile_single()`] on every file
32543274
///
32553275
/// # Examples
@@ -3266,28 +3286,24 @@ impl Lua {
32663286
#[cfg(not(feature = "luau"))]
32673287
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
32683288
#[track_caller]
3269-
pub fn compile_directory(&self, path: &str) -> &Self {
3289+
pub fn compile_directory(&self, path: impl AsRef<std::path::Path>) -> &Self {
32703290
use std::fs;
3271-
use std::string::String;
3291+
use std::path::{Path, PathBuf};
32723292
#[track_caller]
3273-
fn all_files(path: &str) -> std::io::Result<Vec<String>> {
3293+
fn all_files(path: impl AsRef<Path>) -> std::io::Result<Vec<PathBuf>> {
32743294
// here use a BFS to traversal all the files under a directory
3275-
let mut stk = vec![path.to_string()];
3295+
let mut stk = vec![path.as_ref().to_path_buf()];
32763296
let mut ans = Vec::new();
32773297
while let Some(path) = stk.pop() {
32783298
for entry in fs::read_dir(path)? {
32793299
let entry = entry?;
32803300
let path = entry.path();
3281-
let s = path
3282-
.to_str()
3283-
.unwrap_or_else(|| panic!("a path : {} is not UTF-8", path.display()))
3284-
.to_string();
32853301
if path.is_dir() {
3286-
stk.push(s);
3302+
stk.push(path);
32873303
} else {
3288-
if let Some(exe)=path.extension(){
3289-
if exe=="lua"{
3290-
ans.push(s);
3304+
if let Some(exe) = path.extension() {
3305+
if exe == "lua" {
3306+
ans.push(path);
32913307
}
32923308
}
32933309
}
@@ -3297,10 +3313,18 @@ impl Lua {
32973313
Ok(ans)
32983314
}
32993315

3300-
let files = all_files(path).expect("Fail to traversal the directory");
3316+
let path = path.as_ref();
3317+
3318+
let files = all_files(path).unwrap_or_else(|err| {
3319+
panic!(
3320+
"Error caused while traversal the directory\nAt Path: {}\nCaused by:\n\t{:?}",
3321+
path.display(),
3322+
err
3323+
)
3324+
});
33013325

33023326
for path in files {
3303-
self.compile_single(path.as_str());
3327+
self.compile_single(path);
33043328
}
33053329

33063330
self

tests/chunk.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::io;
55

66
use mlua::{Lua, Result};
77

8+
use std::path::{Path, PathBuf};
9+
810
#[test]
911
#[cfg(not(target_arch = "wasm32"))]
1012
fn test_chunk_path() -> Result<()> {
@@ -29,35 +31,59 @@ fn test_chunk_path() -> Result<()> {
2931
}
3032

3133
#[test]
34+
#[cfg(not(feature = "luau"))]
3235
fn test_compile() {
3336
let lua = Lua::new();
3437

38+
let work_dir = Path::new("./tests/scripts/compile/");
39+
3540
let assert = || {
3641
assert_eq!(
37-
lua.load(fs::read("./tests/scripts/a.bin").unwrap())
42+
lua.load(fs::read(work_dir.join("a.bin")).unwrap())
3843
.eval::<String>()
3944
.unwrap(),
4045
"Helloworld".to_string()
4146
);
4247

4348
assert_eq!(
44-
lua.load(fs::read("./tests/scripts/b.bin").unwrap())
49+
lua.load(fs::read(work_dir.join("b.bin")).unwrap())
4550
.eval::<String>()
4651
.unwrap(),
4752
"Helloworld".to_string()
4853
);
4954
};
5055

51-
lua.compile_single("./tests/scripts/a.lua")
52-
.compile_single("./tests/scripts/b.lua");
56+
lua.compile_single(work_dir.join("a.lua"))
57+
.compile_single(work_dir.join("b.lua"));
5358

5459
assert();
5560

56-
lua.compile_directory("./tests/scripts");
61+
lua.compile_directory(work_dir);
5762

5863
assert();
5964
}
6065

66+
#[test]
67+
#[cfg(not(feature = "luau"))]
68+
fn multi_file_def() {
69+
let lua = Lua::new();
70+
71+
let work_dir = Path::new("./tests/scripts/multi_file_def");
72+
73+
lua.compile_directory(work_dir);
74+
75+
lua.load(fs::read(work_dir.join("c.bin")).unwrap())
76+
.exec()
77+
.unwrap();
78+
79+
let value = lua
80+
.load(fs::read(work_dir.join("d.bin")).unwrap())
81+
.eval::<String>()
82+
.unwrap();
83+
84+
assert_eq!(value.as_str(), "Hello world !");
85+
}
86+
6187
#[test]
6288
#[cfg(feature = "macros")]
6389
fn test_chunk_macro() -> Result<()> {
File renamed without changes.
File renamed without changes.

tests/scripts/compile/info.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Belong to [test_compile()](../../chunk.rs)

tests/scripts/multi_file_def/c.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello = "Hello world !"

tests/scripts/multi_file_def/d.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return hello;

tests/scripts/multi_file_def/info.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Belong to [multi_file_def()](../../chunk.rs)

0 commit comments

Comments
 (0)