reinhardt-admin-cli 0.3.2

Command-line tool for Reinhardt project management
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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! End-to-end tests for the `migrate-manouche-v2` command.
//!
//! These tests spawn the compiled `reinhardt-admin` binary and inspect real
//! files on disk, covering command-line behavior that rule-level snapshot
//! tests cannot validate.

use proptest::prelude::*;
use rstest::rstest;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};
use tempfile::TempDir;

const REINHARDT_ADMIN: &str = env!("CARGO_BIN_EXE_reinhardt-admin");

fn run_migrate(root: &Path, args: &[&str]) -> Output {
	Command::new(REINHARDT_ADMIN)
		.arg("migrate-manouche-v2")
		.args(args)
		.arg(root)
		.output()
		.expect("failed to spawn reinhardt-admin migrate-manouche-v2")
}

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

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

fn compact_ws(input: &str) -> String {
	input.chars().filter(|c| !c.is_whitespace()).collect()
}

fn assert_success(output: &Output) {
	assert!(
		output.status.success(),
		"command failed with exit {:?}\nstdout:\n{}\nstderr:\n{}",
		output.status.code(),
		stdout(output),
		stderr(output)
	);
}

fn assert_failure(output: &Output) {
	assert!(
		!output.status.success(),
		"command unexpectedly succeeded\nstdout:\n{}\nstderr:\n{}",
		stdout(output),
		stderr(output)
	);
}

fn write(path: &Path, content: &str) {
	if let Some(parent) = path.parent() {
		fs::create_dir_all(parent).expect("create parent dir");
	}
	fs::write(path, content).expect("write fixture");
}

fn generated_control_flow_page() -> impl Strategy<Value = String> {
	let match_body = prop_oneof![
		Just("match value { item => div { title } }"),
		Just("match value { Some(entry) => div { title }, None => span { fallback } }"),
		Just(
			"match result { Ok(entry) if ready => div { title }, Err(error_name) => span { fallback } }"
		),
		Just(
			"match value { Some(entry) => if (ready) { h1 { title } } else { p { fallback } }, None => span { fallback } }"
		),
	];

	(
		match_body,
		prop::sample::select(vec![
			"for item in items { Row { title } }",
			"while (ready) { span { title } break; }",
			"loop { span { fallback } break; }",
		]),
	)
		.prop_map(|(match_body, loop_body)| {
			format!(
				r#"
fn view(
    value: Option<String>,
    result: Result<String, String>,
    ready: bool,
    title: String,
    fallback: String,
    items: Vec<String>,
) {{
    page! {{
        section {{
            let local_value = fallback;
            {match_body}
            {loop_body}
        }}
    }};
}}
"#
			)
		})
}

#[rstest]
fn rewrites_multiple_files_and_reports_changed_count() {
	let tmp = TempDir::new().expect("tempdir");
	let page_file = tmp.path().join("src/page.rs");
	let props_file = tmp.path().join("src/components.rs");

	write(
		&page_file,
		r#"
fn view(name: String, count: usize) {
    page! {
        div {
            name
            watch {
                span { count }
            }
        }
    };
    use_effect(move || {
        let _ = count;
    });
}
"#,
	);
	write(
		&props_file,
		r#"
#[derive(Clone, Default)]
pub struct CardProps {
    pub title: String,
    pub subtitle: Option<String>,
    pub count: usize,
}
"#,
	);

	let output = run_migrate(tmp.path(), &[]);
	assert_success(&output);

	let page = fs::read_to_string(&page_file).expect("read page file");
	let props = fs::read_to_string(&props_file).expect("read props file");
	assert!(
		page.contains("{ name }") || page.contains("{name}"),
		"bare identifier was not wrapped:\n{page}"
	);
	assert!(
		!page.contains("watch"),
		"watch wrapper should be removed:\n{page}"
	);
	assert!(
		page.contains("compile_error!"),
		"use_effect placeholder deps were not inserted:\n{page}"
	);
	assert!(
		props.contains("bon::Builder"),
		"Props derive was not migrated:\n{props}"
	);
	assert!(
		props.contains("#[builder(default)]"),
		"default builder attributes were not inserted:\n{props}"
	);
	assert!(
		stdout(&output).contains("Done. 2 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&output)
	);
}

#[rstest]
fn preserves_inner_module_docs_when_rewriting_first_item() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("page.rs");
	write(
		&file,
		r#"//! Module docs stay outside the rewritten item.
fn view(title: String) {
    page! { div { title } };
}
"#,
	);

	let output = run_migrate(tmp.path(), &[]);
	assert_success(&output);

	let after = fs::read_to_string(&file).expect("read rewritten file");
	assert!(
		after.starts_with("//! Module docs stay outside the rewritten item."),
		"inner module docs should not be replaced with the first item:\n{after}"
	);
	assert!(
		compact_ws(&after).contains("{title}"),
		"first item should still be migrated:\n{after}"
	);
}

#[rstest]
fn dry_run_reports_changes_without_writing_files() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("page.rs");
	let original = r#"
fn view(name: String) {
    page! { div { name } };
}
"#;
	write(&file, original);

	let output = run_migrate(tmp.path(), &["--dry-run"]);
	assert_success(&output);

	let after = fs::read_to_string(&file).expect("read file after dry-run");
	assert_eq!(after, original, "dry-run must not rewrite files");
	assert!(
		stdout(&output).contains("would rewrite:"),
		"dry-run should list pending rewrite:\n{}",
		stdout(&output)
	);
	assert!(
		stdout(&output).contains("Done. 1 file(s) would change."),
		"unexpected stdout:\n{}",
		stdout(&output)
	);
}

#[rstest]
fn skip_rule_leaves_that_rule_unapplied_but_runs_others() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("page.rs");
	write(
		&file,
		r#"
fn view(name: String, count: usize) {
    page! {
        div {
            name
            watch { span { count } }
        }
    };
}
"#,
	);

	let output = run_migrate(tmp.path(), &["--skip", "bare_ident"]);
	assert_success(&output);

	let after = fs::read_to_string(&file).expect("read rewritten file");
	assert!(
		after.contains("name"),
		"source should still contain the bare identifier:\n{after}"
	);
	assert!(
		!after.contains("{ name }") && !after.contains("{name}"),
		"bare_ident should have been skipped:\n{after}"
	);
	assert!(
		!after.contains("watch"),
		"watch_unwrap should still run when bare_ident is skipped:\n{after}"
	);
}

#[rstest]
fn rerunning_on_migrated_tree_is_idempotent() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("page.rs");
	write(
		&file,
		r#"
fn view(name: String) {
    page! { div { name } };
}
"#,
	);

	let first = run_migrate(tmp.path(), &[]);
	assert_success(&first);
	let after_first = fs::read_to_string(&file).expect("read after first run");

	let second = run_migrate(tmp.path(), &[]);
	assert_success(&second);
	let after_second = fs::read_to_string(&file).expect("read after second run");

	assert_eq!(after_second, after_first, "second run changed the file");
	assert!(
		stdout(&second).contains("Done. 0 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&second)
	);
}

#[rstest]
fn complex_page_syntax_rewrites_nested_control_flow_without_rewrapping_expression_slots() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("complex_page.rs");
	write(
		&file,
		r#"
fn view(
    ready: bool,
    title: String,
    fallback_name: String,
    already_wrapped: String,
    items: Vec<Item>,
    count: usize,
) {
    page! {
        section {
            { already_wrapped }
            if (ready) {
                h1 { title }
            } else {
                p { fallback_name }
            }
            for item in items {
                Row {
                    item_name
                    span { count }
                }
            }
            loop {
                span { count }
                break;
            }
        }
    };
}
"#,
	);

	let first = run_migrate(tmp.path(), &[]);
	assert_success(&first);
	let after_first = fs::read_to_string(&file).expect("read after first run");
	let compact = compact_ws(&after_first);

	assert!(
		compact.contains("{already_wrapped}"),
		"existing expression slot should remain a single expression slot:\n{after_first}"
	);
	assert!(
		compact.contains("{title}"),
		"bare identifier inside parenthesized if body was not wrapped:\n{after_first}"
	);
	assert!(
		compact.contains("{fallback_name}"),
		"bare identifier inside else body was not wrapped:\n{after_first}"
	);
	assert!(
		compact.contains("{item_name}"),
		"bare identifier inside component body was not wrapped:\n{after_first}"
	);
	assert!(
		!after_first.contains("for { item } in"),
		"for-loop pattern variable should not be treated as a page child:\n{after_first}"
	);
	assert!(
		compact.contains("{count}"),
		"bare identifier inside loop or nested element body was not wrapped:\n{after_first}"
	);
	assert!(
		after_first.contains("break"),
		"reserved control-flow keyword should not be wrapped:\n{after_first}"
	);

	let second = run_migrate(tmp.path(), &[]);
	assert_success(&second);
	let after_second = fs::read_to_string(&file).expect("read after second run");

	assert_eq!(
		after_second, after_first,
		"complex migrated page changed on rerun"
	);
	assert!(
		stdout(&second).contains("Done. 0 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&second)
	);
}

#[rstest]
fn match_patterns_and_let_statements_are_not_wrapped_as_page_children() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("match_and_let.rs");
	write(
		&file,
		r#"
fn view(value: Option<String>, fallback: String, title: String) {
    page! {
        section {
            match value {
                item => div { fallback }
            }
            let local = fallback;
            let child = div { title };
            child
        }
    };
}
"#,
	);

	let first = run_migrate(tmp.path(), &[]);
	assert_success(&first);
	let after_first = fs::read_to_string(&file).expect("read after first run");
	let compact = compact_ws(&after_first);

	assert!(
		!compact.contains("{{item}}=>") && !compact.contains("{item}=>"),
		"match arm pattern should not be wrapped:\n{after_first}"
	);
	assert!(
		!compact.contains("let{local}="),
		"let binding pattern should not be wrapped:\n{after_first}"
	);
	assert!(
		!compact.contains("let{child}="),
		"let binding with element initializer should not be wrapped:\n{after_first}"
	);
	assert!(
		compact.contains("div{{fallback}}") || compact.contains("div{{{fallback}}}"),
		"match arm page body should still be migrated:\n{after_first}"
	);
	assert!(
		compact.contains("div{{title}}"),
		"let initializer element body should still be migrated:\n{after_first}"
	);
	assert!(
		compact.contains("{child}"),
		"page child following let statement should still be migrated:\n{after_first}"
	);

	let second = run_migrate(tmp.path(), &[]);
	assert_success(&second);
	let after_second = fs::read_to_string(&file).expect("read after second run");
	assert_eq!(
		after_second, after_first,
		"match and let migration changed on rerun"
	);
}

proptest! {
	#![proptest_config(ProptestConfig::with_cases(24))]

	#[test]
	fn fuzz_migrate_page_control_syntax_preserves_rust_patterns_and_is_idempotent(
		source in generated_control_flow_page(),
	) {
		let tmp = TempDir::new().expect("tempdir");
		let file = tmp.path().join("fuzz_page.rs");
		write(&file, &source);

		let first = run_migrate(tmp.path(), &[]);
		prop_assert!(
			first.status.success(),
			"command failed with exit {:?}\nstdout:\n{}\nstderr:\n{}\nsource:\n{}",
			first.status.code(),
			stdout(&first),
			stderr(&first),
			source
		);
		let after_first = fs::read_to_string(&file).expect("read after first run");
		let compact = compact_ws(&after_first);

		prop_assert!(
			!compact.contains("let{local_value}="),
			"let binding was wrapped as a page child:\n{after_first}"
		);
		for invalid in [
			"{item}=>",
			"{entry}=>",
			"{entry}ifready=>",
			"{ready}=>",
			"{error_name}=>",
			"for{item}in",
		] {
			prop_assert!(
				!compact.contains(invalid),
				"Rust pattern/control syntax was wrapped as a page child ({invalid}):\n{after_first}"
			);
		}
		prop_assert!(
			compact.contains("{title}") || compact.contains("{fallback}"),
			"generated page children were not migrated:\n{after_first}"
		);

		let second = run_migrate(tmp.path(), &[]);
		prop_assert!(
			second.status.success(),
			"second command failed with exit {:?}\nstdout:\n{}\nstderr:\n{}\nsource:\n{}",
			second.status.code(),
			stdout(&second),
			stderr(&second),
			source
		);
		let after_second = fs::read_to_string(&file).expect("read after second run");
		prop_assert_eq!(
			after_second,
			after_first,
			"fuzz-generated page changed on rerun"
		);
	}
}

#[rstest]
fn complex_props_migration_preserves_non_default_derives_and_existing_builder_defaults() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("props.rs");
	write(
		&file,
		r#"
#[repr(C)]
#[derive(Clone)]
#[derive(Debug, Default, PartialEq)]
pub struct PanelProps {
    pub id: String,
    #[builder(default)]
    pub existing: Option<String>,
    pub count: usize,
    pub subtitle: Option<String>,
}
"#,
	);

	let first = run_migrate(tmp.path(), &[]);
	assert_success(&first);
	let after_first = fs::read_to_string(&file).expect("read after first run");

	assert!(
		after_first.contains("#[repr(C)]"),
		"non-derive attributes should be preserved:\n{after_first}"
	);
	assert!(
		after_first.contains("Clone")
			&& after_first.contains("Debug")
			&& after_first.contains("PartialEq")
			&& after_first.contains("bon::Builder"),
		"derive list should preserve non-Default derives and add bon::Builder:\n{after_first}"
	);
	assert!(
		!after_first.contains("Default"),
		"Default derive should be removed:\n{after_first}"
	);
	let builder_default_count = after_first.matches("#[builder(default)]").count();
	assert_eq!(
		builder_default_count, 3,
		"expected exactly one builder default for existing, count, and subtitle:\n{after_first}"
	);

	let second = run_migrate(tmp.path(), &[]);
	assert_success(&second);
	let after_second = fs::read_to_string(&file).expect("read after second run");

	assert_eq!(
		after_second, after_first,
		"complex Props migration changed on rerun"
	);
	assert!(
		stdout(&second).contains("Done. 0 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&second)
	);
}

#[rstest]
fn unknown_skip_rule_fails() {
	let tmp = TempDir::new().expect("tempdir");
	write(&tmp.path().join("page.rs"), "fn view() {}\n");

	let output = run_migrate(tmp.path(), &["--skip", "not_a_rule"]);
	assert_failure(&output);
	assert!(
		stderr(&output).contains("unknown --skip rule(s): not_a_rule"),
		"unexpected stderr:\n{}",
		stderr(&output)
	);
}

#[rstest]
fn missing_path_fails() {
	let tmp = TempDir::new().expect("tempdir");
	let missing = tmp.path().join("does-not-exist");

	let output = run_migrate(&missing, &[]);
	assert_failure(&output);
	assert!(
		stderr(&output).contains("No such file")
			|| stderr(&output).contains("does-not-exist")
			|| stderr(&output).contains("not found"),
		"unexpected stderr:\n{}",
		stderr(&output)
	);
}

#[rstest]
fn invalid_rust_file_is_skipped_while_valid_files_are_rewritten() {
	let tmp = TempDir::new().expect("tempdir");
	let valid = tmp.path().join("valid.rs");
	let invalid = tmp.path().join("invalid.rs");
	let invalid_source = "fn broken(\n";
	write(
		&valid,
		r#"
fn view(name: String) {
    page! { div { name } };
}
"#,
	);
	write(&invalid, invalid_source);

	let output = run_migrate(tmp.path(), &[]);
	assert_success(&output);

	let valid_after = fs::read_to_string(&valid).expect("read valid file");
	let invalid_after = fs::read_to_string(&invalid).expect("read invalid file");
	assert!(
		valid_after.contains("{ name }") || valid_after.contains("{name}"),
		"valid file was not rewritten:\n{valid_after}"
	);
	assert_eq!(
		invalid_after, invalid_source,
		"invalid Rust file should be skipped unchanged"
	);
	assert!(
		stdout(&output).contains("Done. 1 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&output)
	);
}

#[rstest]
fn skips_target_and_hidden_directories() {
	let tmp = TempDir::new().expect("tempdir");
	let source = tmp.path().join("src/page.rs");
	let target = tmp.path().join("target/generated.rs");
	let hidden = tmp.path().join(".git/ignored.rs");
	let body = r#"
fn view(name: String) {
    page! { div { name } };
}
"#;
	write(&source, body);
	write(&target, body);
	write(&hidden, body);

	let output = run_migrate(tmp.path(), &[]);
	assert_success(&output);

	let source_after = fs::read_to_string(&source).expect("read source file");
	let target_after = fs::read_to_string(&target).expect("read target file");
	let hidden_after = fs::read_to_string(&hidden).expect("read hidden file");
	assert!(
		source_after.contains("{ name }") || source_after.contains("{name}"),
		"source file should be rewritten:\n{source_after}"
	);
	assert_eq!(target_after, body, "target/ file should be skipped");
	assert_eq!(
		hidden_after, body,
		"hidden directory file should be skipped"
	);
	assert!(
		stdout(&output).contains("Done. 1 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&output)
	);
}

#[rstest]
fn single_rs_file_path_is_rewritten() {
	let tmp = TempDir::new().expect("tempdir");
	let file = tmp.path().join("single.rs");
	write(
		&file,
		r#"
fn view(name: String) {
    page! { div { name } };
}
"#,
	);

	let output = run_migrate(&file, &[]);
	assert_success(&output);

	let after = fs::read_to_string(&file).expect("read rewritten file");
	assert!(
		after.contains("{ name }") || after.contains("{name}"),
		"single file path was not rewritten:\n{after}"
	);
	assert!(
		stdout(&output).contains("Done. 1 file(s) changed."),
		"unexpected stdout:\n{}",
		stdout(&output)
	);
}