Skip to content

Commit 3177015

Browse files
committed
fix: test execution with external Nginx source tree
1 parent 332eeb9 commit 3177015

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

nginx-sys/build/wrapper.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ 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+
#ifdef NGX_PREFIX
14+
const char *NGX_RS_PREFIX = NGX_PREFIX;
15+
#else
16+
const char NGX_RS_PREFIX[] = {};
17+
#endif
18+
19+
#ifdef NGX_CONF_PREFIX
20+
const char *NGX_RS_CONF_PREFIX = NGX_CONF_PREFIX;
21+
#else
22+
const char NGX_RS_CONF_PREFIX[] = {};
23+
#endif

nginx-sys/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
#![warn(missing_docs)]
3232

3333
use std::fmt;
34+
use std::path::PathBuf;
3435
use std::ptr::copy_nonoverlapping;
3536
use std::slice;
3637

38+
#[cfg(unix)]
39+
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
40+
3741
#[doc(hidden)]
3842
mod bindings {
3943
#![allow(missing_docs)]
@@ -49,6 +53,33 @@ mod bindings {
4953
#[doc(no_inline)]
5054
pub use bindings::*;
5155

56+
/// Convert a NULL-terminated u8 slice to a PathBuf
57+
#[cfg(unix)]
58+
pub fn uchar_to_path(mut val: &[u8]) -> Option<PathBuf> {
59+
while let Some(trimmed) = val.strip_suffix(b"\0") {
60+
val = trimmed;
61+
}
62+
63+
if val.is_empty() {
64+
return None;
65+
}
66+
67+
let s = OsStr::from_bytes(val);
68+
Some(std::path::PathBuf::from(s))
69+
}
70+
71+
/// Returns configured Nginx installation prefix (NGX_PREFIX)
72+
pub fn ngx_install_path() -> Option<PathBuf> {
73+
#[cfg(unix)]
74+
{
75+
uchar_to_path(bindings::NGX_RS_PREFIX)
76+
}
77+
#[cfg(not(unix))]
78+
{
79+
None
80+
}
81+
}
82+
5283
/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
5384
///
5485
/// # Arguments

tests/log_test.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
use std::env;
21
use std::fs;
32
use std::io::Result;
3+
use std::path::{Path, PathBuf};
44
use std::process::Command;
55
use 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
1210
pub struct Nginx {
13-
pub install_path: String,
11+
pub install_path: PathBuf,
1412
}
1513

1614
impl 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

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

Comments
 (0)