sefy-plugin-github 0.8.0

sefy transport that keeps a vault in a git repository
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
//! The transport against a real git repository.
//!
//! The "remote" is a bare repository in a temporary directory, which is a real
//! remote as far as git is concerned — no network, and nothing about the test
//! that a GitHub URL would exercise differently.

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

/// Runs the plugin with a request on stdin, returning (success, stdout, stderr).
fn run_plugin(request: &str, repository: &Path, working_copy: &Path) -> (bool, String, String) {
    use std::io::Write;

    let mut child = Command::new(plugin_binary())
        .arg("run")
        .env("SEFY_GITHUB_REPO", repository)
        .env("SEFY_GITHUB_DIR", working_copy)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("the plugin binary must be runnable");

    child
        .stdin
        .take()
        .unwrap()
        .write_all(request.as_bytes())
        .unwrap();

    let output = child.wait_with_output().unwrap();
    (
        output.status.success(),
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
    )
}

/// Path of the built plugin, beside the test binary.
fn plugin_binary() -> PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop();
    if path.ends_with("deps") {
        path.pop();
    }
    path.join(format!(
        "sefy-plugin-github{}",
        std::env::consts::EXE_SUFFIX
    ))
}

fn git(directory: Option<&Path>, arguments: &[&str]) -> String {
    let mut command = Command::new("git");
    if let Some(directory) = directory {
        command.current_dir(directory);
    }
    let output = command.args(arguments).output().unwrap();
    assert!(
        output.status.success(),
        "git {arguments:?} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_owned()
}

/// A bare repository with one commit, so it has a branch to clone.
///
/// A freshly initialised bare repository has no HEAD to check out, and cloning
/// it produces a working copy git considers to be on an unborn branch — which
/// is not the shape this transport is written against, and not what anyone
/// pushing a vault to a repository they already use would have.
fn remote(directory: &Path) -> PathBuf {
    let remote = directory.join("remote.git");
    git(
        None,
        &["init", "--quiet", "--bare", &remote.display().to_string()],
    );

    let seed = directory.join("seed");
    git(
        None,
        &[
            "clone",
            "--quiet",
            &remote.display().to_string(),
            &seed.display().to_string(),
        ],
    );
    std::fs::write(seed.join("README"), "vault storage\n").unwrap();
    git(Some(&seed), &["add", "README"]);
    git(
        Some(&seed),
        &[
            "-c",
            "user.email=t@example.invalid",
            "-c",
            "user.name=Test",
            "commit",
            "--quiet",
            "-m",
            "first",
        ],
    );
    git(Some(&seed), &["push", "--quiet", "origin", "HEAD"]);

    remote
}

fn request(operation: &str, file: &Path, name: &str) -> String {
    format!(
        r#"{{"operation":"{operation}","file":{},"name":"{name}"}}"#,
        serde_json::to_string(&file.display().to_string()).unwrap()
    )
}

#[test]
fn the_manifest_is_what_sefy_expects() {
    let output = Command::new(plugin_binary())
        .arg("--manifest")
        .output()
        .unwrap();

    assert!(output.status.success());
    let manifest: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(manifest["protocol_version"], 1);
    assert_eq!(manifest["name"], "github");
}

#[test]
fn a_vault_pushed_here_comes_back_from_a_second_machine() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());

    // One machine pushes.
    let vault = directory.path().join("notes.bak");
    std::fs::write(&vault, b"sealed bytes, not a real vault").unwrap();
    let (ok, stdout, stderr) = run_plugin(
        &request("push", &vault, "vault"),
        &remote,
        &directory.path().join("machine-one"),
    );
    assert!(ok, "push failed: {stderr}");
    assert!(stdout.contains("pushed"), "got: {stdout}");

    // Another machine, with its own working copy, pulls.
    let fetched = directory.path().join("fetched.bin");
    let (ok, stdout, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("machine-two"),
    );
    assert!(ok, "pull failed: {stderr}");
    assert!(stdout.contains("fetched"), "got: {stdout}");

    assert_eq!(
        std::fs::read(&fetched).unwrap(),
        b"sealed bytes, not a real vault",
        "what comes back is what went up, byte for byte"
    );
}

#[test]
fn a_second_push_from_the_same_machine_updates_the_remote() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let working_copy = directory.path().join("machine");
    let vault = directory.path().join("notes.bak");

    std::fs::write(&vault, b"first").unwrap();
    let (ok, _, stderr) = run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);
    assert!(ok, "first push failed: {stderr}");

    std::fs::write(&vault, b"second").unwrap();
    let (ok, _, stderr) = run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);
    assert!(ok, "second push failed: {stderr}");

    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("elsewhere"),
    );
    assert!(ok, "pull failed: {stderr}");
    assert_eq!(std::fs::read(&fetched).unwrap(), b"second");
}

#[test]
fn a_push_of_what_is_already_there_says_so_without_committing() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let working_copy = directory.path().join("machine");
    let vault = directory.path().join("notes.bak");
    std::fs::write(&vault, b"unchanged").unwrap();

    run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);
    let before = git(Some(&working_copy), &["rev-parse", "HEAD"]);

    let (ok, stdout, stderr) =
        run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);

    assert!(ok, "push failed: {stderr}");
    assert!(stdout.contains("already"), "got: {stdout}");
    assert_eq!(
        git(Some(&working_copy), &["rev-parse", "HEAD"]),
        before,
        "an unchanged vault must not add a commit"
    );
}

#[test]
fn two_machines_pushing_different_names_do_not_tread_on_each_other() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());

    let one = directory.path().join("one.bak");
    std::fs::write(&one, b"machine one").unwrap();
    let (ok, _, stderr) = run_plugin(
        &request("push", &one, "desktop"),
        &remote,
        &directory.path().join("machine-one"),
    );
    assert!(ok, "{stderr}");

    let two = directory.path().join("two.bak");
    std::fs::write(&two, b"machine two").unwrap();
    let (ok, _, stderr) = run_plugin(
        &request("push", &two, "laptop"),
        &remote,
        &directory.path().join("machine-two"),
    );
    assert!(ok, "{stderr}");

    // The second push must not have dropped the first machine's copy: a
    // transport that reset over the remote's state would.
    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "desktop"),
        &remote,
        &directory.path().join("machine-three"),
    );
    assert!(ok, "the first copy must still be there: {stderr}");
    assert_eq!(std::fs::read(&fetched).unwrap(), b"machine one");
}

#[test]
fn pulling_something_the_repository_does_not_hold_says_which_name() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let fetched = directory.path().join("fetched.bin");

    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "never-pushed"),
        &remote,
        &directory.path().join("machine"),
    );

    assert!(!ok);
    assert!(stderr.contains("never-pushed"), "got: {stderr}");
    assert!(
        !fetched.exists(),
        "nothing must be written when there is nothing to fetch"
    );
}

#[test]
fn without_the_repository_variable_the_error_says_what_to_set() {
    let directory = tempfile::tempdir().unwrap();
    let vault = directory.path().join("notes.bak");
    std::fs::write(&vault, b"sealed").unwrap();

    let mut child = Command::new(plugin_binary())
        .arg("run")
        .env_remove("SEFY_GITHUB_REPO")
        .env("SEFY_GITHUB_DIR", directory.path().join("machine"))
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    {
        use std::io::Write;
        child
            .stdin
            .take()
            .unwrap()
            .write_all(request("push", &vault, "vault").as_bytes())
            .unwrap();
    }
    let output = child.wait_with_output().unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("SEFY_GITHUB_REPO"), "got: {stderr}");
}

#[test]
fn run_by_hand_it_explains_what_it_is_instead_of_waiting_for_input() {
    let output = Command::new(plugin_binary()).output().unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("sefy push"), "got: {stderr}");
}

#[test]
fn a_push_landing_on_top_of_someone_elses_takes_the_remotes_state_first() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());

    // One machine publishes, so the remote has a copy nobody else has seen.
    let theirs = directory.path().join("theirs.bak");
    std::fs::write(&theirs, b"from the other machine").unwrap();
    let (ok, _, stderr) = run_plugin(
        &request("push", &theirs, "vault"),
        &remote,
        &directory.path().join("machine-one"),
    );
    assert!(ok, "{stderr}");

    // A second machine that has never pulled pushes its own. This is the case
    // that would fail outright without the fetch-and-reset in prepare: git
    // would refuse a push whose history the remote has moved past.
    let ours = directory.path().join("ours.bak");
    std::fs::write(&ours, b"from this machine").unwrap();
    let (ok, _, stderr) = run_plugin(
        &request("push", &ours, "vault"),
        &remote,
        &directory.path().join("machine-two"),
    );

    assert!(
        ok,
        "the push must go through, not stall on a diverged history: {stderr}"
    );

    // The push wins outright — which is exactly why sefy pulls before it
    // pushes, and why `sefy sync` exists rather than a bare `push`.
    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("machine-three"),
    );
    assert!(ok, "{stderr}");
    assert_eq!(std::fs::read(&fetched).unwrap(), b"from this machine");
}

#[test]
fn a_stale_working_copy_catches_up_before_it_pushes() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let ours = directory.path().join("machine-one");
    let theirs = directory.path().join("machine-two");

    // This machine pushes once, so it now has a working copy of its own.
    let vault = directory.path().join("notes.bak");
    std::fs::write(&vault, b"first").unwrap();
    let (ok, _, stderr) = run_plugin(&request("push", &vault, "vault"), &remote, &ours);
    assert!(ok, "{stderr}");

    // Another machine publishes twice, moving the remote two commits ahead of
    // what this machine's working copy knows about.
    let other = directory.path().join("other.bak");
    for contents in [b"theirs one".as_slice(), b"theirs two".as_slice()] {
        std::fs::write(&other, contents).unwrap();
        let (ok, _, stderr) = run_plugin(&request("push", &other, "vault"), &remote, &theirs);
        assert!(ok, "{stderr}");
    }

    // Now this machine pushes again from its stale copy. Without catching up
    // first, git refuses the push: the remote has moved past this history.
    std::fs::write(&vault, b"second").unwrap();
    let (ok, _, stderr) = run_plugin(&request("push", &vault, "vault"), &remote, &ours);

    assert!(
        ok,
        "a stale working copy must catch up rather than stall: {stderr}"
    );

    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("machine-three"),
    );
    assert!(ok, "{stderr}");
    assert_eq!(std::fs::read(&fetched).unwrap(), b"second");
}

#[test]
fn a_changed_vault_of_the_same_length_is_still_published() {
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let working_copy = directory.path().join("machine");
    let vault = directory.path().join("notes.bak");

    // The shape that caught this out in practice. A sealed vault is the same
    // length every time it is re-sealed after an ordinary edit, and the vault
    // handed over is a file written before the previous push — so the copy
    // landing in the working copy can carry an mtime *older* than the one git
    // recorded in its index. git then reports the path as unchanged, and a
    // transport that believes it publishes the previous contents, silently,
    // with a success message.
    //
    // The older vault is prepared first so its timestamp is genuinely behind.
    let older = directory.path().join("older.bak");
    let second = vec![b'B'; 53_305];
    std::fs::write(&older, &second).unwrap();

    let first = vec![b'A'; 53_305];
    std::fs::write(&vault, &first).unwrap();
    let (ok, _, stderr) = run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);
    assert!(ok, "{stderr}");

    // fs::copy carries the source's timestamps across on Windows, which is how
    // the working copy ends up looking older than the index believes it to be.
    std::fs::copy(&older, &vault).unwrap();
    let (ok, stdout, stderr) =
        run_plugin(&request("push", &vault, "vault"), &remote, &working_copy);
    assert!(ok, "{stderr}");
    assert!(
        stdout.contains("pushed"),
        "the second push must not report the vault as unchanged: {stdout}"
    );

    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("elsewhere"),
    );
    assert!(ok, "{stderr}");
    assert_eq!(
        std::fs::read(&fetched).unwrap(),
        second,
        "what the remote holds must be the second vault, not the first"
    );
}

#[test]
fn a_push_works_on_a_machine_with_no_git_identity_configured() {
    // git refuses to commit without user.name and user.email, and the machine
    // running this may well have neither — a fresh install, a CI runner. The
    // transport supplies its own rather than failing the sync with git's "tell
    // me who you are", which is a puzzling thing to meet when all you asked was
    // to move a vault.
    let directory = tempfile::tempdir().unwrap();
    let remote = remote(directory.path());
    let vault = directory.path().join("notes.bak");
    std::fs::write(&vault, b"sealed").unwrap();

    // An empty HOME (and the Windows equivalents) means no global config to
    // read an identity from, whatever the real machine has set.
    let empty_home = directory.path().join("no-config");
    std::fs::create_dir_all(&empty_home).unwrap();

    let mut child = Command::new(plugin_binary())
        .arg("run")
        .env("SEFY_GITHUB_REPO", &remote)
        .env("SEFY_GITHUB_DIR", directory.path().join("machine"))
        .env("HOME", &empty_home)
        .env("USERPROFILE", &empty_home)
        .env("GIT_CONFIG_GLOBAL", empty_home.join("gitconfig"))
        .env("GIT_CONFIG_SYSTEM", empty_home.join("gitconfig"))
        .env_remove("GIT_AUTHOR_NAME")
        .env_remove("GIT_AUTHOR_EMAIL")
        .env_remove("GIT_COMMITTER_NAME")
        .env_remove("GIT_COMMITTER_EMAIL")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    {
        use std::io::Write;
        child
            .stdin
            .take()
            .unwrap()
            .write_all(request("push", &vault, "vault").as_bytes())
            .unwrap();
    }
    let output = child.wait_with_output().unwrap();

    assert!(
        output.status.success(),
        "the push must not need an identity: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let fetched = directory.path().join("fetched.bin");
    let (ok, _, stderr) = run_plugin(
        &request("pull", &fetched, "vault"),
        &remote,
        &directory.path().join("elsewhere"),
    );
    assert!(ok, "{stderr}");
    assert_eq!(std::fs::read(&fetched).unwrap(), b"sealed");
}