-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.rs
More file actions
206 lines (177 loc) · 6.16 KB
/
config.rs
File metadata and controls
206 lines (177 loc) · 6.16 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use serde::Deserialize;
use std::{fs::File, path::Path};
#[derive(Deserialize, Debug, Clone)]
pub struct Config {
pub owned_globs: Vec<String>,
#[serde(default = "ruby_package_paths")]
pub ruby_package_paths: Vec<String>,
#[serde(alias = "js_package_paths", default = "javascript_package_paths")]
pub javascript_package_paths: Vec<String>,
#[serde(default = "team_file_glob")]
pub team_file_glob: Vec<String>,
#[serde(default = "unowned_globs")]
pub unowned_globs: Vec<String>,
#[serde(alias = "unbuilt_gems_path", default = "vendored_gems_path")]
pub vendored_gems_path: String,
#[serde(default = "default_cache_directory")]
pub cache_directory: String,
#[serde(default = "default_ignore_dirs")]
pub ignore_dirs: Vec<String>,
#[serde(default = "default_executable_name")]
pub executable_name: String,
#[serde(default = "default_codeowners_path")]
pub codeowners_path: String,
}
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct RubyPackageConfig {
#[serde(alias = "pack_paths")]
pub ruby_package_paths: Vec<String>,
}
fn ruby_package_paths() -> Vec<String> {
vec!["packs/**/*".to_owned(), "components/**".to_owned()]
}
fn team_file_glob() -> Vec<String> {
vec!["config/teams/**/*.yml".to_owned()]
}
fn default_cache_directory() -> String {
String::from("tmp/cache/codeowners")
}
fn javascript_package_paths() -> Vec<String> {
vec!["frontend/**/*".to_owned()]
}
fn unowned_globs() -> Vec<String> {
vec![
"frontend/**/node_modules/**/*".to_owned(),
"frontend/**/__generated__/**/*".to_owned(),
]
}
fn vendored_gems_path() -> String {
"vendored/".to_string()
}
fn default_executable_name() -> String {
"codeowners generate".to_string()
}
fn default_ignore_dirs() -> Vec<String> {
vec![
".git".to_owned(),
".idea".to_owned(),
".vscode".to_owned(),
".yarn".to_owned(),
"ar_doc".to_owned(),
"db".to_owned(),
"helm".to_owned(),
"log".to_owned(),
"node_modules".to_owned(),
"sorbet".to_owned(),
"tmp".to_owned(),
]
}
fn default_codeowners_path() -> String {
".github".to_string()
}
impl Config {
pub fn load_from_path(path: &Path) -> std::result::Result<Self, String> {
let file = File::open(path).map_err(|e| format!("Can't open config file: {} ({})", path.to_string_lossy(), e))?;
serde_yaml::from_reader(file).map_err(|e| format!("Can't parse config file: {} ({})", path.to_string_lossy(), e))
}
}
#[cfg(test)]
mod tests {
use std::{
error::Error,
fs::{self, File},
};
use indoc::indoc;
use tempfile::tempdir;
use super::*;
#[test]
fn test_parse_config() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}\"
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(
config.owned_globs,
vec!["{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}"]
);
assert_eq!(config.ruby_package_paths, vec!["packs/**/*", "components/**"]);
assert_eq!(config.javascript_package_paths, vec!["frontend/**/*"]);
assert_eq!(config.team_file_glob, vec!["config/teams/**/*.yml"]);
assert_eq!(
config.unowned_globs,
vec!["frontend/**/node_modules/**/*", "frontend/**/__generated__/**/*"]
);
assert_eq!(config.vendored_gems_path, "vendored/");
assert_eq!(config.executable_name, "codeowners generate");
Ok(())
}
#[test]
fn test_parse_config_with_custom_executable_name() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
executable_name: my-custom-codeowners
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.executable_name, "my-custom-codeowners");
Ok(())
}
#[test]
fn test_executable_name_defaults_when_not_specified() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.executable_name, "codeowners generate");
Ok(())
}
#[test]
fn test_codeowners_path_defaults_when_not_specified() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.codeowners_path, ".github");
Ok(())
}
#[test]
fn test_codeowners_path_can_be_customized() -> Result<(), Box<dyn Error>> {
let temp_dir = tempdir()?;
let config_path = temp_dir.path().join("config.yml");
let config_str = indoc! {"
---
owned_globs:
- \"**/*.rb\"
codeowners_path: docs
"};
fs::write(&config_path, config_str)?;
let config_file = File::open(&config_path)?;
let config: Config = serde_yaml::from_reader(config_file)?;
assert_eq!(config.codeowners_path, "docs");
Ok(())
}
}