pray-cli 1.16.0

Package manager for the language placed before inference — Prayfile CLI (binary: pray)
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
#[path = "install_network_support.rs"]
mod support;

use serde_json::json;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};

use support::{
    create_add_fixture, find_free_port, run_pray, spawn_server, temporary_directory,
    wait_for_server,
};

fn run_command(
    program: &str,
    directory: &Path,
    arguments: &[&str],
    environment: &[(&str, &str)],
) -> Output {
    let mut command = Command::new(program);
    command.current_dir(directory).args(arguments);
    for (key, value) in environment {
        command.env(key, value);
    }
    command.output().expect("run command")
}

fn assert_success(output: &Output, label: &str) {
    assert!(
        output.status.success(),
        "{label} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

fn git(directory: &Path, arguments: &[&str]) -> Output {
    run_command("git", directory, arguments, &[])
}

fn hg(directory: &Path, arguments: &[&str], environment: &[(&str, &str)]) -> Output {
    run_command("hg", directory, arguments, environment)
}

fn pray(directory: &Path, arguments: &[&str], environment: &[(&str, &str)]) -> Output {
    run_command(
        env!("CARGO_BIN_EXE_pray"),
        directory,
        arguments,
        environment,
    )
}

fn write_revision_config(root: &Path, config: serde_json::Value) {
    let v1 = root.join("v1");
    fs::create_dir_all(&v1).expect("revision config directory");
    fs::write(
        v1.join("revision.json"),
        serde_json::to_string_pretty(&config).expect("serialize revision config"),
    )
    .expect("write revision config");
}

#[test]
fn publish_records_git_revision_and_pushes_to_remote() {
    let workspace = temporary_directory("pray-revision-git");
    let repository = workspace.join("repo");
    let remote = workspace.join("remote.git");
    fs::create_dir_all(&repository).expect("repository workspace");

    assert_success(
        &git(
            &workspace,
            &["init", "--bare", remote.to_str().expect("remote path")],
        ),
        "git init --bare",
    );
    assert_success(&git(&repository, &["init", "-b", "main"]), "git init");
    assert_success(
        &git(&repository, &["config", "user.name", "Pray Test"]),
        "git user.name",
    );
    assert_success(
        &git(&repository, &["config", "user.email", "pray@example.com"]),
        "git user.email",
    );
    assert_success(
        &git(
            &repository,
            &[
                "remote",
                "add",
                "origin",
                remote.to_str().expect("remote path"),
            ],
        ),
        "git remote add",
    );

    create_add_fixture(&repository);
    write_revision_config(
        &repository,
        json!({
            "backend": "git",
            "remote": "origin",
            "push": true,
            "commit_message": "publish revision for git"
        }),
    );

    let add = run_pray(
        &repository,
        &["add", "sample/base", "--path", "packages/base"],
    );
    assert_success(&add, "pray add");

    let publish = pray(
        &repository,
        &[
            "publish",
            "--root",
            repository.to_str().expect("repository path"),
        ],
        &[],
    );
    assert_success(&publish, "pray publish");

    let local_commit = git(&repository, &["rev-parse", "HEAD"]);
    assert_success(&local_commit, "git rev-parse HEAD");
    let remote_commit = git(&remote, &["rev-parse", "refs/heads/main"]);
    assert_success(&remote_commit, "git rev-parse remote branch");

    assert_eq!(
        String::from_utf8_lossy(&local_commit.stdout).trim(),
        String::from_utf8_lossy(&remote_commit.stdout).trim(),
    );

    let commit_message = git(&repository, &["log", "-1", "--pretty=%s"]);
    assert_success(&commit_message, "git log");
    assert_eq!(
        String::from_utf8_lossy(&commit_message.stdout).trim(),
        "publish revision for git"
    );
}

#[test]
fn publish_rejects_git_push_when_remote_has_advanced() {
    let workspace = temporary_directory("pray-revision-git-conflict");
    let repository = workspace.join("repo");
    let remote = workspace.join("remote.git");
    let competing_clone = workspace.join("competing");
    fs::create_dir_all(&repository).expect("repository workspace");

    assert_success(
        &git(
            &workspace,
            &["init", "--bare", remote.to_str().expect("remote path")],
        ),
        "git init --bare",
    );
    assert_success(&git(&repository, &["init", "-b", "main"]), "git init");
    assert_success(
        &git(&repository, &["config", "user.name", "Pray Test"]),
        "git user.name",
    );
    assert_success(
        &git(&repository, &["config", "user.email", "pray@example.com"]),
        "git user.email",
    );
    assert_success(
        &git(
            &repository,
            &[
                "remote",
                "add",
                "origin",
                remote.to_str().expect("remote path"),
            ],
        ),
        "git remote add",
    );

    create_add_fixture(&repository);
    write_revision_config(
        &repository,
        json!({
            "backend": "git",
            "remote": "origin",
            "push": true
        }),
    );

    let add = run_pray(
        &repository,
        &["add", "sample/base", "--path", "packages/base"],
    );
    assert_success(&add, "pray add");

    let publish = pray(
        &repository,
        &[
            "publish",
            "--root",
            repository.to_str().expect("repository path"),
        ],
        &[],
    );
    assert_success(&publish, "pray publish");
    assert_success(
        &git(&remote, &["symbolic-ref", "HEAD", "refs/heads/main"]),
        "bare default branch",
    );

    assert_success(
        &git(
            &workspace,
            &[
                "clone",
                remote.to_str().expect("remote path"),
                competing_clone.to_str().expect("competing path"),
            ],
        ),
        "git clone",
    );
    assert_success(
        &git(&competing_clone, &["config", "user.name", "Pray Test"]),
        "competing user.name",
    );
    assert_success(
        &git(
            &competing_clone,
            &["config", "user.email", "pray@example.com"],
        ),
        "competing user.email",
    );
    fs::write(
        competing_clone.join("remote-note.txt"),
        "remote moved ahead\n",
    )
    .expect("write competing commit");
    assert_success(
        &git(&competing_clone, &["add", "remote-note.txt"]),
        "competing add",
    );
    assert_success(
        &git(&competing_clone, &["commit", "-m", "advance remote"]),
        "competing commit",
    );
    assert_success(
        &git(
            &competing_clone,
            &["push", "origin", "HEAD:refs/heads/main"],
        ),
        "competing push",
    );

    fs::write(
        repository.join("packages/base/README.md"),
        "package readme updated\n",
    )
    .expect("update package readme");

    let failed_publish = pray(
        &repository,
        &[
            "publish",
            "--root",
            repository.to_str().expect("repository path"),
        ],
        &[],
    );
    assert!(!failed_publish.status.success());
    let stderr = String::from_utf8_lossy(&failed_publish.stderr);
    assert!(
        stderr.contains("rejected")
            || stderr.contains("non-fast-forward")
            || stderr.contains("fetch and retry")
            || stderr.contains("remote branch moved")
    );
}

#[test]
fn publish_records_hg_revision_without_configured_backend() {
    let workspace = temporary_directory("pray-revision-hg");
    let repository = workspace.join("repo");
    fs::create_dir_all(&repository).expect("repository workspace");

    assert_success(&hg(&repository, &["init"], &[]), "hg init");
    fs::write(
        repository.join(".hg/hgrc"),
        "[ui]\nusername = Pray Test <pray@example.com>\n",
    )
    .expect("write hg hgrc");

    create_add_fixture(&repository);
    let add = run_pray(
        &repository,
        &["add", "sample/base", "--path", "packages/base"],
    );
    assert_success(&add, "pray add");

    let publish = pray(
        &repository,
        &[
            "publish",
            "--root",
            repository.to_str().expect("repository path"),
        ],
        &[("HGUSER", "Pray Test <pray@example.com>")],
    );
    assert_success(&publish, "pray publish");

    let commit_message = hg(
        &repository,
        &["log", "-r", ".", "--template", "{desc}"],
        &[],
    );
    assert_success(&commit_message, "hg log");
    assert_eq!(
        String::from_utf8_lossy(&commit_message.stdout).trim(),
        "pray publish: update distribution state"
    );
}

#[test]
fn sync_records_custom_revision_commands_after_successful_sync() {
    let workspace = temporary_directory("pray-revision-sync");
    let source = workspace.join("source");
    let upstream = workspace.join("upstream");
    let downstream = workspace.join("downstream");
    fs::create_dir_all(&source).expect("source workspace");
    fs::create_dir_all(&upstream).expect("upstream workspace");
    fs::create_dir_all(&downstream).expect("downstream workspace");

    create_add_fixture(&source);
    let add = run_pray(&source, &["add", "sample/base", "--path", "packages/base"]);
    assert_success(&add, "pray add");

    let publish = run_pray(
        &source,
        &[
            "publish",
            "--root",
            upstream.to_str().expect("upstream path"),
        ],
    );
    assert_success(&publish, "pray publish");

    let commit_log = workspace.join("commit.log");
    let push_log = workspace.join("push.log");
    let commit_script = workspace.join("commit.sh");
    let push_script = workspace.join("push.sh");
    fs::write(
        &commit_script,
        "#!/bin/sh\nprintf 'commit:%s\\n' \"$PWD\" >> \"$1\"\n",
    )
    .expect("write commit script");
    fs::write(
        &push_script,
        "#!/bin/sh\nprintf 'push:%s\\n' \"$PWD\" >> \"$1\"\n",
    )
    .expect("write push script");
    write_revision_config(
        &downstream,
        json!({
            "backend": "other",
            "push": true,
            "commit_command": {
                "program": "sh",
                "args": [commit_script.to_str().expect("commit script path"), commit_log.to_str().expect("commit log path")]
            },
            "push_command": {
                "program": "sh",
                "args": [push_script.to_str().expect("push script path"), push_log.to_str().expect("push log path")]
            }
        }),
    );

    fs::create_dir_all(downstream.join("v1")).expect("downstream v1 workspace");
    let port = find_free_port();
    let mut server = spawn_server(&upstream, port);
    wait_for_server(port);
    let upstream_url = format!("http://127.0.0.1:{port}");
    fs::write(
        downstream.join("v1/peers.json"),
        format!(
            r#"[
                {{
                    "name": "upstream",
                    "url": "{upstream_url}",
                    "public": true
                }}
            ]"#
        ),
    )
    .expect("write peer list");

    let sync = run_pray(
        &workspace,
        &[
            "sync",
            "--root",
            downstream.to_str().expect("downstream path"),
        ],
    );
    assert_success(&sync, "pray sync");

    let synced_index =
        fs::read_to_string(downstream.join("v1/index.json")).expect("downstream index");
    assert!(synced_index.contains("sample/base"));

    let commit_log_text = fs::read_to_string(&commit_log).expect("commit log");
    let push_log_text = fs::read_to_string(&push_log).expect("push log");
    let downstream_path = downstream
        .canonicalize()
        .unwrap_or_else(|_| downstream.clone());
    assert_eq!(
        commit_log_text.trim(),
        format!("commit:{}", downstream_path.display())
    );
    assert_eq!(
        push_log_text.trim(),
        format!("push:{}", downstream_path.display())
    );

    let _ = server.kill();
    let _ = server.wait();
}