homeboy 0.70.0

CLI for multi-component deployment and development workflow automation
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//! Pattern-based code transforms — regex find/replace across a codebase.
//!
//! Applies named transform sets (collections of find/replace rules) to files
//! matching glob patterns. Rules are defined in `homeboy.json` under the
//! `transforms` key, or passed ad-hoc via CLI flags.
//!
//! Phase 1: line-context regex transforms (no AST, no extension scripts).

use glob_match::glob_match;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::error::{Error, Result};
use crate::utils::io;

// ============================================================================
// Rule model
// ============================================================================

/// A named collection of transform rules.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformSet {
    /// Human-readable description of this transform set.
    #[serde(default)]
    pub description: String,
    /// Ordered list of rules to apply.
    pub rules: Vec<TransformRule>,
}

/// A single find/replace rule with a file glob filter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformRule {
    /// Unique identifier within the set.
    pub id: String,
    /// Human-readable description.
    #[serde(default)]
    pub description: String,
    /// Regex pattern to find (supports capture groups).
    pub find: String,
    /// Replacement template (supports `$1`, `$2`, `${name}` capture group refs).
    pub replace: String,
    /// Glob pattern for files to apply to (e.g., `tests/**/*.php`).
    #[serde(default = "default_files_glob")]
    pub files: String,
    /// Match context: "line" (default) or "file" (whole-file regex, for multi-line).
    #[serde(default = "default_context")]
    pub context: String,
}

fn default_files_glob() -> String {
    "**/*".to_string()
}

fn default_context() -> String {
    "line".to_string()
}

// ============================================================================
// Output model
// ============================================================================

/// Result of applying a transform set.
#[derive(Debug, Clone, Serialize)]
pub struct TransformResult {
    /// Name of the transform set (or "ad-hoc" for CLI-provided rules).
    pub name: String,
    /// Per-rule results.
    pub rules: Vec<RuleResult>,
    /// Total replacements across all rules.
    pub total_replacements: usize,
    /// Total files modified.
    pub total_files: usize,
    /// Whether changes were written to disk.
    pub written: bool,
}

/// Result for a single rule.
#[derive(Debug, Clone, Serialize)]
pub struct RuleResult {
    /// Rule ID.
    pub id: String,
    /// Rule description.
    pub description: String,
    /// Matches found.
    pub matches: Vec<TransformMatch>,
    /// Number of replacements.
    pub replacement_count: usize,
}

/// A single match/replacement within a file.
#[derive(Debug, Clone, Serialize)]
pub struct TransformMatch {
    /// File path relative to component root.
    pub file: String,
    /// Line number (1-indexed). For file-context, this is the first line of the match.
    pub line: usize,
    /// Original text that matched.
    pub before: String,
    /// Replacement text.
    pub after: String,
}

// ============================================================================
// Rule loading
// ============================================================================

const HOMEBOY_JSON: &str = "homeboy.json";
const TRANSFORMS_KEY: &str = "transforms";

/// Load a named transform set from `homeboy.json` in the given root directory.
pub fn load_transform_set(root: &Path, name: &str) -> Result<TransformSet> {
    let json_path = root.join(HOMEBOY_JSON);
    if !json_path.exists() {
        return Err(Error::internal_io(
            format!("No homeboy.json found at {}", json_path.display()),
            Some("transform.load".to_string()),
        ));
    }

    let content = io::read_file(&json_path, "read homeboy.json")?;
    let data: serde_json::Value = serde_json::from_str(&content).map_err(|e| {
        Error::internal_io(
            format!("Failed to parse homeboy.json: {}", e),
            Some("transform.load".to_string()),
        )
    })?;

    let transforms = data.get(TRANSFORMS_KEY).ok_or_else(|| {
        Error::config_missing_key(
            TRANSFORMS_KEY.to_string(),
            Some(json_path.to_string_lossy().to_string()),
        )
    })?;

    let set_value = transforms.get(name).ok_or_else(|| {
        // List available transforms for a helpful error
        let available: Vec<&str> = transforms
            .as_object()
            .map(|o| o.keys().map(|k| k.as_str()).collect::<Vec<_>>())
            .unwrap_or_default();
        Error::internal_io(
            format!(
                "Transform set '{}' not found. Available: {:?}",
                name, available
            ),
            Some("transform.load".to_string()),
        )
    })?;

    serde_json::from_value(set_value.clone()).map_err(|e| {
        Error::internal_io(
            format!("Failed to parse transform set '{}': {}", name, e),
            Some("transform.load".to_string()),
        )
    })
}

/// Create a transform set from ad-hoc CLI arguments.
pub fn ad_hoc_transform(find: &str, replace: &str, files: &str) -> TransformSet {
    TransformSet {
        description: "Ad-hoc transform".to_string(),
        rules: vec![TransformRule {
            id: "ad-hoc".to_string(),
            description: String::new(),
            find: find.to_string(),
            replace: replace.to_string(),
            files: files.to_string(),
            context: "line".to_string(),
        }],
    }
}

// ============================================================================
// Transform engine
// ============================================================================

/// Apply a transform set to a codebase rooted at `root`.
///
/// If `write` is true, modified files are written to disk.
/// If `rule_filter` is Some, only the rule with that ID is applied.
pub fn apply_transforms(
    root: &Path,
    name: &str,
    set: &TransformSet,
    write: bool,
    rule_filter: Option<&str>,
) -> Result<TransformResult> {
    // Compile all regexes up front
    let compiled_rules: Vec<(&TransformRule, Regex)> = set
        .rules
        .iter()
        .filter(|r| rule_filter.is_none_or(|f| r.id == f))
        .map(|r| {
            let regex = Regex::new(&r.find).map_err(|e| {
                Error::internal_io(
                    format!("Invalid regex in rule '{}': {}", r.id, e),
                    Some("transform.apply".to_string()),
                )
            })?;
            Ok((r, regex))
        })
        .collect::<Result<Vec<_>>>()?;

    if compiled_rules.is_empty() {
        if let Some(filter) = rule_filter {
            let available: Vec<&str> = set.rules.iter().map(|r| r.id.as_str()).collect();
            return Err(Error::internal_io(
                format!(
                    "Rule '{}' not found in transform set '{}'. Available: {:?}",
                    filter, name, available
                ),
                Some("transform.apply".to_string()),
            ));
        }
    }

    // Walk all files once
    let files = walk_source_files(root);

    // Apply each rule
    let mut rule_results = Vec::new();
    // Track cumulative edits per file: file_path → final content
    let mut file_edits: HashMap<PathBuf, String> = HashMap::new();

    for (rule, regex) in &compiled_rules {
        let matching_files: Vec<&PathBuf> = files
            .iter()
            .filter(|f| {
                let rel = f.strip_prefix(root).unwrap_or(f);
                let rel_str = rel.to_string_lossy();
                // Normalize backslashes for Windows compat
                let normalized = rel_str.replace('\\', "/");
                glob_match(&rule.files, &normalized)
            })
            .collect();

        let mut matches = Vec::new();

        for file_path in matching_files {
            // Read from accumulated edits or original file
            let content = if let Some(edited) = file_edits.get(file_path) {
                edited.clone()
            } else {
                match std::fs::read_to_string(file_path) {
                    Ok(c) => c,
                    Err(_) => continue,
                }
            };

            let relative = file_path
                .strip_prefix(root)
                .unwrap_or(file_path)
                .to_string_lossy()
                .to_string();

            let (new_content, file_matches) = if rule.context == "file" {
                apply_file_context(regex, &rule.replace, &content, &relative)
            } else {
                apply_line_context(regex, &rule.replace, &content, &relative)
            };

            if !file_matches.is_empty() {
                matches.extend(file_matches);
                file_edits.insert(file_path.clone(), new_content);
            }
        }

        let replacement_count = matches.len();
        rule_results.push(RuleResult {
            id: rule.id.clone(),
            description: rule.description.clone(),
            matches,
            replacement_count,
        });
    }

    // Calculate totals
    let total_replacements: usize = rule_results.iter().map(|r| r.replacement_count).sum();
    let total_files = file_edits.len();

    // Write if requested
    if write && !file_edits.is_empty() {
        for (path, content) in &file_edits {
            io::write_file(path, content, "write transformed file")?;
        }
    }

    Ok(TransformResult {
        name: name.to_string(),
        rules: rule_results,
        total_replacements,
        total_files,
        written: write,
    })
}

// ============================================================================
// Context-specific application
// ============================================================================

/// Apply regex per line. Returns (new_content, matches).
fn apply_line_context(
    regex: &Regex,
    replace: &str,
    content: &str,
    relative_path: &str,
) -> (String, Vec<TransformMatch>) {
    let mut matches = Vec::new();
    let mut new_lines = Vec::new();

    for (i, line) in content.lines().enumerate() {
        if regex.is_match(line) {
            let replaced = regex.replace_all(line, replace).to_string();
            if replaced != line {
                matches.push(TransformMatch {
                    file: relative_path.to_string(),
                    line: i + 1,
                    before: line.to_string(),
                    after: replaced.clone(),
                });
                new_lines.push(replaced);
                continue;
            }
        }
        new_lines.push(line.to_string());
    }

    // Preserve trailing newline
    let mut result = new_lines.join("\n");
    if content.ends_with('\n') {
        result.push('\n');
    }

    (result, matches)
}

/// Apply regex to entire file content. Returns (new_content, matches).
fn apply_file_context(
    regex: &Regex,
    replace: &str,
    content: &str,
    relative_path: &str,
) -> (String, Vec<TransformMatch>) {
    let mut matches = Vec::new();

    // Find all matches before replacing (for reporting)
    for cap in regex.find_iter(content) {
        let before_text = &content[..cap.start()];
        let line_num = before_text.chars().filter(|&c| c == '\n').count() + 1;
        let matched = cap.as_str().to_string();
        let replaced = regex.replace(cap.as_str(), replace).to_string();

        if matched != replaced {
            matches.push(TransformMatch {
                file: relative_path.to_string(),
                line: line_num,
                before: matched,
                after: replaced,
            });
        }
    }

    let new_content = regex.replace_all(content, replace).to_string();
    (new_content, matches)
}

// ============================================================================
// File walking
// ============================================================================

const ALWAYS_SKIP_DIRS: &[&str] = &["node_modules", "vendor", ".git", ".svn", ".hg"];
const ROOT_ONLY_SKIP_DIRS: &[&str] = &["build", "dist", "target", "cache", "tmp"];

/// Walk all files in a directory tree (excluding VCS/dependency dirs).
fn walk_source_files(root: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    walk_recursive(root, root, &mut files);
    files
}

fn walk_recursive(dir: &Path, root: &Path, files: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };

    let is_root = dir == root;

    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            let name = path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_default();
            if ALWAYS_SKIP_DIRS.contains(&name.as_str()) {
                continue;
            }
            if is_root && ROOT_ONLY_SKIP_DIRS.contains(&name.as_str()) {
                continue;
            }
            walk_recursive(&path, root, files);
        } else if path.is_file() {
            // No extension filter — glob pattern handles file selection
            files.push(path);
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    // --- Rule model tests ---

    #[test]
    fn deserialize_transform_set() {
        let json = r#"{
            "description": "Test migration",
            "rules": [
                {
                    "id": "fix_code",
                    "find": "old_function",
                    "replace": "new_function",
                    "files": "**/*.php"
                }
            ]
        }"#;
        let set: TransformSet = serde_json::from_str(json).unwrap();
        assert_eq!(set.rules.len(), 1);
        assert_eq!(set.rules[0].id, "fix_code");
        assert_eq!(set.rules[0].context, "line"); // default
    }

    #[test]
    fn deserialize_rule_defaults() {
        let json = r#"{"id": "x", "find": "a", "replace": "b"}"#;
        let rule: TransformRule = serde_json::from_str(json).unwrap();
        assert_eq!(rule.files, "**/*");
        assert_eq!(rule.context, "line");
        assert_eq!(rule.description, "");
    }

    #[test]
    fn ad_hoc_creates_single_rule_set() {
        let set = ad_hoc_transform("foo", "bar", "**/*.rs");
        assert_eq!(set.rules.len(), 1);
        assert_eq!(set.rules[0].id, "ad-hoc");
        assert_eq!(set.rules[0].find, "foo");
        assert_eq!(set.rules[0].replace, "bar");
        assert_eq!(set.rules[0].files, "**/*.rs");
    }

    // --- Line context tests ---

    #[test]
    fn line_context_simple_replace() {
        let regex = Regex::new("rest_forbidden").unwrap();
        let content = "if ($code === 'rest_forbidden') {\n    return false;\n}\n";
        let (new, matches) =
            apply_line_context(&regex, "ability_invalid_permissions", content, "test.php");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].line, 1);
        assert_eq!(matches[0].before, "if ($code === 'rest_forbidden') {");
        assert_eq!(
            matches[0].after,
            "if ($code === 'ability_invalid_permissions') {"
        );
        assert!(new.contains("ability_invalid_permissions"));
        assert!(!new.contains("rest_forbidden"));
    }

    #[test]
    fn line_context_with_capture_groups() {
        let regex = Regex::new(r"\$this->assertIsArray\((.+?)\)").unwrap();
        let content = "$this->assertIsArray($result);\n$this->assertIsArray($other);\n";
        let (new, matches) = apply_line_context(
            &regex,
            "$$this->assertInstanceOf(WP_Error::class, $1)",
            content,
            "test.php",
        );
        assert_eq!(matches.len(), 2);
        assert!(new.contains("assertInstanceOf(WP_Error::class, $result)"));
        assert!(new.contains("assertInstanceOf(WP_Error::class, $other)"));
    }

    #[test]
    fn line_context_no_match_unchanged() {
        let regex = Regex::new("xyz_not_found").unwrap();
        let content = "some normal code\nmore code\n";
        let (new, matches) = apply_line_context(&regex, "replaced", content, "test.php");
        assert!(matches.is_empty());
        assert_eq!(new, content);
    }

    #[test]
    fn line_context_preserves_trailing_newline() {
        let regex = Regex::new("old").unwrap();
        let content = "old\n";
        let (new, _) = apply_line_context(&regex, "new", content, "f.txt");
        assert!(new.ends_with('\n'));
        assert_eq!(new, "new\n");
    }

    #[test]
    fn line_context_no_trailing_newline() {
        let regex = Regex::new("old").unwrap();
        let content = "old";
        let (new, _) = apply_line_context(&regex, "new", content, "f.txt");
        assert!(!new.ends_with('\n'));
        assert_eq!(new, "new");
    }

    // --- File context tests ---

    #[test]
    fn file_context_multiline_match() {
        let regex = Regex::new(r"(?s)function\s+old_name\(\).*?\}").unwrap();
        let content = "function old_name() {\n    return 1;\n}\n";
        let (new, matches) = apply_file_context(
            &regex,
            "function new_name() {\n    return 2;\n}",
            content,
            "test.php",
        );
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].line, 1);
        assert!(new.contains("new_name"));
    }

    // --- Glob matching tests ---

    #[test]
    fn glob_matches_php_test_files() {
        assert!(glob_match("tests/**/*.php", "tests/Unit/FooTest.php"));
        assert!(glob_match("tests/**/*.php", "tests/FooTest.php"));
        assert!(!glob_match("tests/**/*.php", "src/Foo.php"));
    }

    #[test]
    fn glob_matches_all_files() {
        assert!(glob_match("**/*", "any/path/file.rs"));
        assert!(glob_match("**/*.php", "deep/nested/path/file.php"));
    }

    // --- Integration: apply_transforms with temp dir ---

    #[test]
    fn apply_transforms_dry_run() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        // Create test files
        let tests_dir = root.join("tests");
        fs::create_dir_all(&tests_dir).unwrap();
        fs::write(
            tests_dir.join("FooTest.php"),
            "<?php\n$this->assertIsArray($result);\n$code = 'rest_forbidden';\n",
        )
        .unwrap();
        fs::write(root.join("src.php"), "<?php\n$code = 'rest_forbidden';\n").unwrap();

        let set = TransformSet {
            description: "test".into(),
            rules: vec![TransformRule {
                id: "fix_code".into(),
                description: "Fix error code".into(),
                find: "rest_forbidden".into(),
                replace: "ability_invalid_permissions".into(),
                files: "tests/**/*.php".into(),
                context: "line".into(),
            }],
        };

        let result = apply_transforms(root, "test", &set, false, None).unwrap();

        // Should match only the test file, not src.php
        assert_eq!(result.total_replacements, 1);
        assert_eq!(result.total_files, 1);
        assert!(!result.written);

        // File should be unchanged (dry-run)
        let content = fs::read_to_string(tests_dir.join("FooTest.php")).unwrap();
        assert!(content.contains("rest_forbidden"));
    }

    #[test]
    fn apply_transforms_write_mode() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let tests_dir = root.join("tests");
        fs::create_dir_all(&tests_dir).unwrap();
        fs::write(
            tests_dir.join("FooTest.php"),
            "<?php\n$code = 'rest_forbidden';\n",
        )
        .unwrap();

        let set = TransformSet {
            description: "test".into(),
            rules: vec![TransformRule {
                id: "fix".into(),
                description: String::new(),
                find: "rest_forbidden".into(),
                replace: "ability_invalid_permissions".into(),
                files: "tests/**/*.php".into(),
                context: "line".into(),
            }],
        };

        let result = apply_transforms(root, "test", &set, true, None).unwrap();

        assert_eq!(result.total_replacements, 1);
        assert!(result.written);

        // File should be changed
        let content = fs::read_to_string(tests_dir.join("FooTest.php")).unwrap();
        assert!(content.contains("ability_invalid_permissions"));
        assert!(!content.contains("rest_forbidden"));
    }

    #[test]
    fn apply_transforms_rule_filter() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        fs::write(root.join("test.php"), "aaa\nbbb\n").unwrap();

        let set = TransformSet {
            description: "test".into(),
            rules: vec![
                TransformRule {
                    id: "rule_a".into(),
                    description: String::new(),
                    find: "aaa".into(),
                    replace: "AAA".into(),
                    files: "**/*".into(),
                    context: "line".into(),
                },
                TransformRule {
                    id: "rule_b".into(),
                    description: String::new(),
                    find: "bbb".into(),
                    replace: "BBB".into(),
                    files: "**/*".into(),
                    context: "line".into(),
                },
            ],
        };

        // Only apply rule_a
        let result = apply_transforms(root, "test", &set, false, Some("rule_a")).unwrap();
        assert_eq!(result.rules.len(), 1);
        assert_eq!(result.rules[0].id, "rule_a");
        assert_eq!(result.total_replacements, 1);
    }

    #[test]
    fn apply_transforms_multiple_rules_same_file() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        fs::write(root.join("test.php"), "old_a and old_b\n").unwrap();

        let set = TransformSet {
            description: "test".into(),
            rules: vec![
                TransformRule {
                    id: "a".into(),
                    description: String::new(),
                    find: "old_a".into(),
                    replace: "new_a".into(),
                    files: "**/*".into(),
                    context: "line".into(),
                },
                TransformRule {
                    id: "b".into(),
                    description: String::new(),
                    find: "old_b".into(),
                    replace: "new_b".into(),
                    files: "**/*".into(),
                    context: "line".into(),
                },
            ],
        };

        let result = apply_transforms(root, "test", &set, true, None).unwrap();
        assert_eq!(result.total_replacements, 2);
        assert_eq!(result.total_files, 1); // Same file modified by both rules

        let content = fs::read_to_string(root.join("test.php")).unwrap();
        assert!(content.contains("new_a"));
        assert!(content.contains("new_b"));
        assert!(!content.contains("old_a"));
        assert!(!content.contains("old_b"));
    }

    #[test]
    fn apply_transforms_invalid_regex_errors() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let set = TransformSet {
            description: "test".into(),
            rules: vec![TransformRule {
                id: "bad".into(),
                description: String::new(),
                find: "[invalid regex".into(),
                replace: "x".into(),
                files: "**/*".into(),
                context: "line".into(),
            }],
        };

        let result = apply_transforms(root, "test", &set, false, None);
        assert!(result.is_err());
    }

    #[test]
    fn load_transform_set_from_json() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let homeboy_json = serde_json::json!({
            "transforms": {
                "my_migration": {
                    "description": "Test migration",
                    "rules": [
                        {
                            "id": "rule1",
                            "find": "old",
                            "replace": "new",
                            "files": "**/*.php"
                        }
                    ]
                }
            }
        });

        fs::write(
            root.join("homeboy.json"),
            serde_json::to_string_pretty(&homeboy_json).unwrap(),
        )
        .unwrap();

        let set = load_transform_set(root, "my_migration").unwrap();
        assert_eq!(set.description, "Test migration");
        assert_eq!(set.rules.len(), 1);
        assert_eq!(set.rules[0].id, "rule1");
    }

    #[test]
    fn load_transform_set_not_found_lists_available() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let homeboy_json = serde_json::json!({
            "transforms": {
                "exists": {
                    "description": "",
                    "rules": []
                }
            }
        });

        fs::write(
            root.join("homeboy.json"),
            serde_json::to_string_pretty(&homeboy_json).unwrap(),
        )
        .unwrap();

        let err = load_transform_set(root, "not_here").unwrap_err();
        let msg = format!("{:?}", err.details);
        assert!(msg.contains("not_here"));
        assert!(msg.contains("exists"));
    }
}