dotr-dear 0.23.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
use std::{fs, path::PathBuf};

use dotr_dear::{
    cli::{
        Cli, Command, InitArgs, PackagesArgs, PackagesCommand, PackagesListArgs, ProfilesAddArgs,
        ProfilesArgs, ProfilesCommand, ProfilesListArgs, run_cli,
    },
    config::Config,
    package::Package,
    profile::Profile,
};

mod common;

struct TestFixture {
    cwd: PathBuf,
}

impl TestFixture {
    fn new() -> Self {
        let temp_dir = std::env::temp_dir().join(format!("dotr_test_{}", uuid::Uuid::new_v4()));
        fs::create_dir_all(&temp_dir).expect("Failed to create temp dir");
        Self { cwd: temp_dir }
    }

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

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

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

    fn write_file(&self, path: &str, content: &str) {
        let file_path = self.cwd.join(path);
        if let Some(parent) = file_path.parent() {
            fs::create_dir_all(parent).expect("Failed to create parent dir");
        }
        fs::write(file_path, content).expect("Failed to write file");
    }

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

    fn read_file(&self, path: &str) -> String {
        fs::read_to_string(self.cwd.join(path)).expect("Failed to read file")
    }
}

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

// ==========================
// Packages Subcommand Tests
// ==========================

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

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::List(PackagesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
}

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

    // Add some packages to config
    let mut config = fixture.get_config();
    let pkg1 = Package::new("test-pkg1", "dotfiles/pkg1", "dest/pkg1");
    let pkg2 = Package::new("test-pkg2", "dotfiles/pkg2", "dest/pkg2");

    config.packages.insert("test-pkg1".to_string(), pkg1);
    config.packages.insert("test-pkg2".to_string(), pkg2);

    // Add packages to default profile
    config
        .profiles
        .entry("default".to_string())
        .or_insert_with(|| Profile::new("default"))
        .dependencies
        .push("test-pkg1".to_string());
    config
        .profiles
        .get_mut("default")
        .unwrap()
        .dependencies
        .push("test-pkg2".to_string());

    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::List(PackagesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
}

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

    // Add a package with details
    let mut config = fixture.get_config();

    // Create the dependency packages first
    let dep1 = Package::new("dep1", "dotfiles/dep1", "dest/dep1");
    let dep2 = Package::new("dep2", "dotfiles/dep2", "dest/dep2");
    config.packages.insert("dep1".to_string(), dep1);
    config.packages.insert("dep2".to_string(), dep2);

    // Now create the main package with dependencies
    let mut pkg = Package::new("test-pkg", "dotfiles/pkg", "dest/pkg");
    pkg.dependencies = Some(vec!["dep1".to_string(), "dep2".to_string()]);
    pkg.targets
        .insert("target1".to_string(), "dest1".to_string());

    config.packages.insert("test-pkg".to_string(), pkg);
    config
        .profiles
        .entry("default".to_string())
        .or_insert_with(|| Profile::new("default"))
        .dependencies
        .push("test-pkg".to_string());

    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::List(PackagesListArgs { verbose: true })),
    }))));

    if let Err(e) = &result {
        println!("Error: {:?}", e);
    }
    assert!(result.is_ok());
}

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

    // Create packages
    let mut config = fixture.get_config();
    let pkg1 = Package::new("pkg1", "dotfiles/pkg1", "dest/pkg1");
    let pkg2 = Package::new("pkg2", "dotfiles/pkg2", "dest/pkg2");

    config.packages.insert("pkg1".to_string(), pkg1);
    config.packages.insert("pkg2".to_string(), pkg2);

    // Create test profile with only pkg1
    let mut profile = Profile::new("test-profile");
    profile.dependencies.push("pkg1".to_string());
    config.profiles.insert("test-profile".to_string(), profile);

    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: Some("test-profile".to_string()),
        command: Some(PackagesCommand::List(PackagesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
}

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

    // Create package with skip flag
    let mut config = fixture.get_config();
    let mut pkg1 = Package::new("pkg1", "dotfiles/pkg1", "dest/pkg1");
    pkg1.skip = true;
    let pkg2 = Package::new("pkg2", "dotfiles/pkg2", "dest/pkg2");

    config.packages.insert("pkg1".to_string(), pkg1);
    config.packages.insert("pkg2".to_string(), pkg2);

    config
        .profiles
        .entry("default".to_string())
        .or_insert_with(|| Profile::new("default"))
        .dependencies
        .extend(vec!["pkg1".to_string(), "pkg2".to_string()]);

    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: Some(PackagesCommand::List(PackagesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
    // Skipped packages should not appear in the list
}

// ==========================
// Profiles Subcommand Tests
// ==========================

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

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::List(ProfilesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
}

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

    // Add profiles
    let mut config = fixture.get_config();
    let profile1 = Profile::new("profile1");
    let profile2 = Profile::new("profile2");

    config.profiles.insert("profile1".to_string(), profile1);
    config.profiles.insert("profile2".to_string(), profile2);

    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::List(ProfilesListArgs { verbose: false })),
    }))));

    assert!(result.is_ok());
}

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

    // Add profile with details
    let mut config = fixture.get_config();
    let mut profile = Profile::new("test-profile");
    profile.dependencies = vec!["pkg1".to_string(), "pkg2".to_string()];
    profile.variables.insert(
        "VAR1".to_string(),
        toml::Value::String("value1".to_string()),
    );
    profile
        .prompts
        .insert("PROMPT1".to_string(), "Enter value".to_string());

    config.profiles.insert("test-profile".to_string(), profile);
    config.save(&fixture.cwd).expect("Failed to save config");

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::List(ProfilesListArgs { verbose: true })),
    }))));

    assert!(result.is_ok());
}

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

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::Add(ProfilesAddArgs {
            name: "new-profile".to_string(),
            set_as_current: false,
        })),
    }))));

    assert!(result.is_ok());

    // Verify profile was added
    let config = fixture.get_config();
    assert!(
        config.profiles.contains_key("new-profile"),
        "Profile should be added to config"
    );
}

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

    // Add a profile first
    let mut config = fixture.get_config();
    config.profiles.insert(
        "existing-profile".to_string(),
        Profile::new("existing-profile"),
    );
    config.save(&fixture.cwd).expect("Failed to save config");

    // Try to add the same profile again
    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::Add(ProfilesAddArgs {
            name: "existing-profile".to_string(),
            set_as_current: false,
        })),
    }))));

    assert!(result.is_err(), "Adding duplicate profile should fail");
}

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

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::Add(ProfilesAddArgs {
            name: "current-profile".to_string(),
            set_as_current: true,
        })),
    }))));

    assert!(result.is_ok());

    // Verify profile was added
    let config = fixture.get_config();
    assert!(
        config.profiles.contains_key("current-profile"),
        "Profile should be added to config"
    );

    // Verify it was set as current in .uservariables.toml
    fixture.assert_file_exists(".uservariables.toml", "User variables file should exist");
    let uservars_content = fixture.read_file(".uservariables.toml");
    assert!(
        uservars_content.contains("DOTR_PROFILE"),
        "DOTR_PROFILE should be set in user variables"
    );
    assert!(
        uservars_content.contains("current-profile"),
        "Profile name should be in user variables"
    );
}

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

    // Set some existing user variables
    fixture.write_file(".uservariables.toml", r#"EXISTING_VAR = "value""#);

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs {
        command: Some(ProfilesCommand::Add(ProfilesAddArgs {
            name: "new-profile".to_string(),
            set_as_current: true,
        })),
    }))));

    assert!(result.is_ok());

    // Verify existing variable is preserved
    let uservars_content = fixture.read_file(".uservariables.toml");
    assert!(
        uservars_content.contains("EXISTING_VAR"),
        "Existing user variable should be preserved"
    );
    assert!(
        uservars_content.contains("DOTR_PROFILE"),
        "DOTR_PROFILE should be added"
    );
}

// ==========================
// No Command Tests
// ==========================

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

    let result = run_cli(fixture.get_cli(Some(Command::Packages(PackagesArgs {
        profile: None,
        command: None,
    }))));

    assert!(
        result.is_ok(),
        "Should handle missing subcommand gracefully"
    );
}

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

    let result = run_cli(fixture.get_cli(Some(Command::Profiles(ProfilesArgs { command: None }))));

    assert!(
        result.is_ok(),
        "Should handle missing subcommand gracefully"
    );
}