gitcraft 0.1.123

A template project for GitHub-related utilities.
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
use assert_cmd::Command as AssertCommand;
use predicates::prelude::*;
use std::fs;

// Import utility functions
use crate::common::test_utils::{
    assert_file_contains, assert_file_exists, create_git_repo, setup_test_env,
};

/**
 Integration tests for the `gitcraft` gitignore subcommand.

 This test suite covers the following scenarios:

- `test_gitignore_add_rust`: Verifies that adding a Rust gitignore creates the correct file with expected content.
- `test_gitignore_add_rust.gitignore`: Ensures that adding a Rust gitignore with a .gitignore extension creates the correct file with expected content.
- `test_gitignore_add_multiple`: Validates that multiple gitignore templates can be added and merged.
- `test_gitignore_add_with_dir`: Ensures that a gitignore can be added to a specified directory.
- `test_gitignore_add_force_overwrite`: Tests that an existing .gitignore file is not overwritten unless the `--force` flag is used.
- `test_gitignore_add_all`: Tests the addition of all available gitignore templates.
- `test_gitignore_add_invalid_template`: Confirms that an unknown template returns an appropriate error.
- `test_gitignore_add_no_template`: Ensures that running add without templates or --all returns an error.
- `test_gitignore_add_update_cache`: Ensures that the add command with --update-cache refreshes the cache.
- `test_gitignore_add_valid_and_invalid_template`: Tests adding a valid template alongside an invalid one, ensuring the valid template is added while the invalid one is reported.
- `test_gitignore_list_default`: Ensures the list command displays popular templates.
- `test_gitignore_list_popular`: Ensures the list command with --popular displays popular templates.
- `test_gitignore_list_global`: Ensures the list command with --global displays global templates.
- `test_gitignore_list_community`: Ensures the list command with --community displays community templates.
- `test_gitignore_list_update_cache`: Ensures the list command with --update-cache refreshes the cache.
- `test_gitignore_preview_single_template`: Tests previewing a single gitignore template.
- `test_gitignore_preview_rust.gitignore`: Tests previewing a gitignore template with a .gitignore extension.
- `test_gitignore_preview_multiple_templates`: Tests previewing multiple gitignore templates.
- `test_gitignore_preview_update_cache`: Tests previewing a template with cache update.
- `test_gitignore_preview_invalid_template`: Ensures that previewing an invalid template returns an error.
- `test_gitignore_preview_no_template`: Ensures that running preview without templates returns an
- `test_gitignore_help_command`: Validates that the help command displays usage information for the gitignore subcommands.

Each test uses a temporary directory to avoid side effects and leverages `assert_cmd` and `predicates` for command-line assertions.
*/

// --------     ADD COMMAND TESTS     --------

#[test]
fn test_gitignore_add_rust() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );

    assert_file_exists(&temp_path.join(".gitignore"));
    assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}

#[test]
fn test_gitignore_add_rust_gitignore() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust.gitignore"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );

    assert_file_exists(&temp_path.join(".gitignore"));
    assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}

#[test]
fn test_gitignore_add_multiple() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust", "python"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );

    let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
    assert!(content.contains("Rust"));
    assert!(content.contains("Python"));
}

#[test]
fn test_gitignore_add_with_dir() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();
    let target_dir = temp_path.join("custom_dir");
    fs::create_dir_all(&target_dir).unwrap();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.args(&[
        "add",
        "gitignore",
        "rust",
        "--dir",
        target_dir.to_str().unwrap(),
    ])
    .assert()
    .success()
    .stdout(
        predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
    );

    assert_file_exists(&target_dir.join(".gitignore"));
    assert_file_contains(&target_dir.join(".gitignore"), "Rust");
}

#[test]
fn test_gitignore_add_append() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    // First add Rust template
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
cmd.args(&["add", "gitignore", "rust"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );

    assert_file_exists(&temp_path.join(".gitignore"));
    assert_file_contains(&temp_path.join(".gitignore"), "Rust");

    // Then append Python template
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "python", "--append"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates: rust")
                .or(predicate::str::contains("")),
        );

    let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
    assert!(content.contains("Rust"));
    assert!(content.contains("Python"));
}

#[test]
fn test_gitignore_add_force_overwrite() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    // Create initial .gitignore file
    fs::write(temp_path.join(".gitignore"), "existing content").unwrap();

    // Try to add without force (should fail)
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("already exists"));

    // Try with force flag (should succeed)
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust", "--force"])
        .assert()
        .success();

    assert_file_contains(&temp_path.join(".gitignore"), "Rust");
    let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
    assert!(!content.contains("existing content"));
}

#[test]
#[ignore] // This test takes a long time to run as it downloads all templates
fn test_gitignore_add_all() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "--all"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Downloaded and merged all gitignore templates")
                .or(predicate::str::contains("")),
        );

    assert_file_exists(&temp_path.join(".gitignore"));
    let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
    assert!(content.contains(".gitignore"));
}

#[test]
fn test_gitignore_add_invalid_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "not-a-template"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}

#[test]
fn test_gitignore_add_no_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "not-a-template"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}

#[test]
fn test_gitignore_add_update_cache() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust", "--update-cache"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );
}

#[test]
fn test_gitignore_add_valid_and_invalid_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    // Try to add one valid and one invalid template
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust", "not-a-template"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        )
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));

    // The .gitignore file should be created and contain the valid template's content
    assert_file_exists(&temp_path.join(".gitignore"));
    assert_file_contains(&temp_path.join(".gitignore"), "Rust");
    let content = fs::read_to_string(temp_path.join(".gitignore")).unwrap();
    assert!(!content.contains("not-a-template"));
}

#[test]
fn test_gitignore_add_default_with_output() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    // Add rust template with output file .gitignore
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "rust", "-o", ".gitignore"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Added gitignore templates").or(predicate::str::contains("")),
        );

    assert_file_exists(&temp_path.join(".gitignore"));
    assert_file_contains(&temp_path.join(".gitignore"), "Rust");
}

#[test]
fn test_gitignore_add_multiple_templates_uneven_output_files() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    create_git_repo(&temp_path);

    // Pass 3 output files for 2 templates (should error)
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&[
        "add",
        "gitignore",
        "python",
        "rust",
        "-o",
        "Python.gitignore",
        "Rust.gitignore",
        "Ada.gitignore",
    ])
    .assert()
    .failure()
    .stderr(predicate::str::contains(
        "Number of output files must be either 1 or match the number of templates when not using --use-remote-name",
    ));
}

// --------     LIST COMMAND TESTS     --------

#[test]
fn test_gitignore_list_default() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignores"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Available gitignore templates"))
        .stdout(predicate::str::contains("POPULAR"))
        .stdout(predicate::str::contains("GLOBAL"))
        .stdout(predicate::str::contains("COMMUNITY"));
}

#[test]
fn test_gitignore_list_popular() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignores", "--popular"])
        .assert()
        .success()
        .stdout(predicate::str::contains("POPULAR"));
}

#[test]
fn test_gitignore_list_global() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignores", "--global"])
        .assert()
        .success()
        .stdout(predicate::str::contains("global"));
}

#[test]
fn test_gitignore_list_community() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignores", "--community"])
        .assert()
        .success()
        .stdout(predicate::str::contains("community"));
}

#[test]
fn test_gitignore_list_update_cache() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignores", "--update-cache"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Available gitignore templates"));
}

// --------     PREVIEW COMMAND TESTS     --------

#[test]
fn test_gitignore_preview_single_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "rust"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Rust"));
}

#[test]
fn test_gitignore_preview_single_template_update_cache() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "rust", "--update-cache"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Rust"));
}

#[test]
fn test_gitignore_preview_multiple_templates() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "rust", "python"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Rust"))
        .stdout(predicate::str::contains("Python"));
}

#[test]
fn test_gitignore_preview_update_cache() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "rust", "--update-cache"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Rust"));
}

#[test]
fn test_gitignore_preview_invalid_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "not-a-template"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Unknown")));
}

#[test]
fn test_gitignore_preview_no_template() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No gitignore template specified"));
}
// --------     HELP COMMAND TEST     --------

#[test]
fn test_gitignore_help_command() {
    let temp_dir = setup_test_env();
    let temp_path = temp_dir.path().to_path_buf();

    // add gitignore --help
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["add", "gitignore", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Add gitignore templates"))
        .stdout(predicate::str::contains("Usage: gitcraft add gitignore"))
        .stdout(predicate::str::contains("--dir"))
        .stdout(predicate::str::contains("--force"))
        .stdout(predicate::str::contains("--update-cache"))
        .stdout(predicate::str::contains("--output").or(predicate::str::contains("-o")));

    // list gitignore --help
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["list", "gitignore", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("List available gitignore templates"))
        .stdout(predicate::str::contains("Usage: gitcraft list gitignore"))
        .stdout(predicate::str::contains("-p").or(predicate::str::contains("--popular")))
        .stdout(predicate::str::contains("--update-cache"));

    // preview gitignore --help
    let mut cmd = AssertCommand::cargo_bin("gitcraft").unwrap();
    cmd.current_dir(&temp_path);
    cmd.args(&["preview", "gitignore", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Preview a gitignore template"))
        .stdout(predicate::str::contains("Usage: gitcraft preview gitignore"))
        .stdout(predicate::str::contains("--update-cache"));
}