1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::{Rule, RuleContext, RuleOutcome};
#[derive(Default, Debug)]
pub struct CargoMetadataReadable;
impl Rule for CargoMetadataReadable {
fn description(&self) -> &'static str {
"Should have a well-formed Cargo.toml file readable by `cargo metadata`"
}
fn evaluate(&self, context: RuleContext) -> RuleOutcome {
match *context.metadata {
None => RuleOutcome::Failure,
Some(_) => RuleOutcome::Success,
}
}
}
#[cfg(test)]
mod tests {
use super::super::test_support::*;
use super::*;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;
#[test]
fn cargo_metadata_readable_happy_path_flat_project() {
let dir = tempdir().expect("Failed to make a temp dir");
{
let cargo_path = dir.path().join("Cargo.toml");
let mut cargo_file = File::create(cargo_path).expect("Could not make target file");
cargo_file
.write_all(
br##"[package]
name = "a_minimal_package"
version = "0.1.0"
authors = []
[dependencies]
[dev-dependencies]
"##,
)
.expect("Could not write to Cargo.toml file");
}
write_src_lib_file(dir.path());
let rule = CargoMetadataReadable::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Success, verbose.outcome);
assert_eq!(RuleOutcome::Success, not_verbose.outcome);
}
#[test]
fn cargo_metadata_readable_happy_path_workspace_project() {
let base_dir = tempdir().expect("Failed to make a temp dir");
{
let workspace_cargo_path = base_dir.path().join("Cargo.toml");
create_workspace_cargo_toml(workspace_cargo_path);
}
let subproject_dir = base_dir.path().join("kid");
create_dir_all(&subproject_dir).expect("Could not create subproject dir");
{
let cargo_path = subproject_dir.join("Cargo.toml");
let mut cargo_file = File::create(cargo_path).expect("Could not make target file");
cargo_file
.write_all(
br##"[package]
name = "kid"
version = "0.1.0"
authors = []
[dependencies]
[dev-dependencies]
"##,
)
.expect("Could not write to Cargo.toml file");
write_src_lib_file(&subproject_dir);
}
let rule = CargoMetadataReadable::default();
{
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(base_dir.path(), &rule);
assert_eq!(RuleOutcome::Success, verbose.outcome);
assert_eq!(RuleOutcome::Success, not_verbose.outcome);
}
{
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(&subproject_dir, &rule);
assert_eq!(RuleOutcome::Success, verbose.outcome);
assert_eq!(RuleOutcome::Success, not_verbose.outcome);
}
}
fn write_src_lib_file(project_dir: &Path) {
let src_dir = project_dir.join("src");
create_dir_all(&src_dir).expect("Could not create src dir");
let file_path = src_dir.join("lib.rs");
let mut file = File::create(file_path).expect("Could not make target file");
file.write_all(
br##"//! Sample rust file for testing cargo-culture
fn hello() { println!("Hello"); }
"##,
).expect("Could not write to target file");
}
#[test]
fn empty_dir_fails() {
let dir = tempdir().expect("Failed to make a temp dir");
let rule = CargoMetadataReadable::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Failure, verbose.outcome);
assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
}
#[test]
fn non_toml_manifest_fails() {
let dir = tempdir().expect("Failed to make a temp dir");
{
let cargo_path = dir.path().join("Cargo.toml");
let mut cargo_file = File::create(cargo_path).expect("Could not make target file");
cargo_file
.write_all(br##"{"wat": true}"##)
.expect("Could not write to Cargo.toml file");
}
write_src_lib_file(dir.path());
let rule = CargoMetadataReadable::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Failure, verbose.outcome);
assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
}
}