env vars should be referenced, not copied

Just because the type that gets used right now is Copy and Clone doesn’t mean that when we pass mock ones in for tests they’ll be those two as well. So we have to go through and add &s everywhere.
This commit is contained in:
Benjamin Sago 2017-08-13 11:14:58 +01:00
parent 33e83accd0
commit 6740faa781
5 changed files with 17 additions and 17 deletions

View File

@ -69,7 +69,7 @@ impl Vars for LiveVars {
impl<'args, 'w, W: Write + 'w> Exa<'args, 'w, W> {
pub fn new<I>(args: I, writer: &'w mut W) -> Result<Exa<'args, 'w, W>, Misfire>
where I: Iterator<Item=&'args OsString> {
Options::parse(args, LiveVars).map(move |(options, args)| {
Options::parse(args, &LiveVars).map(move |(options, args)| {
Exa { options, writer, args }
})
}

View File

@ -130,21 +130,21 @@ mod test {
#[test]
fn help() {
let args = [ os("--help") ];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert!(opts.is_err())
}
#[test]
fn help_with_file() {
let args = [ os("--help"), os("me") ];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert!(opts.is_err())
}
#[test]
fn unhelpful() {
let args = [];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert!(opts.is_ok()) // no help when --help isnt passed
}
}

View File

@ -115,7 +115,7 @@ impl Options {
/// struct and a list of free filenames, using the environment variables
/// for extra options.
#[allow(unused_results)]
pub fn parse<'args, I, V>(args: I, vars: V) -> Result<(Options, Vec<&'args OsStr>), Misfire>
pub fn parse<'args, I, V>(args: I, vars: &V) -> Result<(Options, Vec<&'args OsStr>), Misfire>
where I: IntoIterator<Item=&'args OsString>,
V: Vars {
use options::parser::{Matches, Strictness};
@ -151,7 +151,7 @@ impl Options {
/// Determines the complete set of options based on the given command-line
/// arguments, after theyve been parsed.
fn deduce<V: Vars>(matches: &MatchedFlags, vars: V) -> Result<Options, Misfire> {
fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<Options, Misfire> {
let dir_action = DirAction::deduce(matches)?;
let filter = FileFilter::deduce(matches)?;
let view = View::deduce(matches, vars)?;
@ -230,28 +230,28 @@ pub mod test {
#[test]
fn files() {
let args = [ os("this file"), os("that file") ];
let outs = Options::parse(&args, None).unwrap().1;
let outs = Options::parse(&args, &None).unwrap().1;
assert_eq!(outs, vec![ &os("this file"), &os("that file") ])
}
#[test]
fn no_args() {
let nothing: Vec<OsString> = Vec::new();
let outs = Options::parse(&nothing, None).unwrap().1;
let outs = Options::parse(&nothing, &None).unwrap().1;
assert!(outs.is_empty()); // Listing the `.` directory is done in main.rs
}
#[test]
fn long_across() {
let args = [ os("--long"), os("--across") ];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert_eq!(opts.unwrap_err(), Misfire::Useless(&flags::ACROSS, true, &flags::LONG))
}
#[test]
fn oneline_across() {
let args = [ os("--oneline"), os("--across") ];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert_eq!(opts.unwrap_err(), Misfire::Useless(&flags::ACROSS, true, &flags::ONE_LINE))
}
}

View File

@ -54,7 +54,7 @@ mod test {
#[test]
fn help() {
let args = [ os("--version") ];
let opts = Options::parse(&args, None);
let opts = Options::parse(&args, &None);
assert!(opts.is_err())
}
}

View File

@ -13,7 +13,7 @@ use info::filetype::FileExtensions;
impl View {
/// Determine which view to use and all of that views arguments.
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: V) -> Result<View, Misfire> {
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<View, Misfire> {
let mode = Mode::deduce(matches, vars)?;
let colours = Colours::deduce(matches)?;
let style = FileStyle::deduce(matches)?;
@ -25,7 +25,7 @@ impl View {
impl Mode {
/// Determine the mode from the command-line arguments.
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: V) -> Result<Mode, Misfire> {
pub fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<Mode, Misfire> {
use options::misfire::Misfire::*;
let long = || {
@ -97,7 +97,7 @@ impl Mode {
if matches.has(&flags::GRID)? {
let other_options_mode = other_options_scan()?;
if let Mode::Grid(grid) = other_options_mode {
let row_threshold = Some(5);
let row_threshold = RowThreshold::deduce(vars)?;
return Ok(Mode::GridDetails(grid_details::Options { grid, details, row_threshold }));
}
else {
@ -153,7 +153,7 @@ impl TerminalWidth {
/// Determine a requested terminal width from the command-line arguments.
///
/// Returns an error if a requested width doesnt parse to an integer.
fn deduce<V: Vars>(vars: V) -> Result<TerminalWidth, Misfire> {
fn deduce<V: Vars>(vars: &V) -> Result<TerminalWidth, Misfire> {
if let Some(columns) = vars.get("COLUMNS").and_then(|s| s.into_string().ok()) {
match columns.parse() {
Ok(width) => Ok(TerminalWidth::Set(width)),
@ -482,7 +482,7 @@ mod test {
/// Like above, but with $vars.
#[test]
fn $name() {
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, $vars)) {
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &$vars)) {
assert_eq!(result.unwrap_err(), $result);
}
}
@ -492,7 +492,7 @@ mod test {
/// Like further above, but with $vars.
#[test]
fn $name() {
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, $vars)) {
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &$vars)) {
println!("Testing {:?}", result);
match result {
$pat => assert!(true),