Import files from folders above: import ..a.b, import ...a.b
Leading dots go up one folder less than their count (.. is one up, ... is two). The language server index and formatter understand them, and a missing file is reported with its real path. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b496a7b03
commit
910bde692a
11 files changed
+139
-8
No files matched your search
@@ -0,0 +1,66 @@
|
||||
//! Importing files from other folders: `import ..a.b` (one folder up), `import ...a.b` (two up).
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn tree(files: &[(&str, &str)]) -> PathBuf {
|
||||
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||
let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let dir = std::env::temp_dir().join(format!("rex-imports-{}-{n}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
for (name, text) in files {
|
||||
let f = dir.join(name);
|
||||
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
|
||||
std::fs::write(f, text).unwrap();
|
||||
}
|
||||
dir
|
||||
}
|
||||
|
||||
fn rex(dir: &PathBuf, sub: &str, file: &str) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_rex")).args(["run", file]).current_dir(dir.join(sub)).output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_to_file() {
|
||||
use rex::loader::key_file;
|
||||
assert_eq!(key_file("utils"), "utils.rex");
|
||||
assert_eq!(key_file("a.b"), "a/b.rex");
|
||||
assert_eq!(key_file("..a"), "../a.rex");
|
||||
assert_eq!(key_file("..a.b"), "../a/b.rex");
|
||||
assert_eq!(key_file("...a.b"), "../../a/b.rex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_from_a_folder_above() {
|
||||
let dir = tree(&[
|
||||
("engine/engine.rex", "pub struct Ball {\n pub x: float\n pub func moved(): float { return x + 1.0 }\n}\n\npub func hi(): int { return 7 }\n"),
|
||||
("game/app/main.rex", "import ...engine.engine\n\nfunc main() {\n var b = engine.Ball{2.0}\n print(engine.hi(), b.moved())\n}\n"),
|
||||
("game/side.rex", "pub func two(): int { return 2 }\n"),
|
||||
("game/app/other.rex", "import ..side\n\nfunc main() {\n print(side.two())\n}\n"),
|
||||
]);
|
||||
let o = rex(&dir, "game/app", "main.rex");
|
||||
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
|
||||
assert_eq!(String::from_utf8_lossy(&o.stdout), "7 3.0\n");
|
||||
let o = rex(&dir, "game/app", "other.rex");
|
||||
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
|
||||
assert_eq!(String::from_utf8_lossy(&o.stdout), "2\n");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_above_names_the_path() {
|
||||
let dir = tree(&[("a/main.rex", "import ..nope\n\nfunc main() { }\n")]);
|
||||
let o = rex(&dir, "a", "main.rex");
|
||||
let err = String::from_utf8_lossy(&o.stderr).to_string();
|
||||
assert!(!o.status.success());
|
||||
assert!(err.contains("cannot find module `..nope`") && err.contains("../nope.rex"), "{err}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_dot_is_not_an_import_path() {
|
||||
let dir = tree(&[("a/main.rex", "import .nope\n\nfunc main() { }\n")]);
|
||||
let o = rex(&dir, "a", "main.rex");
|
||||
assert!(!o.status.success());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user