File: upstream.rs

package info (click to toggle)
rust-ognibuild 0.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,676 kB
  • sloc: makefile: 17
file content (305 lines) | stat: -rw-r--r-- 9,586 bytes parent folder | download
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
//! This module provides a trait for dependencies that can find their upstream metadata.
use crate::dependency::Dependency;
use lazy_static::lazy_static;
use std::sync::RwLock;
pub use upstream_ontologist::UpstreamMetadata;

/// Type alias for custom upstream metadata providers.
pub type UpstreamProvider = Box<dyn Fn(&dyn Dependency) -> Option<UpstreamMetadata> + Send + Sync>;

lazy_static! {
    /// Global registry of custom upstream metadata providers.
    static ref CUSTOM_PROVIDERS: RwLock<Vec<UpstreamProvider>> = RwLock::new(Vec::new());
}

/// Register a custom upstream metadata provider.
///
/// Custom providers are checked before built-in providers when finding upstream metadata.
///
/// # Arguments
/// * `provider` - A function that takes a dependency and returns optional upstream metadata
///
/// # Example
/// ```no_run
/// use ognibuild::upstream::{register_upstream_provider, UpstreamMetadata};
/// use ognibuild::dependency::Dependency;
///
/// register_upstream_provider(|dep| {
///     if dep.family() == "custom" {
///         Some(UpstreamMetadata::default())
///     } else {
///         None
///     }
/// });
/// ```
pub fn register_upstream_provider<F>(provider: F)
where
    F: Fn(&dyn Dependency) -> Option<UpstreamMetadata> + Send + Sync + 'static,
{
    CUSTOM_PROVIDERS.write().unwrap().push(Box::new(provider));
}

/// Clear all registered custom upstream providers.
///
/// This is useful for testing to ensure a clean state between tests.
pub fn clear_custom_providers() {
    CUSTOM_PROVIDERS.write().unwrap().clear();
}

/// A trait for dependencies that can find their upstream metadata.
pub trait FindUpstream: Dependency {
    /// Find the upstream metadata for this dependency.
    fn find_upstream(&self) -> Option<UpstreamMetadata>;
}

/// Find the upstream metadata for a dependency.
///
/// This function attempts to find upstream metadata (like repository URL,
/// homepage, etc.) for the given dependency. It first checks any registered
/// custom providers, then falls back to trying to downcast the dependency to
/// various concrete dependency types that implement the FindUpstream trait.
///
/// # Arguments
/// * `dependency` - The dependency to find upstream metadata for
///
/// # Returns
/// * `Some(UpstreamMetadata)` if upstream metadata was found
/// * `None` if no upstream metadata could be found
pub fn find_upstream(dependency: &dyn Dependency) -> Option<UpstreamMetadata> {
    // First try custom providers
    for provider in CUSTOM_PROVIDERS.read().unwrap().iter() {
        if let Some(metadata) = provider(dependency) {
            return Some(metadata);
        }
    }
    #[cfg(feature = "debian")]
    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::debian::DebianDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::python::PythonPackageDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::RubyGemDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::node::NodePackageDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::CargoCrateDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::go::GoPackageDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::perl::PerlModuleDependency>()
    {
        return dep.find_upstream();
    }

    if let Some(dep) = dependency
        .as_any()
        .downcast_ref::<crate::dependencies::haskell::HaskellPackageDependency>()
    {
        return dep.find_upstream();
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::any::Any;
    use std::sync::Mutex;

    // Global test lock to ensure tests using custom providers don't interfere with each other
    lazy_static! {
        static ref TEST_LOCK: Mutex<()> = Mutex::new(());
    }

    #[derive(Debug)]
    struct TestDependency {
        #[allow(dead_code)]
        name: String,
    }

    impl Dependency for TestDependency {
        fn family(&self) -> &'static str {
            "test"
        }

        fn present(&self, _session: &dyn crate::session::Session) -> bool {
            false
        }

        fn project_present(&self, _session: &dyn crate::session::Session) -> bool {
            false
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[test]
    fn test_register_custom_provider() {
        // Acquire test lock to prevent interference from parallel tests
        let _lock = TEST_LOCK.lock().unwrap();

        // Clear any existing providers from other tests
        clear_custom_providers();

        let test_dep = TestDependency {
            name: "test-package".to_string(),
        };

        // Initially, no upstream metadata should be found
        let initial_result = find_upstream(&test_dep);
        assert!(
            initial_result.is_none(),
            "Expected no metadata initially, but found: {:?}",
            initial_result
        );

        // Register a custom provider
        register_upstream_provider(|dep| {
            if dep.family() == "test" {
                let mut metadata = UpstreamMetadata::default();
                metadata.insert(upstream_ontologist::UpstreamDatumWithMetadata {
                    datum: upstream_ontologist::UpstreamDatum::Repository(
                        "https://github.com/test/repo".to_string(),
                    ),
                    certainty: Some(upstream_ontologist::Certainty::Certain),
                    origin: None,
                });
                Some(metadata)
            } else {
                None
            }
        });

        // Now upstream metadata should be found via the custom provider
        let metadata = find_upstream(&test_dep).unwrap();
        assert_eq!(metadata.repository(), Some("https://github.com/test/repo"));

        // Clean up
        clear_custom_providers();
    }

    #[test]
    fn test_multiple_custom_providers() {
        // Acquire test lock to prevent interference from parallel tests
        let _lock = TEST_LOCK.lock().unwrap();

        // Clear any existing providers from other tests
        clear_custom_providers();

        let test_dep = TestDependency {
            name: "special-package".to_string(),
        };

        // Register first provider (doesn't match)
        register_upstream_provider(|dep| {
            if dep.family() == "other" {
                let mut metadata = UpstreamMetadata::default();
                metadata.insert(upstream_ontologist::UpstreamDatumWithMetadata {
                    datum: upstream_ontologist::UpstreamDatum::Repository(
                        "https://example.com/wrong".to_string(),
                    ),
                    certainty: Some(upstream_ontologist::Certainty::Certain),
                    origin: None,
                });
                Some(metadata)
            } else {
                None
            }
        });

        // Register second provider (matches)
        register_upstream_provider(|dep| {
            if dep.family() == "test" {
                let mut metadata = UpstreamMetadata::default();
                metadata.insert(upstream_ontologist::UpstreamDatumWithMetadata {
                    datum: upstream_ontologist::UpstreamDatum::Repository(
                        "https://example.com/correct".to_string(),
                    ),
                    certainty: Some(upstream_ontologist::Certainty::Certain),
                    origin: None,
                });
                Some(metadata)
            } else {
                None
            }
        });

        // Should find metadata from the second provider
        let metadata = find_upstream(&test_dep).unwrap();
        assert_eq!(metadata.repository(), Some("https://example.com/correct"));

        clear_custom_providers();
    }

    #[test]
    fn test_clear_custom_providers() {
        // Acquire test lock to prevent interference from parallel tests
        let _lock = TEST_LOCK.lock().unwrap();

        clear_custom_providers();

        let test_dep = TestDependency {
            name: "test-package".to_string(),
        };

        // Register a provider
        register_upstream_provider(|dep| {
            if dep.family() == "test" {
                let mut metadata = UpstreamMetadata::default();
                metadata.insert(upstream_ontologist::UpstreamDatumWithMetadata {
                    datum: upstream_ontologist::UpstreamDatum::Homepage(
                        "https://example.com".to_string(),
                    ),
                    certainty: Some(upstream_ontologist::Certainty::Certain),
                    origin: None,
                });
                Some(metadata)
            } else {
                None
            }
        });

        // Verify it works
        assert!(find_upstream(&test_dep).is_some());

        // Clear providers
        clear_custom_providers();

        // Verify provider is gone
        assert!(find_upstream(&test_dep).is_none());
    }
}