Skip to content

Commit

Permalink
added some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Gadorek committed Jan 9, 2025
1 parent 93bddb7 commit efb834f
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/idf_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ mod tests {
assert_eq!(read_config.idf_installed.len(), config.idf_installed.len());

// Test appending to existing config
let mut new_installation = IdfInstallation {
let new_installation = IdfInstallation {
activation_script: String::from("/esp/idf/v5.1/export.sh"),
id: String::from("5.1"),
idf_tools_path: String::from("/home/user/.espressif/tools"),
Expand Down
46 changes: 46 additions & 0 deletions src/idf_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,49 @@ pub async fn get_idf_names() -> Vec<String> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_idf_versions_by_target() {
let releases = Releases {
VERSIONS: vec![
Version {
name: "v4.4.5".to_string(),
pre_release: false,
old: false,
end_of_life: false,
has_targets: true,
supported_targets: vec!["esp32".to_string(), "esp32s2".to_string()],
},
Version {
name: "v5.0.0".to_string(),
pre_release: false,
old: false,
end_of_life: false,
has_targets: true,
supported_targets: vec!["esp32".to_string()],
},
],
IDF_TARGETS: vec![
IDFTarget {
text: "ESP32".to_string(),
value: "esp32".to_string(),
},
IDFTarget {
text: "ESP32-S2".to_string(),
value: "esp32s2".to_string(),
},
],
RELEASES: HashMap::new(),
};

let versions_by_target = get_idf_versions_by_target(&releases);

assert_eq!(versions_by_target.len(), 2);
assert_eq!(versions_by_target.get("esp32").unwrap().len(), 2);
assert_eq!(versions_by_target.get("esp32s2").unwrap().len(), 1);
}
}
131 changes: 131 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,134 @@ pub fn remove_directory_all<P: AsRef<Path>>(path: P) -> io::Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs::{self, File};
use std::io::Write;
use tempfile::TempDir;

#[test]
fn test_find_directories_by_name() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();

// Create test directory structure
let test_dir1 = base_path.join("test_dir");
let test_dir2 = base_path.join("subdir").join("test_dir");
fs::create_dir_all(&test_dir1).unwrap();
fs::create_dir_all(&test_dir2).unwrap();

let results = find_directories_by_name(base_path, "test_dir");
assert_eq!(results.len(), 2);
assert!(results
.iter()
.any(|p| p.contains(test_dir1.to_str().unwrap())));
assert!(results
.iter()
.any(|p| p.contains(test_dir2.to_str().unwrap())));
}

#[test]
fn test_is_valid_idf_directory() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();

// Create invalid directory (no tools.json)
assert!(!is_valid_idf_directory(base_path.to_str().unwrap()));

// Create valid IDF directory structure
let tools_dir = base_path.join("tools");
fs::create_dir_all(&tools_dir).unwrap();
let tools_json_path = tools_dir.join("tools.json");
let mut file = File::create(tools_json_path).unwrap();
write!(file, r#"{{"tools": [], "version": 1}}"#).unwrap();

assert!(is_valid_idf_directory(base_path.to_str().unwrap()));
}

#[test]
fn test_filter_duplicate_paths() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();

// Create test files with different content
let file1_path = base_path.join("file1.txt");
let file2_path = base_path.join("file2.txt");

fs::write(&file1_path, "content1").unwrap();
fs::write(&file2_path, "content2").unwrap();

let paths = vec![
file1_path.to_string_lossy().to_string(),
file1_path.to_string_lossy().to_string(), // Duplicate
file2_path.to_string_lossy().to_string(),
];

let filtered = filter_duplicate_paths(paths);
assert_eq!(filtered.len(), 2);
}

#[test]
fn test_filter_subpaths() {
let paths = vec![
"/path/to/dir".to_string(),
"/path/to/dir/subdir".to_string(),
"/path/to/another".to_string(),
];

let filtered = filter_subpaths(paths);
assert_eq!(filtered.len(), 2);
assert!(filtered.contains(&"/path/to/dir".to_string()));
assert!(filtered.contains(&"/path/to/another".to_string()));
assert!(!filtered.contains(&"/path/to/dir/subdir".to_string()));
}

#[test]
fn test_remove_directory_all() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();

// Create test directory structure
let test_dir = base_path.join("test_dir");
let test_subdir = test_dir.join("subdir");
let test_file = test_dir.join("test.txt");

fs::create_dir_all(&test_subdir).unwrap();
fs::write(&test_file, "test content").unwrap();

// Test removal
assert!(remove_directory_all(&test_dir).is_ok());
assert!(!test_dir.exists());
}

#[test]
fn test_remove_directory_all_nonexistent() {
let temp_dir = TempDir::new().unwrap();
let non_existent = temp_dir.path().join("non_existent");

assert!(remove_directory_all(&non_existent).is_ok());
}

#[test]
fn test_remove_directory_all_readonly() {
let temp_dir = TempDir::new().unwrap();
let test_dir = temp_dir.path().join("readonly_dir");
let test_file = test_dir.join("readonly.txt");

fs::create_dir_all(&test_dir).unwrap();
fs::write(&test_file, "readonly content").unwrap();

#[cfg(windows)]
{
let metadata = fs::metadata(&test_file).unwrap();
let mut permissions = metadata.permissions();
permissions.set_readonly(true);
fs::set_permissions(&test_file, permissions).unwrap();
}

assert!(remove_directory_all(&test_dir).is_ok());
assert!(!test_dir.exists());
}
}

0 comments on commit efb834f

Please sign in to comment.