dotr-dear 2.0.1

A dotfiles manager as dear as a daughter.
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
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
use std::fs;

use dotr_dear::{
    cli::{
        Cli, Command, DeployArgs, DiffArgs, ImportArgs, PackagesArgs, PackagesCommand,
        RemovePackageArgs, UpdateArgs, run_cli,
    },
    config::Config,
};

mod common;

const PLAYGROUND_DIR: &str = "tests/playground";

struct TestFixture {
    cwd: std::path::PathBuf,
}

impl TestFixture {
    fn new() -> Self {
        Self {
            cwd: std::path::PathBuf::from(PLAYGROUND_DIR),
        }
    }

    fn get_cli(&self, command: Option<Command>) -> Cli {
        Cli {
            command,
            working_dir: Some(PLAYGROUND_DIR.to_string()),
        }
    }

    fn init(&self) {
        run_cli(self.get_cli(Some(Command::Init(dotr_dear::cli::InitArgs {}))))
            .expect("Init failed");
    }

    fn get_config(&self) -> Config {
        Config::from_path(&self.cwd).expect("Failed to load config")
    }

    fn create_test_file(&self, name: &str, content: &str) {
        fs::create_dir_all(self.cwd.join("dotfiles")).expect("Failed to create dotfiles dir");
        fs::write(self.cwd.join(format!("dotfiles/{}", name)), content)
            .expect("Failed to create test file");
    }

    fn assert_file_exists(&self, path: &str) {
        assert!(self.cwd.join(path).exists(), "File should exist: {}", path);
    }

    fn assert_file_not_exists(&self, path: &str) {
        assert!(
            !self.cwd.join(path).exists(),
            "File should not exist: {}",
            path
        );
    }
}

impl Drop for TestFixture {
    fn drop(&mut self) {
        common::teardown(&self.cwd);
    }
}

#[test]
fn test_packages_import_subcommand() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a test file to import
    fs::create_dir_all(fixture.cwd.join("src")).expect("Failed to create src dir");
    fs::write(fixture.cwd.join("src/.test_import"), "test content\n")
        .expect("Failed to create test file");

    // Import using packages import subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Import(ImportArgs {
            path: "src/.test_import".to_string(),
            symlink: false,
            name: None,
            profile: None,
        })),
    }))))
    .expect("Packages import failed");

    // Verify package was imported
    let config = fixture.get_config();
    assert!(
        config.packages.contains_key("f_test_import"),
        "Package should be imported"
    );

    // Verify file was backed up
    fixture.assert_file_exists("dotfiles/f_test_import");
}

#[test]
fn test_packages_deploy_subcommand() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a test package
    fixture.create_test_file("f_deploy_test", "deploy test\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_deploy_test".to_string(),
        src: "dotfiles/f_deploy_test".to_string(),
        dest: "src/.deploy_test".to_string(),
        ..Default::default()
    };
    config.packages.insert("f_deploy_test".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy using packages deploy subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Deploy(DeployArgs {
            packages: Some(vec!["f_deploy_test".to_string()]),
            profile: None,
            ignore_errors: false,
            clean: Some(false),
            dry_run: false,
            ..Default::default()
        })),
    }))))
    .expect("Packages deploy failed");

    // Verify file was deployed
    fixture.assert_file_exists("src/.deploy_test");
}

#[test]
fn test_packages_update_subcommand() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create and deploy a test package
    fixture.create_test_file("f_update_test", "original content\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_update_test".to_string(),
        src: "dotfiles/f_update_test".to_string(),
        dest: "src/.update_test".to_string(),
        ..Default::default()
    };
    config.packages.insert("f_update_test".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy first
    run_cli(fixture.get_cli(Some(Command::Deploy(DeployArgs {
        packages: Some(vec!["f_update_test".to_string()]),
        profile: None,
        ignore_errors: false,
        clean: Some(false),
        dry_run: false,
        ..Default::default()
    }))))
    .expect("Deploy failed");

    // Modify the deployed file
    fs::write(fixture.cwd.join("src/.update_test"), "modified content\n")
        .expect("Failed to modify file");

    // Update using packages update subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Update(UpdateArgs {
            packages: Some(vec!["f_update_test".to_string()]),
            profile: None,
            ignore_errors: false,
            clean: Some(false),
            dry_run: false,
        })),
    }))))
    .expect("Packages update failed");

    // Verify the backup was updated
    let content = fs::read_to_string(fixture.cwd.join("dotfiles/f_update_test"))
        .expect("Failed to read backup");
    assert_eq!(content, "modified content\n");
}

#[test]
fn test_packages_diff_subcommand() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create and deploy a test package
    fixture.create_test_file("f_diff_test", "original content\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_diff_test".to_string(),
        src: "dotfiles/f_diff_test".to_string(),
        dest: "src/.diff_test".to_string(),
        ..Default::default()
    };
    config.packages.insert("f_diff_test".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy first
    run_cli(fixture.get_cli(Some(Command::Deploy(DeployArgs {
        packages: Some(vec!["f_diff_test".to_string()]),
        profile: None,
        ignore_errors: false,
        clean: Some(false),
        dry_run: false,
        ..Default::default()
    }))))
    .expect("Deploy failed");

    // Modify the deployed file
    fs::write(fixture.cwd.join("src/.diff_test"), "modified content\n")
        .expect("Failed to modify file");

    // Diff using packages diff subcommand - should not fail
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Diff(DiffArgs {
            packages: Some(vec!["f_diff_test".to_string()]),
            profile: None,
            ignore_errors: false,
        })),
    }))))
    .expect("Packages diff failed");
}

#[test]
fn test_packages_remove_subcommand() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a test package
    fixture.create_test_file("f_remove_test", "remove test\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_remove_test".to_string(),
        src: "dotfiles/f_remove_test".to_string(),
        dest: "src/.remove_test".to_string(),
        ..Default::default()
    };
    config.packages.insert("f_remove_test".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Remove using packages remove subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Remove(RemovePackageArgs {
            packages: Some(vec!["f_remove_test".to_string()]),
            force: false,
            remove_orphans: false,
            dry_run: false,
            profile: None,
        })),
    }))))
    .expect("Packages remove failed");

    // Verify package was removed from config
    let config = fixture.get_config();
    assert!(
        !config.packages.contains_key("f_remove_test"),
        "Package should be removed from config"
    );

    // Verify source file was removed
    fixture.assert_file_not_exists("dotfiles/f_remove_test");
}

#[test]
fn test_packages_import_with_profile() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a profile
    let mut config = fixture.get_config();
    let profile = dotr_dear::profile::Profile::new("test_profile");
    config.profiles.insert("test_profile".to_string(), profile);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Create a test file to import
    fs::create_dir_all(fixture.cwd.join("src")).expect("Failed to create src dir");
    fs::write(
        fixture.cwd.join("src/.test_profile_import"),
        "profile test\n",
    )
    .expect("Failed to create test file");

    // Import with profile using packages import subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: Some("test_profile".to_string()),
        command: Some(PackagesCommand::Import(ImportArgs {
            path: "src/.test_profile_import".to_string(),
            symlink: false,
            name: None,
            profile: Some("test_profile".to_string()),
        })),
    }))))
    .expect("Packages import with profile failed");

    // Verify package was added to profile
    let config = fixture.get_config();
    let profile = config.profiles.get("test_profile").unwrap();
    assert!(
        profile
            .dependencies
            .contains(&"f_test_profile_import".to_string()),
        "Package should be added to profile dependencies"
    );
}

#[test]
fn test_packages_deploy_with_profile() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a profile
    let mut config = fixture.get_config();
    let mut profile = dotr_dear::profile::Profile::new("deploy_profile");
    profile.dependencies.push("f_profile_deploy".to_string());
    config
        .profiles
        .insert("deploy_profile".to_string(), profile);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Create a test package
    fixture.create_test_file("f_profile_deploy", "profile deploy\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_profile_deploy".to_string(),
        src: "dotfiles/f_profile_deploy".to_string(),
        dest: "src/.profile_deploy".to_string(),
        ..Default::default()
    };
    config
        .packages
        .insert("f_profile_deploy".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy with profile using packages deploy subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: Some("deploy_profile".to_string()),
        command: Some(PackagesCommand::Deploy(DeployArgs {
            packages: None, // Deploy all packages in profile
            profile: Some("deploy_profile".to_string()),
            ignore_errors: false,
            clean: Some(false),
            dry_run: false,
            ..Default::default()
        })),
    }))))
    .expect("Packages deploy with profile failed");

    // Verify file was deployed
    fixture.assert_file_exists("src/.profile_deploy");
}

#[test]
fn test_packages_deploy_dry_run() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create a test package
    fixture.create_test_file("f_dryrun_test", "dry run test\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_dryrun_test".to_string(),
        src: "dotfiles/f_dryrun_test".to_string(),
        dest: "src/.dryrun_test".to_string(),
        ..Default::default()
    };
    config.packages.insert("f_dryrun_test".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy with dry-run using packages deploy subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Deploy(DeployArgs {
            packages: Some(vec!["f_dryrun_test".to_string()]),
            profile: None,
            ignore_errors: false,
            clean: Some(false),
            dry_run: true,
            ..Default::default()
        })),
    }))))
    .expect("Packages deploy dry-run failed");

    // Verify file was NOT actually deployed
    fixture.assert_file_not_exists("src/.dryrun_test");
}

#[test]
fn test_packages_update_dry_run() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create and deploy a test package
    fixture.create_test_file("f_update_dryrun", "original\n");

    let mut config = fixture.get_config();
    let package = dotr_dear::package::Package {
        name: "f_update_dryrun".to_string(),
        src: "dotfiles/f_update_dryrun".to_string(),
        dest: "src/.update_dryrun".to_string(),
        ..Default::default()
    };
    config
        .packages
        .insert("f_update_dryrun".to_string(), package);
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy first
    run_cli(fixture.get_cli(Some(Command::Deploy(DeployArgs {
        packages: Some(vec!["f_update_dryrun".to_string()]),
        profile: None,
        ignore_errors: false,
        clean: Some(false),
        dry_run: false,
        ..Default::default()
    }))))
    .expect("Deploy failed");

    // Modify the deployed file
    fs::write(fixture.cwd.join("src/.update_dryrun"), "modified\n").expect("Failed to modify file");

    // Update with dry-run using packages update subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Update(UpdateArgs {
            packages: Some(vec!["f_update_dryrun".to_string()]),
            profile: None,
            ignore_errors: false,
            clean: Some(false),
            dry_run: true,
        })),
    }))))
    .expect("Packages update dry-run failed");

    // Verify the backup was NOT updated
    let content = fs::read_to_string(fixture.cwd.join("dotfiles/f_update_dryrun"))
        .expect("Failed to read backup");
    assert_eq!(
        content, "original\n",
        "Backup should not be updated in dry-run"
    );
}

#[test]
fn test_packages_multiple_packages_deploy() {
    let fixture = TestFixture::new();
    fixture.init();

    // Create multiple test packages
    fixture.create_test_file("f_multi1", "content1\n");
    fixture.create_test_file("f_multi2", "content2\n");
    fixture.create_test_file("f_multi3", "content3\n");

    let mut config = fixture.get_config();
    for i in 1..=3 {
        let package = dotr_dear::package::Package {
            name: format!("f_multi{}", i),
            src: format!("dotfiles/f_multi{}", i),
            dest: format!("src/.multi{}", i),
            ..Default::default()
        };
        config.packages.insert(format!("f_multi{}", i), package);
    }
    config.save(&fixture.cwd).expect("Failed to save config");

    // Deploy multiple packages using packages deploy subcommand
    run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::Deploy(DeployArgs {
            packages: Some(vec![
                "f_multi1".to_string(),
                "f_multi2".to_string(),
                "f_multi3".to_string(),
            ]),
            profile: None,
            ignore_errors: false,
            clean: Some(false),
            dry_run: false,
            ..Default::default()
        })),
    }))))
    .expect("Packages deploy multiple failed");

    // Verify all files were deployed
    fixture.assert_file_exists("src/.multi1");
    fixture.assert_file_exists("src/.multi2");
    fixture.assert_file_exists("src/.multi3");
}