-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.rs
More file actions
583 lines (470 loc) · 18.5 KB
/
loader.rs
File metadata and controls
583 lines (470 loc) · 18.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Configuration file loading and saving
use super::Repository;
use crate::utils::filters;
use crate::utils::validators;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recipe {
pub name: String,
pub steps: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub repositories: Vec<Repository>,
#[serde(default)]
pub recipes: Vec<Recipe>,
}
impl Config {
/// Load configuration from a file
pub fn load(path: &str) -> Result<Self> {
let content = std::fs::read_to_string(path)?;
let mut config: Config = serde_yaml::from_str(&content)?;
// Set the config directory for each repository
let config_path = Path::new(path);
let config_dir = config_path.parent().map(|p| p.to_path_buf());
for repo in &mut config.repositories {
repo.set_config_dir(config_dir.clone());
}
// Validate the loaded configuration
validators::validate_repositories(&config.repositories)
.map_err(validators::validation_errors_to_anyhow)?;
Ok(config)
}
/// Save configuration to a file
pub fn save(&self, path: &str) -> Result<()> {
let yaml = serde_yaml::to_string(self)?;
// Fix indentation for yamllint compliance
// yamllint expects array items to be indented relative to their parent
let fixed_yaml = Self::fix_yaml_indentation(&yaml);
// Make yamllint compliant by adding document separator and ensuring newline at end
let yaml_content = format!("---\n{}\n", fixed_yaml);
std::fs::write(path, yaml_content)?;
Ok(())
}
/// Fix YAML indentation to be yamllint compliant
fn fix_yaml_indentation(yaml: &str) -> String {
let lines: Vec<&str> = yaml.lines().collect();
let mut result = Vec::new();
for line in lines {
// If line starts with a dash (array item), indent it by 2 spaces
if line.starts_with("- ") {
result.push(format!(" {}", line));
} else if line.starts_with(" ") && !line.starts_with(" ") {
// If line is already indented by 2 spaces but not 4, make it 4 spaces
// This handles the properties of array items
result.push(format!(" {}", line));
} else {
result.push(line.to_string());
}
}
result.join("\n")
}
/// Filter repositories by specific names
pub fn filter_by_names(&self, names: &[String]) -> Vec<Repository> {
filters::filter_by_names(&self.repositories, names)
}
/// Filter repositories by tag
pub fn filter_by_tag(&self, tag: Option<&str>) -> Vec<Repository> {
filters::filter_by_tag(&self.repositories, tag)
}
/// Filter repositories by multiple tags (OR logic)
pub fn filter_by_any_tag(&self, tags: &[String]) -> Vec<Repository> {
filters::filter_by_any_tag(&self.repositories, tags)
}
/// Filter repositories by multiple tags (AND logic)
pub fn filter_by_all_tags(&self, tags: &[String]) -> Vec<Repository> {
filters::filter_by_all_tags(&self.repositories, tags)
}
/// Get repository by name
pub fn get_repository(&self, name: &str) -> Option<&Repository> {
self.repositories.iter().find(|repo| repo.name == name)
}
/// Get mutable repository by name
pub fn get_repository_mut(&mut self, name: &str) -> Option<&mut Repository> {
self.repositories.iter_mut().find(|repo| repo.name == name)
}
/// Add a repository to the configuration
pub fn add_repository(&mut self, repo: Repository) -> Result<()> {
// Check for duplicate names
if self.get_repository(&repo.name).is_some() {
return Err(anyhow::anyhow!("Repository '{}' already exists", repo.name));
}
// Validate the repository
repo.validate()?;
self.repositories.push(repo);
Ok(())
}
/// Remove a repository from the configuration
pub fn remove_repository(&mut self, name: &str) -> bool {
let initial_len = self.repositories.len();
self.repositories.retain(|repo| repo.name != name);
self.repositories.len() != initial_len
}
/// Get all unique tags across all repositories
pub fn get_all_tags(&self) -> Vec<String> {
let mut tags: Vec<String> = self
.repositories
.iter()
.flat_map(|repo| repo.tags.iter())
.cloned()
.collect();
tags.sort();
tags.dedup();
tags
}
/// Validate the entire configuration
pub fn validate(&self) -> Result<()> {
validators::validate_repositories(&self.repositories)
.map_err(validators::validation_errors_to_anyhow)
}
/// Create a new empty configuration
pub fn new() -> Self {
Self {
repositories: Vec::new(),
recipes: Vec::new(),
}
}
/// Find a recipe by name
pub fn find_recipe(&self, name: &str) -> Option<&Recipe> {
self.recipes.iter().find(|r| r.name == name)
}
/// Alias for load method for backwards compatibility
pub fn load_config(path: &str) -> Result<Self> {
Self::load(path)
}
/// Filter repositories by tag (alias for backwards compatibility)
pub fn filter_repositories_by_tag(&self, tag: Option<&str>) -> Vec<Repository> {
self.filter_by_tag(tag)
}
/// Filter repositories by context (combining tag inclusion, exclusion, and names filters)
pub fn filter_repositories(
&self,
include_tags: &[String],
exclude_tags: &[String],
repos: Option<&[String]>,
) -> Vec<Repository> {
filters::filter_repositories(&self.repositories, include_tags, exclude_tags, repos)
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_config() -> Config {
let mut repo1 = Repository::new(
"repo1".to_string(),
"git@github.com:owner/repo1.git".to_string(),
);
repo1.add_tag("frontend".to_string());
repo1.add_tag("web".to_string());
let mut repo2 = Repository::new(
"repo2".to_string(),
"git@github.com:owner/repo2.git".to_string(),
);
repo2.add_tag("backend".to_string());
repo2.add_tag("api".to_string());
Config {
repositories: vec![repo1, repo2],
recipes: Vec::new(),
}
}
#[test]
fn test_filter_by_tag() {
let config = create_test_config();
let frontend_repos = config.filter_by_tag(Some("frontend"));
assert_eq!(frontend_repos.len(), 1);
assert_eq!(frontend_repos[0].name, "repo1");
let all_repos = config.filter_by_tag(None);
assert_eq!(all_repos.len(), 2);
}
#[test]
fn test_filter_by_any_tag() {
let config = create_test_config();
let web_repos = config.filter_by_any_tag(&["frontend".to_string(), "api".to_string()]);
assert_eq!(web_repos.len(), 2); // Both repos match
let no_match = config.filter_by_any_tag(&["mobile".to_string()]);
assert_eq!(no_match.len(), 0);
}
#[test]
fn test_get_all_tags() {
let config = create_test_config();
let tags = config.get_all_tags();
assert_eq!(tags, vec!["api", "backend", "frontend", "web"]);
}
#[test]
fn test_filter_by_names() {
let config = create_test_config();
let specific_repos = config.filter_by_names(&["repo1".to_string()]);
assert_eq!(specific_repos.len(), 1);
assert_eq!(specific_repos[0].name, "repo1");
let multiple_repos = config.filter_by_names(&["repo1".to_string(), "repo2".to_string()]);
assert_eq!(multiple_repos.len(), 2);
let no_match = config.filter_by_names(&["nonexistent".to_string()]);
assert_eq!(no_match.len(), 0);
let empty_filter = config.filter_by_names(&[]);
assert_eq!(empty_filter.len(), 2); // Should return all repos
}
#[test]
fn test_filter_repositories_combined() {
let config = create_test_config();
// Test with both tag and repo names
let filtered = config.filter_repositories(
&["frontend".to_string()],
&[],
Some(&["repo1".to_string()]),
);
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo1");
// Test with tag and repo names that don't match
let filtered =
config.filter_repositories(&["backend".to_string()], &[], Some(&["repo1".to_string()]));
assert_eq!(filtered.len(), 0); // repo1 doesn't have backend tag
// Test with only repo names
let filtered = config.filter_repositories(&[], &[], Some(&["repo1".to_string()]));
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo1");
// Test with only tag
let filtered = config.filter_repositories(&["frontend".to_string()], &[], None);
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo1");
// Test with neither (should return all)
let filtered = config.filter_repositories(&[], &[], None);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_add_remove_repository() {
let mut config = Config::new();
let repo = Repository::new(
"test".to_string(),
"git@github.com:owner/test.git".to_string(),
);
config.add_repository(repo).unwrap();
assert_eq!(config.repositories.len(), 1);
let removed = config.remove_repository("test");
assert!(removed);
assert_eq!(config.repositories.len(), 0);
let not_removed = config.remove_repository("nonexistent");
assert!(!not_removed);
}
#[test]
fn test_filter_by_empty_names_list() {
let config = create_test_config();
// Empty names list should return all repositories
let filtered = config.filter_by_names(&[]);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_filter_by_nonexistent_names() {
let config = create_test_config();
// Non-existent names should return empty list
let filtered =
config.filter_by_names(&["nonexistent1".to_string(), "nonexistent2".to_string()]);
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_by_partial_match_names() {
let config = create_test_config();
// Mix of existing and non-existing names
let filtered = config.filter_by_names(&["repo1".to_string(), "nonexistent".to_string()]);
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo1");
}
#[test]
fn test_filter_by_nonexistent_tag() {
let config = create_test_config();
// Non-existent tag should return empty list
let filtered = config.filter_by_tag(Some("nonexistent"));
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_by_case_sensitive_tag() {
let config = create_test_config();
// Tag filtering should be case sensitive
let filtered = config.filter_by_tag(Some("BACKEND"));
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_repositories_with_nonexistent_tag_and_names() {
let config = create_test_config();
// Non-existent tag with valid names should return empty
let filtered = config.filter_repositories(
&["nonexistent".to_string()],
&[],
Some(&["repo1".to_string()]),
);
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_repositories_with_valid_tag_and_nonexistent_names() {
let config = create_test_config();
// Valid tag with non-existent names should return empty
let filtered = config.filter_repositories(
&["backend".to_string()],
&[],
Some(&["nonexistent".to_string()]),
);
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_by_any_tag_with_empty_list() {
let config = create_test_config();
// Empty tag list should return all repositories
let filtered = config.filter_by_any_tag(&[]);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_filter_by_all_tags_with_empty_list() {
let config = create_test_config();
// Empty tag list should return all repositories
let filtered = config.filter_by_all_tags(&[]);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_filter_by_any_tag_with_nonexistent_tags() {
let config = create_test_config();
// Non-existent tags should return empty
let filtered =
config.filter_by_any_tag(&["nonexistent1".to_string(), "nonexistent2".to_string()]);
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_filter_by_all_tags_impossible_combination() {
let config = create_test_config();
// Tags that can't all exist on same repo should return empty
let filtered = config.filter_by_all_tags(&["backend".to_string(), "frontend".to_string()]);
assert_eq!(filtered.len(), 0);
}
#[test]
fn test_get_repository_case_sensitive() {
let config = create_test_config();
// Repository lookup should be case sensitive
let repo = config.get_repository("REPO1");
assert!(repo.is_none());
let repo = config.get_repository("repo1");
assert!(repo.is_some());
}
#[test]
fn test_add_repository_duplicate_name() {
let mut config = create_test_config();
let duplicate_repo = Repository::new(
"repo1".to_string(), // Same name as existing
"git@github.com:other/repo.git".to_string(),
);
let result = config.add_repository(duplicate_repo);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("already exists"));
}
#[test]
fn test_remove_repository_case_sensitive() {
let mut config = create_test_config();
// Should not remove with wrong case
let removed = config.remove_repository("REPO1");
assert!(!removed);
assert_eq!(config.repositories.len(), 2);
// Should remove with correct case
let removed = config.remove_repository("repo1");
assert!(removed);
assert_eq!(config.repositories.len(), 1);
}
#[test]
fn test_fix_yaml_indentation() {
// Test basic array item indentation
let yaml = "repositories:\n- name: test\n url: test.git";
let fixed = Config::fix_yaml_indentation(yaml);
assert!(fixed.contains(" - name: test"));
assert!(fixed.contains(" url: test.git"));
// Test already correctly indented yaml
let yaml = "repositories:\n - name: test\n url: test.git";
let fixed = Config::fix_yaml_indentation(yaml);
assert!(fixed.contains(" - name: test"));
assert!(fixed.contains(" url: test.git"));
// Test lines with different indentation levels
let yaml = "key: value\narray:\n- item1\n subkey: subvalue";
let fixed = Config::fix_yaml_indentation(yaml);
assert!(fixed.contains("key: value"));
assert!(fixed.contains(" - item1"));
assert!(fixed.contains(" subkey: subvalue"));
}
#[test]
fn test_find_recipe() {
let mut config = Config::new();
let recipe = Recipe {
name: "test-recipe".to_string(),
steps: vec!["echo hello".to_string()],
};
config.recipes.push(recipe);
let found = config.find_recipe("test-recipe");
assert!(found.is_some());
assert_eq!(found.unwrap().name, "test-recipe");
let not_found = config.find_recipe("nonexistent");
assert!(not_found.is_none());
}
#[test]
fn test_config_new_default() {
let config1 = Config::new();
let config2 = Config::default();
assert_eq!(config1.repositories.len(), config2.repositories.len());
assert_eq!(config1.recipes.len(), config2.recipes.len());
assert!(config1.repositories.is_empty());
assert!(config1.recipes.is_empty());
}
#[test]
fn test_config_validate_empty() {
let config = Config::new();
assert!(config.validate().is_ok());
}
#[test]
fn test_load_config_alias() {
// Test that load_config is an alias for load
// We can't test actual file loading without creating temp files,
// but we can test that the method exists and has the same signature
let result1 = Config::load("nonexistent.yaml");
let result2 = Config::load_config("nonexistent.yaml");
// Both should fail in the same way (file not found)
assert!(result1.is_err());
assert!(result2.is_err());
// The error types should be similar (both IO errors for missing file)
assert_eq!(
result1.unwrap_err().to_string().contains("No such file"),
result2.unwrap_err().to_string().contains("No such file")
);
}
#[test]
fn test_filter_repositories_by_tag_alias() {
let config = create_test_config();
let result1 = config.filter_by_tag(Some("frontend"));
let result2 = config.filter_repositories_by_tag(Some("frontend"));
assert_eq!(result1.len(), result2.len());
assert_eq!(result1[0].name, result2[0].name);
}
#[test]
fn test_filter_repositories_exclude_tags() {
let config = create_test_config();
// Test excluding tags
let filtered = config.filter_repositories(
&[], // no include filter
&["frontend".to_string()], // exclude frontend
None,
);
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo2"); // Only repo2 should remain
// Test excluding all repos
let filtered =
config.filter_repositories(&[], &["frontend".to_string(), "backend".to_string()], None);
assert_eq!(filtered.len(), 0);
// Test include and exclude together
let filtered = config.filter_repositories(
&["backend".to_string(), "api".to_string()], // include backend AND api (only repo2 has both)
&["frontend".to_string()], // but exclude frontend
None,
);
assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].name, "repo2"); // repo2 has backend AND api, not frontend
}
}