1
- use std:: env;
2
1
use std:: fs;
3
2
use std:: io:: Result ;
3
+ #[ cfg( unix) ]
4
+ use std:: os:: unix:: ffi:: OsStrExt ;
5
+ use std:: path:: { Path , PathBuf } ;
4
6
use std:: process:: Command ;
5
7
use std:: process:: Output ;
6
8
7
- const NGX_DEFAULT_VERSION : & str = "1.24.0" ;
8
- const NGINX_BIN : & str = "sbin/nginx" ;
9
- const NGINX_CONFIG : & str = "conf/nginx.conf" ;
9
+ use ngx:: ffi:: { NGX_CONF_PATH , NGX_PREFIX , NGX_SBIN_PATH } ;
10
+
11
+ /// Convert a CStr to a PathBuf
12
+ pub fn cstr_to_path ( val : & std:: ffi:: CStr ) -> Option < PathBuf > {
13
+ if val. is_empty ( ) {
14
+ return None ;
15
+ }
16
+
17
+ #[ cfg( unix) ]
18
+ let str = std:: ffi:: OsStr :: from_bytes ( val. to_bytes ( ) ) ;
19
+ #[ cfg( not( unix) ) ]
20
+ let str = std:: str:: from_utf8 ( val. to_bytes ( ) ) . ok ( ) ?;
21
+
22
+ Some ( PathBuf :: from ( str) )
23
+ }
10
24
11
25
/// harness to test nginx
12
26
pub struct Nginx {
13
- pub install_path : String ,
27
+ pub install_path : PathBuf ,
28
+ pub config_path : PathBuf ,
14
29
}
15
30
16
31
impl Default for Nginx {
17
32
/// create nginx with default
18
33
fn default ( ) -> Nginx {
19
- let path = env:: current_dir ( ) . unwrap ( ) ;
20
- let version = env:: var ( "NGX_VERSION" ) . unwrap_or ( NGX_DEFAULT_VERSION . into ( ) ) ;
21
- let platform = target_triple:: TARGET ;
22
- let ngx_path = format ! ( ".cache/nginx/{}/{}" , version, platform) ;
23
- let install_path = format ! ( "{}/{}" , path. display( ) , ngx_path) ;
24
- Nginx { install_path }
34
+ let install_path = cstr_to_path ( NGX_PREFIX ) . expect ( "installation prefix" ) ;
35
+ Nginx :: new ( install_path)
25
36
}
26
37
}
27
38
28
39
impl Nginx {
29
- pub fn new ( path : String ) -> Nginx {
30
- Nginx { install_path : path }
40
+ pub fn new < P : AsRef < Path > > ( path : P ) -> Nginx {
41
+ let install_path = path. as_ref ( ) ;
42
+ let config_path = cstr_to_path ( NGX_CONF_PATH ) . expect ( "configuration path" ) ;
43
+ let config_path = install_path. join ( config_path) ;
44
+
45
+ Nginx {
46
+ install_path : install_path. into ( ) ,
47
+ config_path,
48
+ }
31
49
}
32
50
33
51
/// get bin path to nginx instance
34
- pub fn bin_path ( & mut self ) -> String {
35
- format ! ( "{}/{}" , self . install_path, NGINX_BIN )
52
+ pub fn bin_path ( & mut self ) -> PathBuf {
53
+ let bin_path = cstr_to_path ( NGX_SBIN_PATH ) . expect ( "binary path" ) ;
54
+ self . install_path . join ( bin_path)
36
55
}
37
56
38
57
/// start nginx process with arguments
@@ -70,10 +89,9 @@ impl Nginx {
70
89
}
71
90
72
91
// replace config with another config
73
- pub fn replace_config ( & mut self , from : & str ) -> Result < u64 > {
74
- let config_path = format ! ( "{}/{}" , self . install_path, NGINX_CONFIG ) ;
75
- println ! ( "copying config from: {} to: {}" , from, config_path) ; // replace with logging
76
- fs:: copy ( from, config_path)
92
+ pub fn replace_config < P : AsRef < Path > > ( & mut self , from : P ) -> Result < u64 > {
93
+ println ! ( "copying config from: {:?} to: {:?}" , from. as_ref( ) , self . config_path) ; // replace with logging
94
+ fs:: copy ( from, & self . config_path )
77
95
}
78
96
}
79
97
@@ -99,7 +117,7 @@ mod tests {
99
117
) ;
100
118
101
119
nginx
102
- . replace_config ( & test_config_path. to_string_lossy ( ) )
120
+ . replace_config ( & test_config_path)
103
121
. expect ( format ! ( "Unable to load config file: {}" , test_config_path. to_string_lossy( ) ) . as_str ( ) ) ;
104
122
let output = nginx. restart ( ) . expect ( "Unable to restart NGINX" ) ;
105
123
assert ! ( output. status. success( ) ) ;
0 commit comments