Skip to content

Commit 0294a37

Browse files
author
Lisa Ugray
committed
Evaluate expand-path in the shadowenv root directory
1 parent bb7fb4a commit 0294a37

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/hash.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const GROUP_SEPARATOR: &'static str = "\x1D";
1313

1414
#[derive(Debug, Clone)]
1515
pub struct Source {
16+
pub dir: String,
1617
pub files: Vec<SourceFile>,
1718
}
1819

@@ -50,8 +51,11 @@ pub struct Hash {
5051
struct WrongInputSize;
5152

5253
impl Source {
53-
pub fn new() -> Self {
54-
Source { files: vec![] }
54+
pub fn new(dir: String) -> Self {
55+
Source {
56+
dir: dir,
57+
files: vec![],
58+
}
5559
}
5660

5761
pub fn add_file(&mut self, name: String, contents: String) -> Result<(), Error> {
@@ -67,6 +71,8 @@ impl Source {
6771
return Ok(0);
6872
}
6973
let mut hasher = VarBlake2b::new(8)?;
74+
hasher.input(&self.dir);
75+
hasher.input(FILE_SEPARATOR);
7076
for file in self.files.iter() {
7177
hasher.input(&file.name);
7278
hasher.input(GROUP_SEPARATOR);
@@ -117,6 +123,7 @@ mod tests {
117123
impl Arbitrary for Source {
118124
fn arbitrary(g: &mut Gen) -> Source {
119125
Source {
126+
dir: Arbitrary::arbitrary(g),
120127
files: Arbitrary::arbitrary(g),
121128
}
122129
}

src/lang.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::shadowenv::Shadowenv;
33

44
use failure::Fail;
55
use ketos::{Error, FromValueRef, Value};
6+
use std::env;
67
use std::fs;
8+
use std::path::Path;
79
use std::path::PathBuf;
810
use std::rc::Rc;
911
pub struct ShadowLang {}
@@ -246,6 +248,8 @@ impl ShadowLang {
246248

247249
let mut files = source.files.clone();
248250
files.sort();
251+
let original_path = env::current_dir();
252+
let _ = env::set_current_dir(Path::new(&source.dir));
249253

250254
for source_file in &files {
251255
let fname = format!("__shadowenv__{}", source_file.name);
@@ -274,6 +278,9 @@ impl ShadowLang {
274278
return Err(err);
275279
};
276280
}
281+
if let Ok(dir) = original_path {
282+
let _ = env::set_current_dir(dir);
283+
}
277284

278285
Ok(())
279286
}

src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn find_root(at: PathBuf, relative_component: &str) -> Result<Option<PathBuf
2929
///
3030
/// Note that this function assumes that the dirpath is trusted.
3131
pub fn load(dirpath: PathBuf) -> Result<Option<Source>, Error> {
32-
let mut source = Source::new();
32+
let mut source = Source::new(dirpath.parent().unwrap().to_str().unwrap().to_string());
3333

3434
for entry in fs::read_dir(dirpath)? {
3535
if let Ok(entry) = entry {

0 commit comments

Comments
 (0)