entangle-mirror 0.1.2

Easy setup for mirroring GitHub repos to Tangled.org in one command
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
//! Integration tests for `entangle set`.
//!
//! These tests spawn the compiled `entangle` binary and verify exit codes,
//! stdout/stderr content, and config file state. They complement the unit
//! tests in `src/commands/set.rs`, which test the core logic directly.
//!
//! ## What these tests cover
//!
//! **Happy-path writes**
//! - `entangle set gh-user <value>` creates/updates the config file.
//! - `entangle set tngl-user <value>` does the same for the Tangled username.
//! - `entangle set origin <value>` does the same for origin preference.
//! - Aliases (`github-user`, `tangled-user`) are accepted.
//! - Case normalisation: `CyrusAE` → `cyrusae` in the saved file.
//! - Sequential `set` calls accumulate without clobbering other fields.
//! - Success prints a confirmation message to stdout.
//!
//! **Argument-parsing edge cases (clap rejects)**
//! - `entangle set gh-user` with no VALUE → exits non-zero.
//! - `entangle set tngl-user` with no VALUE → exits non-zero.
//! - `entangle set origin` with no VALUE → exits non-zero.
//! - `entangle set` with no KEY and no VALUE → exits non-zero.
//!
//! **Validation errors**
//! - Invalid GitHub username (`-bad`) → exits non-zero with error on stderr.
//! - Invalid Tangled username (`nodot`) → same.
//! - Unknown origin value (`gitlab`) → same.
//! - Config must not be written when validation fails.
//!
//! ## Isolation
//!
//! Every test sets `ENTANGLE_CONFIG_PATH` to a per-test temp file so the
//! user's real config is never touched.

use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// Spawn `entangle set <args>` with `ENTANGLE_CONFIG_PATH` pointing at
/// `config_path`, and return the raw [`Output`].
fn run_set(args: &[&str], config_path: &Path) -> Output {
    Command::new(env!("CARGO_BIN_EXE_entangle"))
        .arg("set")
        .args(args)
        .env("ENTANGLE_CONFIG_PATH", config_path)
        .output()
        .expect("failed to spawn entangle set")
}

fn stdout(o: &Output) -> String {
    String::from_utf8_lossy(&o.stdout).into_owned()
}
fn stderr(o: &Output) -> String {
    String::from_utf8_lossy(&o.stderr).into_owned()
}

/// Create a fresh temp dir and return a config path inside it.
fn fresh_config() -> (TempDir, PathBuf) {
    let dir = TempDir::new().unwrap();
    let config_path = dir.path().join("config.json");
    (dir, config_path)
}

/// Read the config file as a `serde_json::Value` for field inspection.
fn read_config(path: &Path) -> serde_json::Value {
    let content = std::fs::read_to_string(path)
        .unwrap_or_else(|e| panic!("could not read config at {}: {e}", path.display()));
    serde_json::from_str(&content)
        .unwrap_or_else(|e| panic!("config not valid JSON: {e}\ncontent: {content}"))
}

// ---------------------------------------------------------------------------
// Happy-path: each key writes the correct field
// ---------------------------------------------------------------------------

#[test]
fn set_gh_user_creates_config_with_github_username() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user", "cyrusae"], &config_path);
    assert!(
        out.status.success(),
        "set gh-user must succeed\nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(config_path.exists(), "config file must be created by set");

    let cfg = read_config(&config_path);
    assert_eq!(
        cfg["github_username"], "cyrusae",
        "github_username must match the supplied value"
    );
    // Other fields must not be written when they weren't provided.
    assert!(
        cfg["tangled_username"].is_null(),
        "tangled_username must not be written by set gh-user"
    );
}

#[test]
fn set_tngl_user_creates_config_with_tangled_username() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["tngl-user", "atdot.fyi"], &config_path);
    assert!(
        out.status.success(),
        "set tngl-user must succeed\nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );

    let cfg = read_config(&config_path);
    assert_eq!(cfg["tangled_username"], "atdot.fyi");
    assert!(cfg["github_username"].is_null());
}

#[test]
fn set_origin_creates_config_with_origin_preference() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin", "github"], &config_path);
    assert!(
        out.status.success(),
        "set origin must succeed\nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );

    let cfg = read_config(&config_path);
    assert_eq!(cfg["origin_preference"], "github");
}

/// `github-user` is the long alias for `gh-user` — it must be accepted.
#[test]
fn set_github_user_long_alias_accepted() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["github-user", "cyrusae"], &config_path);
    assert!(
        out.status.success(),
        "long alias 'github-user' must be accepted\nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );

    let cfg = read_config(&config_path);
    assert_eq!(cfg["github_username"], "cyrusae");
}

/// `tangled-user` is the long alias for `tngl-user` — it must be accepted.
#[test]
fn set_tangled_user_long_alias_accepted() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["tangled-user", "atdot.fyi"], &config_path);
    assert!(
        out.status.success(),
        "long alias 'tangled-user' must be accepted\nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );

    let cfg = read_config(&config_path);
    assert_eq!(cfg["tangled_username"], "atdot.fyi");
}

/// Origin alias `gh` → stored as `"github"`.
#[test]
fn set_origin_gh_alias_stores_github() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin", "gh"], &config_path);
    assert!(out.status.success(), "set origin gh must succeed");

    let cfg = read_config(&config_path);
    assert_eq!(
        cfg["origin_preference"], "github",
        "alias 'gh' must be stored as canonical 'github'"
    );
}

/// Origin alias `tngl` → stored as `"tangled"`.
#[test]
fn set_origin_tngl_alias_stores_tangled() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin", "tngl"], &config_path);
    assert!(out.status.success(), "set origin tngl must succeed");

    let cfg = read_config(&config_path);
    assert_eq!(
        cfg["origin_preference"], "tangled",
        "alias 'tngl' must be stored as canonical 'tangled'"
    );
}

// ---------------------------------------------------------------------------
// Case normalisation
// ---------------------------------------------------------------------------

/// Mixed-case GitHub username is lowercased before saving.
///
/// This pins the sanitize-first contract at the binary level: `CyrusAE`
/// must become `cyrusae` in the config file, not produce an error.
#[test]
fn set_gh_user_normalises_mixed_case() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user", "CyrusAE"], &config_path);
    assert!(
        out.status.success(),
        "mixed-case username must succeed (sanitized before validation)\
         \nstdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );

    let cfg = read_config(&config_path);
    assert_eq!(
        cfg["github_username"], "cyrusae",
        "username must be lowercased before saving"
    );
}

/// Mixed-case Tangled username is lowercased before saving.
#[test]
fn set_tngl_user_normalises_mixed_case() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["tngl-user", "AtDot.FYI"], &config_path);
    assert!(
        out.status.success(),
        "mixed-case Tangled username must succeed"
    );

    let cfg = read_config(&config_path);
    assert_eq!(cfg["tangled_username"], "atdot.fyi");
}

/// Mixed-case origin alias is lowercased before alias matching.
#[test]
fn set_origin_normalises_mixed_case() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin", "GitHub"], &config_path);
    assert!(out.status.success(), "mixed-case 'GitHub' must be accepted");

    let cfg = read_config(&config_path);
    assert_eq!(cfg["origin_preference"], "github");
}

// ---------------------------------------------------------------------------
// Sequential writes accumulate — no clobber
// ---------------------------------------------------------------------------

/// Three separate `set` calls populate all three fields independently.
///
/// After all three, `entangle init` must be able to load the config. This
/// pins the non-clobber guarantee at the binary level.
#[test]
fn sequential_set_calls_accumulate_all_fields() {
    let (_dir, config_path) = fresh_config();

    run_set(&["gh-user", "cyrusae"], &config_path);
    run_set(&["tngl-user", "atdot.fyi"], &config_path);
    run_set(&["origin", "github"], &config_path);

    let cfg = read_config(&config_path);
    assert_eq!(cfg["github_username"], "cyrusae");
    assert_eq!(cfg["tangled_username"], "atdot.fyi");
    assert_eq!(cfg["origin_preference"], "github");
}

/// Updating one field must leave the others unchanged.
#[test]
fn set_gh_user_does_not_clobber_existing_tangled_username() {
    let (_dir, config_path) = fresh_config();

    // Establish both fields.
    run_set(&["gh-user", "old-name"], &config_path);
    run_set(&["tngl-user", "atdot.fyi"], &config_path);

    // Overwrite only the GitHub username.
    let out = run_set(&["gh-user", "cyrusae"], &config_path);
    assert!(out.status.success(), "update must succeed");

    let cfg = read_config(&config_path);
    assert_eq!(
        cfg["github_username"], "cyrusae",
        "github_username must be updated"
    );
    assert_eq!(
        cfg["tangled_username"], "atdot.fyi",
        "tangled_username must remain unchanged"
    );
}

// ---------------------------------------------------------------------------
// Confirmation output
// ---------------------------------------------------------------------------

/// A successful `set` must print a confirmation to stdout so the user knows
/// what was stored, without requiring them to open the config file.
#[test]
fn set_gh_user_prints_confirmation_to_stdout() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user", "cyrusae"], &config_path);
    assert!(out.status.success(), "must succeed");

    let out_str = stdout(&out);
    assert!(
        !out_str.is_empty(),
        "stdout must contain a confirmation message"
    );
    assert!(
        out_str.contains("cyrusae"),
        "confirmation must mention the set value: {out_str}"
    );
}

// ---------------------------------------------------------------------------
// Argument-parsing edge cases (clap rejects)
// ---------------------------------------------------------------------------

/// `entangle set gh-user` with no VALUE → clap must reject with a usage error.
#[test]
fn set_gh_user_without_value_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user"], &config_path);
    assert!(
        !out.status.success(),
        "set gh-user with no value must exit non-zero"
    );
    // clap writes usage errors to stderr.
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain an error for missing value"
    );
}

/// `entangle set tngl-user` with no VALUE → clap must reject.
#[test]
fn set_tngl_user_without_value_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["tngl-user"], &config_path);
    assert!(
        !out.status.success(),
        "set tngl-user with no value must exit non-zero"
    );
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain an error for missing value"
    );
}

/// `entangle set origin` with no VALUE → clap must reject.
#[test]
fn set_origin_without_value_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin"], &config_path);
    assert!(
        !out.status.success(),
        "set origin with no value must exit non-zero"
    );
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain an error for missing value"
    );
}

/// `entangle set` with no KEY and no VALUE → clap must reject.
#[test]
fn set_with_no_args_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&[], &config_path);
    assert!(
        !out.status.success(),
        "set with no arguments must exit non-zero"
    );
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain an error for missing key"
    );
}

/// Config must not be written when clap rejects the invocation.
#[test]
fn set_with_no_args_does_not_create_config() {
    let (_dir, config_path) = fresh_config();

    run_set(&[], &config_path);
    assert!(
        !config_path.exists(),
        "config must not be created when set receives no arguments"
    );
}

// ---------------------------------------------------------------------------
// Validation errors
// ---------------------------------------------------------------------------

/// Invalid GitHub username → exits non-zero with an actionable error on stderr.
#[test]
fn set_gh_user_invalid_username_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user", "-invalid-leading-hyphen"], &config_path);
    assert!(
        !out.status.success(),
        "invalid GitHub username must exit non-zero"
    );
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain a validation error message"
    );
}

/// Config must not be written when the GitHub username is invalid.
#[test]
fn set_gh_user_invalid_username_does_not_write_config() {
    let (_dir, config_path) = fresh_config();

    run_set(&["gh-user", "-invalid-leading-hyphen"], &config_path);
    assert!(
        !config_path.exists(),
        "config must not be created when validation fails"
    );
}

/// Invalid Tangled username → exits non-zero.
#[test]
fn set_tngl_user_invalid_username_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["tngl-user", "nodot"], &config_path);
    assert!(
        !out.status.success(),
        "invalid Tangled username must exit non-zero"
    );
    let err = stderr(&out);
    assert!(
        !err.is_empty(),
        "stderr must contain a validation error for invalid Tangled username"
    );
}

/// Unrecognised origin value → exits non-zero with an error.
#[test]
fn set_origin_unknown_value_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["origin", "gitlab"], &config_path);
    assert!(
        !out.status.success(),
        "unknown origin 'gitlab' must exit non-zero"
    );
    let err = stderr(&out);
    assert!(!err.is_empty(), "stderr must describe the accepted values");
}

/// Config must not be written when the origin value is unrecognised.
#[test]
fn set_origin_unknown_value_does_not_write_config() {
    let (_dir, config_path) = fresh_config();

    run_set(&["origin", "gitlab"], &config_path);
    assert!(
        !config_path.exists(),
        "config must not be created for an unrecognised origin value"
    );
}

/// Shell metacharacter in username → rejected by sanitization (not a clap error).
#[test]
fn set_gh_user_dangerous_char_exits_nonzero() {
    let (_dir, config_path) = fresh_config();

    let out = run_set(&["gh-user", "cyrus$ae"], &config_path);
    assert!(
        !out.status.success(),
        "dangerous character in username must exit non-zero"
    );
}

// ---------------------------------------------------------------------------
// File-system error paths
// ---------------------------------------------------------------------------

/// When the config directory exists but is not writable, `entangle set` must
/// exit non-zero and print a clear error rather than panicking or silently
/// succeeding. Permissions tests are Unix-only; on Windows the permission
/// model differs and this specific scenario requires different setup.
#[test]
#[cfg(unix)]
fn set_readonly_config_dir_exits_nonzero_with_error() {
    use std::os::unix::fs::PermissionsExt;

    let dir = TempDir::new().unwrap();
    let config_dir = dir.path().join("cfg");
    std::fs::create_dir(&config_dir).unwrap();

    // Make the directory read+execute only (no write).
    let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
    perms.set_mode(0o555);
    std::fs::set_permissions(&config_dir, perms).unwrap();

    let config_path = config_dir.join("config.json");
    let out = run_set(&["gh-user", "cyrusae"], &config_path);

    // Restore write permission before the TempDir guard drops so cleanup works.
    let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
    perms.set_mode(0o755);
    std::fs::set_permissions(&config_dir, perms).unwrap();

    assert!(
        !out.status.success(),
        "set must fail when the config directory is not writable"
    );
    let err = stderr(&out);
    assert!(
        err.contains("Error"),
        "stderr must contain an error message: {err}"
    );
    assert!(
        !config_path.exists(),
        "config file must not be created when the directory is read-only"
    );
}