Repository navigation
Expand file tree
/
Copy pathcli.rs
More file actions
104 lines (94 loc) · 3.43 KB
/
Copy pathcli.rs
File metadata and controls
104 lines (94 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Command-line argument resolution.
use std::path::Path;
/// Resolves the CLI arg to a concrete script file. Accepts any of:
/// - path to a `.lua` file
/// - path to a directory containing `main.lua`
/// - path without extension that has a sibling `.lua` file
///
/// Errors with a helpful message if none match.
pub fn resolve_script_path(arg: &str) -> Result<String, String> {
let path = Path::new(arg);
if path.is_dir() {
let main = path.join("main.lua");
if main.exists() {
return main
.to_str()
.map(String::from)
.ok_or_else(|| format!("non-utf8 path: {}", main.display()));
}
return Err(format!(
"no main.lua found in directory '{}'. Create a main.lua there, or pass a .lua file directly.",
path.display()
));
}
if path.is_file() {
return Ok(arg.to_string());
}
let with_lua = path.with_extension("lua");
if with_lua.is_file() {
return with_lua
.to_str()
.map(String::from)
.ok_or_else(|| format!("non-utf8 path: {}", with_lua.display()));
}
Err(format!(
"script not found: '{}'. Pass a .lua file, a directory with main.lua, or a name with a sibling .lua.",
arg
))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn resolves_direct_lua_file() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("game.lua");
fs::write(&file, "-- test").unwrap();
let arg = file.to_str().unwrap();
assert_eq!(resolve_script_path(arg).unwrap(), arg);
}
#[test]
fn resolves_dir_with_main_lua() {
let dir = TempDir::new().unwrap();
let main = dir.path().join("main.lua");
fs::write(&main, "-- test").unwrap();
let resolved = resolve_script_path(dir.path().to_str().unwrap()).unwrap();
assert_eq!(resolved, main.to_str().unwrap());
}
#[test]
fn errors_for_dir_without_main_lua() {
let dir = TempDir::new().unwrap();
let err = resolve_script_path(dir.path().to_str().unwrap()).unwrap_err();
assert!(err.contains("no main.lua"), "got: {err}");
}
#[test]
fn errors_for_missing_path() {
let err = resolve_script_path("/definitely/not/a/real/path/xyzzy").unwrap_err();
assert!(err.contains("not found"), "got: {err}");
}
#[test]
fn adds_lua_extension_when_missing() {
let dir = TempDir::new().unwrap();
let file = dir.path().join("foo.lua");
fs::write(&file, "-- test").unwrap();
let bare = dir.path().join("foo");
let resolved = resolve_script_path(bare.to_str().unwrap()).unwrap();
assert_eq!(resolved, file.to_str().unwrap());
}
#[test]
fn dir_takes_precedence_over_sibling_lua() {
// If `foo/` exists as a dir with main.lua AND `foo.lua` exists, the
// dir should win because is_dir() is checked first.
let dir = TempDir::new().unwrap();
let subdir = dir.path().join("foo");
fs::create_dir(&subdir).unwrap();
let main = subdir.join("main.lua");
fs::write(&main, "-- dir").unwrap();
let sibling = dir.path().join("foo.lua");
fs::write(&sibling, "-- sibling").unwrap();
let resolved = resolve_script_path(subdir.to_str().unwrap()).unwrap();
assert_eq!(resolved, main.to_str().unwrap());
}
}