Skip to main content

qtcloud_devops_cli/
contract.rs

1/// 契约模块 — 基于 `quanttide-devops` toolkit 的适配层。
2pub use quanttide_devops::contract::{
3    detect_language_by_files, normalize_version, read_all_config_versions, validate_version,
4    BuildTool, Contract, Language, Pipeline, Platform, Registry, Scope, SourceControl, SourceType,
5    StageBuild, StageRelease, StageTest, VersionSource,
6};
7pub use quanttide_devops::source::git::{GitSourceError, VersionStatus};
8
9use std::path::Path;
10
11// ═══════════════════════════════════════════════════════════════════════
12// 加载(保留向后兼容的行为)
13// ═══════════════════════════════════════════════════════════════════════
14
15pub fn load(repo_path: &Path) -> Contract {
16    match quanttide_devops::contract::load(repo_path) {
17        Ok(c) => c,
18        Err(e) => {
19            eprintln!("  ℹ contract.yaml: {},使用默认契约", e);
20            Contract::default()
21        }
22    }
23}
24
25pub fn load_scopes(repo_path: &Path) -> Vec<Scope> {
26    load(repo_path).scopes
27}
28
29pub fn detect_by_files(dir: &Path) -> Language {
30    detect_language_by_files(dir)
31}
32
33// ═══════════════════════════════════════════════════════════════════════
34// 版本状态(适配 toolkit 的 Result → 旧签名)
35// ═══════════════════════════════════════════════════════════════════════
36
37/// 检查 scope 版本一致性。失败时返回空的 VersionStatus。
38pub fn version_status(repo_path: &Path, scope: &Scope) -> VersionStatus {
39    quanttide_devops::source::git::version_status(repo_path, scope).unwrap_or_else(|e| {
40        eprintln!("  ⚠ 版本状态检查失败: {}", e);
41        VersionStatus {
42            tag_version: None,
43            config_version: None,
44            consistent: false,
45            config_files: vec![],
46        }
47    })
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_version_status_git_error_returns_fallback() {
56        // 不存在的路径 → git2 打开失败 → version_status 返回 Err → fallback
57        let scope = Scope {
58            name: "test".into(),
59            dir: ".".into(),
60            language: Language::Rust,
61            framework: String::new(),
62            build_tool: BuildTool::Cargo,
63            registry: Registry::None,
64            release: StageRelease::default(),
65            test_threshold: None,
66            ci_workflow: None,
67        };
68        let vs = version_status(Path::new("/nonexistent"), &scope);
69        assert!(!vs.consistent);
70        assert_eq!(vs.tag_version, None);
71        assert_eq!(vs.config_version, None);
72        assert!(vs.config_files.is_empty());
73    }
74}