1 // Copyright 2014 Carl Lerche, Steve Klabnik, Alex Crichton, Ben Longbons,
2 // Michael Gehring, Yehuda Katz
3 // Copyright 2015 Carl Lerche, Alex Crichton, Graham Dennis, Robin Gloster
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std
::path
::{Path, PathBuf}
;
15 use {success, expect, Matcher, MatchResult}
;
17 #[derive(Clone, Copy)]
24 #[derive(Clone, Copy)]
25 pub struct ExistingPath
{
30 fn match_path_type(&self, actual
: &Path
) -> MatchResult
{
31 let metadata
= fs
::metadata(actual
);
32 match self.path_type
{
35 metadata
.map(|m
| m
.is_file()).unwrap_or(false),
36 format
!("`{}` was not a file", actual
.display()))
40 metadata
.map(|m
| m
.is_dir()).unwrap_or(false),
41 format
!("`{}` was not a dir", actual
.display()))
48 impl fmt
::Display
for ExistingPath
{
49 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
50 write
!(f
, "an existing file")
54 impl<'a
> Matcher
<&'a PathBuf
> for ExistingPath
{
55 fn matches(&self, actual
: &'a PathBuf
) -> MatchResult
{
56 self.matches(&**actual
)
60 impl<'a
> Matcher
<&'a Path
> for ExistingPath
{
61 fn matches(&self, actual
: &Path
) -> MatchResult
{
62 expect(fs
::metadata(actual
).is_ok(),
63 format
!("{} was missing", actual
.display()))
64 .and(self.match_path_type(actual
))
68 pub fn existing_path() -> ExistingPath
{
69 ExistingPath { path_type: PathType::AnyType }
72 pub fn existing_file() -> ExistingPath
{
73 ExistingPath { path_type: PathType::File }
76 pub fn existing_dir() -> ExistingPath
{
77 ExistingPath { path_type: PathType::Dir }
83 use std
::borrow
::ToOwned
;
84 use std
::path
::{Path, PathBuf}
;
85 use {assert_that, is, is_not, existing_file, existing_dir, existing_path}
;
88 fn an_existing_file() {
89 let path
= path(env
::var("TEST_EXISTS_FILE"), "./README.md");
91 assert_that(&path
, is(existing_path()));
92 assert_that(&path
, is(existing_file()));
93 assert_that(&path
, is_not(existing_dir()));
97 fn an_existing_dir() {
98 let path
= path(env
::var("TEST_EXISTS_DIR"), "./target");
100 assert_that(&path
, is(existing_path()));
101 assert_that(&path
, is(existing_dir()));
102 assert_that(&path
, is_not(existing_file()));
106 fn a_nonexisting_path() {
107 let path
= path(env
::var("TEST_EXISTS_NONE"), "./zomg.txt");
109 assert_that(&path
, is_not(existing_path()));
110 assert_that(&path
, is_not(existing_file()));
111 assert_that(&path
, is_not(existing_dir()));
114 fn path(path
: Result
<String
, env
::VarError
>, default: &str) -> PathBuf
{
115 Path
::new(&path
.unwrap_or(default.to_string())).to_owned()