Skip to content

Commit

Permalink
Use .entry to add default parameters to a submit unless those paramet…
Browse files Browse the repository at this point in the history
…ers are already set
  • Loading branch information
BradenEverson committed Sep 5, 2024
1 parent 9773909 commit 5fe49ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions configs/generic_simple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
name = "HelloGeneric"
kind = "Generic"
command = "echo Hello ${name}"

[[backends]]
name = "HelloGenericWithDefaults"
kind = "Generic"
command = "echo I have ${ram} mb of ram"
default-ram = 4096
39 changes: 33 additions & 6 deletions src/engine/service/runner/backend/backend_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl BackendConfig {
// Replace default flags only if it isn't already set

if let Some(cpu) = self.default_cpu {
if !replacements.contains_key("cpu") {
replacements.insert("cpu".to_string(), cpu.to_string());
}
replacements
.entry("cpu".to_string())
.or_insert(cpu.to_string());
}

if let Some(ram) = self.default_ram {
if !replacements.contains_key("ram") {
replacements.insert("cpu".to_string(), ram.to_string());
}
replacements
.entry("ram".to_string())
.or_insert(ram.to_string());
}

match &self.kind {
Expand Down Expand Up @@ -110,4 +110,31 @@ mod tests {
.expect("Get output from generic backend");
assert_eq!(output.stdout, b"Hello Kids24\n");
}

#[test]
fn generic_config_with_defaults_uses_them() {
let config = Config::load_from_file("configs/generic_simple.toml")
.expect("Load from example config");
let backend = &config.backends[1];
let mut replacements = HashMap::new();

let output = backend
.submit(&mut replacements, "${", "}")
.expect("Get output from generic backend");
assert_eq!(output.stdout, b"I have 4096 mb of ram\n");
}

#[test]
fn generic_config_with_defaults_and_parameters_set_uses_parameters() {
let config = Config::load_from_file("configs/generic_simple.toml")
.expect("Load from example config");
let backend = &config.backends[1];
let mut replacements = HashMap::new();
replacements.insert("ram".to_string(), 2.to_string());

let output = backend
.submit(&mut replacements, "${", "}")
.expect("Get output from generic backend");
assert_eq!(output.stdout, b"I have 2 mb of ram\n");
}
}

0 comments on commit 5fe49ed

Please sign in to comment.