Skip to content

Commit

Permalink
added build and test pipeline (#63)
Browse files Browse the repository at this point in the history
* added build and test pipeline
* added some more tests
* pretty format test output

---------

Co-authored-by: Petr Gadorek <[email protected]>
  • Loading branch information
Hahihula and Petr Gadorek authored Jan 10, 2025
1 parent 0c133ff commit 227c28a
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Rust

on:
push:
tags:
- "v*"
branches:
- master
pull_request:
branches:
- master
release:
types:
- created
workflow_dispatch:

jobs:
build:
name: Build for multiple platforms
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
package_name: linux-x64
- os: windows-latest
package_name: windows-x64
# - os: windows-latest
# package_name: windows-arm64
# target: aarch64-pc-windows-msvc
- os: macos-latest
package_name: macos-aarch64
- os: macos-13
package_name: macos-x64

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
target: ${{ matrix.target }}

- name: Build
run: |
if [ "${{ matrix.target }}" != "" ]; then
cargo build --release --target ${{ matrix.target }}
else
cargo build --release
fi
shell: bash

- name: Test
run: |
if [ "${{ matrix.target }}" != "" ]; then
cargo test --no-fail-fast --lib --target ${{ matrix.target }} > result.txt
else
cargo test --no-fail-fast --lib > result.txt
fi
shell: bash

- name: Format test results
uses: hahihula/rust-test-results-formatter@v1
with:
results-file: "result.txt"
162 changes: 162 additions & 0 deletions src/idf_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,165 @@ impl IdfConfig {
pub fn parse_idf_config<P: AsRef<Path>>(path: P) -> Result<IdfConfig> {
IdfConfig::from_file(path)
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;

fn create_test_config() -> IdfConfig {
IdfConfig {
git_path: String::from("/opt/homebrew/bin/git"),
idf_installed: vec![
IdfInstallation {
activation_script: String::from("/tmp/esp-new/activate_idf_v5.4.sh"),
id: String::from("esp-idf-5705c12db93b4d1a8b084c6986173c1b"),
idf_tools_path: String::from("/tmp/esp-new/v5.4/tools"),
name: String::from("ESP-IDF v5.4"),
path: String::from("/tmp/esp-new/v5.4/esp-idf"),
python: String::from("/tmp/esp-new/v5.4/tools/python/bin/python3"),
},
IdfInstallation {
activation_script: String::from("/tmp/esp-new/activate_idf_v5.1.5.sh"),
id: String::from("esp-idf-5f014e6764904e4c914eeb365325bfcd"),
idf_tools_path: String::from("/tmp/esp-new/v5.1.5/tools"),
name: String::from("v5.1.5"),
path: String::from("/tmp/esp-new/v5.1.5/esp-idf"),
python: String::from("/tmp/esp-new/v5.1.5/tools/python/bin/python3"),
},
],
idf_selected_id: String::from("esp-idf-5705c12db93b4d1a8b084c6986173c1b"),
}
}

#[test]
fn test_get_selected_installation() {
let config = create_test_config();
let selected = config.get_selected_installation().unwrap();
assert_eq!(selected.id, "esp-idf-5705c12db93b4d1a8b084c6986173c1b");
assert_eq!(selected.name, "ESP-IDF v5.4");
}

#[test]
fn test_update_installation_name() {
let mut config = create_test_config();

// Test updating by ID
assert!(config.update_installation_name(
"esp-idf-5705c12db93b4d1a8b084c6986173c1b",
String::from("New Name")
));
assert_eq!(config.idf_installed[0].name, "New Name");

// Test updating by name
assert!(config.update_installation_name("v5.1.5", String::from("Updated v5.1.5")));
assert_eq!(config.idf_installed[1].name, "Updated v5.1.5");

// Test updating non-existent installation
assert!(!config.update_installation_name("non-existent", String::from("Invalid")));
}

#[test]
fn test_select_installation() {
let mut config = create_test_config();

// Test selecting by ID
assert!(config.select_installation("esp-idf-5f014e6764904e4c914eeb365325bfcd"));
assert_eq!(
config.idf_selected_id,
"esp-idf-5f014e6764904e4c914eeb365325bfcd"
);

// Test selecting by name
assert!(config.select_installation("ESP-IDF v5.4"));
assert_eq!(
config.idf_selected_id,
"esp-idf-5705c12db93b4d1a8b084c6986173c1b"
);

// Test selecting non-existent installation
assert!(!config.select_installation("non-existent"));
}

#[test]
fn test_remove_installation() {
let mut config = create_test_config();

// Test removing by ID
assert!(config.remove_installation("esp-idf-5705c12db93b4d1a8b084c6986173c1b"));
assert_eq!(config.idf_installed.len(), 1);
assert!(config.idf_selected_id.is_empty()); // Should clear selection when removing selected installation

// Test removing by name
assert!(config.remove_installation("v5.1.5"));
assert!(config.idf_installed.is_empty());

// Test removing non-existent installation
assert!(!config.remove_installation("non-existent"));
}

#[test]
fn test_file_operations() -> Result<()> {
let dir = tempdir()?;
let config_path = dir.path().join("test_config.json");
let mut config = create_test_config();

// Test writing config to file
config.to_file(&config_path, true)?;
assert!(config_path.exists());

// Test reading config from file
let read_config = IdfConfig::from_file(&config_path)?;
assert_eq!(read_config.git_path, config.git_path);
assert_eq!(read_config.idf_selected_id, config.idf_selected_id);
assert_eq!(read_config.idf_installed.len(), config.idf_installed.len());

// Test appending to existing config
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"),
name: String::from("ESP-IDF v5.1"),
path: String::from("/esp/idf/v5.1"),
python: String::from("/usr/bin/python3"),
};

config.idf_installed = vec![new_installation.clone()];
config.to_file(&config_path, true)?;

let updated_config = IdfConfig::from_file(&config_path)?;
assert!(updated_config
.idf_installed
.iter()
.any(|install| install.id == "5.1"));
assert!(updated_config
.idf_installed
.iter()
.any(|install| install.id == "esp-idf-5705c12db93b4d1a8b084c6986173c1b"));

Ok(())
}

#[test]
fn test_parse_idf_config() -> Result<()> {
let dir = tempdir()?;
let config_path = dir.path().join("parse_test_config.json");
let config = create_test_config();

// Write test config to file
let json = serde_json::to_string_pretty(&config)?;
fs::write(&config_path, json)?;

// Test parsing
let parsed_config = parse_idf_config(&config_path)?;
assert_eq!(parsed_config.git_path, config.git_path);
assert_eq!(parsed_config.idf_selected_id, config.idf_selected_id);
assert_eq!(
parsed_config.idf_installed.len(),
config.idf_installed.len()
);

Ok(())
}
}
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);
}
}
Loading

0 comments on commit 227c28a

Please sign in to comment.