From 021d82a224ffe6d3bfc4802fe0178c2f0a92f113 Mon Sep 17 00:00:00 2001 From: Thomas Haessle Date: Sun, 5 Jul 2020 19:20:11 +0200 Subject: [PATCH] fix: manage sandboxed version of OCaml (#1433) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: manage sandboxed version of OCaml * fmt: apply cargo fmt Co-authored-by: Thomas Haesslé --- src/modules/nodejs.rs | 20 +++++++++++++++++++- src/modules/ocaml.rs | 20 +++++++++++++++++--- src/utils.rs | 4 ++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index e1717a0a..100717f8 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -18,7 +18,12 @@ pub fn module<'a>(context: &'a Context) -> Option> { .set_folders(&["node_modules"]) .is_match(); - if !is_js_project { + let is_esy_project = context + .try_begin_scan()? + .set_folders(&["esy.lock"]) + .is_match(); + + if !is_js_project || is_esy_project { return None; } @@ -63,6 +68,19 @@ mod tests { dir.close() } + #[test] + fn folder_with_package_json_and_esy_lock() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("package.json"))?.sync_all()?; + let esy_lock = dir.path().join("esy.lock"); + fs::create_dir_all(&esy_lock)?; + + let actual = render_module("nodejs", dir.path(), None); + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_node_version() -> io::Result<()> { let dir = tempfile::tempdir()?; diff --git a/src/modules/ocaml.rs b/src/modules/ocaml.rs index ad61305c..be132b0e 100644 --- a/src/modules/ocaml.rs +++ b/src/modules/ocaml.rs @@ -24,7 +24,17 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let ocaml_version = utils::exec_cmd("ocaml", &["-vnum"])?.stdout; + let is_esy_project = context + .try_begin_scan()? + .set_folders(&["esy.lock"]) + .is_match(); + + let ocaml_version = if is_esy_project { + utils::exec_cmd("esy", &["ocaml", "-vnum"])?.stdout + } else { + utils::exec_cmd("ocaml", &["-vnum"])?.stdout + }; + let formatted_version = format!("v{}", &ocaml_version); let mut module = context.new_module("ocaml"); @@ -79,9 +89,13 @@ mod tests { fn folder_with_esy_lock_directory() -> io::Result<()> { let dir = tempfile::tempdir()?; fs::create_dir_all(dir.path().join("esy.lock"))?; - + File::create(dir.path().join("package.json"))?.sync_all()?; + fs::write( + dir.path().join("package.lock"), + "{\"dependencies\": {\"ocaml\": \"4.8.1000\"}}", + )?; let actual = render_module("ocaml", dir.path(), None); - let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.10.0"))); + let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐫 v4.08.1"))); assert_eq!(expected, actual); dir.close() } diff --git a/src/utils.rs b/src/utils.rs index ee7af608..79b8d883 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -87,6 +87,10 @@ active boot switches: -d:release\n", stdout: String::from("4.10.0"), stderr: String::default(), }), + "esy ocaml -vnum" => Some(CommandOutput { + stdout: String::from("4.08.1"), + stderr: String::default(), + }), "php -nr echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;" => { Some(CommandOutput { stdout: String::from("7.3.8"),