Skip to content

Commit

Permalink
fix elf rom folder detection function
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Gadorek committed Jan 14, 2025
1 parent 227c28a commit 72b288c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ jobs:
- name: Test
run: |
if [ "${{ matrix.target }}" != "" ]; then
cargo test --no-fail-fast --lib --target ${{ matrix.target }} > result.txt
cargo test --no-fail-fast --lib --target ${{ matrix.target }} 2>&1 | tee result.txt
else
cargo test --no-fail-fast --lib > result.txt
cargo test --no-fail-fast --lib 2>&1 | tee result.txt
fi
shell: bash
continue-on-error: true

- name: Format test results
uses: hahihula/rust-test-results-formatter@v1
Expand Down
64 changes: 60 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,9 @@ fn get_elf_rom_dir(idf_tools_path: &PathBuf) -> Result<PathBuf, std::io::Error>

// Sort the subdirectories
subdirs.sort();
if let Some(first_subdir) = subdirs.first() {
// Set the first subdirectory as the ELF ROM directory
let elf_rom_dir = first_subdir.as_path();
log::debug!("Using ELF ROM directory: {}", elf_rom_dir.display());
if let Some(last_subdir) = subdirs.last() {
log::debug!("Using ELF ROM directory: {}", last_subdir.display());
return Ok(last_subdir.clone());
} else {
log::warn!("No ELF ROM directories found in {}", elf_rom_dir.display());
}
Expand Down Expand Up @@ -1134,6 +1133,7 @@ mod tests {
use super::*;
use std::fs;
use std::io::Write;
use tempfile::TempDir;

#[test]
fn test_verify_file_checksum_with_valid_file() {
Expand Down Expand Up @@ -1247,4 +1247,60 @@ mod tests {

assert_eq!(expanded_path, home_dir.join("test_directory"));
}

#[test]
fn test_get_elf_rom_dir_with_valid_structure() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary directory that will be automatically cleaned up
let temp_dir = TempDir::new()?;
let idf_tools_path = temp_dir.path().to_path_buf();

// Create the directory structure
let tools_dir = idf_tools_path.join("tools");
let esp_rom_dir = tools_dir.join("esp-rom-elfs");
let version_dir = esp_rom_dir.join("20243982");

fs::create_dir_all(&version_dir)?;

// Call the function
let result = get_elf_rom_dir(&idf_tools_path)?;

// Verify the result
assert_eq!(result, version_dir);

Ok(())
}
#[test]
fn test_get_elf_rom_dir_with_empty_directory() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let idf_tools_path = temp_dir.path().to_path_buf();

// Create empty directory structure
let tools_dir = idf_tools_path.join("tools");
let esp_rom_dir = tools_dir.join("esp-rom-elfs");
fs::create_dir_all(&esp_rom_dir)?;

// Call the function
let result = get_elf_rom_dir(&idf_tools_path)?;

// Should return the esp-rom-elfs directory even if empty
assert_eq!(result, esp_rom_dir);

Ok(())
}

#[test]
fn test_get_elf_rom_dir_with_nonexistent_directory() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let idf_tools_path = temp_dir.path().to_path_buf();

// Don't create any directories

// Call the function
let result = get_elf_rom_dir(&idf_tools_path)?;

// Should return a path to the (nonexistent) esp-rom-elfs directory
assert_eq!(result, idf_tools_path.join("tools").join("esp-rom-elfs"));

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ mod tests {
let file2_path = base_path.join("file2.txt");

fs::write(&file1_path, "content1").unwrap();
let duration = std::time::Duration::from_millis(1000); // Sleep for 1 second
std::thread::sleep(duration); // because on windows we use the modified time to identify duplicates
fs::write(&file2_path, "content2").unwrap();

let paths = vec![
Expand Down

0 comments on commit 72b288c

Please sign in to comment.