1- use std:: env;
21use std:: fs;
32use std:: io:: Result ;
3+ use std:: path:: { Path , PathBuf } ;
44use std:: process:: Command ;
55use std:: process:: Output ;
66
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 } ;
108
119/// harness to test nginx
1210pub struct Nginx {
13- pub install_path : String ,
11+ pub install_path : PathBuf ,
1412}
1513
1614impl Default for Nginx {
1715 /// create nginx with default
1816 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" ) ;
2418 Nginx { install_path }
2519 }
2620}
2721
2822impl 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+ }
3127 }
3228
3329 /// 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)
3633 }
3734
3835 /// start nginx process with arguments
@@ -70,9 +67,10 @@ impl Nginx {
7067 }
7168
7269 // 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
7674 fs:: copy ( from, config_path)
7775 }
7876}
@@ -99,7 +97,7 @@ mod tests {
9997 ) ;
10098
10199 nginx
102- . replace_config ( & test_config_path. to_string_lossy ( ) )
100+ . replace_config ( & test_config_path)
103101 . expect ( format ! ( "Unable to load config file: {}" , test_config_path. to_string_lossy( ) ) . as_str ( ) ) ;
104102 let output = nginx. restart ( ) . expect ( "Unable to restart NGINX" ) ;
105103 assert ! ( output. status. success( ) ) ;
0 commit comments