bendis 0.5.12

A patch tool for Bender to work better in HERIS project
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
use anyhow::{Context, Result};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::path::Path;

#[derive(Debug, Clone)]
enum Source {
    Git(String),
    Path(String),
}

impl Serialize for Source {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(1))?;
        match self {
            Source::Git(url) => map.serialize_entry("Git", url)?,
            Source::Path(path) => map.serialize_entry("Path", path)?,
        }
        map.end()
    }
}

impl<'de> Deserialize<'de> for Source {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let map = HashMap::<String, String>::deserialize(deserializer)?;

        if let Some(git_url) = map.get("Git") {
            Ok(Source::Git(git_url.clone()))
        } else if let Some(path) = map.get("Path") {
            Ok(Source::Path(path.clone()))
        } else {
            Err(serde::de::Error::custom("Expected 'Git' or 'Path' key in source"))
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct Package {
    revision: Option<String>,
    version: Option<String>,
    source: Source,
    dependencies: Vec<String>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct LockFile {
    packages: BTreeMap<String, Package>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(untagged)]
enum DependencySpec {
    Simple(String),
    Detailed(HashMap<String, serde_yaml::Value>),
}

#[derive(Debug, Deserialize, Serialize)]
struct BenderYml {
    dependencies: Option<HashMap<String, DependencySpec>>,
}

#[derive(Debug, Deserialize, Serialize)]
struct DotBenderYml {
    overrides: Option<HashMap<String, HashMap<String, serde_yaml::Value>>>,
}

/// Convert GitHub URL to IHEP internal Git URL
fn convert_url(git_url: &str) -> String {
    if git_url.contains("github.com/pulp-platform") {
        // Extract repository name
        if let Some(repo_name) = git_url.split('/').last() {
            return format!("git@code.ihep.ac.cn:heris/heris-platform/{}", repo_name);
        }
    }
    git_url.to_string()
}

/// Extract dependencies from lock file
fn extract_dependencies_from_lock(lock_data: &LockFile) -> HashMap<String, HashMap<String, serde_yaml::Value>> {
    let mut dependencies = HashMap::new();

    for (pkg_name, pkg_info) in &lock_data.packages {
        match &pkg_info.source {
            Source::Git(git_url) => {
                let converted_url = convert_url(git_url);
                let mut dep_info = HashMap::new();
                dep_info.insert("git".to_string(), serde_yaml::Value::String(converted_url));

                // Add version or revision
                if let Some(version) = &pkg_info.version {
                    dep_info.insert("version".to_string(), serde_yaml::to_value(version).unwrap());
                } else if let Some(revision) = &pkg_info.revision {
                    dep_info.insert("rev".to_string(), serde_yaml::Value::String(revision.clone()));
                }

                dependencies.insert(pkg_name.clone(), dep_info);
            }
            Source::Path(path) => {
                let mut dep_info = HashMap::new();
                dep_info.insert("path".to_string(), serde_yaml::Value::String(path.clone()));
                dependencies.insert(pkg_name.clone(), dep_info);
            }
        }
    }

    dependencies
}

/// Extract dependency names from Bender.yml
fn extract_dependencies_from_yml(yml_data: &BenderYml) -> HashSet<String> {
    if let Some(deps) = &yml_data.dependencies {
        deps.keys().cloned().collect()
    } else {
        HashSet::new()
    }
}

/// Extract existing overrides from .bender.yml
fn extract_overrides_from_bender_yml(bender_yml_data: &DotBenderYml) -> HashMap<String, HashMap<String, serde_yaml::Value>> {
    if let Some(overrides) = &bender_yml_data.overrides {
        overrides.clone()
    } else {
        HashMap::new()
    }
}

/// Compare two version strings
fn compare_versions(v1: &Option<String>, v2: &Option<String>) -> i8 {
    match (v1, v2) {
        (None, None) => 0,
        (None, Some(_)) => -1,
        (Some(_), None) => 1,
        (Some(v1_str), Some(v2_str)) => {
            let v1_parts: Vec<&str> = v1_str.split('.').collect();
            let v2_parts: Vec<&str> = v2_str.split('.').collect();

            let max_len = v1_parts.len().max(v2_parts.len());

            for i in 0..max_len {
                let p1 = v1_parts.get(i).and_then(|s| s.parse::<u32>().ok()).unwrap_or(0);
                let p2 = v2_parts.get(i).and_then(|s| s.parse::<u32>().ok()).unwrap_or(0);

                if p1 > p2 {
                    return 1;
                } else if p1 < p2 {
                    return -1;
                }
            }
            0
        }
    }
}

/// Find missing dependencies that need to be added to overrides
fn find_missing_dependencies(
    lock_deps: &HashMap<String, HashMap<String, serde_yaml::Value>>,
    yml_deps: &HashSet<String>,
    existing_overrides: &HashMap<String, HashMap<String, serde_yaml::Value>>,
) -> HashMap<String, HashMap<String, serde_yaml::Value>> {
    let mut missing_deps = HashMap::new();

    for (dep_name, dep_info) in lock_deps {
        // Skip if already in Bender.yml
        if yml_deps.contains(dep_name) {
            continue;
        }

        // Check if already in overrides
        if let Some(existing_info) = existing_overrides.get(dep_name) {
            // Both have git URLs, check versions
            if dep_info.contains_key("git") && existing_info.contains_key("git") {
                let new_version = dep_info.get("version").and_then(|v| {
                    if let serde_yaml::Value::String(s) = v {
                        Some(s.clone())
                    } else {
                        v.as_f64().map(|f| f.to_string())
                    }
                });

                let existing_version = existing_info.get("version").and_then(|v| {
                    if let serde_yaml::Value::String(s) = v {
                        Some(s.clone())
                    } else {
                        v.as_f64().map(|f| f.to_string())
                    }
                });

                // If new version is newer, update it
                if compare_versions(&new_version, &existing_version) > 0 {
                    missing_deps.insert(dep_name.clone(), dep_info.clone());
                }
            } else if dep_info.contains_key("git") && existing_info.contains_key("path") {
                // Prefer git over path
                missing_deps.insert(dep_name.clone(), dep_info.clone());
            }
        } else {
            // Not in overrides, add it
            missing_deps.insert(dep_name.clone(), dep_info.clone());
        }
    }

    missing_deps
}

/// Format a single dependency line in inline YAML style
fn format_dependency_line(
    name: &str,
    dep_info: &HashMap<String, serde_yaml::Value>,
    max_name_len: usize,
    max_url_len: usize,
) -> String {
    let name_padding = " ".repeat(max_name_len.saturating_sub(name.len()));

    if let Some(serde_yaml::Value::String(path)) = dep_info.get("path") {
        let url_part = format!(r#"{{ path: "{}" "#, path);
        let url_padding = " ".repeat(max_url_len.saturating_sub(url_part.len()));
        format!("  {}:{} {}{}}}", name, name_padding, url_part, url_padding)
    } else if let Some(serde_yaml::Value::String(git_url)) = dep_info.get("git") {
        if let Some(version) = dep_info.get("version") {
            let version_str = match version {
                serde_yaml::Value::String(s) => s.clone(),
                serde_yaml::Value::Number(n) => n.to_string(),
                _ => format!("{:?}", version),
            };
            let git_part = format!(r#"{{ git: "{}","#, git_url);
            let url_padding = " ".repeat(max_url_len.saturating_sub(git_part.len()));
            format!("  {}:{} {}{} version: {} }}", name, name_padding, git_part, url_padding, version_str)
        } else if let Some(serde_yaml::Value::String(rev)) = dep_info.get("rev") {
            let git_part = format!(r#"{{ git: "{}","#, git_url);
            let url_padding = " ".repeat(max_url_len.saturating_sub(git_part.len()));
            format!(r#"  {}:{} {}{} rev: "{}" }}"#, name, name_padding, git_part, url_padding, rev)
        } else {
            let git_part = format!(r#"{{ git: "{}"#, git_url);
            let url_padding = " ".repeat(max_url_len.saturating_sub(git_part.len()));
            format!("  {}:{} {}{} }}", name, name_padding, git_part, url_padding)
        }
    } else {
        format!("  {}:{} {{}}", name, name_padding)
    }
}

/// Generate formatted override lines
fn generate_overrides_lines(all_overrides: &HashMap<String, HashMap<String, serde_yaml::Value>>) -> Vec<String> {
    if all_overrides.is_empty() {
        return Vec::new();
    }

    // Calculate maximum name length
    let max_name_len = all_overrides.keys().map(|k| k.len()).max().unwrap_or(0);

    // Calculate maximum URL part length
    let mut max_url_len = 0;
    for dep_info in all_overrides.values() {
        let url_part_len = if let Some(serde_yaml::Value::String(path)) = dep_info.get("path") {
            format!(r#"{{ path: "{}" "#, path).len()
        } else if let Some(serde_yaml::Value::String(git_url)) = dep_info.get("git") {
            if dep_info.contains_key("version") || dep_info.contains_key("rev") {
                format!(r#"{{ git: "{}","#, git_url).len()
            } else {
                format!(r#"{{ git: "{}"#, git_url).len()
            }
        } else {
            0
        };
        max_url_len = max_url_len.max(url_part_len);
    }

    let mut lines = Vec::new();
    let mut names: Vec<&String> = all_overrides.keys().collect();
    names.sort_by(|left, right| {
        left.to_ascii_lowercase()
            .cmp(&right.to_ascii_lowercase())
            .then_with(|| left.cmp(right))
    });
    for name in names {
        let dep_info = &all_overrides[name];
        lines.push(format_dependency_line(name, dep_info, max_name_len, max_url_len));
    }

    lines
}

/// Update .bender.yml with new overrides
fn update_bender_yml_overrides(
    bender_yml_text: &str,
    existing_overrides: &HashMap<String, HashMap<String, serde_yaml::Value>>,
    new_overrides: &HashMap<String, HashMap<String, serde_yaml::Value>>,
) -> String {
    // Merge overrides
    let mut all_overrides = existing_overrides.clone();
    all_overrides.extend(new_overrides.clone());

    // Generate formatted override lines
    let override_lines = generate_overrides_lines(&all_overrides);

    if override_lines.is_empty() {
        return bender_yml_text.to_string();
    }

    // Build new overrides section
    let new_overrides_section = format!("overrides:\n{}", override_lines.join("\n"));

    // Replace overrides section - find "overrides:" and everything until the next top-level key or end
    // Split into lines and rebuild, replacing the overrides section
    let lines: Vec<&str> = bender_yml_text.lines().collect();
    let mut result = Vec::new();
    let mut in_overrides = false;
    let mut overrides_found = false;

    for line in lines {
        if line.starts_with("overrides:") {
            // Found the overrides section, replace it
            result.push(new_overrides_section.as_str());
            in_overrides = true;
            overrides_found = true;
        } else if in_overrides {
            // Check if this is a new top-level section (starts with a letter and ends with :)
            if !line.is_empty() && !line.starts_with(' ') && !line.starts_with('\t') {
                // New section, stop skipping
                in_overrides = false;
                result.push(line);
            }
            // Otherwise skip this line (it's part of the old overrides)
        } else {
            result.push(line);
        }
    }

    // If we didn't find an overrides section, append it at the end
    if !overrides_found {
        result.push("\n");
        result.push(new_overrides_section.as_str());
    }

    result.join("\n")
}

/// Main conversion function
pub fn convert(
    bendis_dir: &Path,
    root_dir: &Path,
) -> Result<bool> {
    // Read input files
    let lock_path = bendis_dir.join("Bender.lock");
    let yml_path = bendis_dir.join("Bender.yml");
    let bender_yml_path = bendis_dir.join(".bender.yml");

    let lock_content = fs::read_to_string(&lock_path)
        .context("Failed to read bendis_workspace/Bender.lock")?;
    let mut lock_data: LockFile = serde_yaml::from_str(&lock_content)
        .context("Failed to parse Bender.lock")?;
    normalize_lock_paths(&mut lock_data, bendis_dir, root_dir)?;
    let workspace_lock = serde_yaml::to_string(&lock_data)
        .context("Failed to serialize normalized bendis_workspace/Bender.lock")?;
    fs::write(&lock_path, workspace_lock)
        .context("Failed to write normalized bendis_workspace/Bender.lock")?;

    let yml_content = fs::read_to_string(&yml_path)
        .context("Failed to read bendis_workspace/Bender.yml")?;
    let yml_data: BenderYml = serde_yaml::from_str(&yml_content)
        .unwrap_or(BenderYml { dependencies: None });

    let bender_yml_text = fs::read_to_string(&bender_yml_path)
        .context("Failed to read bendis_workspace/.bender.yml")?;
    let bender_yml_data: DotBenderYml = serde_yaml::from_str(&bender_yml_text)
        .unwrap_or(DotBenderYml { overrides: None });

    // Extract dependencies
    let lock_deps = extract_dependencies_from_lock(&lock_data);
    let yml_deps = extract_dependencies_from_yml(&yml_data);
    let existing_overrides = extract_overrides_from_bender_yml(&bender_yml_data);

    // Find missing dependencies
    let missing_deps = find_missing_dependencies(&lock_deps, &yml_deps, &existing_overrides);

    // Update .bender.yml
    let updated_bender_yml = update_bender_yml_overrides(&bender_yml_text, &existing_overrides, &missing_deps);
    let mut effective_overrides = existing_overrides;
    effective_overrides.extend(missing_deps);
    let mut root_lock_data = lock_data.clone();
    for (package, package_info) in &mut root_lock_data.packages {
        let Some(override_info) = effective_overrides.get(package) else {
            continue;
        };
        let Some(serde_yaml::Value::String(git_url)) = override_info.get("git") else {
            continue;
        };
        if matches!(package_info.source, Source::Git(_)) {
            package_info.source = Source::Git(git_url.clone());
        }
    }
    let updated_lock = serde_yaml::to_string(&root_lock_data)
        .context("Failed to serialize converted Bender.lock")?;

    let root_yml_path = root_dir.join("Bender.yml");
    let output_path = root_dir.join(".bender.yml");
    let root_lock_path = root_dir.join("Bender.lock");
    let changed = fs::read_to_string(&root_yml_path).ok().as_deref() != Some(&yml_content)
        || fs::read_to_string(&output_path).ok().as_deref() != Some(&updated_bender_yml)
        || fs::read_to_string(&root_lock_path).ok().as_deref() != Some(&updated_lock);

    fs::write(&root_yml_path, &yml_content)
        .context("Failed to copy Bender.yml to root")?;

    // Write to root
    fs::write(&output_path, updated_bender_yml)
        .context("Failed to write .bender.yml to root")?;
    fs::write(&root_lock_path, updated_lock)
        .context("Failed to write Bender.lock to root")?;

    Ok(changed)
}

fn normalize_lock_paths(lock: &mut LockFile, bendis_dir: &Path, root_dir: &Path) -> Result<()> {
    let bendis_dir = bendis_dir
        .canonicalize()
        .with_context(|| format!("Failed to resolve {}", bendis_dir.display()))?;
    let root_dir = root_dir
        .canonicalize()
        .with_context(|| format!("Failed to resolve {}", root_dir.display()))?;
    for (package, package_info) in &mut lock.packages {
        let Source::Path(path) = &mut package_info.source else {
            continue;
        };
        let absolute = Path::new(path);
        if !absolute.is_absolute() {
            continue;
        }
        let resolved = absolute
            .canonicalize()
            .unwrap_or_else(|_| absolute.to_path_buf());
        let relative = resolved
            .strip_prefix(&bendis_dir)
            .or_else(|_| resolved.strip_prefix(&root_dir))
            .with_context(|| {
                format!(
                    "local dependency path for {package} is outside the project: {path}"
                )
            })?;
        *path = relative.to_string_lossy().replace('\\', "/");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{convert, generate_overrides_lines};
    use serde_yaml::Value;
    use std::collections::HashMap;
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn convert_reports_when_hard_config_is_replaced() {
        let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
        let base: PathBuf = std::env::temp_dir().join(format!("bendis-convert-{nonce}"));
        let workspace = base.join("bendis_workspace");
        fs::create_dir_all(&workspace).unwrap();
        fs::write(workspace.join("Bender.yml"), "dependencies: {}\n").unwrap();
        fs::write(workspace.join(".bender.yml"), "overrides: {}\n").unwrap();
        fs::write(workspace.join("Bender.lock"), "packages: {}\n").unwrap();
        fs::write(base.join("Bender.yml"), "dependencies:\n  core: { path: '../aegisrtl/core' }\n").unwrap();
        fs::write(base.join(".bender.yml"), "overrides:\n  core: { path: '../aegisrtl/core' }\n").unwrap();

        assert!(convert(&workspace, &base).unwrap());
        assert!(!convert(&workspace, &base).unwrap());

        fs::remove_dir_all(base).unwrap();
    }

    #[test]
    fn convert_writes_exact_lock_with_effective_override_sources() {
        let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
        let base: PathBuf = std::env::temp_dir().join(format!("bendis-lock-convert-{nonce}"));
        let workspace = base.join("bendis_workspace");
        fs::create_dir_all(&workspace).unwrap();
        fs::write(
            workspace.join("Bender.yml"),
            "dependencies:\n  core: { git: 'https://github.com/pulp-platform/core.git', version: 1.2 }\n",
        )
        .unwrap();
        fs::write(
            workspace.join(".bender.yml"),
            "overrides:\n  core: { git: 'git@code.ihep.ac.cn:heris/heris-platform/core.git', version: 1.2 }\n",
        )
        .unwrap();
        fs::write(
            workspace.join("Bender.lock"),
            "packages:\n  core:\n    revision: abc123\n    version: 1.2.3\n    source:\n      Git: https://github.com/pulp-platform/core.git\n    dependencies:\n      - helper\n  helper:\n    revision: def456\n    version: null\n    source:\n      Git: https://github.com/pulp-platform/helper.git\n    dependencies: []\n",
        )
        .unwrap();

        convert(&workspace, &base).unwrap();

        let lock_text = fs::read_to_string(base.join("Bender.lock")).unwrap();
        assert!(lock_text.find("  core:").unwrap() < lock_text.find("  helper:").unwrap());
        let lock: Value = serde_yaml::from_str(&lock_text).unwrap();
        assert_eq!(
            lock["packages"]["core"]["source"]["Git"],
            "git@code.ihep.ac.cn:heris/heris-platform/core.git"
        );
        assert_eq!(lock["packages"]["core"]["revision"], "abc123");
        assert_eq!(lock["packages"]["core"]["version"], "1.2.3");
        assert_eq!(lock["packages"]["core"]["dependencies"][0], "helper");
        assert_eq!(
            lock["packages"]["helper"]["source"]["Git"],
            "git@code.ihep.ac.cn:heris/heris-platform/helper.git"
        );
        assert_eq!(lock["packages"]["helper"]["revision"], "def456");
        let workspace_lock: Value = serde_yaml::from_str(
            &fs::read_to_string(workspace.join("Bender.lock")).unwrap(),
        )
        .unwrap();
        assert_eq!(
            workspace_lock["packages"]["core"]["source"]["Git"],
            "https://github.com/pulp-platform/core.git"
        );

        fs::remove_dir_all(base).unwrap();
    }

    #[test]
    fn convert_rebases_project_local_absolute_lock_paths() {
        let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
        let base: PathBuf = std::env::temp_dir().join(format!("bendis-path-rebase-{nonce}"));
        let workspace = base.join("bendis_workspace");
        let gpio = base.join("hw/vendored_ips/gpio");
        fs::create_dir_all(&workspace).unwrap();
        fs::create_dir_all(&gpio).unwrap();
        fs::write(
            workspace.join("Bender.yml"),
            "dependencies:\n  gpio: { path: 'hw/vendored_ips/gpio' }\n",
        )
        .unwrap();
        fs::write(workspace.join(".bender.yml"), "overrides: {}\n").unwrap();
        fs::write(
            workspace.join("Bender.lock"),
            format!(
                "packages:\n  gpio:\n    revision: null\n    version: null\n    source:\n      Path: {}\n    dependencies: []\n",
                gpio.display()
            ),
        )
        .unwrap();

        convert(&workspace, &base).unwrap();

        let lock: Value =
            serde_yaml::from_str(&fs::read_to_string(base.join("Bender.lock")).unwrap()).unwrap();
        assert_eq!(
            lock["packages"]["gpio"]["source"]["Path"],
            "hw/vendored_ips/gpio"
        );
        fs::remove_dir_all(base).unwrap();
    }

    #[test]
    fn convert_rebases_absolute_lock_paths_with_relative_project_directories() {
        let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
        let base = PathBuf::from("target").join(format!("bendis-relative-path-{nonce}"));
        let workspace = base.join("bendis_workspace");
        let gpio = base.join("hw/vendored_ips/gpio");
        fs::create_dir_all(&workspace).unwrap();
        fs::create_dir_all(&gpio).unwrap();
        let absolute_gpio = gpio.canonicalize().unwrap();
        fs::write(
            workspace.join("Bender.yml"),
            "dependencies:\n  gpio: { path: 'hw/vendored_ips/gpio' }\n",
        )
        .unwrap();
        fs::write(workspace.join(".bender.yml"), "overrides: {}\n").unwrap();
        fs::write(
            workspace.join("Bender.lock"),
            format!(
                "packages:\n  gpio:\n    revision: null\n    version: null\n    source:\n      Path: {}\n    dependencies: []\n",
                absolute_gpio.display()
            ),
        )
        .unwrap();

        convert(&workspace, &base).unwrap();

        let lock: Value =
            serde_yaml::from_str(&fs::read_to_string(base.join("Bender.lock")).unwrap()).unwrap();
        assert_eq!(
            lock["packages"]["gpio"]["source"]["Path"],
            "hw/vendored_ips/gpio"
        );
        let workspace_lock: Value = serde_yaml::from_str(
            &fs::read_to_string(workspace.join("Bender.lock")).unwrap(),
        )
        .unwrap();
        assert_eq!(
            workspace_lock["packages"]["gpio"]["source"]["Path"],
            "hw/vendored_ips/gpio"
        );
        fs::remove_dir_all(base).unwrap();
    }

    #[test]
    fn generated_overrides_are_sorted_by_dependency_name() {
        let mut overrides = HashMap::new();
        for name in ["zeta", "theta", "omega", "gamma", "delta", "beta", "alpha"] {
            overrides.insert(
                name.to_string(),
                HashMap::from([(
                    "path".to_string(),
                    Value::String(format!("hw/{name}")),
                )]),
            );
        }

        let lines = generate_overrides_lines(&overrides);
        let names: Vec<&str> = lines
            .iter()
            .map(|line| line.trim_start().split(':').next().unwrap())
            .collect();

        assert_eq!(
            names,
            vec!["alpha", "beta", "delta", "gamma", "omega", "theta", "zeta"]
        );
    }
}