Skip to main content

buildl_core/ports/
no_remote.rs

1//! The remote cache of a build that has none.
2//!
3//! Its own file because every bundle without a remote adapter names it, and a pure port that
4//! holds nothing belongs in the crate that declares the port.
5//!
6//! Responsibilities: [`NoRemote`].
7//!
8//! Non-responsibilities: reaching a real remote, which an adapter does.
9
10use crate::error::{Error, PortName, Result};
11use crate::ports::{BlobSource, RemoteCache};
12use crate::types::{
13    Blob, DecodedTree, Diagnostic, Executed, RemoteAction, RemoteHit, RemoteUpload, Uploaded,
14};
15
16/// A remote cache that holds nothing and stores nothing.
17///
18/// Every lookup misses and every upload is accepted and dropped, so a build that has no remote
19/// behaves as one that was never offered one. It runs nothing: a request to run an action is
20/// refused.
21#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
22pub struct NoRemote;
23
24impl RemoteCache for NoRemote {
25    fn lookup(&self, _action: &RemoteAction) -> Result<Option<RemoteHit>> {
26        Ok(None)
27    }
28
29    fn download(&self, _blobs: &[Blob]) -> Result<Vec<Vec<u8>>> {
30        Err(Error::Adapter {
31            port: PortName::RemoteCache,
32            diagnostic: Diagnostic::new("there is no remote cache to download from"),
33        })
34    }
35
36    fn decode_tree(&self, _bytes: &[u8]) -> Result<DecodedTree> {
37        Ok(DecodedTree::Unreadable)
38    }
39
40    fn upload(
41        &self,
42        _action: &RemoteAction,
43        _produced: &RemoteUpload,
44        _blobs: &dyn BlobSource,
45    ) -> Result<Uploaded> {
46        Ok(Uploaded::Done)
47    }
48
49    fn execute(&self, _action: &RemoteAction) -> Result<Executed> {
50        Err(Error::Adapter {
51            port: PortName::RemoteCache,
52            diagnostic: Diagnostic::new("there is no remote cache to run an action"),
53        })
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    #![expect(
60        clippy::unwrap_used,
61        reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
62    )]
63
64    use std::collections::BTreeMap;
65
66    use super::NoRemote;
67    use crate::error::{Error, PortName, Result};
68    use crate::ports::{BlobSource, RemoteCache};
69    use crate::types::{
70        Argument, Blob, Command, DecodedTree, Digest, Platform, PlatformProperties, RemoteAction,
71        RemotePlatform, RemoteUpload, Timeout, Uploaded,
72    };
73
74    fn action() -> RemoteAction {
75        RemoteAction {
76            salt: Digest::of(b"key"),
77            command: Command::new(vec![Argument::parse("cc").unwrap()]).unwrap(),
78            env: BTreeMap::new(),
79            outputs: Vec::new(),
80            files: Vec::new(),
81            platform: RemotePlatform::of(&Platform::parse("linux/x86_64").unwrap()),
82            timeout: Timeout::default(),
83            properties: PlatformProperties::default(),
84        }
85    }
86
87    /// A source holding nothing.
88    struct Empty;
89
90    impl BlobSource for Empty {
91        fn read(&self, _digest: &Digest) -> Result<Option<Vec<u8>>> {
92            Ok(None)
93        }
94    }
95
96    #[test]
97    fn no_remote_never_holds_a_result() {
98        assert_eq!(NoRemote.lookup(&action()).unwrap(), None);
99    }
100
101    #[test]
102    fn no_remote_holds_no_blob() {
103        let error = NoRemote.download(&[Blob::of(b"x")]).unwrap_err();
104
105        assert!(
106            matches!(
107                error,
108                Error::Adapter {
109                    port: PortName::RemoteCache,
110                    ..
111                }
112            ),
113            "{error:?}"
114        );
115        assert!(error.to_string().starts_with("remote cache: "), "{error}");
116    }
117
118    #[test]
119    fn no_remote_refuses_to_execute() {
120        let error = NoRemote.execute(&action()).unwrap_err();
121
122        assert!(
123            matches!(
124                error,
125                Error::Adapter {
126                    port: PortName::RemoteCache,
127                    ..
128                }
129            ),
130            "{error:?}"
131        );
132        assert!(error.to_string().starts_with("remote cache: "), "{error}");
133    }
134
135    #[test]
136    fn no_remote_reads_no_tree() {
137        assert_eq!(
138            NoRemote.decode_tree(b"tree").unwrap(),
139            DecodedTree::Unreadable
140        );
141    }
142
143    #[test]
144    fn no_remote_accepts_an_upload_and_keeps_nothing() {
145        let produced = RemoteUpload {
146            outputs: Vec::new(),
147            stdout: Blob::of(b""),
148            stderr: Blob::of(b""),
149        };
150
151        let uploaded = NoRemote.upload(&action(), &produced, &Empty).unwrap();
152
153        assert_eq!(uploaded, Uploaded::Done);
154    }
155}