Skip to content

Commit d24abd5

Browse files
committed
tests: logs and fixes
1 parent 2a14753 commit d24abd5

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/scarb.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, fs, path::PathBuf};
1+
use std::{env::current_dir, fs, path::PathBuf};
22

33
use anyhow::Context;
44
use cairo_lang_test_plugin::TestCompilation;
@@ -10,19 +10,25 @@ use scarb::{
1010
ops::{self, collect_metadata, CompileOpts, MetadataOptions},
1111
};
1212

13-
pub fn prepare_crate_for_exercise(file_path: &PathBuf, crate_path: PathBuf) {
14-
let lib_path = crate_path.join("src/lib.cairo");
15-
match fs::copy(file_path, lib_path) {
13+
pub fn prepare_crate_for_exercise(file_path: &PathBuf) -> PathBuf {
14+
let crate_path = current_dir().unwrap().join(PathBuf::from("runner-crate"));
15+
let src_dir = crate_path.join("src");
16+
if !src_dir.exists() {
17+
let _ = fs::create_dir(&src_dir);
18+
}
19+
let lib_path = src_dir.join("lib.cairo");
20+
let file_path = current_dir().unwrap().join(file_path);
21+
22+
match fs::copy(&file_path, &lib_path) {
1623
Ok(_) => {}
17-
Err(err) => panic!("Error occurred while preparing the exercise:\n {err:?}"),
24+
Err(err) => panic!("Error occurred while preparing the exercise,\nExercise: {file_path:?}\nLib path: {lib_path:?}\n{err:?}"),
1825
};
26+
crate_path
1927
}
2028

2129
pub fn scarb_build(file_path: &PathBuf) -> anyhow::Result<String> {
22-
let path = env::current_dir().unwrap();
23-
let (config, crate_path) = scarb_config(path.join(PathBuf::from("runner-crate")));
24-
25-
prepare_crate_for_exercise(file_path, crate_path);
30+
let crate_path = prepare_crate_for_exercise(file_path);
31+
let config = scarb_config(crate_path);
2632

2733
match compile(&config, false) {
2834
Ok(_) => Ok("".into()),
@@ -31,10 +37,8 @@ pub fn scarb_build(file_path: &PathBuf) -> anyhow::Result<String> {
3137
}
3238

3339
pub fn scarb_test(file_path: &PathBuf) -> anyhow::Result<String> {
34-
let path = env::current_dir().unwrap();
35-
let (config, crate_path) = scarb_config(path.join(PathBuf::from("runner-crate")));
36-
37-
prepare_crate_for_exercise(file_path, crate_path);
40+
let crate_path = prepare_crate_for_exercise(file_path);
41+
let config = scarb_config(crate_path);
3842

3943
let ws = ops::read_workspace(config.manifest_path(), &config)?;
4044

@@ -49,7 +53,7 @@ pub fn scarb_test(file_path: &PathBuf) -> anyhow::Result<String> {
4953
)
5054
.unwrap();
5155

52-
let profile = env::var("SCARB_PROFILE").unwrap_or("dev".into());
56+
let profile = "dev";
5357
let default_target_dir = metadata.runtime_manifest.join("target");
5458

5559
let target_dir = metadata
@@ -88,12 +92,12 @@ pub fn scarb_test(file_path: &PathBuf) -> anyhow::Result<String> {
8892
anyhow::Ok("".into())
8993
}
9094

91-
pub fn scarb_config(crate_path: PathBuf) -> (Config, PathBuf) {
95+
pub fn scarb_config(crate_path: PathBuf) -> Config {
9296
let path = Utf8PathBuf::from_path_buf(crate_path.join(PathBuf::from("Scarb.toml"))).unwrap();
9397

9498
let config = Config::builder(path).build().unwrap();
9599

96-
(config, crate_path)
100+
config
97101
}
98102

99103
pub fn compile(config: &Config, test_targets: bool) -> anyhow::Result<()> {

0 commit comments

Comments
 (0)