commit 76b37adeb9bfa40f6cfdb03a4d896d4c3637330a
parent 20d800b81701184856de8ceaeb913fde53f592de
Author: Stefan Koch <programming@stefan-koch.name>
Date: Sun, 10 May 2020 12:25:10 +0200
on slash at end of path use previous folder as project name
Diffstat:
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/src/config.rs b/src/config.rs
@@ -1,5 +1,6 @@
use std::fs;
use std::path::PathBuf;
+use std::vec::Vec;
use serde::Deserialize;
use toml;
@@ -61,7 +62,18 @@ impl ExecutionConfig {
// TODO: This approach only works for URLs, not for local paths.
// TODO: Move the name() function to the CodeSource
pub fn name(&self) -> String {
- self.repo_url.split('/').last().unwrap().to_string()
+ let components: Vec<&str> = self.repo_url.split('/').collect();
+
+ // TODO: Make more Rusty
+ // TODO: Always use the canonical path for getting project name? e.g.
+ // when user defines "." as path, still use the folder name
+ if components.last().is_some() && components.last().unwrap().to_string() != "" {
+ return components.last().unwrap().to_string();
+ } else if components.len() >= 2 {
+ return components[components.len() - 2].to_string();
+ } else {
+ return "".to_string();
+ }
}
pub fn cinderella_file(&self, folder: &PathBuf) -> PathBuf {
@@ -92,6 +104,27 @@ mod tests {
use tempfile::NamedTempFile;
#[test]
+ fn test_extract_project_name_from_path() {
+ let config = ExecutionConfig {
+ repo_url: "/path/to/repo".to_string(),
+ branch: Some("master".to_string()),
+ tag: None,
+ cinderella_filepath: None,
+ };
+ assert_eq!(config.name(), "repo");
+
+ // even if the path ends with a slash, we should extract the right
+ // project name
+ let config = ExecutionConfig {
+ repo_url: "/path/to/repo.git/".to_string(),
+ branch: Some("master".to_string()),
+ tag: None,
+ cinderella_filepath: None,
+ };
+ assert_eq!(config.name(), "repo.git");
+ }
+
+ #[test]
fn test_load_valid_config() {
let config = r#"
[email]