Skip to content

Commit c6f59c5

Browse files
committed
add e2e
1 parent a69f4a2 commit c6f59c5

File tree

6 files changed

+145
-30
lines changed

6 files changed

+145
-30
lines changed

e2e/src/exec_builder.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::{env::current_dir, error::Error, path::PathBuf, process::Command};
2+
3+
#[derive(Debug, Clone)]
4+
pub struct Executor {
5+
pub current: PathBuf,
6+
pub envs: Vec<(String, String)>,
7+
}
8+
9+
impl Executor {
10+
pub fn exec(&self, shell: &str) -> Result<String, Box<dyn Error>> {
11+
let shell_vec = shell
12+
.split(" ")
13+
.map(|item| item.trim())
14+
.collect::<Vec<&str>>();
15+
16+
if let Some((bin_name, args)) = shell_vec.split_first() {
17+
let output = Command::new(bin_name)
18+
.envs(self.envs.clone())
19+
.args(args)
20+
.current_dir(&self.current)
21+
.output()?;
22+
23+
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
24+
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
25+
26+
println!(
27+
r##"
28+
Exec shell: {}
29+
Stdout: {}
30+
Stderr: {}
31+
"##,
32+
shell, stdout, stderr
33+
);
34+
Ok(stdout)
35+
} else {
36+
Err("Invalid shell command".into())
37+
}
38+
}
39+
}
40+
41+
pub struct ExecBuilder {
42+
executor: Executor,
43+
}
44+
45+
impl ExecBuilder {
46+
pub fn builder() -> Self {
47+
Self {
48+
executor: Executor {
49+
current: PathBuf::new(),
50+
envs: vec![],
51+
},
52+
}
53+
}
54+
55+
pub fn current(&mut self, current: &PathBuf) -> &mut Self {
56+
self.executor.current = current.clone();
57+
self
58+
}
59+
60+
pub fn envs(&mut self, envs: Vec<(String, String)>) -> &mut Self {
61+
let binary_path = current_dir().unwrap().join("tests");
62+
self.executor.envs = vec![
63+
vec![("PATH".to_string(), binary_path.display().to_string())],
64+
envs,
65+
]
66+
.concat();
67+
self
68+
}
69+
70+
pub fn build(&self) -> Executor {
71+
self.executor.clone()
72+
}
73+
}

e2e/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod exec_builder;

e2e/src/main.rs

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20.11.1

e2e/tests/snm_install_test.rs

+64-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::{
22
env::current_dir,
33
error::Error,
4+
fs,
5+
path::PathBuf,
46
process::{Command, Output},
57
};
68

9+
use e2e::exec_builder::ExecBuilder;
710
use tempfile::tempdir;
811

912
// #[cfg(windows)]
@@ -13,7 +16,11 @@ use tempfile::tempdir;
1316

1417
const SNM_CMD: &str = "snm";
1518

16-
fn exec(shell: &str, envs: &Vec<(&str, String)>) -> Result<Output, Box<dyn Error>> {
19+
fn exec(
20+
shell: &str,
21+
current: &PathBuf,
22+
envs: &Vec<(&str, String)>,
23+
) -> Result<String, Box<dyn Error>> {
1724
let shell_vec = shell
1825
.split(" ")
1926
.map(|item| item.trim())
@@ -23,46 +30,48 @@ fn exec(shell: &str, envs: &Vec<(&str, String)>) -> Result<Output, Box<dyn Error
2330
let output = Command::new(bin_name)
2431
.envs(envs.clone())
2532
.args(args)
33+
.current_dir(current)
2634
.output()?;
27-
Ok(output)
35+
36+
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
37+
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
38+
39+
println!(
40+
r##"
41+
Exec shell: {}
42+
Stdout: {}
43+
Stderr: {}
44+
"##,
45+
shell, stdout, stderr
46+
);
47+
Ok(stdout)
2848
} else {
2949
Err("Invalid shell command".into())
3050
}
3151
}
3252

3353
#[test]
34-
fn test_install_node() -> Result<(), Box<dyn Error>> {
54+
fn should_auto_install() -> Result<(), Box<dyn Error>> {
3555
let node_version = "16.0.0";
3656

3757
let dir = tempdir()?.path().to_path_buf();
3858

39-
let path_dir = current_dir()?.join("tests");
40-
41-
println!("Current dir: {:?}", path_dir);
59+
let current_dir_path_buf = current_dir()?;
4260

4361
let envs = vec![
44-
("PATH", path_dir.display().to_string()),
45-
("SNM_HOME_DIR", dir.display().to_string()),
46-
("SNM_NODE_INSTALL_STRATEGY", "auto".to_string()),
62+
("SNM_HOME_DIR".to_string(), dir.display().to_string()),
63+
("SNM_NODE_INSTALL_STRATEGY".to_string(), "auto".to_string()),
4764
];
4865

49-
let install_output = exec(&format!("{} node install {}", SNM_CMD, node_version), &envs)?;
50-
println!(
51-
"Install stdout: {}",
52-
String::from_utf8_lossy(&install_output.stdout)
53-
);
54-
println!(
55-
"Install stderr: {}",
56-
String::from_utf8_lossy(&install_output.stderr)
57-
);
66+
let executor = ExecBuilder::builder()
67+
.current(&current_dir_path_buf)
68+
.envs(envs)
69+
.build();
70+
71+
executor.exec(&format!("{} node install {}", SNM_CMD, node_version))?;
72+
73+
let stdout = executor.exec(&format!("{} node list", SNM_CMD))?;
5874

59-
let list_output = exec(&format!("{} node list", SNM_CMD), &envs)?;
60-
let stdout = String::from_utf8(list_output.stdout)?;
61-
println!("List stdout: {}", &stdout);
62-
println!(
63-
"List stderr: {}",
64-
String::from_utf8_lossy(&list_output.stderr)
65-
);
6675
assert!(
6776
stdout.contains(node_version),
6877
"Expected to find node version {} in stdout, but got: {}",
@@ -72,3 +81,33 @@ fn test_install_node() -> Result<(), Box<dyn Error>> {
7281

7382
Ok(())
7483
}
84+
85+
#[test]
86+
fn should_auto_install_and_exec() -> Result<(), Box<dyn Error>> {
87+
let dir = tempdir()?.path().to_path_buf();
88+
89+
let cwd = current_dir()?
90+
.join("tests")
91+
.join("features")
92+
.join("node-proxy");
93+
94+
let node_version = fs::read_to_string(cwd.join(".node-version"))?;
95+
96+
let envs = vec![
97+
("SNM_HOME_DIR".to_string(), dir.display().to_string()),
98+
("SNM_NODE_INSTALL_STRATEGY".to_string(), "auto".to_string()),
99+
];
100+
101+
let executor = ExecBuilder::builder().current(&cwd).envs(envs).build();
102+
103+
let stdout = executor.exec("node -v")?;
104+
105+
assert!(
106+
stdout.contains(node_version.as_str()),
107+
"Expected to find node version {} in stdout, but got: {}",
108+
node_version,
109+
stdout
110+
);
111+
112+
Ok(())
113+
}

justfile

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ dev:
1313

1414
qtest:
1515
echo "Running tests..."
16-
cargo qtest
16+
cargo test --workspace --exclude e2e -- --nocapture
1717

1818
test:
1919
echo "Running tests..."
20-
cargo t -- --nocapture
20+
cargo test --workspace --exclude e2e -- --nocapture
21+
22+
e2e:
23+
echo "Running end-to-end tests..."
24+
cargo test --package e2e -- --nocapture

0 commit comments

Comments
 (0)