Skip to content

Commit 4e6520f

Browse files
committed
fix: test execution with external Nginx source tree
1 parent b9efd55 commit 4e6520f

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

nginx-sys/build/wrapper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ const size_t NGX_RS_HTTP_SRV_CONF_OFFSET = NGX_HTTP_SRV_CONF_OFFSET;
99
const size_t NGX_RS_HTTP_LOC_CONF_OFFSET = NGX_HTTP_LOC_CONF_OFFSET;
1010

1111
const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE;
12+
13+
// `--prefix=` results in not emitting the declaration
14+
#ifndef NGX_PREFIX
15+
#define NGX_PREFIX ""
16+
#endif
17+
18+
#ifndef NGX_CONF_PREFIX
19+
#define NGX_CONF_PREFIX NGX_PREFIX
20+
#endif

tests/log_test.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1-
use std::env;
21
use std::fs;
32
use std::io::Result;
3+
#[cfg(unix)]
4+
use std::os::unix::ffi::OsStrExt;
5+
use std::path::{Path, PathBuf};
46
use std::process::Command;
57
use std::process::Output;
68

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+
}
1024

1125
/// harness to test nginx
1226
pub struct Nginx {
13-
pub install_path: String,
27+
pub install_path: PathBuf,
28+
pub config_path: PathBuf,
1429
}
1530

1631
impl Default for Nginx {
1732
/// create nginx with default
1833
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)
2536
}
2637
}
2738

2839
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+
}
3149
}
3250

3351
/// 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)
3655
}
3756

3857
/// start nginx process with arguments
@@ -70,10 +89,9 @@ impl Nginx {
7089
}
7190

7291
// 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)
7795
}
7896
}
7997

@@ -99,7 +117,7 @@ mod tests {
99117
);
100118

101119
nginx
102-
.replace_config(&test_config_path.to_string_lossy())
120+
.replace_config(&test_config_path)
103121
.expect(format!("Unable to load config file: {}", test_config_path.to_string_lossy()).as_str());
104122
let output = nginx.restart().expect("Unable to restart NGINX");
105123
assert!(output.status.success());

0 commit comments

Comments
 (0)