From 2c49350a007d7bd6907c33b8aebe8e9999fef967 Mon Sep 17 00:00:00 2001 From: Kousuke Saruta Date: Thu, 10 Aug 2023 11:13:53 +0900 Subject: [PATCH] fix(oli): oli commands don't work properly for files in CWD (#2833) * Fix oli to handle files in CWD. * Fix clippy. --- bin/oli/src/config/mod.rs | 5 ++++- bin/oli/tests/cat.rs | 22 ++++++++++++++++++++++ bin/oli/tests/cp.rs | 21 +++++++++++++++++++++ bin/oli/tests/rm.rs | 16 ++++++++++++++++ bin/oli/tests/stat.rs | 24 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/bin/oli/src/config/mod.rs b/bin/oli/src/config/mod.rs index 1452375b2c40..adb39665a0b1 100644 --- a/bin/oli/src/config/mod.rs +++ b/bin/oli/src/config/mod.rs @@ -135,7 +135,10 @@ impl Config { fs_builder.root(if base.is_empty() { "/" } else { base }); filename } - _ => s, + _ => { + fs_builder.root("."); + s + } }; return Ok((Operator::new(fs_builder)?.finish(), filename.into())); diff --git a/bin/oli/tests/cat.rs b/bin/oli/tests/cat.rs index 44c86fab0fdf..7ab9eea75116 100644 --- a/bin/oli/tests/cat.rs +++ b/bin/oli/tests/cat.rs @@ -40,3 +40,25 @@ async fn test_basic_cat() -> Result<()> { assert_eq!(output_stdout, actual); Ok(()) } + +#[tokio::test] +async fn test_cat_for_path_in_current_dir() -> Result<()> { + let dir = tempfile::tempdir()?; + let dst_path = dir.path().join("dst.txt"); + let expect = "hello"; + fs::write(&dst_path, expect)?; + + let mut cmd = Command::cargo_bin("oli")?; + + cmd.arg("cat") + .arg("dst.txt") + .current_dir(dir.path().clone()); + let actual = fs::read_to_string(&dst_path)?; + let res = cmd.assert().success(); + let output = res.get_output().stdout.clone(); + + let output_stdout = String::from_utf8(output)?; + + assert_eq!(output_stdout, actual); + Ok(()) +} diff --git a/bin/oli/tests/cp.rs b/bin/oli/tests/cp.rs index ed6afd7c4da4..d9ef28efa69d 100644 --- a/bin/oli/tests/cp.rs +++ b/bin/oli/tests/cp.rs @@ -40,3 +40,24 @@ async fn test_basic_cp() -> Result<()> { assert_eq!(expect, actual); Ok(()) } + +#[tokio::test] +async fn test_cp_for_path_in_current_dir() -> Result<()> { + let dir = tempfile::tempdir()?; + let src_path = dir.path().join("src.txt"); + let dst_path = dir.path().join("dst.txt"); + let expect = "hello"; + fs::write(src_path, expect)?; + + let mut cmd = Command::cargo_bin("oli")?; + + cmd.arg("cp") + .arg("src.txt") + .arg("dst.txt") + .current_dir(dir.path().clone()); + cmd.assert().success(); + + let actual = fs::read_to_string(dst_path)?; + assert_eq!(expect, actual); + Ok(()) +} diff --git a/bin/oli/tests/rm.rs b/bin/oli/tests/rm.rs index 98ee6b3813b1..81e8c90a61b1 100644 --- a/bin/oli/tests/rm.rs +++ b/bin/oli/tests/rm.rs @@ -36,3 +36,19 @@ async fn test_basic_rm() -> Result<()> { assert!(fs::read_to_string(&dst_path).is_err()); Ok(()) } + +#[tokio::test] +async fn test_rm_for_path_in_current_dir() -> Result<()> { + let dir = tempfile::tempdir()?; + let dst_path = dir.path().join("dst.txt"); + let expect = "hello"; + fs::write(&dst_path, expect)?; + + let mut cmd = Command::cargo_bin("oli")?; + + cmd.arg("rm").arg("dst.txt").current_dir(dir.path().clone()); + cmd.assert().success(); + + assert!(fs::read_to_string(&dst_path).is_err()); + Ok(()) +} diff --git a/bin/oli/tests/stat.rs b/bin/oli/tests/stat.rs index 62e6e3180b63..aa4da57957e3 100644 --- a/bin/oli/tests/stat.rs +++ b/bin/oli/tests/stat.rs @@ -44,3 +44,27 @@ async fn test_basic_stat() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn test_stat_for_path_in_current_dir() -> Result<()> { + let dir = tempfile::tempdir()?; + let dst_path = dir.path().join("dst.txt"); + let expect = "hello"; + fs::write(dst_path, expect)?; + + let mut cmd = Command::cargo_bin("oli")?; + + cmd.arg("stat") + .arg("dst.txt") + .current_dir(dir.path().clone()); + let res = cmd.assert().success(); + let output = res.get_output().stdout.clone(); + + let output_stdout = String::from_utf8(output)?; + assert!(output_stdout.contains("path: dst.txt")); + assert!(output_stdout.contains("size: 5")); + assert!(output_stdout.contains("type: file")); + assert!(output_stdout.contains("last-modified: ")); + + Ok(()) +}