|
| 1 | +use std::sync::{ |
| 2 | + Arc, Mutex, |
| 3 | + atomic::{AtomicUsize, Ordering}, |
| 4 | +}; |
| 5 | + |
| 6 | +use async_trait::async_trait; |
| 7 | +use serde_json::{Value, json}; |
| 8 | + |
| 9 | +use super::CustomFetcherPicker; |
| 10 | +use crate::{CustomFetcher, HookError}; |
| 11 | + |
| 12 | +struct ScriptedFetcher { |
| 13 | + can_fetch: bool, |
| 14 | + response: Value, |
| 15 | + can_fetch_calls: AtomicUsize, |
| 16 | + fetch_calls: AtomicUsize, |
| 17 | + seen_pkg_ids: Mutex<Vec<String>>, |
| 18 | + seen_resolutions: Mutex<Vec<Value>>, |
| 19 | + seen_opts: Mutex<Vec<Value>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl ScriptedFetcher { |
| 23 | + fn answering(response: Value) -> Self { |
| 24 | + ScriptedFetcher { |
| 25 | + can_fetch: true, |
| 26 | + response, |
| 27 | + can_fetch_calls: AtomicUsize::new(0), |
| 28 | + fetch_calls: AtomicUsize::new(0), |
| 29 | + seen_pkg_ids: Mutex::new(Vec::new()), |
| 30 | + seen_resolutions: Mutex::new(Vec::new()), |
| 31 | + seen_opts: Mutex::new(Vec::new()), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[async_trait] |
| 37 | +impl CustomFetcher for ScriptedFetcher { |
| 38 | + async fn can_fetch(&self, pkg_id: &str, resolution: Value) -> Result<bool, HookError> { |
| 39 | + self.can_fetch_calls.fetch_add(1, Ordering::SeqCst); |
| 40 | + self.seen_pkg_ids.lock().unwrap().push(pkg_id.to_string()); |
| 41 | + self.seen_resolutions.lock().unwrap().push(resolution); |
| 42 | + Ok(self.can_fetch) |
| 43 | + } |
| 44 | + |
| 45 | + async fn fetch( |
| 46 | + &self, |
| 47 | + _pkg_id: &str, |
| 48 | + _resolution: Value, |
| 49 | + opts: Value, |
| 50 | + ) -> Result<Value, HookError> { |
| 51 | + self.fetch_calls.fetch_add(1, Ordering::SeqCst); |
| 52 | + self.seen_opts.lock().unwrap().push(opts); |
| 53 | + Ok(self.response.clone()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn fetch_result() -> Value { |
| 58 | + json!({ |
| 59 | + "filesIndex": { |
| 60 | + "package.json": { "integrity": "sha512-abc123", "mode": 420 }, |
| 61 | + "index.js": { "integrity": "sha512-def456", "mode": 420 }, |
| 62 | + } |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +#[tokio::test] |
| 67 | +async fn returns_result_from_first_matching_fetcher() { |
| 68 | + let fetcher = Arc::new(ScriptedFetcher::answering(fetch_result())); |
| 69 | + let picker = CustomFetcherPicker::new(vec![Arc::clone(&fetcher) as Arc<dyn CustomFetcher>]); |
| 70 | + |
| 71 | + let resolution = json!({ "type": "@custom/registry", "url": "https://example.com/pkg" }); |
| 72 | + let opts = json!({ "manifest": { "name": "foo", "version": "1.0.0" } }); |
| 73 | + |
| 74 | + let result = picker.try_fetch("foo@1.0.0", &resolution, &opts).await.unwrap(); |
| 75 | + |
| 76 | + assert!(result.is_some()); |
| 77 | + assert_eq!(result.unwrap(), fetch_result()); |
| 78 | + assert_eq!(fetcher.can_fetch_calls.load(Ordering::SeqCst), 1); |
| 79 | + assert_eq!(fetcher.fetch_calls.load(Ordering::SeqCst), 1); |
| 80 | +} |
| 81 | + |
| 82 | +#[tokio::test] |
| 83 | +async fn returns_none_when_no_fetcher_claims_package() { |
| 84 | + let fetcher = Arc::new(ScriptedFetcher { |
| 85 | + can_fetch: false, |
| 86 | + ..ScriptedFetcher::answering(fetch_result()) |
| 87 | + }); |
| 88 | + let picker = CustomFetcherPicker::new(vec![Arc::clone(&fetcher) as Arc<dyn CustomFetcher>]); |
| 89 | + |
| 90 | + let resolution = json!({ "type": "@custom/registry" }); |
| 91 | + let opts = json!({}); |
| 92 | + |
| 93 | + let result = picker.try_fetch("foo@1.0.0", &resolution, &opts).await.unwrap(); |
| 94 | + |
| 95 | + assert!(result.is_none()); |
| 96 | + assert_eq!(fetcher.can_fetch_calls.load(Ordering::SeqCst), 1); |
| 97 | + assert_eq!(fetcher.fetch_calls.load(Ordering::SeqCst), 0); |
| 98 | +} |
| 99 | + |
| 100 | +#[tokio::test] |
| 101 | +async fn tries_fetchers_in_order_stops_at_first_match() { |
| 102 | + let first = |
| 103 | + Arc::new(ScriptedFetcher { can_fetch: false, ..ScriptedFetcher::answering(json!({})) }); |
| 104 | + let second = Arc::new(ScriptedFetcher::answering(fetch_result())); |
| 105 | + let third = Arc::new(ScriptedFetcher::answering(json!({"other": true}))); |
| 106 | + |
| 107 | + let picker = CustomFetcherPicker::new(vec![ |
| 108 | + Arc::clone(&first) as Arc<dyn CustomFetcher>, |
| 109 | + Arc::clone(&second) as Arc<dyn CustomFetcher>, |
| 110 | + Arc::clone(&third) as Arc<dyn CustomFetcher>, |
| 111 | + ]); |
| 112 | + |
| 113 | + let resolution = json!({ "tarball": "https://example.com/foo.tgz" }); |
| 114 | + let opts = json!({}); |
| 115 | + |
| 116 | + let result = picker.try_fetch("foo@1.0.0", &resolution, &opts).await.unwrap(); |
| 117 | + |
| 118 | + assert_eq!(result.unwrap(), fetch_result()); |
| 119 | + assert_eq!(first.can_fetch_calls.load(Ordering::SeqCst), 1); |
| 120 | + assert_eq!(first.fetch_calls.load(Ordering::SeqCst), 0); |
| 121 | + assert_eq!(second.can_fetch_calls.load(Ordering::SeqCst), 1); |
| 122 | + assert_eq!(second.fetch_calls.load(Ordering::SeqCst), 1); |
| 123 | + assert_eq!( |
| 124 | + third.can_fetch_calls.load(Ordering::SeqCst), |
| 125 | + 0, |
| 126 | + "must not consult fetchers after a match", |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +#[tokio::test] |
| 131 | +async fn skips_fetcher_without_can_fetch() { |
| 132 | + let fetcher = Arc::new(NoCanFetchFetcher); |
| 133 | + let picker = CustomFetcherPicker::new(vec![fetcher]); |
| 134 | + |
| 135 | + let result = picker.try_fetch("foo@1.0.0", &json!({}), &json!({})).await.unwrap(); |
| 136 | + |
| 137 | + assert!(result.is_none()); |
| 138 | +} |
| 139 | + |
| 140 | +struct NoCanFetchFetcher; |
| 141 | + |
| 142 | +#[async_trait] |
| 143 | +impl CustomFetcher for NoCanFetchFetcher { |
| 144 | + fn has_can_fetch(&self) -> bool { |
| 145 | + false |
| 146 | + } |
| 147 | + |
| 148 | + async fn can_fetch(&self, _: &str, _: Value) -> Result<bool, HookError> { |
| 149 | + panic!("should not be called"); |
| 150 | + } |
| 151 | + |
| 152 | + async fn fetch(&self, _: &str, _: Value, _: Value) -> Result<Value, HookError> { |
| 153 | + panic!("should not be called"); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#[tokio::test] |
| 158 | +async fn empty_picker_returns_none() { |
| 159 | + let picker = CustomFetcherPicker::new(vec![]); |
| 160 | + assert!(picker.is_empty()); |
| 161 | + |
| 162 | + let result = picker.try_fetch("foo@1.0.0", &json!({}), &json!({})).await.unwrap(); |
| 163 | + assert!(result.is_none()); |
| 164 | +} |
0 commit comments