Skip to content

Commit

Permalink
Extract .nu-env tests and more granularity
Browse files Browse the repository at this point in the history
The autoenv logic mutates environment variables in the running session as
it operates and decides what to do for trusted directories containing `.nu-env`
files. Few of the ways to interact with it were all in a single test function.

We separate out all the ways that were done in the single test function to document
 it better. This will greatly help once we start refactoring our way out from setting
 environment variables this way to just setting them to `Scope`.

This is part of an on-going effort to keep variables (`PATH` and `ENV`)
in our `Scope` and rely on it for everything related to variables.

We expect to move away from setting (`std::*`) envrironment variables in the current
running process. This is non-trivial since we need to handle cases from vars
coming in from the outside world, prioritize, and also compare to the ones
we have both stored in memory and in configuration files.

Also to send out our in-memory (in `Scope`) variables properly to external
programs once we no longer rely on `std::env` vars from the running process.
  • Loading branch information
andrasio committed Feb 19, 2021
1 parent deff1aa commit 49f5bfe
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 245 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -57,8 +57,10 @@ log = "0.4.14"
pretty_env_logger = "0.4.0"

[dev-dependencies]
dunce = "1.0.1"
nu-test-support = { version = "0.27.1", path = "./crates/nu-test-support" }
dunce = "1.0.1"
serial_test = "0.5.1"


[build-dependencies]

Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/commands/autoenv.rs
Expand Up @@ -25,6 +25,7 @@ pub fn file_is_trusted(nu_env_file: &Path, content: &[u8]) -> Result<bool, Shell
let nufile = std::fs::canonicalize(nu_env_file)?;

let trusted = read_trusted()?;

Ok(trusted.files.get(&nufile.to_string_lossy().to_string()) == Some(&contentdigest))
}

Expand Down
11 changes: 11 additions & 0 deletions crates/nu-test-support/src/lib.rs
Expand Up @@ -4,6 +4,17 @@ pub mod macros;
pub mod playground;
pub mod value;

pub struct Outcome {
pub out: String,
pub err: String,
}

impl Outcome {
pub fn new(out: String, err: String) -> Outcome {
Outcome { out, err }
}
}

pub fn pipeline(commands: &str) -> String {
commands
.lines()
Expand Down
15 changes: 2 additions & 13 deletions crates/nu-test-support/src/macros.rs
Expand Up @@ -71,7 +71,7 @@ macro_rules! nu {

println!("=== stderr\n{}", err);

$crate::macros::Outcome::new(out,err.into_owned())
$crate::Outcome::new(out,err.into_owned())
}};
}

Expand Down Expand Up @@ -147,21 +147,10 @@ macro_rules! nu_with_plugins {

println!("=== stderr\n{}", err);

$crate::macros::Outcome::new(out,err.into_owned())
$crate::Outcome::new(out,err.into_owned())
}};
}

pub struct Outcome {
pub out: String,
pub err: String,
}

impl Outcome {
pub fn new(out: String, err: String) -> Outcome {
Outcome { out, err }
}
}

pub fn read_std(std: &[u8]) -> String {
let out = String::from_utf8_lossy(std);
let out = out.lines().skip(1).collect::<Vec<_>>().join("\n");
Expand Down
19 changes: 19 additions & 0 deletions tests/shell/environment/mod.rs
@@ -0,0 +1,19 @@
mod nu_env;

pub mod support {
use nu_test_support::{nu, playground::*, Outcome};

pub struct Trusted;

impl Trusted {
pub fn in_path(dirs: &Dirs, block: impl FnOnce() -> Outcome) -> Outcome {
let for_env_manifest = dirs.test().to_string_lossy();

nu!(cwd: dirs.root(), format!("autoenv trust \"{}\"", for_env_manifest.to_string()));
let out = block();
nu!(cwd: dirs.root(), format!("autoenv untrust \"{}\"", for_env_manifest.to_string()));

out
}
}
}

0 comments on commit 49f5bfe

Please sign in to comment.