Skip to content

Commit

Permalink
Add automated tests for FFI defn's
Browse files Browse the repository at this point in the history
Also correct a number of mistakes!
  • Loading branch information
alexcrichton committed Sep 16, 2015
1 parent 16d32f2 commit bfc1d68
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 48 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ script:
- export CARGO_TARGET_DIR=`pwd`/target
- cargo build --verbose
- cargo test --verbose
- cargo run --manifest-path systest/Cargo.toml
- if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
cargo test --features unstable;
cargo test --verbose --manifest-path git2-curl/Cargo.toml;
Expand Down
60 changes: 20 additions & 40 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct git_time {
pub type git_off_t = i64;
pub type git_time_t = i64;

pub type git_revparse_mode_t = c_int;
pub type git_revparse_mode_t = c_uint;
pub const GIT_REVPARSE_SINGLE: c_int = 1 << 0;
pub const GIT_REVPARSE_RANGE: c_int = 1 << 1;
pub const GIT_REVPARSE_MERGE_BASE: c_int = 1 << 2;
Expand Down Expand Up @@ -237,7 +237,7 @@ pub struct git_checkout_options {
pub our_label: *const c_char,
pub their_label: *const c_char,
pub perfdata_cb: Option<git_checkout_perfdata_cb>,
pub perdata_payload: *mut c_void,
pub perfdata_payload: *mut c_void,
}

pub type git_checkout_notify_cb = extern fn(git_checkout_notify_t,
Expand Down Expand Up @@ -334,7 +334,7 @@ pub type git_transport_certificate_check_cb = extern fn(*mut git_cert,
c_int,
*const c_char,
*mut c_void) -> c_int;
pub type git_push_negotiation = extern fn(*const *const git_push_update,
pub type git_push_negotiation = extern fn(*mut *const git_push_update,
size_t,
*mut c_void) -> c_int;

Expand All @@ -361,15 +361,15 @@ pub struct git_cert {

#[repr(C)]
pub struct git_cert_hostkey {
pub cert_type: git_cert_t,
pub parent: git_cert,
pub kind: git_cert_ssh_t,
pub hash_md5: [u8; 16],
pub hash_sha1: [u8; 20],
}

#[repr(C)]
pub struct git_cert_x509 {
pub cert_type: git_cert_t,
pub parent: git_cert,
pub data: *mut c_void,
pub len: size_t,
}
Expand Down Expand Up @@ -690,6 +690,8 @@ pub struct git_config_entry {
pub name: *const c_char,
pub value: *const c_char,
pub level: git_config_level_t,
pub free: extern fn(*mut git_config_entry),
pub payload: *mut c_void,
}

#[repr(C)]
Expand Down Expand Up @@ -893,7 +895,7 @@ pub struct git_diff_hunk {
pub header: [u8; 128],
}

pub type git_diff_line_t = u8;
pub type git_diff_line_t = c_uint;
pub const GIT_DIFF_LINE_CONTEXT: u8 = ' ' as u8;
pub const GIT_DIFF_LINE_ADDITION: u8 = '+' as u8;
pub const GIT_DIFF_LINE_DELETION: u8 = '-' as u8;
Expand All @@ -902,7 +904,7 @@ pub const GIT_DIFF_LINE_ADD_EOFNL: u8 = '>' as u8;
pub const GIT_DIFF_LINE_DEL_EOFNL: u8 = '<' as u8;
pub const GIT_DIFF_LINE_FILE_HDR: u8 = 'F' as u8;
pub const GIT_DIFF_LINE_HUNK_HDR: u8 = 'H' as u8;
pub const GIT_DIFF_LINE_LINE_BINARY: u8 = 'B' as u8;
pub const GIT_DIFF_LINE_BINARY: u8 = 'B' as u8;

#[repr(C)]
pub struct git_diff_line {
Expand Down Expand Up @@ -956,7 +958,6 @@ pub type git_diff_notify_cb = extern fn(*const git_diff,
*const c_char,
*mut c_void) -> c_int;

pub type git_diff_options_t = u32;
pub const GIT_DIFF_NORMAL: u32 = 0;
pub const GIT_DIFF_REVERSE: u32 = 1 << 0;
pub const GIT_DIFF_INCLUDE_IGNORED: u32 = 1 << 1;
Expand Down Expand Up @@ -1043,7 +1044,7 @@ pub struct git_diff_binary {

#[repr(C)]
pub struct git_diff_binary_file {
pub type_: git_diff_binary_t,
pub kind: git_diff_binary_t,
pub data: *const c_char,
pub datalen: size_t,
pub inflatedlen: size_t,
Expand Down Expand Up @@ -1115,7 +1116,7 @@ pub struct git_transport {
*const git_remote_callbacks) -> c_int,
pub negotiate_fetch: extern fn(*mut git_transport,
*mut git_repository,
*const *const git_remote_head,
*mut *const git_remote_head,
size_t) -> c_int,
pub download_pack: extern fn(*mut git_transport,
*mut git_repository,
Expand Down Expand Up @@ -1297,7 +1298,7 @@ pub fn openssl_init() {}
extern {
// threads
pub fn git_libgit2_init() -> c_int;
pub fn git_libgit2_shutdown();
pub fn git_libgit2_shutdown() -> c_int;

// repository
pub fn git_repository_free(repo: *mut git_repository);
Expand Down Expand Up @@ -1382,7 +1383,6 @@ extern {
// giterr
pub fn giterr_last() -> *const git_error;
pub fn giterr_clear();
pub fn giterr_detach(cpy: *mut git_error) -> c_int;
pub fn giterr_set_str(error_class: c_int, string: *const c_char);

// remote
Expand All @@ -1406,7 +1406,7 @@ extern {
pub fn git_remote_connect(remote: *mut git_remote,
dir: git_direction,
callbacks: *const git_remote_callbacks) -> c_int;
pub fn git_remote_connected(remote: *mut git_remote) -> c_int;
pub fn git_remote_connected(remote: *const git_remote) -> c_int;
pub fn git_remote_disconnect(remote: *mut git_remote);
pub fn git_remote_add_fetch(repo: *mut git_repository,
remote: *const c_char,
Expand Down Expand Up @@ -1522,7 +1522,7 @@ extern {
pub fn git_reset(repo: *mut git_repository,
target: *mut git_object,
reset_type: git_reset_t,
checkout_opts: *mut git_checkout_options) -> c_int;
checkout_opts: *const git_checkout_options) -> c_int;
pub fn git_reset_default(repo: *mut git_repository,
target: *mut git_object,
pathspecs: *mut git_strarray) -> c_int;
Expand All @@ -1545,7 +1545,7 @@ extern {
repo: *mut git_repository,
name: *const c_char) -> c_int;
pub fn git_reference_peel(out: *mut *mut git_object,
r: *const git_reference,
r: *mut git_reference,
otype: git_otype) -> c_int;
pub fn git_reference_rename(new_ref: *mut *mut git_reference,
r: *mut git_reference,
Expand Down Expand Up @@ -1624,7 +1624,7 @@ extern {
pub fn git_submodule_set_update(repo: *mut git_repository,
name: *const c_char,
update: git_submodule_update_t)
-> git_submodule_update_t;
-> c_int;
pub fn git_submodule_set_url(repo: *mut git_repository,
name: *const c_char,
url: *const c_char) -> c_int;
Expand Down Expand Up @@ -1752,7 +1752,7 @@ extern {
message: *const c_char,
tree: *const git_tree,
parent_count: size_t,
parents: *const *const git_commit) -> c_int;
parents: *mut *const git_commit) -> c_int;
pub fn git_commit_header_field(out: *mut git_buf,
commit: *const git_commit,
field: *const c_char) -> c_int;
Expand Down Expand Up @@ -1957,26 +1957,6 @@ extern {
pub fn git_cred_username_new(cred: *mut *mut git_cred,
username: *const c_char) -> c_int;

// push
pub fn git_push_add_refspec(push: *mut git_push,
refspec: *const c_char) -> c_int;
pub fn git_push_finish(push: *mut git_push) -> c_int;
pub fn git_push_free(push: *mut git_push);
pub fn git_push_init_options(opts: *mut git_push_options,
version: c_uint) -> c_int;
pub fn git_push_new(out: *mut *mut git_push,
remote: *mut git_remote) -> c_int;
pub fn git_push_set_options(push: *mut git_push,
opts: *const git_push_options) -> c_int;
pub fn git_push_update_tips(push: *mut git_push,
signature: *const git_signature,
reflog_message: *const c_char) -> c_int;
pub fn git_push_status_foreach(push: *mut git_push,
cb: extern fn(*const c_char,
*const c_char,
*mut c_void) -> c_int,
data: *mut c_void) -> c_int;

// tags
pub fn git_tag_annotation_create(oid: *mut git_oid,
repo: *mut git_repository,
Expand Down Expand Up @@ -2052,7 +2032,7 @@ extern {
pub fn git_merge_init_options(opts: *mut git_merge_options,
version: c_uint) -> c_int;
pub fn git_merge(repo: *mut git_repository,
their_heads: *const *const git_annotated_commit,
their_heads: *mut *const git_annotated_commit,
len: size_t,
merge_opts: *const git_merge_options,
checkout_opts: *const git_checkout_options) -> c_int;
Expand Down Expand Up @@ -2168,7 +2148,7 @@ extern {
pos: size_t) -> *const c_char;
pub fn git_pathspec_match_list_failed_entrycount(
m: *const git_pathspec_match_list) -> size_t;
pub fn git_pathspec_match_list_free(m: *const git_pathspec_match_list);
pub fn git_pathspec_match_list_free(m: *mut git_pathspec_match_list);
pub fn git_pathspec_match_tree(out: *mut *mut git_pathspec_match_list,
tree: *mut git_tree,
flags: u32,
Expand Down Expand Up @@ -2331,6 +2311,6 @@ fn smoke() {
unsafe { git_threads_init(); }
}

pub fn issue_14344_workaround() {
pub fn __issue_14344_workaround() {
libssh2::issue_14344_workaround();
}
2 changes: 1 addition & 1 deletion src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ impl<'a> DiffLine<'a> {
raw::GIT_DIFF_LINE_DEL_EOFNL => '<',
raw::GIT_DIFF_LINE_FILE_HDR => 'F',
raw::GIT_DIFF_LINE_HUNK_HDR => 'H',
raw::GIT_DIFF_LINE_LINE_BINARY => 'B',
raw::GIT_DIFF_LINE_BINARY => 'B',
_ => ' ',
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ fn init() {
"couldn't initialize the libgit2 library: {}", r);
assert_eq!(libc::atexit(shutdown), 0);
});
extern fn shutdown() { unsafe { raw::git_libgit2_shutdown() } }
extern fn shutdown() {
unsafe { raw::git_libgit2_shutdown(); }
}
}

unsafe fn opt_bytes<'a, T>(_anchor: &'a T,
Expand Down
2 changes: 1 addition & 1 deletion src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'repo> Reference<'repo> {
pub fn peel(&self, kind: ObjectType) -> Result<Object<'repo>, Error> {
let mut raw = 0 as *mut raw::git_object;
unsafe {
try_call!(raw::git_reference_peel(&mut raw, &*self.raw, kind));
try_call!(raw::git_reference_peel(&mut raw, self.raw, kind));
Ok(Binding::from_raw(raw))
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,9 @@ impl Repository {
tree: &Tree,
parents: &[&Commit]) -> Result<Oid, Error> {
let update_ref = try!(::opt_cstr(update_ref));
let parent_ptrs: Vec<*const raw::git_commit> = parents.iter().map(|p| {
let mut parent_ptrs = parents.iter().map(|p| {
p.raw() as *const raw::git_commit
}).collect();
}).collect::<Vec<_>>();
let message = try!(CString::new(message));
let mut raw = raw::git_oid { id: [0; raw::GIT_OID_RAWSZ] };
unsafe {
Expand All @@ -747,7 +747,7 @@ impl Repository {
message,
tree.raw(),
parents.len() as size_t,
parent_ptrs.as_ptr()));
parent_ptrs.as_mut_ptr()));
Ok(Binding::from_raw(&raw as *const _))
}
}
Expand Down Expand Up @@ -1092,12 +1092,12 @@ impl Repository {
c.configure(&mut raw_checkout_opts);
}

let commit_ptrs = annotated_commits.iter().map(|c| {
let mut commit_ptrs = annotated_commits.iter().map(|c| {
c.raw() as *const raw::git_annotated_commit
}).collect::<Vec<_>>();

try_call!(raw::git_merge(self.raw,
commit_ptrs.as_ptr(),
commit_ptrs.as_mut_ptr(),
annotated_commits.len() as size_t,
merge_opts.map(|o| o.raw())
.unwrap_or(0 as *const _),
Expand Down
12 changes: 12 additions & 0 deletions systest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "systest"
version = "0.1.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
build = "build.rs"

[dependencies]
libgit2-sys = { path = "../libgit2-sys" }
libc = "0.1"

[build-dependencies]
ctest = { git = "https://github.com/alexcrichton/ctest" }
32 changes: 32 additions & 0 deletions systest/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extern crate ctest;

use std::env;
use std::path::PathBuf;

fn main() {
let root = PathBuf::from(env::var_os("DEP_GIT2_ROOT").unwrap());

let mut cfg = ctest::TestGenerator::new();
cfg.header("git2.h")
.header("git2/sys/transport.h")
.header("git2/cred_helpers.h")
.include(root.join("include"))
.type_name(|s, _| s.to_string());
cfg.field_name(|_, f| {
match f {
"kind" => "type".to_string(),
_ => f.to_string(),
}
});
cfg.skip_signededness(|s| {
match s {
s if s.ends_with("_cb") => true,
s if s.ends_with("_callback") => true,
"git_push_transfer_progress" |
"git_push_negotiation" |
"git_packbuilder_progress" => true,
_ => false,
}
});
cfg.generate("../libgit2-sys/lib.rs", "all.rs");
}
9 changes: 9 additions & 0 deletions systest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(bad_style, improper_ctypes)]

extern crate libgit2_sys;
extern crate libc;

use libc::*;
use libgit2_sys::*;

include!(concat!(env!("OUT_DIR"), "/all.rs"));

0 comments on commit bfc1d68

Please sign in to comment.