fix: test runner now supports module imports

The `lux test` command used Parser::parse_source() and
check_program() directly, which meant test files with `import`
statements would fail with type errors. Now uses ModuleLoader
and check_program_with_modules() to properly resolve imports,
and run_with_modules() for execution.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-18 17:11:16 -05:00
parent fffacd2467
commit c23d9c7078

View File

@@ -1085,7 +1085,7 @@ fn run_tests(args: &[String]) {
for test_file in &test_files {
let path_str = test_file.to_string_lossy().to_string();
// Read and parse the file
// Read and parse the file (with module loading)
let source = match fs::read_to_string(test_file) {
Ok(s) => s,
Err(e) => {
@@ -1095,7 +1095,13 @@ fn run_tests(args: &[String]) {
}
};
let program = match Parser::parse_source(&source) {
use modules::ModuleLoader;
let mut loader = ModuleLoader::new();
if let Some(parent) = test_file.parent() {
loader.add_search_path(parent.to_path_buf());
}
let program = match loader.load_source(&source, Some(test_file.as_path())) {
Ok(p) => p,
Err(e) => {
println!(" {} {} {}", c(colors::RED, "\u{2717}"), path_str, c(colors::RED, &format!("parse error: {}", e)));
@@ -1104,9 +1110,9 @@ fn run_tests(args: &[String]) {
}
};
// Type check
// Type check with module support
let mut checker = typechecker::TypeChecker::new();
if let Err(errors) = checker.check_program(&program) {
if let Err(errors) = checker.check_program_with_modules(&program, &loader) {
println!(" {} {} {}", c(colors::RED, "\u{2717}"), path_str, c(colors::RED, "type error"));
for err in errors {
eprintln!(" {}", err);
@@ -1134,7 +1140,7 @@ fn run_tests(args: &[String]) {
interp.register_auto_migrations(&auto_migrations);
interp.reset_test_results();
match interp.run(&program) {
match interp.run_with_modules(&program, &loader) {
Ok(_) => {
let results = interp.get_test_results();
if results.failed == 0 && results.passed == 0 {
@@ -1168,8 +1174,8 @@ fn run_tests(args: &[String]) {
interp.register_auto_migrations(&auto_migrations);
interp.reset_test_results();
// First run the file to define all functions
if let Err(e) = interp.run(&program) {
// First run the file to define all functions and load imports
if let Err(e) = interp.run_with_modules(&program, &loader) {
println!(" {} {} {}", c(colors::RED, "\u{2717}"), test_name, c(colors::RED, &e.to_string()));
total_failed += 1;
continue;