uvm_gc 0.2.0

Garbage collector for Unity Version Manager
Documentation
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
use log::{info, trace, warn};
use std::{env, io};
use std::{
    fs,
    path::{Path, PathBuf},
    time::{Duration, SystemTime},
};

pub const DEFAULT_MAX_AGE_ENV: &'static str = "UVM_GC_MAX_AGE";
pub const DEFAULT_MAX_AGE: Duration = Duration::from_secs(2_630_016);
pub const DEFAULT_MAX_AGE_HUMAN: &'static str = "1month";

pub const DEFAULT_ENABLED_ENV: &'static str = "UVM_GC_ENABLED";
pub const DEFAULT_ENABLED: bool = true;

pub fn gc_enabled() -> bool {
    env::var(DEFAULT_ENABLED_ENV).ok()
    .and_then(|v| v.parse().ok())
    .unwrap_or_else(|| {
        trace!("No GC enabled value found in environment variable: {}. Using default GC enabled: {}", DEFAULT_ENABLED_ENV, DEFAULT_ENABLED);
        DEFAULT_ENABLED
    })
}

fn parse_max_age_from_string(max_age_human_value: &str) -> Duration {
    trace!("Use max age value: {}", max_age_human_value);

    match humantime::parse_duration(max_age_human_value) {
        Ok(duration) => duration,
        Err(_) => {
            warn!("Invalid GC max age value: {}", max_age_human_value);
            warn!("Using default GCmax age: {}", DEFAULT_MAX_AGE_HUMAN);
            DEFAULT_MAX_AGE
        }
    }
}

pub fn default_max_age() -> Duration {
    // Keep behavior: env overrides; otherwise use the compile-time default
    let max_age_human_value = env::var(DEFAULT_MAX_AGE_ENV).unwrap_or_else(|_| {
        trace!(
            "No max age value found in environment variable: {}. Using default max age: {}",
            DEFAULT_MAX_AGE_ENV, DEFAULT_MAX_AGE_HUMAN
        );
        DEFAULT_MAX_AGE_HUMAN.to_owned()
    });

    parse_max_age_from_string(&max_age_human_value)
}
/// Garbage collector for Unity Version Manager
///
/// This collector is used to clean up old files in the cache directory.
/// The collector will delete files older than the specified max age.
/// The collector will run in dry run mode by default.

pub struct GarbageCollector {
    dry_run: bool,
    max_age: Duration,
    base_dir: PathBuf,
    version: Option<String>,
}

impl GarbageCollector {
    /// Create a new GarbageCollector
    ///
    /// # Arguments
    ///
    /// * `base_dir` - The base directory to clean up
    ///
    /// # Returns
    ///
    /// A new GarbageCollector
    pub fn new<P: AsRef<Path>>(base_dir: P) -> Self {
        Self {
            dry_run: true,
            max_age: default_max_age(),
            base_dir: base_dir.as_ref().to_path_buf(),
            version: None,
        }
    }
    /// Set the dry run mode
    ///
    /// # Arguments
    ///
    /// * `dry_run` - The dry run mode
    pub fn with_dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    /// Set the maximum age of files to delete
    pub fn with_max_age(mut self, max_age: Duration) -> Self {
        self.max_age = max_age;
        self
    }

    pub fn with_version(mut self, version: String) -> Self {
        self.version = Some(version);
        self
    }

    /// Collect the garbage
    pub fn collect(&self) -> io::Result<()> {
        let prefix = if self.dry_run { "[DRY RUN] " } else { "" };
        info!(
            "{}Cleaning up files older than {} in {}",
            prefix,
            humantime::format_duration(self.max_age),
            self.base_dir.display()
        );
        for (entry, accessed_elapsed) in walkdir::WalkDir::new(&self.base_dir)
            .into_iter()
            .filter_map(Result::ok)
            .filter(|entry| entry.path().is_file())
            .filter(|entry| {
                if let Some(version) = &self.version {
                    return entry.path().display().to_string().contains(version);
                }
                true
            })
            .filter_map(|entry| fs::metadata(entry.path()).ok().map(|m| (entry, m)))
            .filter_map(|(entry, metadata)| {
                let accessed = metadata.accessed().unwrap_or_else(|_| SystemTime::now());
                let accessed_elapsed = accessed.elapsed().unwrap_or_default();
                if accessed_elapsed > self.max_age {
                    Some((entry, accessed_elapsed))
                } else {
                    trace!(
                        "{}Skipping file: {} ({} old)",
                        prefix,
                        entry.path().display(),
                        humantime::format_duration(accessed_elapsed)
                    );
                    None
                }
            })
        {
            info!(
                "{}Deleting file: {} ({} old)",
                prefix,
                entry.path().display(),
                humantime::format_duration(accessed_elapsed)
            );
            if !self.dry_run {
                fs::remove_file(entry.path())?;
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use tempfile::tempdir;

    #[test]
    fn test_garbage_collector_respects_large_max_age() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("recent_file.txt");
        File::create(&file1).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(365 * 24 * 60 * 60));

        let result = gc.collect();
        assert!(result.is_ok());

        assert!(file1.exists());
    }

    #[test]
    fn test_garbage_collector_respects_zero_max_age() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("any_file.txt");
        File::create(&file1).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(0));

        let result = gc.collect();
        assert!(result.is_ok());

        assert!(!file1.exists());
    }

    #[test]
    fn test_garbage_collector_dry_run_preserves_files() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("test1.txt");
        let file2 = temp_path.join("test2.txt");
        File::create(&file1).expect("Failed to create test file");
        File::create(&file2).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(true)
            .with_max_age(Duration::from_secs(0));

        let result = gc.collect();
        assert!(result.is_ok());

        assert!(file1.exists());
        assert!(file2.exists());
    }

    #[test]
    fn test_garbage_collector_execute_deletes_files() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("test1.txt");
        let file2 = temp_path.join("test2.txt");
        File::create(&file1).expect("Failed to create test file");
        File::create(&file2).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(0));

        let result = gc.collect();
        assert!(result.is_ok());

        assert!(!file1.exists());
        assert!(!file2.exists());
    }

    #[test]
    fn default_human_matches_const() {
        let parsed = humantime::parse_duration(DEFAULT_MAX_AGE_HUMAN).unwrap();
        assert_eq!(parsed, DEFAULT_MAX_AGE);
    }

    #[test]
    fn test_parse_max_age_with_valid_durations() {
        let test_cases = vec![
            ("30days", Duration::from_secs(30 * 24 * 60 * 60)),
            ("2weeks", Duration::from_secs(2 * 7 * 24 * 60 * 60)),
            ("1hour", Duration::from_secs(60 * 60)),
            ("45min", Duration::from_secs(45 * 60)),
            ("3600s", Duration::from_secs(3600)),
            ("1month", Duration::from_secs(2_630_016)), // Default value
        ];

        for (input, expected) in test_cases {
            let result = parse_max_age_from_string(input);
            assert_eq!(result, expected, "Failed for input: {}", input);
        }
    }

    #[test]
    fn test_parse_max_age_with_invalid_durations() {
        let invalid_values = vec![
            "completely_invalid",
            "not_a_duration",
            "",
            "xyz123",
            "1.5.3seconds",
        ];

        for invalid_value in invalid_values {
            let result = parse_max_age_from_string(invalid_value);
            assert_eq!(
                result, DEFAULT_MAX_AGE,
                "Failed for invalid input: {}",
                invalid_value
            );
        }
    }

    #[test]
    fn test_parse_max_age_with_whitespace() {
        let whitespace_values = vec![
            ("  ", DEFAULT_MAX_AGE),                  // Invalid, should return default
            ("\t", DEFAULT_MAX_AGE),                  // Invalid, should return default
            ("\n", DEFAULT_MAX_AGE),                  // Invalid, should return default
            ("  1hour  ", Duration::from_secs(3600)), // Valid with trimming
        ];

        for (input, expected) in whitespace_values {
            let result = parse_max_age_from_string(input);
            assert_eq!(result, expected, "Failed for input: {:?}", input);
        }
    }

    #[test]
    fn test_parse_max_age_edge_cases() {
        let edge_cases = vec![
            ("0s", Duration::from_secs(0)),
            ("1ns", Duration::from_nanos(1)),
            ("999999999ns", Duration::from_nanos(999999999)),
        ];

        for (input, expected) in edge_cases {
            let result = parse_max_age_from_string(input);
            assert_eq!(result, expected, "Failed for edge case: {}", input);
        }
    }

    #[test]
    fn test_default_max_age_uses_default_when_no_env() {
        // This test assumes the env var is not set in the test environment
        // If it is set, this test might fail, but that's expected behavior
        let result = default_max_age();
        // We can't easily test the env var behavior without setting it,
        // but we can at least verify the function returns a valid duration
        assert!(result.as_secs() > 0);
    }

    #[test]
    fn test_garbage_collector_with_version_filter_matches() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        // Create files with different version strings in their names
        let matching_file = temp_path.join("unity-2023.1.1f1-install.log");
        let non_matching_file = temp_path.join("unity-2022.3.5f1-install.log");
        let another_matching = temp_path.join("2023.1.1f1-build-cache.dat");
        
        File::create(&matching_file).expect("Failed to create test file");
        File::create(&non_matching_file).expect("Failed to create test file");
        File::create(&another_matching).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(0))
            .with_version("2023.1.1f1".to_string());

        let result = gc.collect();
        assert!(result.is_ok());

        // Files containing the version should be deleted
        assert!(!matching_file.exists());
        assert!(!another_matching.exists());
        // Files not containing the version should be preserved
        assert!(non_matching_file.exists());
    }

    #[test]
    fn test_garbage_collector_with_version_filter_dry_run() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let matching_file = temp_path.join("unity-2023.1.1f1-install.log");
        let non_matching_file = temp_path.join("unity-2022.3.5f1-install.log");
        
        File::create(&matching_file).expect("Failed to create test file");
        File::create(&non_matching_file).expect("Failed to create test file");

        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(true)
            .with_max_age(Duration::from_secs(0))
            .with_version("2023.1.1f1".to_string());

        let result = gc.collect();
        assert!(result.is_ok());

        // All files should be preserved in dry run mode
        assert!(matching_file.exists());
        assert!(non_matching_file.exists());
    }

    #[test]
    fn test_garbage_collector_without_version_processes_all_files() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("unity-2023.1.1f1-install.log");
        let file2 = temp_path.join("unity-2022.3.5f1-install.log");
        let file3 = temp_path.join("some-other-file.txt");
        
        File::create(&file1).expect("Failed to create test file");
        File::create(&file2).expect("Failed to create test file");
        File::create(&file3).expect("Failed to create test file");

        // Create GC without version filter (version is None by default)
        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(0));

        let result = gc.collect();
        assert!(result.is_ok());

        // All files should be deleted when no version filter is applied
        assert!(!file1.exists());
        assert!(!file2.exists());
        assert!(!file3.exists());
    }

    #[test]
    fn test_garbage_collector_version_filter_respects_max_age() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let matching_file = temp_path.join("unity-2023.1.1f1-install.log");
        let non_matching_file = temp_path.join("unity-2022.3.5f1-install.log");
        
        File::create(&matching_file).expect("Failed to create test file");
        File::create(&non_matching_file).expect("Failed to create test file");

        // Use a large max age so files won't be deleted due to age
        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(365 * 24 * 60 * 60))
            .with_version("2023.1.1f1".to_string());

        let result = gc.collect();
        assert!(result.is_ok());

        // Both files should still exist because they're not old enough
        assert!(matching_file.exists());
        assert!(non_matching_file.exists());
    }

    #[test]
    fn test_garbage_collector_version_filter_empty_string() {
        let temp_dir = tempdir().expect("Failed to create temp directory");
        let temp_path = temp_dir.path();

        let file1 = temp_path.join("unity-2023.1.1f1-install.log");
        let file2 = temp_path.join("some-other-file.txt");
        
        File::create(&file1).expect("Failed to create test file");
        File::create(&file2).expect("Failed to create test file");

        // Test with empty string version filter
        let gc = GarbageCollector::new(&temp_path)
            .with_dry_run(false)
            .with_max_age(Duration::from_secs(0))
            .with_version("".to_string());

        let result = gc.collect();
        assert!(result.is_ok());

        // All files should be processed since empty string matches everything
        assert!(!file1.exists());
        assert!(!file2.exists());
    }
}