Skip to main content

stellar_registry_cli/commands/
current_version.rs

1use clap::Parser;
2use stellar_cli::commands::contract::invoke;
3use stellar_registry_build::named_registry::PrefixedName;
4
5use crate::commands::global;
6
7#[derive(Parser, Debug, Clone)]
8pub struct Cmd {
9    /// Name of published Wasm
10    pub wasm_name: PrefixedName,
11
12    #[command(flatten)]
13    pub config: global::Args,
14}
15
16#[derive(thiserror::Error, Debug)]
17pub enum Error {
18    #[error(transparent)]
19    Invoke(#[from] invoke::Error),
20    #[error(transparent)]
21    Config(#[from] stellar_cli::config::Error),
22    #[error(transparent)]
23    Registry(#[from] stellar_registry_build::Error),
24}
25
26impl Cmd {
27    pub async fn run(&self) -> Result<(), Error> {
28        let version = self.current_version().await?;
29        println!("{version}");
30        Ok(())
31    }
32
33    pub async fn current_version(&self) -> Result<String, Error> {
34        let registry = self.wasm_name.registry(&self.config).await?;
35        let slop = ["current_version", "--wasm-name", &self.wasm_name.name];
36        let raw = registry
37            .as_contract()
38            .invoke_with_result(&slop, true)
39            .await?;
40        Ok(raw.trim_matches('"').to_string())
41    }
42}
43
44#[cfg(feature = "integration-tests")]
45#[cfg(test)]
46mod tests {
47    use stellar_scaffold_test::RegistryTest;
48
49    #[tokio::test]
50    async fn simple() {
51        let registry = RegistryTest::new().await;
52        let v1 = registry.hello_wasm_v1();
53
54        // First publish the contract
55        registry
56            .registry_cli("publish")
57            .arg("--wasm")
58            .arg(v1.to_str().unwrap())
59            .arg("--binver")
60            .arg("0.0.1")
61            .arg("--wasm-name")
62            .arg("hello")
63            .assert()
64            .success();
65
66        let version = registry
67            .parse_cmd::<super::Cmd>(&["hello"])
68            .unwrap()
69            .current_version()
70            .await
71            .unwrap();
72        assert_eq!(version, "0.0.1");
73    }
74
75    #[tokio::test]
76    async fn multiple_versions() {
77        let registry = RegistryTest::new().await;
78        let v1 = registry.hello_wasm_v1();
79        let v2 = registry.hello_wasm_v2();
80
81        // Publish v1
82        registry
83            .registry_cli("publish")
84            .arg("--wasm")
85            .arg(v1.to_str().unwrap())
86            .arg("--binver")
87            .arg("0.0.1")
88            .arg("--wasm-name")
89            .arg("hello")
90            .assert()
91            .success();
92
93        let version = registry
94            .parse_cmd::<super::Cmd>(&["hello"])
95            .unwrap()
96            .current_version()
97            .await
98            .unwrap();
99        assert_eq!(version, "0.0.1");
100
101        // Publish v2
102        registry
103            .registry_cli("publish")
104            .arg("--wasm")
105            .arg(v2.to_str().unwrap())
106            .arg("--binver")
107            .arg("0.0.2")
108            .arg("--wasm-name")
109            .arg("hello")
110            .assert()
111            .success();
112
113        let version = registry
114            .parse_cmd::<super::Cmd>(&["hello"])
115            .unwrap()
116            .current_version()
117            .await
118            .unwrap();
119        assert_eq!(version, "0.0.2");
120    }
121
122    #[tokio::test]
123    async fn unverified() {
124        let registry = RegistryTest::new().await;
125        let v1 = registry.hello_wasm_v1();
126
127        registry
128            .registry_cli("publish")
129            .arg("--wasm")
130            .arg(v1.to_str().unwrap())
131            .arg("--binver")
132            .arg("0.0.1")
133            .arg("--wasm-name")
134            .arg("unverified/hello")
135            .assert()
136            .success();
137
138        let version = registry
139            .parse_cmd::<super::Cmd>(&["unverified/hello"])
140            .unwrap()
141            .current_version()
142            .await
143            .unwrap();
144        assert_eq!(version, "0.0.1");
145    }
146}