1
- use std:: env;
2
1
use std:: fs;
3
2
use std:: io:: Result ;
3
+ use std:: path:: { Path , PathBuf } ;
4
4
use std:: process:: Command ;
5
5
use std:: process:: Output ;
6
6
7
- const NGX_DEFAULT_VERSION : & str = "1.24.0" ;
8
- const NGINX_BIN : & str = "sbin/nginx" ;
9
- const NGINX_CONFIG : & str = "conf/nginx.conf" ;
7
+ use ngx:: ffi:: { NGX_CONF_PATH , NGX_SBIN_PATH } ;
10
8
11
9
/// harness to test nginx
12
10
pub struct Nginx {
13
- pub install_path : String ,
11
+ pub install_path : PathBuf ,
14
12
}
15
13
16
14
impl Default for Nginx {
17
15
/// create nginx with default
18
16
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) ;
17
+ let install_path = ngx:: ffi:: ngx_install_path ( ) . expect ( "installation prefix" ) ;
24
18
Nginx { install_path }
25
19
}
26
20
}
27
21
28
22
impl Nginx {
29
- pub fn new ( path : String ) -> Nginx {
30
- Nginx { install_path : path }
23
+ pub fn new < P : AsRef < Path > > ( path : P ) -> Nginx {
24
+ Nginx {
25
+ install_path : path. as_ref ( ) . into ( ) ,
26
+ }
31
27
}
32
28
33
29
/// get bin path to nginx instance
34
- pub fn bin_path ( & mut self ) -> String {
35
- format ! ( "{}/{}" , self . install_path, NGINX_BIN )
30
+ pub fn bin_path ( & mut self ) -> PathBuf {
31
+ let bin_path = ngx:: ffi:: uchar_to_path ( NGX_SBIN_PATH ) . expect ( "binary path" ) ;
32
+ self . install_path . join ( bin_path)
36
33
}
37
34
38
35
/// start nginx process with arguments
@@ -70,9 +67,10 @@ impl Nginx {
70
67
}
71
68
72
69
// 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
70
+ pub fn replace_config ( & mut self , from : impl AsRef < Path > ) -> Result < u64 > {
71
+ let config_path = ngx:: ffi:: uchar_to_path ( NGX_CONF_PATH ) . expect ( "configuration path" ) ;
72
+ let config_path = self . install_path . join ( config_path) ;
73
+ println ! ( "copying config from: {:?} to: {:?}" , from. as_ref( ) , config_path) ; // replace with logging
76
74
fs:: copy ( from, config_path)
77
75
}
78
76
}
@@ -99,7 +97,7 @@ mod tests {
99
97
) ;
100
98
101
99
nginx
102
- . replace_config ( & test_config_path. to_string_lossy ( ) )
100
+ . replace_config ( & test_config_path)
103
101
. expect ( format ! ( "Unable to load config file: {}" , test_config_path. to_string_lossy( ) ) . as_str ( ) ) ;
104
102
let output = nginx. restart ( ) . expect ( "Unable to restart NGINX" ) ;
105
103
assert ! ( output. status. success( ) ) ;
0 commit comments