forked from brettcannon/python-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
128 lines (112 loc) · 3.34 KB
/
mod.rs
File metadata and controls
128 lines (112 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::collections::HashMap;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::path::PathBuf;
use tempfile::TempDir;
pub struct EnvVarState {
changed: HashMap<OsString, Option<OsString>>,
}
impl Drop for EnvVarState {
fn drop(&mut self) {
self.changed.iter().for_each(|(k, v)| match &v {
Some(original_v) => env::set_var(&k, original_v),
None => env::remove_var(&k),
});
}
}
impl EnvVarState {
pub fn new() -> Self {
Self {
changed: HashMap::new(),
}
}
#[allow(dead_code)]
pub fn empty() -> Self {
let mut state = Self::new();
state.change("PATH", None);
for env_var in ["VIRTUAL_ENV", "PY_PYTHON", "PY_PYTHON3", "PY_PYTHON2"].iter() {
state.change(env_var, None);
}
state
}
pub fn change(&mut self, k: &str, v: Option<&str>) {
let os_k = OsStr::new(k);
if !self.changed.contains_key(os_k) {
let original_v = env::var_os(k);
self.changed.insert(os_k.to_os_string(), original_v);
}
match v {
Some(new_v) => env::set_var(k, new_v),
None => env::remove_var(k),
}
}
}
pub struct CurrentDir {
_original_dir: PathBuf,
pub dir: TempDir,
}
impl Drop for CurrentDir {
fn drop(&mut self) {
env::set_current_dir(&self._original_dir).unwrap();
}
}
impl CurrentDir {
#[allow(dead_code)]
pub fn new() -> Self {
let _original_dir = env::current_dir().unwrap();
let dir = TempDir::new().unwrap();
env::set_current_dir(dir.path()).unwrap();
Self { _original_dir, dir }
}
}
pub fn touch_file(path: PathBuf) -> PathBuf {
let file = File::create(&path).unwrap();
file.sync_all().unwrap();
path
}
#[allow(dead_code)]
pub struct EnvState {
_dir1: TempDir,
_dir2: TempDir,
pub env_vars: EnvVarState,
pub python27: PathBuf,
pub python36: PathBuf,
pub python37: PathBuf,
}
impl EnvState {
/// Create a testing environment within the OS.
/// - Create two temp directories (referred to as `dir1` and `dir2` from now on)
/// - `dir1/python2.7`
/// - `dir1/python3.6`
/// - `dir2/python3.6`
/// - `dir2/python3.7`
/// - `PATH` environment variable is set to `dir1` and `dir2`
/// - `VIRTUAL_ENV` is unset
/// - `PY_PYTHON` is unset
/// - `PY_PYTHON3` is unset
/// - `PY_PYTHON2` is unset
#[allow(dead_code)]
pub fn new() -> Self {
let dir1 = TempDir::new().unwrap();
let dir2 = TempDir::new().unwrap();
let python27 = touch_file(dir1.path().join("python2.7"));
let python36 = touch_file(dir1.path().join("python3.6"));
touch_file(dir2.path().join("python3.6"));
let python37 = touch_file(dir2.path().join("python3.7"));
let new_path = env::join_paths([dir1.path(), dir2.path()].iter()).unwrap();
let mut env_changes = EnvVarState::new();
env_changes.change("PATH", Some(new_path.to_str().unwrap()));
for env_var in ["VIRTUAL_ENV", "PY_PYTHON", "PY_PYTHON3", "PY_PYTHON2"].iter() {
env_changes.change(env_var, None);
}
Self {
_dir1: dir1,
_dir2: dir2,
env_vars: env_changes,
python27,
python36,
python37,
}
}
}