1
1
use std:: {
2
2
env:: current_dir,
3
3
error:: Error ,
4
+ fs,
5
+ path:: PathBuf ,
4
6
process:: { Command , Output } ,
5
7
} ;
6
8
9
+ use e2e:: exec_builder:: ExecBuilder ;
7
10
use tempfile:: tempdir;
8
11
9
12
// #[cfg(windows)]
@@ -13,7 +16,11 @@ use tempfile::tempdir;
13
16
14
17
const SNM_CMD : & str = "snm" ;
15
18
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 > > {
17
24
let shell_vec = shell
18
25
. split ( " " )
19
26
. map ( |item| item. trim ( ) )
@@ -23,46 +30,48 @@ fn exec(shell: &str, envs: &Vec<(&str, String)>) -> Result<Output, Box<dyn Error
23
30
let output = Command :: new ( bin_name)
24
31
. envs ( envs. clone ( ) )
25
32
. args ( args)
33
+ . current_dir ( current)
26
34
. 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)
28
48
} else {
29
49
Err ( "Invalid shell command" . into ( ) )
30
50
}
31
51
}
32
52
33
53
#[ test]
34
- fn test_install_node ( ) -> Result < ( ) , Box < dyn Error > > {
54
+ fn should_auto_install ( ) -> Result < ( ) , Box < dyn Error > > {
35
55
let node_version = "16.0.0" ;
36
56
37
57
let dir = tempdir ( ) ?. path ( ) . to_path_buf ( ) ;
38
58
39
- let path_dir = current_dir ( ) ?. join ( "tests" ) ;
40
-
41
- println ! ( "Current dir: {:?}" , path_dir) ;
59
+ let current_dir_path_buf = current_dir ( ) ?;
42
60
43
61
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( ) ) ,
47
64
] ;
48
65
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 ) ) ?;
58
74
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
- ) ;
66
75
assert ! (
67
76
stdout. contains( node_version) ,
68
77
"Expected to find node version {} in stdout, but got: {}" ,
@@ -72,3 +81,33 @@ fn test_install_node() -> Result<(), Box<dyn Error>> {
72
81
73
82
Ok ( ( ) )
74
83
}
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
+ }
0 commit comments