Skip to content

Commit b7b62ef

Browse files
Add possibility to specify scarb version in snippet config (#3650)
<!-- Reference any GitHub issues resolved by this PR --> Closes # ## Introduced changes Allow to set `scarb_version` in `SnippetConfig`. If scarb version is incompatible, snippet will be ignored. ## Checklist <!-- Make sure all of these are complete --> - [ ] Linked relevant issue - [x] Updated relevant documentation - [ ] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent 970b658 commit b7b62ef

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/docs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ toml_edit.workspace = true
1515
camino.workspace = true
1616
tempfile.workspace = true
1717
anyhow.workspace = true
18+
semver.workspace = true
19+
scarb-api = { path = "../scarb-api" }
1820

1921
[features]
2022
testing = []

crates/docs/src/snippet.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::sync::LazyLock;
22

33
use regex::Regex;
4+
use scarb_api::ScarbCommand;
5+
use semver::VersionReq;
46
use serde::{Deserialize, Serialize};
57

68
static RE_SNCAST: LazyLock<Regex> = LazyLock::new(|| {
@@ -52,13 +54,36 @@ impl SnippetType {
5254
}
5355
}
5456

55-
#[derive(Debug, Deserialize, Serialize)]
57+
#[derive(Debug, Serialize)]
5658
#[serde(default)]
5759
pub struct SnippetConfig {
5860
pub ignored: bool,
5961
pub package_name: Option<String>,
6062
pub ignored_output: bool,
6163
pub replace_network: bool,
64+
pub scarb_version: Option<VersionReq>,
65+
}
66+
67+
#[derive(Deserialize)]
68+
#[serde(default)]
69+
struct SnippetConfigProxy {
70+
ignored: bool,
71+
package_name: Option<String>,
72+
ignored_output: bool,
73+
replace_network: bool,
74+
scarb_version: Option<VersionReq>,
75+
}
76+
77+
impl Default for SnippetConfigProxy {
78+
fn default() -> Self {
79+
Self {
80+
ignored: false,
81+
package_name: None,
82+
ignored_output: false,
83+
replace_network: true,
84+
scarb_version: None,
85+
}
86+
}
6287
}
6388

6489
impl Default for SnippetConfig {
@@ -68,10 +93,47 @@ impl Default for SnippetConfig {
6893
package_name: None,
6994
ignored_output: false,
7095
replace_network: true,
96+
scarb_version: None,
97+
}
98+
}
99+
}
100+
101+
impl SnippetConfig {
102+
fn check_scarb_compatibility(&mut self) {
103+
if let Some(ref scarb_version_req) = self.scarb_version {
104+
let current_scarb_version = ScarbCommand::version()
105+
.run()
106+
.expect("Failed to get scarb version")
107+
.scarb;
108+
109+
if !scarb_version_req.matches(&current_scarb_version) {
110+
self.ignored = true;
111+
}
71112
}
72113
}
73114
}
74115

116+
impl<'de> Deserialize<'de> for SnippetConfig {
117+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
118+
where
119+
D: serde::de::Deserializer<'de>,
120+
{
121+
let proxy = SnippetConfigProxy::deserialize(deserializer)?;
122+
123+
let mut config = SnippetConfig {
124+
ignored: proxy.ignored,
125+
package_name: proxy.package_name,
126+
ignored_output: proxy.ignored_output,
127+
replace_network: proxy.replace_network,
128+
scarb_version: proxy.scarb_version,
129+
};
130+
131+
config.check_scarb_compatibility();
132+
133+
Ok(config)
134+
}
135+
}
136+
75137
#[derive(Debug)]
76138
pub struct Snippet {
77139
pub command: String,

docs/src/appendix/cheatcodes/l1_handler.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ Test code:
3030
{{#include ../../../listings/cheatcodes_reference/tests/test_l1_handler.cairo}}
3131
```
3232

33-
<!-- { "package_name": "cheatcodes_reference" } -->
3433
Let's run the test:
34+
35+
<!-- { "package_name": "cheatcodes_reference", "scarb_version": ">=2.11.4" } -->
3536
```shell
3637
$ snforge test test_l1_handler
3738
```

docs/src/appendix/cheatcodes/mock_call.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Test code:
4343
```
4444

4545
Let's run the test:
46+
47+
<!-- { "package_name": "cheatcodes_reference", "scarb_version": ">=2.11.4" } -->
4648
```shell
4749
$ snforge test test_mock_call
4850
```

docs/src/development/shell-snippets.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ account: https://sepolia.starkscan.co/contract/[..]
3636
- `ignored` - if set to `true`, the snippet will be ignored and not run.
3737
- `package_name` - the name of the Scarb package in which the snippet should be run.
3838
- `ignored_output` - if set to `true`, the output of executed command will be ignored.
39+
- `replace_network` - if set to `true`, the snippet will replace the `--network` argument with devnet used in tests.
40+
- `scarb_version` - specifies the Scarb version required to run the snippet. If the current Scarb version does not match, the snippet will be ignored. The version should be in the format compatible with [semver](https://semver.org/).

0 commit comments

Comments
 (0)