Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: canonicalize path before parsing it as path. #155

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{config::get_default_author, consts};
use anyhow::anyhow;
use clap::Parser;
use minijinja::{context, Environment};
use rattler_conda_types::Platform;
use std::io::{Error, ErrorKind};
use std::{fs, path::PathBuf};

/// Creates a new project
Expand Down Expand Up @@ -40,7 +42,7 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments

pub async fn execute(args: Args) -> anyhow::Result<()> {
let env = Environment::new();
let dir = args.path;
let dir = get_dir(args.path)?;
let manifest_path = dir.join(consts::PROJECT_MANIFEST);
let gitignore_path = dir.join(".gitignore");

Expand All @@ -53,7 +55,15 @@ pub async fn execute(args: Args) -> anyhow::Result<()> {
fs::create_dir_all(&dir).ok();

// Write pixi.toml
let name = dir.file_name().unwrap().to_string_lossy();
let name = dir
.file_name()
.ok_or_else(|| {
anyhow!(
"Cannot get file or directory name from the path: {}",
dir.to_string_lossy()
)
})?
.to_string_lossy();
let version = "0.1.0";
let author = get_default_author();
let channels = if args.channels.is_empty() {
Expand Down Expand Up @@ -93,3 +103,53 @@ pub async fn execute(args: Args) -> anyhow::Result<()> {

Ok(())
}

fn get_dir(path: PathBuf) -> Result<PathBuf, Error> {
if path.components().count() == 1 {
Ok(std::env::current_dir().unwrap_or_default().join(path))
} else {
path.canonicalize().map_err(|e| match e.kind() {
ErrorKind::NotFound => Error::new(
ErrorKind::NotFound,
format!(
"Cannot find '{}' please make sure the folder is reachable",
path.to_string_lossy()
),
),
_ => Error::new(
ErrorKind::InvalidInput,
"Cannot canonicalize the given path",
),
})
}
}

#[cfg(test)]
mod tests {
use crate::cli::init::get_dir;
use std::path::PathBuf;

#[test]
fn test_get_name() {
assert_eq!(
get_dir(PathBuf::from(".")).unwrap(),
std::env::current_dir().unwrap()
);
assert_eq!(
get_dir(PathBuf::from("test_folder")).unwrap(),
std::env::current_dir().unwrap().join("test_folder")
);
assert_eq!(
get_dir(std::env::current_dir().unwrap()).unwrap(),
PathBuf::from(std::env::current_dir().unwrap().canonicalize().unwrap())
);
}

#[test]
fn test_get_name_panic() {
match get_dir(PathBuf::from("invalid/path")) {
Ok(_) => panic!("Expected error, but got OK"),
Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound),
}
}
}