Skip to content

Get rid of git_attr__* symbols #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 9 additions & 31 deletions src/attr.rs
Original file line number Diff line number Diff line change
@@ -87,36 +87,26 @@ impl PartialEq for AttrValue<'_> {
#[cfg(test)]
mod tests {
use super::AttrValue;
use std::ffi::CStr;
use std::os::raw::c_char;

extern "C" {
// libgit2 defines them as mutable, so they are also declared mutable here.
// However, libgit2 never mutates them, thus it's always safe to access them in Rust.
static mut git_attr__true: *const c_char;
static mut git_attr__false: *const c_char;
static mut git_attr__unset: *const c_char;
}

macro_rules! test_attr_value {
($function:ident, $variant:ident) => {
let attr_true = unsafe { CStr::from_ptr(git_attr__true) }.to_str().unwrap();
let attr_false = unsafe { CStr::from_ptr(git_attr__false) }.to_str().unwrap();
let attr_unset = unsafe { CStr::from_ptr(git_attr__unset) }.to_str().unwrap();
const ATTR_TRUE: &str = "[internal]__TRUE__";
const ATTR_FALSE: &str = "[internal]__FALSE__";
const ATTR_UNSET: &str = "[internal]__UNSET__";
let as_bytes = AsRef::<[u8]>::as_ref;
// Use `matches!` here since the `PartialEq` implementation does not differentiate
// between `String` and `Bytes`.
assert!(matches!(
AttrValue::$function(Some(attr_true.to_owned().as_ref())),
AttrValue::$variant(s) if as_bytes(s) == attr_true.as_bytes()
AttrValue::$function(Some(ATTR_TRUE.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_TRUE.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some(attr_false.to_owned().as_ref())),
AttrValue::$variant(s) if as_bytes(s) == attr_false.as_bytes()
AttrValue::$function(Some(ATTR_FALSE.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_FALSE.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some(attr_unset.to_owned().as_ref())),
AttrValue::$variant(s) if as_bytes(s) == attr_unset.as_bytes()
AttrValue::$function(Some(ATTR_UNSET.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_UNSET.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some("foo".as_ref())),
@@ -126,18 +116,6 @@ mod tests {
AttrValue::$function(Some("bar".as_ref())),
AttrValue::$variant(s) if as_bytes(s) == b"bar"
));
assert_eq!(
AttrValue::$function(Some(attr_true.as_ref())),
AttrValue::True
);
assert_eq!(
AttrValue::$function(Some(attr_false.as_ref())),
AttrValue::False
);
assert_eq!(
AttrValue::$function(Some(attr_unset.as_ref())),
AttrValue::Unspecified
);
assert_eq!(AttrValue::$function(None), AttrValue::Unspecified);
};
}