shipslip 0.14.0

Deploy Laravel apps over SSH with a preview, a deploy lock, and a receipt.
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
425
426
427
428
429
430
use super::*;
use crate::setup::{RepairOutcome, SetupPrompts};
use crate::setup_fixture::{quote, Fixture, SCRUBBED};
use std::fs;

#[derive(Default)]
struct Prompts {
    identities: Vec<&'static str>,
    confirmations: Vec<String>,
    answer: Option<bool>,
}
impl SetupPrompts for Prompts {
    async fn identity(
        &mut self,
        field: &'static str,
        _interrupts: &mut Interrupts,
    ) -> io::Result<Option<String>> {
        self.identities.push(field);
        Ok(Some(
            if field == "user.name" {
                "Tester"
            } else {
                "tester@example.com"
            }
            .into(),
        ))
    }
    async fn confirm(
        &mut self,
        _plan: &shipslip::setup::SetupPlan,
        question: &str,
        _interrupts: &mut Interrupts,
    ) -> io::Result<Option<bool>> {
        self.confirmations.push(question.into());
        Ok(self.answer)
    }
}

fn request() -> CreateRequest {
    CreateRequest {
        name: "shop".into(),
        options: InstallerOptions {
            starter_kit: StarterKit::None,
            auth: Auth::None,
            database: Database::Sqlite,
            testing: Testing::Pest,
            boost: false,
        },
        branch: "feature/shop".into(),
    }
}

fn fixture() -> Fixture {
    let fixture = Fixture::new();
    let git = fixture.root.join("sources/git");
    let body = fs::read_to_string(&git).unwrap().replace(
        "  *) exit 99 ;;",
        "  'check-ref-format --branch '*) echo branch ;;\n  *) exit 99 ;;",
    );
    fixture.script(&git, &body);
    fixture.complete();
    fixture
}

fn interrupts() -> Interrupts {
    Interrupts::from_channel(tokio::sync::mpsc::unbounded_channel().1)
}

async fn prepare(
    fixture: &Fixture,
    request: CreateRequest,
    prompts: &mut Prompts,
) -> Result<CreationPreparation, Box<dyn Error>> {
    prepare_creation(
        request,
        &fixture.root,
        &fixture.context,
        fixture.root.join("ops"),
        &mut interrupts(),
        prompts,
    )
    .await
}

#[tokio::test]
async fn invalid_destinations_and_options_fail_before_any_detection_or_setup() {
    let fixture = fixture();
    fs::create_dir(fixture.root.join("existing")).unwrap();
    std::os::unix::fs::symlink(fixture.root.join("missing"), fixture.root.join("link")).unwrap();
    fixture.script(
        &fixture.root.join("brew/bin/php"),
        &format!(
            "echo probe > {}/probe; exit 1",
            quote(&fixture.root.to_string_lossy())
        ),
    );
    for name in ["existing", "link", "nested/shop"] {
        let mut request = request();
        request.name = name.into();
        let mut prompts = Prompts::default();
        assert!(prepare(&fixture, request, &mut prompts).await.is_err());
        assert!(prompts.confirmations.is_empty());
        assert!(!fixture.root.join("probe").exists());
        assert!(!fixture.root.join("ops").exists());
    }
    for bad_options in [true, false] {
        let mut request = request();
        if bad_options {
            request.options.auth = Auth::Laravel;
        } else {
            request.branch = "bad branch".into();
        }
        assert!(prepare(&fixture, request, &mut Prompts::default())
            .await
            .is_err());
        assert!(!fixture.root.join("probe").exists());
    }
}

#[tokio::test]
async fn publish_failures_are_warnings_and_never_enter_the_creation_repair_plan() {
    let fixture = fixture();
    fs::remove_file(fixture.root.join("brew/bin/gh")).unwrap();
    let combined = shipslip::setup::detect(
        &fixture.context,
        &DetectionOptions {
            purposes: vec![Purpose::Create, Purpose::Publish],
            root: fixture.root.clone(),
            installer_options: Some(request().options),
        },
    )
    .await
    .unwrap();
    let warnings = publish_warnings(&combined);
    assert!(warnings.contains("Publishing later will need:"));
    assert!(warnings.contains("gh: missing"));
    assert!(!warnings.contains("Not ready:"));
    let mut prompts = Prompts::default();
    let CreationPreparation::Ready(preview) =
        prepare(&fixture, request(), &mut prompts).await.unwrap()
    else {
        panic!("publishing must not block creation")
    };
    assert_eq!(preview.request.name, "shop");
    assert!(prompts.confirmations.is_empty());
    assert!(!fixture.root.join("actions").exists());
}

#[tokio::test]
async fn missing_node_gets_one_setup_approval_and_retains_all_creation_answers() {
    let fixture = fixture();
    for tool in ["node", "npm", "gh"] {
        fs::remove_file(fixture.root.join("brew/bin").join(tool)).unwrap();
    }
    let mut request = request();
    request.options = InstallerOptions {
        starter_kit: StarterKit::Livewire,
        auth: Auth::Laravel,
        database: Database::Mariadb,
        testing: Testing::Phpunit,
        boost: true,
    };
    let expected = request.clone();
    let mut prompts = Prompts {
        answer: Some(true),
        ..Prompts::default()
    };
    let CreationPreparation::Ready(preview) =
        prepare(&fixture, request, &mut prompts).await.unwrap()
    else {
        panic!("repair must resume creation")
    };
    assert_eq!(
        prompts.confirmations,
        ["Apply setup and continue creating shop?"]
    );
    assert!(prompts.identities.is_empty());
    assert_eq!(preview.request.options, expected.options);
    assert_eq!(preview.request.branch, expected.branch);
    assert_eq!(preview.request.name, expected.name);
    assert_eq!(
        fs::read_to_string(fixture.root.join("actions")).unwrap(),
        "install node\n"
    );
    assert!(!fixture.root.join("brew/bin/gh").exists());
    assert!(preview
        .installer_args
        .iter()
        .any(|arg| arg == "--database=mariadb"));
}

#[tokio::test]
async fn declined_guidance_failed_and_interrupted_repairs_stop_before_creation_preview() {
    for mode in ["decline", "guidance", "failed", "interrupted"] {
        let mut fixture = fixture();
        for tool in ["node", "npm"] {
            fs::remove_file(fixture.root.join("brew/bin").join(tool)).unwrap();
        }
        let mut prompts = Prompts {
            answer: Some(false),
            ..Prompts::default()
        };
        let expected = match mode {
            "guidance" => {
                fixture.context.macos = false;
                RepairOutcome::NotReady
            }
            "failed" => {
                fixture.brew("exit 11");
                prompts.answer = Some(true);
                RepairOutcome::Failed
            }
            "interrupted" => {
                prompts.answer = None;
                RepairOutcome::Interrupted
            }
            _ => RepairOutcome::Declined,
        };
        let CreationPreparation::Stopped(outcome) =
            prepare(&fixture, request(), &mut prompts).await.unwrap()
        else {
            panic!("{mode}: must stop")
        };
        assert_eq!(outcome, expected);
        assert!(!fixture.root.join("shop").exists());
        assert_eq!(prompts.confirmations.len(), usize::from(mode != "guidance"));
    }
}

#[tokio::test]
async fn identity_is_kept_when_in_flow_install_fails() {
    let fixture = fixture();
    for path in ["name", "email", "brew/bin/node", "brew/bin/npm"] {
        fs::remove_file(fixture.root.join(path)).unwrap();
    }
    fixture.brew("exit 11");
    let mut prompts = Prompts {
        answer: Some(true),
        ..Prompts::default()
    };
    assert!(matches!(
        prepare(&fixture, request(), &mut prompts).await.unwrap(),
        CreationPreparation::Stopped(RepairOutcome::Failed)
    ));
    assert_eq!(prompts.identities, ["user.name", "user.email"]);
    assert_eq!(
        fs::read_to_string(fixture.root.join("actions")).unwrap(),
        "name\nemail\n"
    );
    let report = fixture.detect().await;
    assert!(shipslip::setup::plan_setup(
        &fixture.context,
        &report,
        &shipslip::setup::SetupInputs::default()
    )
    .unwrap()
    .inputs_needed
    .is_empty());
}

#[test]
fn creation_rerun_round_trips_every_answer_and_shell_metacharacters() {
    let mut request = request();
    request.name = "my 'app $()".into();
    for boost in [true, false] {
        request.options.boost = boost;
        let command = rerun_command(&request);
        let output = std::process::Command::new("/bin/sh")
            .args(["-c", &format!("set -- {command}; printf '%s\\n' \"$@\"")])
            .output()
            .unwrap();
        assert!(output.status.success());
        let words = String::from_utf8(output.stdout)
            .unwrap()
            .lines()
            .skip(2)
            .map(String::from)
            .collect::<Vec<_>>();
        let parsed = Args::parse(&words).unwrap();
        assert_eq!(parsed.name, request.name);
        assert_eq!(parsed.branch, request.branch);
        assert_eq!(parsed.starter_kit, Some(request.options.starter_kit));
        assert_eq!(parsed.auth, Some(request.options.auth));
        assert_eq!(parsed.database, Some(request.options.database));
        assert_eq!(parsed.testing, Some(request.options.testing));
        assert_eq!(parsed.boost, Some(boost));
    }
}

#[tokio::test]
async fn missing_node_demo_repairs_resumes_and_verifies_the_created_app() {
    let fixture = fixture();
    for tool in ["node", "npm"] {
        fs::remove_file(fixture.root.join("brew/bin").join(tool)).unwrap();
    }
    let laravel = fixture.root.join("home/.composer/vendor/bin/laravel");
    let body = fs::read_to_string(&laravel).unwrap();
    let version_and_help = body.replace("else printf", "elif [ \"$2\" = --help ]; then printf");
    let version_and_help = version_and_help.trim_end().strip_suffix("fi").unwrap();
    fixture.script(&laravel, &format!(r#"
{version_and_help}
else
{SCRUBBED}
project=$2
/bin/mkdir -p "$project/vendor" "$project/public/build/assets"
printf artisan > "$project/artisan"
printf autoload > "$project/vendor/autoload.php"
printf '%s' '{{"name":"laravel/laravel","require":{{"laravel/framework":"^13.0"}}}}' > "$project/composer.json"
printf '%s' '{{"packages":[{{"name":"laravel/framework","version":"v13.34.0"}}],"packages-dev":[{{"name":"pestphp/pest","version":"v4.0.0"}}]}}' > "$project/composer.lock"
printf '%s' '{{"scripts":{{"build":"vite build"}}}}' > "$project/package.json"
printf '{{}}' > "$project/package-lock.json"
printf '%s' '{{"app":{{"file":"assets/app.js"}}}}' > "$project/public/build/manifest.json"
printf built > "$project/public/build/assets/app.js"
fi
"#));
    let php = fixture.root.join("brew/bin/php");
    let body = fs::read_to_string(&php).unwrap().replace(
        "  *) exit 99 ;;",
        &format!("  artisan) {SCRUBBED}\n    printf tested > .test-ran ;;\n  *) exit 99 ;;"),
    );
    fixture.script(&php, &body);
    let mut prompts = Prompts {
        answer: Some(true),
        ..Prompts::default()
    };
    let CreationPreparation::Ready(preview) =
        prepare(&fixture, request(), &mut prompts).await.unwrap()
    else {
        panic!()
    };
    let confirmation = preview.confirm();
    let (_signal, receiver) = watch::channel(false);
    let project = create::execute_create(*preview, confirmation, receiver, |_| {})
        .await
        .unwrap();
    assert!(project.root.join("artisan").exists());
    assert_eq!(
        fs::read_to_string(project.root.join(".test-ran")).unwrap(),
        "tested"
    );
    assert_eq!(prompts.confirmations.len(), 1);
}

struct ProjectQuestions {
    outcome: Option<super::super::publish::PublishOutcome>,
    questions: Vec<String>,
}
impl ProjectPrompts for ProjectQuestions {
    async fn confirm(
        &mut self,
        question: &str,
        _interrupts: &mut Interrupts,
    ) -> io::Result<Option<bool>> {
        self.questions.push(question.into());
        Ok(Some(question == "Publish to GitHub now?"))
    }
    async fn publish(
        &mut self,
        _root: &Path,
        _interrupts: &mut Interrupts,
    ) -> Result<super::super::publish::PublishOutcome, Box<dyn Error>> {
        Ok(self.outcome.take().unwrap())
    }
    async fn server(
        &mut self,
        _root: &Path,
        _interrupts: &mut Interrupts,
    ) -> Result<Option<PathBuf>, Box<dyn Error>> {
        panic!("server was declined")
    }
}

#[tokio::test]
async fn chained_not_ready_and_declined_publish_continue_to_server_and_exit_zero() {
    use super::super::publish::PublishOutcome;
    let fixture = fixture();
    for outcome in [PublishOutcome::NotReady, PublishOutcome::Declined] {
        let mut prompts = ProjectQuestions {
            outcome: Some(outcome),
            questions: vec![],
        };
        let mut interrupts = interrupts();
        assert_eq!(
            finish_local_project(&fixture.root, true, &mut interrupts, &mut prompts)
                .await
                .unwrap(),
            ExitCode::SUCCESS
        );
        assert_eq!(
            prompts.questions,
            ["Publish to GitHub now?", "Configure a server now?"]
        );
        assert!(!interrupts.pending);
    }
}

#[tokio::test]
async fn chained_interrupt_stops_before_server_and_exits_130() {
    let fixture = fixture();
    let mut prompts = ProjectQuestions {
        outcome: Some(super::super::publish::PublishOutcome::Interrupted),
        questions: vec![],
    };
    assert_eq!(
        finish_local_project(&fixture.root, true, &mut interrupts(), &mut prompts)
            .await
            .unwrap(),
        ExitCode::from(INTERRUPTED)
    );
    assert_eq!(prompts.questions, ["Publish to GitHub now?"]);
}

#[tokio::test]
async fn failed_chained_repair_preserves_failure_and_stops_before_server() {
    use super::super::publish::PublishOutcome;
    let fixture = fixture();
    for (outcome, code) in [(PublishOutcome::Failed, 1), (PublishOutcome::Invalid, 2)] {
        let mut prompts = ProjectQuestions {
            outcome: Some(outcome),
            questions: vec![],
        };
        assert_eq!(
            finish_local_project(&fixture.root, true, &mut interrupts(), &mut prompts)
                .await
                .unwrap(),
            ExitCode::from(code)
        );
        assert_eq!(prompts.questions, ["Publish to GitHub now?"]);
    }
}