This commit is contained in:
Shunsuke Shibayama 2023-06-01 12:07:59 +09:00
parent 888395d589
commit ef3bb68044
5 changed files with 80 additions and 9 deletions

View file

@ -155,9 +155,13 @@ impl InputKind {
if let Self::File(path) = self {
let mut path = path.clone();
path.pop();
path
if path.parent().is_none() {
PathBuf::from(".")
} else {
path
}
} else {
PathBuf::new()
PathBuf::from(".")
}
}
}

View file

@ -1,9 +1,8 @@
use std::mem;
use std::path::Path;
use std::path::{Path, PathBuf};
use erg_common::config::ErgConfig;
use erg_common::pathutil::squash;
use erg_common::python_util::BUILTIN_PYTHON_MODS;
use erg_common::traits::Locational;
use erg_common::Str;
use erg_common::{enum_unwrap, log};
@ -384,13 +383,21 @@ impl<'a> HIRLinker<'a> {
/// x = __import__("a.x").x
/// ```
fn replace_py_import(&self, expr: &mut Expr) {
let mut dir = self.cfg.input.dir();
let args = &mut enum_unwrap!(expr, Expr::Call).args;
let mod_name_lit = enum_unwrap!(args.remove_left_or_key("Path").unwrap(), Expr::Lit);
let mod_name_str = enum_unwrap!(mod_name_lit.value.clone(), ValueObj::Str);
if BUILTIN_PYTHON_MODS.contains(&&mod_name_str[..]) {
args.push_pos(PosArg::new(Expr::Lit(mod_name_lit)));
return;
let mut dir = self.cfg.input.dir();
let mod_path = self
.cfg
.input
.resolve_decl_path(Path::new(&mod_name_str[..]))
.unwrap();
if !mod_path
.canonicalize()
.unwrap()
.starts_with(&dir.canonicalize().unwrap())
{
dir = PathBuf::new();
}
let mod_name_str = if let Some(stripped) = mod_name_str.strip_prefix("./") {
stripped

View file

@ -9,6 +9,7 @@ use erg_common::style::{colors::DEBUG_MAIN, RESET};
use erg_common::traits::{ExitStatus, Runnable, Stream};
use erg_compiler::error::CompileErrors;
use erg_compiler::Compiler;
use erg::DummyVM;
@ -52,6 +53,31 @@ pub(crate) fn expect_success(file_path: &'static str, num_warns: usize) -> Resul
}
}
pub(crate) fn expect_compile_success(file_path: &'static str, num_warns: usize) -> Result<(), ()> {
match exec_compiler(file_path) {
Ok(stat) if stat.succeed() => {
if stat.num_warns == num_warns {
Ok(())
} else {
println!(
"err: number of warnings should be {num_warns}, but got {}",
stat.num_warns
);
Err(())
}
}
Ok(stat) => {
println!("err: should succeed, but end with {}", stat.code);
Err(())
}
Err(errs) => {
println!("err: should succeed, but got compile errors");
errs.fmt_all_stderr();
Err(())
}
}
}
pub(crate) fn expect_repl_failure(
name: &'static str,
lines: Vec<String>,
@ -175,6 +201,13 @@ pub fn _exec_repl(name: &'static str, lines: Vec<String>) -> Result<ExitStatus,
Ok(stat)
}
pub fn _exec_compiler(file_path: &'static str) -> Result<ExitStatus, CompileErrors> {
println!("{DEBUG_MAIN}[test] exec compiler: {file_path}{RESET}");
let cfg = ErgConfig::with_main_path(PathBuf::from(file_path));
let mut compiler = Compiler::new(set_cfg(cfg));
compiler.exec()
}
pub(crate) fn exec_file(file_path: &'static str) -> Result<ExitStatus, CompileErrors> {
exec_new_thread(move || _exec_file(file_path), file_path)
}
@ -185,3 +218,7 @@ pub(crate) fn exec_repl(
) -> Result<ExitStatus, CompileErrors> {
exec_new_thread(move || _exec_repl(name, lines), name)
}
pub(crate) fn exec_compiler(file_path: &'static str) -> Result<ExitStatus, CompileErrors> {
exec_new_thread(move || _exec_compiler(file_path), file_path)
}

View file

@ -0,0 +1,12 @@
time = pyimport "time"
tqdm = pyimport "tqdm"
for! tqdm.Tqdm!(0..<10), _ =>
time.sleep! 0.00001
plt = pyimport "matplotlib/pyplot"
discard plt.plot! 0..<10, [2, 3, 2, 3, 2, 3, 2, 3, 2, 3]
discard plt.title! "My Plot"
discard plt.xlabel! "X"
# plt.show!()

View file

@ -1,5 +1,6 @@
mod common;
use common::{expect_end_with, expect_failure, expect_success};
use common::{expect_compile_success, expect_end_with, expect_failure, expect_success};
use erg_common::python_util::{module_exists, opt_which_python};
#[test]
fn exec_addition_ok() -> Result<(), ()> {
@ -56,6 +57,16 @@ fn exec_dict() -> Result<(), ()> {
expect_success("examples/dict.er", 0)
}
#[test]
fn exec_external() -> Result<(), ()> {
let py_command = opt_which_python().unwrap();
if module_exists(&py_command, "matplotlib") && module_exists(&py_command, "tqdm") {
expect_success("tests/should_ok/external.er", 0)
} else {
expect_compile_success("tests/should_ok/external.er", 0)
}
}
#[test]
fn exec_fib() -> Result<(), ()> {
expect_success("examples/fib.er", 0)