Skip to content

Commit

Permalink
Disable self-reference code in test
Browse files Browse the repository at this point in the history
  • Loading branch information
eraserhd committed Feb 23, 2020
1 parent ef863cb commit cec2198
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 65 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ https://github.com/eraserhd/parinfer-rust/compare/v0.4.2...HEAD[Unreleased]

* Sometimes dlerror() is null on second invocation, causing SEGV instead
of an informative panic.
* Fix for rust+glibc 2.30 test failures: self-reference code no longer
works in test because glibc 2.30 refuses to load position-independent
code or no-library code, so it is disabled.

https://github.com/eraserhd/parinfer-rust/compare/v0.4.1...v0.4.2[0.4.2]
------------------------------------------------------------------------
Expand Down
73 changes: 8 additions & 65 deletions src/c_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod reference_hack {
use libc::{c_void, dladdr, dlerror, dlopen};
use libc::Dl_info;

static mut INITIALIZED: bool = false;
pub static mut INITIALIZED: bool = false;

#[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "bitrig"))]
mod netbsdlike {
Expand Down Expand Up @@ -90,9 +90,11 @@ mod reference_hack {
use std::os::windows::ffi::{OsStringExt};
use winapi::um::winnt::{LPCWSTR};
use winapi::um::libloaderapi::{ GET_MODULE_HANDLE_EX_FLAG_PIN,
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
GetModuleHandleExW, GetModuleFileNameW};

pub static mut INITIALIZED: bool = false;

pub unsafe fn initialize() {
let mut out = ptr::null_mut();
if GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
Expand Down Expand Up @@ -122,10 +124,14 @@ mod reference_hack {

#[cfg(all(not(windows), not(unix)))]
mod reference_hack {
pub static mut INITIALIZED: bool = true;

pub fn initialize() {
}
}

pub use self::reference_hack::INITIALIZED;

unsafe fn unwrap_c_pointers(json: *const c_char) -> Result<CString, Error> {
let json_str = CStr::from_ptr(json).to_str()?;
let response = common_wrapper::internal_run(json_str)?;
Expand Down Expand Up @@ -155,66 +161,3 @@ pub unsafe extern "C" fn run_parinfer(json: *const c_char) -> *const c_char {
buffer.borrow().as_ref().unwrap().as_ptr()
})
}

#[cfg(test)]
mod tests {
use super::run_parinfer;
use std::ffi::{CStr, CString};
use serde_json;
use serde_json::{Number, Value};

#[test]
fn it_works() {
unsafe {
let json = CString::new(
r#"{
"mode": "indent",
"text": "(def x",
"options": {
"cursorX": 3,
"cursorLine": 0
}
}"#,
).unwrap();
let out = CStr::from_ptr(run_parinfer(json.as_ptr()))
.to_str()
.unwrap();
let answer: Value = serde_json::from_str(out).unwrap();
assert_eq!(
Value::Bool(true),
answer["success"],
"successfully runs parinfer"
);
assert_eq!(
Value::String(String::from("(def x)")),
answer["text"],
"returns correct text"
);
assert_eq!(
Value::Number(Number::from(3)),
answer["cursorX"],
"returns the correct cursorX"
);
assert_eq!(
Value::Number(Number::from(0)),
answer["cursorLine"],
"returns the correct cursorLine"
);
assert_eq!(
Value::Array(vec![]),
answer["tabStops"],
"returns the correct tab stops"
);
let mut obj: serde_json::map::Map<String, Value> = serde_json::map::Map::new();
obj.insert(String::from("endX"), Value::Number(Number::from(7)));
obj.insert(String::from("lineNo"), Value::Number(Number::from(0)));
obj.insert(String::from("startX"), Value::Number(Number::from(6)));
assert_eq!(
Value::Array(vec![Value::Object(obj)]),
answer["parenTrails"],
"returns the paren trails"
);
assert_eq!(Value::Array(vec![]), answer["parens"], "returns the parens");
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mod c_wrapper;
#[cfg(not(target_arch = "wasm32"))]
pub use c_wrapper::run_parinfer;

#[cfg(not(target_arch = "wasm32"))]
pub use c_wrapper::INITIALIZED;

#[cfg(not(target_arch = "wasm32"))]
mod emacs_wrapper;

Expand Down
1 change: 1 addition & 0 deletions tests/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ struct Source {
#[cfg(not(target_arch = "wasm32"))]
fn run(input: &str) -> String {
unsafe {
parinfer_rust::INITIALIZED = true;
let c_input = CString::new(input).unwrap();
String::from(CStr::from_ptr(parinfer_rust::run_parinfer(c_input.as_ptr())).to_str().unwrap())
}
Expand Down

0 comments on commit cec2198

Please sign in to comment.