qtcloud-devops-cli 0.4.0

量潮DevOps云命令行工具
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
use std::path::Path;
use std::process::Command;

use crate::model::release::{FileStorage, ReleaseRecord, ReleaseStatus, Storage, TransitionError};

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum Registry {
    PyPI,
    PubDev,
    Crates,
}

// ===== utility functions =====

pub fn validate_version(version: &str) -> bool {
    let re = regex::Regex::new(
        r"^(v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?|[a-zA-Z0-9_.-]+/v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?)$",
    ).unwrap();
    re.is_match(version)
}

fn normalize_version(version: &str) -> String {
    let s = version.strip_prefix('v').unwrap_or(version);
    s.split("/v").last().unwrap_or(s).to_string()
}

pub fn precheck_version_changelog(version: &str, changelog_path: &Path) -> Vec<String> {
    let mut errors = Vec::new();
    if !validate_version(version) {
        errors.push(format!("版本号格式错误: {}", version));
    }
    if changelog_path.exists() {
        let content = std::fs::read_to_string(changelog_path).unwrap_or_default();
        let ver = normalize_version(version);
        let marker = format!("## [{}]", ver);
        if !content.contains(&marker) {
            errors.push(format!("CHANGELOG.md 未找到 {} 版本记录", ver));
        }
    } else {
        errors.push(format!("CHANGELOG.md 不存在: {}", changelog_path.display()));
    }
    errors
}

pub fn extract_notes(version: &str, changelog_path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(changelog_path).ok()?;
    let ver = normalize_version(version);
    let start_marker = format!("## [{}]", ver);
    let mut capture = false;
    let mut notes: Vec<&str> = Vec::new();
    for line in content.lines() {
        if line.trim().starts_with(&start_marker) {
            capture = true;
            continue;
        }
        if capture {
            if line.starts_with("## [") {
                break;
            }
            notes.push(line);
        }
    }
    let text = notes.join("\n").trim().to_string();
    if text.is_empty() { None } else { Some(text) }
}

pub fn confirm_release(version: &str, yes: bool) -> bool {
    if yes { return true; }
    use std::io::Write;
    println!("\n发布版本: {}", version);
    print!("确认发布? (y/N): ");
    std::io::stdout().flush().ok();
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).ok();
    let input = input.trim().to_lowercase();
    input == "y" || input == "yes"
}

fn git_args(args: &[&str], repo_path: &Path) -> Command {
    let mut cmd = Command::new("git");
    cmd.arg("-C");
    cmd.arg(repo_path);
    cmd.args(args);
    cmd
}

pub fn create_tag(version: &str, repo_path: &Path) -> bool {
    match git_args(&["tag", version], repo_path).output() {
        Ok(out) if out.status.success() => true,
        Ok(out) => {
            let msg = String::from_utf8_lossy(&out.stderr).trim().to_string();
            if msg.contains("already exists") || msg.contains("已存在") {
                return true; // 幂等:tag 已存在时不报错
            }
            eprintln!("创建标签失败: {}", msg);
            false
        }
        Err(e) => { eprintln!("创建标签失败: {}", e); false }
    }
}

pub fn push_tag(version: &str, repo_path: &Path) -> bool {
    match git_args(&["push", "origin", version], repo_path).output() {
        Ok(out) if out.status.success() => true,
        Ok(out) => {
            let msg = String::from_utf8_lossy(&out.stderr).trim().to_string();
            if msg.contains("does not appear") || msg.contains("repository '' does not exist") {
                return true; // 没有 remote 时静默跳过(本地 tag 已创建)
            }
            eprintln!("推送标签失败: {}", msg);
            false
        }
        Err(e) => { eprintln!("推送标签失败: {}", e); false }
    }
}

pub fn get_remote_repo(repo_path: &Path) -> Option<String> {
    let result = git_args(&["remote", "get-url", "origin"], repo_path).output().ok()?;
    if !result.status.success() { return None; }
    let url = String::from_utf8_lossy(&result.stdout).trim().to_string();
    parse_github_repo(&url)
}

pub fn parse_github_repo(url: &str) -> Option<String> {
    let re = regex::Regex::new(r"github\.com[/:]([^/]+/[^/]+?)(?:\.git)?$").ok()?;
    let caps = re.captures(url)?;
    Some(caps.get(1)?.as_str().to_string())
}

pub fn create_release(version: &str, notes: &str, repo: &str) -> bool {
    match Command::new("gh")
        .args(["release", "create", version, "--title", version, "--notes", notes, "--repo", repo])
        .output()
    {
        Ok(out) if out.status.success() => true,
        Ok(out) => { eprintln!("创建 Release 失败: {}", String::from_utf8_lossy(&out.stderr).trim()); false }
        Err(e) => { eprintln!("创建 Release 失败: {}", e); false }
    }
}

pub fn rollback_tag(version: &str, repo_path: &Path) {
    git_args(&["tag", "-d", version], repo_path).output().ok();
    git_args(&["push", "origin", "--delete", version], repo_path).output().ok();
    println!("↻ 标签 {} 已回滚", version);
}

// ===== stage =====

fn is_prerelease(version: &str) -> bool {
    let base = version.split('/').last().unwrap_or(version);
    base.contains('-')
}

pub fn stage(version: &str, repo_path: &Path) -> Result<String, Box<dyn std::error::Error>> {
    if !validate_version(version) {
        return Err(format!("版本号格式错误: {}", version).into());
    }
    if !is_prerelease(version) {
        return Err(format!("stage 仅用于预发布版本(含 -rc.N、-alpha.N 等后缀),正式版请直接 publish: {}", version).into());
    }
    let mut storage = FileStorage::new(repo_path);
    if let Some(existing) = storage.load(version) {
        match existing.status {
            ReleaseStatus::Published => return Err(format!("版本 {} 已发布,不可重复 stage", version).into()),
            ReleaseStatus::Staged => {
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs().to_string();
                let mut updated = existing.clone();
                updated.updated_at = now;
                storage.save(&updated)?;
                // re-push tag (idempotent)
                if create_tag(version, repo_path) && push_tag(version, repo_path) {
                    println!("✓ 标签 {} 已更新并推送", version);
                }
                return Ok(updated.id);
            }
            ReleaseStatus::Cancelled => {}
            ReleaseStatus::Retired => return Err(format!("版本 {} 已退役,不可重复 stage", version).into()),
        }
    }
    let record = ReleaseRecord::new_staged(version);
    storage.save(&record)?;
    if !create_tag(version, repo_path) {
        return Err(format!("创建标签 {} 失败", version).into());
    }
    if !push_tag(version, repo_path) {
        rollback_tag(version, repo_path);
        return Err(format!("推送标签 {} 失败", version).into());
    }
    println!("✓ 版本 {} 已进入 Staged 状态 (发布尝试 ID: {})", version, record.id);
    println!("✓ 标签 {} 已创建并推送", version);

    let changelog_path = repo_path.join("CHANGELOG.md");
    let notes = extract_notes(version, &changelog_path);
    if let Some(repo) = get_remote_repo(repo_path) {
        if create_release(version, notes.as_deref().unwrap_or(""), &repo) {
            println!("✓ GitHub Release {} 已创建", version);
            println!("  https://github.com/{}/releases/tag/{}", repo, version);
        }
    }

    Ok(record.id)
}

// ===== publish =====

pub fn publish(version: &str, repo_path: &Path, yes: bool, registry: Option<Registry>) -> Result<String, Box<dyn std::error::Error>> {
    let mut storage = FileStorage::new(repo_path);
    let mut record = storage
        .load(version)
        .ok_or_else(|| format!("版本 {} 不存在,请先执行 stage", version))?;
    if record.status != ReleaseStatus::Staged {
        return Err(format!("版本 {} 不处于 Staged 状态 (当前: {:?})", version, record.status).into());
    }
    if !confirm_release(version, yes) {
        return Err("已取消发布".into());
    }
    if !create_tag(version, repo_path) {
        return Err(format!("创建标签 {} 失败", version).into());
    }
    if !push_tag(version, repo_path) {
        rollback_tag(version, repo_path);
        return Err(format!("推送标签 {} 失败", version).into());
    }
    println!("✓ 标签 {} 已创建并推送", version);

    let changelog_path = repo_path.join("CHANGELOG.md");
    let notes = extract_notes(version, &changelog_path);
    if let Some(repo) = get_remote_repo(repo_path) {
        if !create_release(version, notes.as_deref().unwrap_or(""), &repo) {
            rollback_tag(version, repo_path);
            return Err("创建 GitHub Release 失败".into());
        }
        println!("✓ GitHub Release {} 已创建", version);
        println!("  https://github.com/{}/releases/tag/{}", repo, version);
    }

    if let Some(reg) = registry {
        println!("  {:?} 由 CI 自动发布,无需本地操作", reg);
    }

    record.status = ReleaseStatus::Published;
    record.updated_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs().to_string();
    storage.save(&record)?;
    let id = record.id.clone();
    println!("✓ 版本 {} 已发布 (发布尝试 ID: {})", version, id);
    Ok(id)
}

// ===== retire =====

pub fn retire(version: &str, repo_path: &Path) -> Result<String, Box<dyn std::error::Error>> {
    let mut storage = FileStorage::new(repo_path);
    let mut record = storage
        .load(version)
        .ok_or_else(|| format!("版本 {} 不存在", version))?;
    if record.status != ReleaseStatus::Published {
        return Err(Box::new(TransitionError::NotPublished(version.to_string())));
    }
    record.status = ReleaseStatus::Retired;
    record.updated_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs().to_string();
    storage.save(&record)?;
    let id = record.id.clone();
    println!("✓ 版本 {} 已退役 (发布尝试 ID: {})", version, id);
    Ok(id)
}

// ===== release_status =====

pub fn release_status(repo_path: &Path) -> Result<String, Box<dyn std::error::Error>> {
    let storage = FileStorage::new(repo_path);
    let mut records = storage.list();
    if records.is_empty() {
        println!("当前无发布记录");
        return Ok(String::new());
    }

    records.sort_by(|a, b| b.created_at.cmp(&a.created_at));

    let staged: Vec<&ReleaseRecord> = records.iter().filter(|r| r.status == ReleaseStatus::Staged).collect();
    let published: Vec<&ReleaseRecord> = records.iter().filter(|r| r.status == ReleaseStatus::Published).collect();

    println!("发布状态报告");
    println!("{}", "-".repeat(40));
    println!("待发布: {}", staged.len());
    for r in &staged {
        println!("  {} (尝试: {})", r.version, &r.id[..8]);
    }
    println!("已发布: {}", published.len());
    for r in &published {
        println!("  {} (尝试: {})", r.version, &r.id[..8]);
    }
    println!();

    println!("最新发布:");
    for r in records.iter().take(5) {
        let status_str = match r.status {
            ReleaseStatus::Staged => "Staged",
            ReleaseStatus::Published => "Published",
            ReleaseStatus::Cancelled => "Cancelled",
            ReleaseStatus::Retired => "Retired",
        };
        println!("  {:<25} {:<12} {}", r.version, status_str, r.updated_at);
    }

    Ok(records.len().to_string())
}

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

    fn make_record(version: &str, status: ReleaseStatus) -> ReleaseRecord {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs().to_string();
        ReleaseRecord {
            id: uuid::Uuid::new_v4().to_string(),
            version: version.to_string(),
            status,
            created_at: now.clone(),
            updated_at: now,
        }
    }

    // validate_version

    #[test]
    fn test_validate_version_v_prefix() { assert!(validate_version("v1.2.3")); }
    #[test]
    fn test_validate_version_with_suffix() { assert!(validate_version("v1.2.3-alpha.1")); assert!(validate_version("v1.2.3-rc1")); }
    #[test]
    fn test_validate_version_pkg() { assert!(validate_version("pkg/v1.2.3")); assert!(validate_version("cli/v0.1.0")); }
    #[test]
    fn test_validate_version_invalid() { assert!(!validate_version("1.2.3")); assert!(!validate_version("v1.2")); assert!(!validate_version("abc")); }

    // parse_github_repo

    #[test]
    fn test_parse_github_repo_https() { assert_eq!(parse_github_repo("https://github.com/owner/repo.git"), Some("owner/repo".into())); }
    #[test]
    fn test_parse_github_repo_ssh() { assert_eq!(parse_github_repo("git@github.com:owner/repo.git"), Some("owner/repo".into())); }
    #[test]
    fn test_parse_github_repo_not_github() { assert_eq!(parse_github_repo("https://gitlab.com/owner/repo.git"), None); }

    // extract_notes

    #[test]
    fn test_extract_notes_found() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("CHANGELOG.md"), "## [1.0.0]\n\ncontent").unwrap();
        assert!(extract_notes("v1.0.0", &dir.path().join("CHANGELOG.md")).is_some());
    }

    #[test]
    fn test_extract_notes_not_found() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("CHANGELOG.md"), "## [1.0.0]\n\ncontent").unwrap();
        assert!(extract_notes("v2.0.0", &dir.path().join("CHANGELOG.md")).is_none());
    }

    // confirm_release

    #[test]
    fn test_confirm_release_yes_flag() { assert!(confirm_release("v1.0.0", true)); }

    // precheck_changelog

    #[test]
    fn test_precheck_changelog_no_errors() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("CHANGELOG.md"), "## [1.0.0]\n\ncontent").unwrap();
        assert!(precheck_version_changelog("v1.0.0", &dir.path().join("CHANGELOG.md")).is_empty());
    }

    #[test]
    fn test_precheck_changelog_missing_entry() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("CHANGELOG.md"), "## [1.0.0]\n\ncontent").unwrap();
        assert!(precheck_version_changelog("v2.0.0", &dir.path().join("CHANGELOG.md")).iter().any(|e| e.contains("未找到")));
    }

    // stage helpers

    fn git_init(path: &std::path::Path) {
        std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(path)
            .output().unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "t@t"])
            .current_dir(path).output().unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "t"])
            .current_dir(path).output().unwrap();
        std::fs::write(path.join("f"), "").unwrap();
        std::process::Command::new("git")
            .args(["add", "."]).current_dir(path).output().unwrap();
        std::process::Command::new("git")
            .args(["commit", "-m", "init"]).current_dir(path).output().unwrap();
    }

    #[test]
    fn test_stage_new_version() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        let id = stage("v1.0.0-rc.1", dir.path()).unwrap();
        assert!(!id.is_empty());
        let s = FileStorage::new(dir.path());
        assert_eq!(s.load("v1.0.0-rc.1").unwrap().status, ReleaseStatus::Staged);
    }

    #[test]
    fn test_stage_invalid_version() { assert!(stage("bad", tempfile::tempdir().unwrap().path()).is_err()); }

    #[test]
    fn test_stage_formal_rejected() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        let err = stage("v1.0.0", dir.path()).unwrap_err().to_string();
        assert!(err.contains("仅用于预发布"));
    }

    #[test]
    fn test_stage_published_rejected() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        let mut s = FileStorage::new(dir.path());
        s.save(&make_record("v1.0.0-rc.1", ReleaseStatus::Published)).unwrap();
        assert!(stage("v1.0.0-rc.1", dir.path()).unwrap_err().to_string().contains("已发布"));
    }

    #[test]
    fn test_stage_cancelled_restage() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        let old_id;
        {
            let mut s = FileStorage::new(dir.path());
            let r = make_record("v1.0.0-rc.1", ReleaseStatus::Cancelled);
            old_id = r.id.clone();
            s.save(&r).unwrap();
        }
        let id = stage("v1.0.0-rc.1", dir.path()).unwrap();
        assert_ne!(id, old_id);
    }

    #[test]
    fn test_stage_retired_rejected() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        let mut s = FileStorage::new(dir.path());
        s.save(&make_record("v1.0.0-rc.1", ReleaseStatus::Retired)).unwrap();
        assert!(stage("v1.0.0-rc.1", dir.path()).unwrap_err().to_string().contains("退役"));
    }

    #[test]
    fn test_stage_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        assert_eq!(stage("v1.0.0-rc.1", dir.path()).unwrap(), stage("v1.0.0-rc.1", dir.path()).unwrap());
    }

    // publish

    #[test]
    fn test_publish_not_found() {
        let dir = tempfile::tempdir().unwrap();
        assert!(publish("v1.0.0", dir.path(), true, None).unwrap_err().to_string().contains("请先执行 stage"));
    }

    #[test]
    fn test_publish_not_staged() {
        let dir = tempfile::tempdir().unwrap();
        let mut s = FileStorage::new(dir.path());
        let mut r = ReleaseRecord::new_staged("v1.0.0");
        r.status = ReleaseStatus::Cancelled;
        s.save(&r).unwrap();
        assert!(publish("v1.0.0", dir.path(), true, None).is_err());
    }

    // retire

    #[test]
    fn test_retire_nonexistent() { assert!(retire("v9.9.9", tempfile::tempdir().unwrap().path()).is_err()); }

    #[test]
    fn test_retire_not_published() {
        let dir = tempfile::tempdir().unwrap();
        let mut s = FileStorage::new(dir.path());
        s.save(&make_record("v1.0.0", ReleaseStatus::Staged)).unwrap();
        assert!(retire("v1.0.0", dir.path()).is_err());
    }

    #[test]
    fn test_retire_from_published() {
        let dir = tempfile::tempdir().unwrap();
        {
            let mut s = FileStorage::new(dir.path());
            s.save(&make_record("v1.0.0", ReleaseStatus::Published)).unwrap();
        }
        retire("v1.0.0", dir.path()).unwrap();
        assert_eq!(FileStorage::new(dir.path()).load("v1.0.0").unwrap().status, ReleaseStatus::Retired);
    }

    // release_status

    #[test]
    fn test_release_status_empty() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(release_status(dir.path()).unwrap(), "");
    }

    #[test]
    fn test_release_status_with_records() {
        let dir = tempfile::tempdir().unwrap();
        let mut s = FileStorage::new(dir.path());
        s.save(&make_record("v1.0.0", ReleaseStatus::Staged)).unwrap();
        s.save(&make_record("v2.0.0", ReleaseStatus::Published)).unwrap();
        assert_eq!(release_status(dir.path()).unwrap(), "2");
    }

    #[test]
    fn test_release_status_multiple_staged() {
        let dir = tempfile::tempdir().unwrap();
        let mut s = FileStorage::new(dir.path());
        s.save(&make_record("v1.0.0", ReleaseStatus::Staged)).unwrap();
        s.save(&make_record("v2.0.0", ReleaseStatus::Staged)).unwrap();
        s.save(&make_record("v3.0.0", ReleaseStatus::Published)).unwrap();
        assert_eq!(release_status(dir.path()).unwrap(), "3");
    }

    // is_prerelease

    #[test]
    fn test_is_prerelease_rc() { assert!(is_prerelease("v1.0.0-rc.1")); }
    #[test]
    fn test_is_prerelease_alpha() { assert!(is_prerelease("v1.0.0-alpha.1")); }
    #[test]
    fn test_is_prerelease_beta() { assert!(is_prerelease("v1.0.0-beta.2")); }
    #[test]
    fn test_is_prerelease_scoped() { assert!(is_prerelease("cli/v0.3.2-rc.1")); }
    #[test]
    fn test_is_prerelease_formal() { assert!(!is_prerelease("v1.0.0")); }
    #[test]
    fn test_is_prerelease_formal_scoped() { assert!(!is_prerelease("cli/v0.3.2")); }

    // Registry

    #[test]
    fn test_registry_debug() {
        assert_eq!(format!("{:?}", Registry::PyPI), "PyPI");
        assert_eq!(format!("{:?}", Registry::PubDev), "PubDev");
        assert_eq!(format!("{:?}", Registry::Crates), "Crates");
    }

    #[test]
    fn test_registry_clone_eq() {
        assert_eq!(Registry::PyPI, Registry::PyPI);
        assert_ne!(Registry::PyPI, Registry::PubDev);
    }

    // normalize_version

    #[test]
    fn test_normalize_version_v_prefix() { assert_eq!(normalize_version("v1.2.3"), "1.2.3"); }

    #[test]
    fn test_normalize_version_pkg() { assert_eq!(normalize_version("pkg/v1.2.3"), "1.2.3"); }

    #[test]
    fn test_normalize_version_no_prefix() { assert_eq!(normalize_version("1.2.3"), "1.2.3"); }

    #[test]
    fn test_normalize_version_scoped() { assert_eq!(normalize_version("cli/v0.3.2"), "0.3.2"); }

    // validate_version edge cases

    #[test]
    fn test_validate_version_formal() { assert!(validate_version("v1.0.0")); }
    #[test]
    fn test_validate_version_prerelease() { assert!(validate_version("v1.0.0-rc.1")); }
    #[test]
    fn test_validate_version_no_v() { assert!(!validate_version("1.0.0")); }
    #[test]
    fn test_validate_version_empty() { assert!(!validate_version("")); }
    #[test]
    fn test_validate_version_scope_only() { assert!(!validate_version("cli/")); }

    // get_remote_repo (no remote)

    #[test]
    fn test_get_remote_repo_no_git_repo() { assert_eq!(get_remote_repo(tempfile::tempdir().unwrap().path()), None); }

    // create_release without gh (should fail gracefully)

    #[test]
    fn test_create_release_no_gh() { assert!(!create_release("v0.0.0-test", "", "no/repo")); }

    // rollback_tag in temp repo

    #[test]
    fn test_precheck_changelog_file_not_found() {
        let dir = tempfile::tempdir().unwrap();
        let errors = precheck_version_changelog("v1.0.0", &dir.path().join("NONEXISTENT.md"));
        assert!(errors.iter().any(|e| e.contains("不存在")));
    }

    #[test]
    fn test_precheck_changelog_version_invalid() {
        let dir = tempfile::tempdir().unwrap();
        let errors = precheck_version_changelog("bad", &dir.path().join("CHANGELOG.md"));
        assert!(errors.iter().any(|e| e.contains("格式错误")));
    }

    #[test]
    fn test_create_tag_in_non_git_dir() {
        assert!(!create_tag("v0.0.0-test", tempfile::tempdir().unwrap().path()));
    }

    #[test]
    fn test_create_tag_duplicate_fails() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        assert!(create_tag("v0.0.0-test", dir.path()));
        // second create should fail (tag already exists)
        assert!(!create_tag("v0.0.0-test", dir.path()));
    }

    #[test]
    fn test_push_tag_in_non_git_dir() {
        assert!(!push_tag("v0.0.0-test", tempfile::tempdir().unwrap().path()));
    }

    #[test]
    fn test_push_tag_fails_with_non_existent_remote() {
        let dir = tempfile::tempdir().unwrap();
        git_init(dir.path());
        assert!(create_tag("v0.0.0-test-remote", dir.path()));
        // add a non-existent remote so push fails with real error, not "no remote"
        std::process::Command::new("git")
            .args(["remote", "add", "origin", "https://nonexistent.invalid/repo.git"])
            .current_dir(dir.path()).output().unwrap();
        assert!(!push_tag("v0.0.0-test-remote", dir.path()));
    }

    #[test]
    fn test_get_remote_repo_in_git_without_remote() {
        let dir = tempfile::tempdir().unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"]).current_dir(dir.path()).output().unwrap();
        assert_eq!(get_remote_repo(dir.path()), None);
    }

    #[test]
    fn test_rollback_tag_removes_tag() {
        let dir = tempfile::tempdir().unwrap();
        std::process::Command::new("git")
            .args(["init", "-b", "main"]).current_dir(dir.path()).output().unwrap();
        std::fs::write(dir.path().join("f"), "").unwrap();
        std::process::Command::new("git")
            .args(["add", "."]).current_dir(dir.path()).output().unwrap();
        std::process::Command::new("git")
            .args(["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-m", "x"])
            .current_dir(dir.path()).output().unwrap();
        assert!(create_tag("v0.0.0-test-rollback", dir.path()));
        rollback_tag("v0.0.0-test-rollback", dir.path());
        let output = std::process::Command::new("git")
            .args(["tag", "-l"]).current_dir(dir.path()).output().unwrap();
        assert!(!String::from_utf8_lossy(&output.stdout).contains("v0.0.0-test-rollback"));
    }
}