Skip to content
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

Support specifying range of versions. #75

Merged
merged 8 commits into from Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 67 additions & 8 deletions src/lib.rs
Expand Up @@ -71,6 +71,7 @@ use std::error;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::io;
use std::ops::{Bound, RangeBounds};
use std::path::{PathBuf, Path};
use std::process::{Command, Output};
use std::str;
Expand All @@ -84,10 +85,11 @@ pub fn target_supported() -> bool {
(host == target || env::var_os("PKG_CONFIG_ALLOW_CROSS").is_some())
}

#[derive(Clone, Default)]
#[derive(Clone)]
pub struct Config {
statik: Option<bool>,
atleast_version: Option<String>,
min_version: Bound<String>,
max_version: Bound<String>,
extra_args: Vec<OsString>,
cargo_metadata: bool,
env_metadata: bool,
Expand Down Expand Up @@ -264,7 +266,8 @@ impl Config {
pub fn new() -> Config {
Config {
statik: None,
atleast_version: None,
min_version: Bound::Unbounded,
max_version: Bound::Unbounded,
extra_args: vec![],
print_system_libs: true,
cargo_metadata: true,
Expand All @@ -283,7 +286,33 @@ impl Config {

/// Indicate that the library must be at least version `vers`.
pub fn atleast_version(&mut self, vers: &str) -> &mut Config {
self.atleast_version = Some(vers.to_string());
self.min_version = Bound::Included(vers.to_string());
self.max_version = Bound::Unbounded;
self
}

/// Indicate that the library must be equal to version `vers`.
pub fn eq_version(&mut self, vers: &str) -> &mut Config {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe exactly_version()?

self.min_version = Bound::Included(vers.to_string());
self.max_version = Bound::Included(vers.to_string());
self
}

/// Indicate that the library's version must be in `range`.
pub fn range_version<'a, R>(&mut self, range: R) -> &mut Config
where
R: RangeBounds<&'a str>
sdroege marked this conversation as resolved.
Show resolved Hide resolved
{
self.min_version = match range.start_bound() {
Bound::Included(vers) => Bound::Included(vers.to_string()),
Bound::Excluded(vers) => Bound::Excluded(vers.to_string()),
Bound::Unbounded => Bound::Unbounded,
};
self.max_version = match range.end_bound() {
Bound::Included(vers) => Bound::Included(vers.to_string()),
Bound::Excluded(vers) => Bound::Excluded(vers.to_string()),
Bound::Unbounded => Bound::Unbounded,
};
self
}

Expand Down Expand Up @@ -408,10 +437,24 @@ impl Config {
if self.print_system_libs {
cmd.env("PKG_CONFIG_ALLOW_SYSTEM_LIBS", "1");
}
if let Some(ref version) = self.atleast_version {
cmd.arg(&format!("{} >= {}", name, version));
} else {
cmd.arg(name);
cmd.arg(name);
match self.min_version {
Bound::Included(ref version) => {
cmd.arg(&format!("{} >= {}", name, version));
}
Bound::Excluded(ref version) => {
cmd.arg(&format!("{} > {}", name, version));
}
_ => (),
}
match self.max_version {
Bound::Included(ref version) => {
cmd.arg(&format!("{} <= {}", name, version));
}
Bound::Excluded(ref version) => {
cmd.arg(&format!("{} < {}", name, version));
}
_ => (),
}
cmd
}
Expand All @@ -438,6 +481,22 @@ impl Config {
}
}


// Implement Default manualy since Bound does not implement Default.
impl Default for Config {
fn default() -> Config {
Config {
statik: None,
min_version: Bound::Unbounded,
max_version: Bound::Unbounded,
extra_args: vec![],
print_system_libs: false,
cargo_metadata: false,
env_metadata: false,
}
}
}

impl Library {
fn new() -> Library {
Library {
Expand Down
82 changes: 82 additions & 0 deletions tests/test.rs
Expand Up @@ -116,3 +116,85 @@ fn version() {
reset();
assert_eq!(&find("foo").unwrap().version[..], "3.10.0.SVN");
}

#[test]
fn atleast_version_ok() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().atleast_version("3.10").probe("foo").unwrap();
}

#[test]
#[should_panic]
fn atleast_version_ng() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().atleast_version("3.11").probe("foo").unwrap();
}

#[test]
fn eq_version_ok() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().eq_version("3.10.0.SVN").probe("foo").unwrap();
}

#[test]
#[should_panic]
fn eq_version_ng() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().eq_version("3.10.0").probe("foo").unwrap();
}

#[test]
fn range_version_range_ok() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version("3.10".."3.12").probe("foo").unwrap();
}

#[test]
#[should_panic]
fn range_version_range_ng() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version("3.12".."3.15").probe("foo").unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add tests for inclusive vs. exclusive ranges

}

#[test]
fn range_version_range_from_ok() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version("3.10"..).probe("foo").unwrap();
}

#[test]
#[should_panic]
fn range_version_range_from_ng() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version("3.12"..).probe("foo").unwrap();
}

#[test]
fn range_version_range_to_ok() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version(.."3.12").probe("foo").unwrap();
}

#[test]
#[should_panic]
fn range_version_range_to_ng() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version(.."3.9").probe("foo").unwrap();
}

#[test]
fn range_version_full() {
let _g = LOCK.lock();
reset();
pkg_config::Config::new().range_version(..).probe("foo").unwrap();
}