nu_test_support/playground/
play.rs

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::Director;
use crate::fs::{self, Stub};
use nu_glob::glob;
#[cfg(not(target_arch = "wasm32"))]
use nu_path::Path;
use nu_path::{AbsolutePath, AbsolutePathBuf};
use std::str;
use tempfile::{tempdir, TempDir};

#[derive(Default, Clone, Debug)]
pub struct EnvironmentVariable {
    pub name: String,
    pub value: String,
}

impl EnvironmentVariable {
    fn new(name: &str, value: &str) -> Self {
        Self {
            name: name.to_string(),
            value: value.to_string(),
        }
    }
}

pub struct Playground<'a> {
    _root: TempDir,
    tests: String,
    cwd: AbsolutePathBuf,
    config: Option<AbsolutePathBuf>,
    environment_vars: Vec<EnvironmentVariable>,
    dirs: &'a Dirs,
}

#[derive(Clone)]
pub struct Dirs {
    pub root: AbsolutePathBuf,
    pub test: AbsolutePathBuf,
    pub fixtures: AbsolutePathBuf,
}

impl Dirs {
    pub fn formats(&self) -> AbsolutePathBuf {
        self.fixtures.join("formats")
    }

    pub fn root(&self) -> &AbsolutePath {
        &self.root
    }

    pub fn test(&self) -> &AbsolutePath {
        &self.test
    }
}

impl<'a> Playground<'a> {
    pub fn root(&self) -> &AbsolutePath {
        &self.dirs.root
    }

    pub fn cwd(&self) -> &AbsolutePath {
        &self.cwd
    }

    pub fn back_to_playground(&mut self) -> &mut Self {
        self.cwd = self.root().join(&self.tests);
        self
    }

    pub fn play(&mut self) -> &mut Self {
        self
    }

    pub fn setup(topic: &str, block: impl FnOnce(Dirs, &mut Playground)) {
        let temp = tempdir().expect("Could not create a tempdir");

        let root = AbsolutePathBuf::try_from(temp.path())
            .expect("Tempdir is not an absolute path")
            .canonicalize()
            .expect("Could not canonicalize tempdir");

        let test = root.join(topic);
        if test.exists() {
            std::fs::remove_dir_all(&test).expect("Could not remove directory");
        }
        std::fs::create_dir(&test).expect("Could not create directory");
        let test = test
            .canonicalize()
            .expect("Could not canonicalize test path");

        let fixtures = fs::fixtures()
            .canonicalize()
            .expect("Could not canonicalize fixtures path");

        let dirs = Dirs {
            root: root.into(),
            test: test.as_path().into(),
            fixtures: fixtures.into(),
        };

        let mut playground = Playground {
            _root: temp,
            tests: topic.to_string(),
            cwd: test.into(),
            config: None,
            environment_vars: Vec::default(),
            dirs: &dirs,
        };

        block(dirs.clone(), &mut playground);
    }

    pub fn with_config(&mut self, source_file: AbsolutePathBuf) -> &mut Self {
        self.config = Some(source_file);
        self
    }

    pub fn with_env(&mut self, name: &str, value: &str) -> &mut Self {
        self.environment_vars
            .push(EnvironmentVariable::new(name, value));
        self
    }

    pub fn get_config(&self) -> Option<&str> {
        self.config
            .as_ref()
            .map(|cfg| cfg.to_str().expect("could not convert path."))
    }

    pub fn build(&mut self) -> Director {
        Director {
            cwd: Some(self.dirs.test().into()),
            config: self.config.clone().map(|cfg| cfg.into()),
            environment_vars: self.environment_vars.clone(),
            ..Default::default()
        }
    }

    pub fn cococo(&mut self, arg: &str) -> Director {
        self.build().cococo(arg)
    }

    pub fn pipeline(&mut self, commands: &str) -> Director {
        self.build().pipeline(commands)
    }

    pub fn mkdir(&mut self, directory: &str) -> &mut Self {
        self.cwd.push(directory);
        std::fs::create_dir_all(&self.cwd).expect("can not create directory");
        self.back_to_playground();
        self
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn symlink(&mut self, from: impl AsRef<Path>, to: impl AsRef<Path>) -> &mut Self {
        let from = self.cwd.join(from);
        let to = self.cwd.join(to);

        let create_symlink = {
            #[cfg(unix)]
            {
                std::os::unix::fs::symlink
            }

            #[cfg(windows)]
            {
                if from.is_file() {
                    std::os::windows::fs::symlink_file
                } else if from.is_dir() {
                    std::os::windows::fs::symlink_dir
                } else {
                    panic!("symlink from must be a file or dir")
                }
            }
        };

        create_symlink(from, to).expect("can not create symlink");
        self.back_to_playground();
        self
    }

    pub fn with_files(&mut self, files: &[Stub]) -> &mut Self {
        let endl = fs::line_ending();

        files
            .iter()
            .map(|f| {
                let mut permission_set = false;
                let mut write_able = true;
                let (file_name, contents) = match *f {
                    Stub::EmptyFile(name) => (name, "fake data".to_string()),
                    Stub::FileWithContent(name, content) => (name, content.to_string()),
                    Stub::FileWithContentToBeTrimmed(name, content) => (
                        name,
                        content
                            .lines()
                            .skip(1)
                            .map(|line| line.trim())
                            .collect::<Vec<&str>>()
                            .join(&endl),
                    ),
                    Stub::FileWithPermission(name, is_write_able) => {
                        permission_set = true;
                        write_able = is_write_able;
                        (name, "check permission".to_string())
                    }
                };

                let path = self.cwd.join(file_name);

                std::fs::write(&path, contents.as_bytes()).expect("can not create file");
                if permission_set {
                    let err_perm = "can not set permission";
                    let mut perm = std::fs::metadata(path.clone())
                        .expect(err_perm)
                        .permissions();
                    perm.set_readonly(!write_able);
                    std::fs::set_permissions(path, perm).expect(err_perm);
                }
            })
            .for_each(drop);
        self.back_to_playground();
        self
    }

    pub fn within(&mut self, directory: &str) -> &mut Self {
        self.cwd.push(directory);
        if !(self.cwd.exists() && self.cwd.is_dir()) {
            std::fs::create_dir(&self.cwd).expect("can not create directory");
        }
        self
    }

    pub fn glob_vec(pattern: &str) -> Vec<std::path::PathBuf> {
        let glob = glob(pattern);

        glob.expect("invalid pattern")
            .map(|path| {
                if let Ok(path) = path {
                    path
                } else {
                    unreachable!()
                }
            })
            .collect()
    }
}