]> git.proxmox.com Git - rustc.git/blob - src/vendor/hamcrest/src/matchers/existing_path.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / vendor / hamcrest / src / matchers / existing_path.rs
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
4 //
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.
10
11 use std::fmt;
12 use std::path::{Path, PathBuf};
13 use std::fs;
14
15 use {success, expect, Matcher, MatchResult};
16
17 #[derive(Clone, Copy)]
18 pub enum PathType {
19 AnyType,
20 File,
21 Dir
22 }
23
24 #[derive(Clone, Copy)]
25 pub struct ExistingPath {
26 path_type: PathType
27 }
28
29 impl ExistingPath {
30 fn match_path_type(&self, actual: &Path) -> MatchResult {
31 let metadata = fs::metadata(actual);
32 match self.path_type {
33 PathType::File => {
34 expect(
35 metadata.map(|m| m.is_file()).unwrap_or(false),
36 format!("`{}` was not a file", actual.display()))
37 }
38 PathType::Dir => {
39 expect(
40 metadata.map(|m| m.is_dir()).unwrap_or(false),
41 format!("`{}` was not a dir", actual.display()))
42 }
43 _ => success(),
44 }
45 }
46 }
47
48 impl fmt::Display for ExistingPath {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 write!(f, "an existing file")
51 }
52 }
53
54 impl<'a> Matcher<&'a PathBuf> for ExistingPath {
55 fn matches(&self, actual: &'a PathBuf) -> MatchResult {
56 self.matches(&**actual)
57 }
58 }
59
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))
65 }
66 }
67
68 pub fn existing_path() -> ExistingPath {
69 ExistingPath { path_type: PathType::AnyType }
70 }
71
72 pub fn existing_file() -> ExistingPath {
73 ExistingPath { path_type: PathType::File }
74 }
75
76 pub fn existing_dir() -> ExistingPath {
77 ExistingPath { path_type: PathType::Dir }
78 }
79
80 #[cfg(test)]
81 mod test {
82 use std::env;
83 use std::borrow::ToOwned;
84 use std::path::{Path, PathBuf};
85 use {assert_that, is, is_not, existing_file, existing_dir, existing_path};
86
87 #[test]
88 fn an_existing_file() {
89 let path = path(env::var("TEST_EXISTS_FILE"), "./README.md");
90
91 assert_that(&path, is(existing_path()));
92 assert_that(&path, is(existing_file()));
93 assert_that(&path, is_not(existing_dir()));
94 }
95
96 #[test]
97 fn an_existing_dir() {
98 let path = path(env::var("TEST_EXISTS_DIR"), "./target");
99
100 assert_that(&path, is(existing_path()));
101 assert_that(&path, is(existing_dir()));
102 assert_that(&path, is_not(existing_file()));
103 }
104
105 #[test]
106 fn a_nonexisting_path() {
107 let path = path(env::var("TEST_EXISTS_NONE"), "./zomg.txt");
108
109 assert_that(&path, is_not(existing_path()));
110 assert_that(&path, is_not(existing_file()));
111 assert_that(&path, is_not(existing_dir()));
112 }
113
114 fn path(path: Result<String, env::VarError>, default: &str) -> PathBuf {
115 Path::new(&path.unwrap_or(default.to_string())).to_owned()
116 }
117 }