Skip to content

Commit

Permalink
Merge pull request #218 from FedericoPonzi/fix-217
Browse files Browse the repository at this point in the history
Fixes #217
  • Loading branch information
FedericoPonzi authored Dec 23, 2023
2 parents 3d16e07 + 3a9f44d commit 2918e95
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "horust"
version = "0.1.7"
version = "0.1.8"
authors = ["Federico Ponzi <me@fponzi.me>"]
description = "A complete supervisor and init system, designed for running in containers."
edition = "2021"
Expand Down
4 changes: 2 additions & 2 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ keep-env = false
re-export = [ "PATH", "DB_PASS"]
additional = { key = "value"}
```
* **`keep-env` = `bool`**: default: true. Pass over all the environment variables.
Regardless the value of keep-env, the following keys will be updated / defined:
* **`keep-env` = `bool`**: default: false. Pass over all the environment variables.
Regardless of the value of keep-env, the following keys will be updated / defined:
* `USER`
* `HOSTNAME`
* `HOME`
Expand Down
10 changes: 3 additions & 7 deletions src/horust/formats/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Service {
P: AsRef<Path> + ?Sized + AsRef<OsStr> + Debug,
{
let preconfig = std::fs::read_to_string(path)?;
let postconfig = shellexpand::full(&preconfig)?.to_string();
toml::from_str::<Service>(&postconfig).map_err(Error::from)
let postconfig = shellexpand::full(&preconfig)?;
Ok(toml::from_str::<Service>(&postconfig)?)
}
/// Creates the environment K=V variables, used for exec into the new process.
/// User defined environment variables overwrite the predefined values.
Expand Down Expand Up @@ -203,7 +203,7 @@ impl From<&str> for LogOutput {
#[derive(Serialize, Clone, Default, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Environment {
#[serde(default = "Environment::default_keep_env")]
#[serde(default)]
pub keep_env: bool,
#[serde(default)]
pub re_export: Vec<String>,
Expand All @@ -212,10 +212,6 @@ pub struct Environment {
}

impl Environment {
fn default_keep_env() -> bool {
true
}

fn get_hostname_val() -> String {
let hostname_path = "/etc/hostname";
let localhost = "localhost".to_string();
Expand Down
30 changes: 26 additions & 4 deletions tests/section_environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use assert_cmd::prelude::*;
use predicates::prelude::*;
use predicates::str::contains;
use std::env::temp_dir;

Check warning on line 4 in tests/section_environment.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

unused import: `std::env::temp_dir`

Check warning on line 4 in tests/section_environment.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

unused import: `std::env::temp_dir`

Check warning on line 4 in tests/section_environment.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused import: `std::env::temp_dir`

#[allow(dead_code)]
mod utils;
Expand Down Expand Up @@ -28,18 +29,39 @@ additional = { TERM = "bar" }
}

#[test]
fn test_environment_keep_env() {
fn test_environment_keep_env_absent() {
let (mut cmd, temp_dir) = get_cli();
// By default, keep-env is false
store_service_script(temp_dir.path(), ENVIRONMENT_SCRIPT, Some(""), None);
cmd.env("DB_PASS", "MyPassword")
.assert()
.success()
.stdout(contains("MyPassword").not());
}
#[test]
fn test_environment_keep_env_true() {
let (mut cmd, temp_dir) = get_cli();
// keep-env should keep the env :D
let service = r#"[environment]
keep-env = true
"#;
keep-env = true
"#;
store_service_script(temp_dir.path(), ENVIRONMENT_SCRIPT, Some(service), None);
cmd.env("DB_PASS", "MyPassword")
.assert()
.success()
.stdout(contains("MyPassword"));
}
#[test]
fn test_environment_keep_env_false() {
let (mut cmd, temp_dir) = get_cli();
let service = r#"[environment]
keep-env = false
"#;
store_service_script(temp_dir.path(), ENVIRONMENT_SCRIPT, Some(service), None);
cmd.env("DB_PASS", "MyPassword")
.assert()
.success()
.stdout(contains("MyPassword").not());
}

#[test]
fn test_environment_re_export() {
Expand Down

0 comments on commit 2918e95

Please sign in to comment.