Skip to content

Commit 6b483d5

Browse files
committed
Run tests on a new and compatibility plugin
commit-id:aa2a40e5
1 parent 8bad799 commit 6b483d5

File tree

25 files changed

+424
-262
lines changed

25 files changed

+424
-262
lines changed

.github/workflows/scheduled.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
run: |
9292
curl -L https://raw.githubusercontent.com/software-mansion/cairo-coverage/main/scripts/install.sh | sh
9393
94-
- run: cargo test --release -p forge e2e
94+
- run: cargo test --release -p forge --features skip_plugin_diagnostics e2e
9595

9696
test-cast:
9797
if: github.event.repository.fork == false
@@ -151,6 +151,7 @@ jobs:
151151
uses: ./.github/workflows/_build-plugin-binaries.yml
152152
with:
153153
overridden_plugin_version: ${{ needs.get-version.outputs.version }}-test.${{ github.run_id }}
154+
plugin_name: 'snforge-scarb-plugin'
154155

155156
publish-plugin:
156157
needs: [get-version, build-plugin-binaries]
@@ -159,8 +160,23 @@ jobs:
159160
with:
160161
overridden_plugin_version: ${{ needs.get-version.outputs.version }}-test.${{ github.run_id }}
161162

163+
build-compatibility-plugin-binaries:
164+
name: Build compatibility plugin binaries
165+
needs: get-version
166+
uses: ./.github/workflows/_build-plugin-binaries.yml
167+
with:
168+
overridden_plugin_version: ${{ needs.get-version.outputs.version }}-test.${{ github.run_id }}
169+
plugin_name: 'snforge-scarb-plugin-compatibility'
170+
171+
publish-compatibility-plugin:
172+
needs: [get-version, build-compatibility-plugin-binaries]
173+
uses: ./.github/workflows/_publish-plugin.yml
174+
secrets: inherit
175+
with:
176+
overridden_plugin_version: ${{ needs.get-version.outputs.version }}-test.${{ github.run_id }}
177+
162178
publish-std:
163-
needs: [get-version, publish-plugin]
179+
needs: [get-version, publish-plugin, publish-compatibility-plugin]
164180
uses: ./.github/workflows/publish-std.yml
165181
secrets: inherit
166182
with:

Cargo.lock

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

crates/forge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ scarb_2_9_1 = []
1111
no_scarb_installed = []
1212
debugging = []
1313
assert_non_exact_gas = ["test_utils/assert_non_exact_gas"]
14+
skip_plugin_diagnostics = []
1415
supports-panic-backtrace = []
1516

1617
[dependencies]

crates/forge/src/new.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ impl TemplateManifestConfig {
7676
Ok(())
7777
}
7878

79-
fn update_config(&self, scarb_manifest_path: &Path) -> Result<()> {
79+
fn update_config(
80+
&self,
81+
scarb_manifest_path: &Path,
82+
use_snforge_std_compatibility: bool,
83+
) -> Result<()> {
8084
let scarb_toml_content = fs::read_to_string(scarb_manifest_path)?;
8185
let mut document = scarb_toml_content
8286
.parse::<DocumentMut>()
@@ -89,7 +93,7 @@ impl TemplateManifestConfig {
8993
set_cairo_edition(&mut document, CAIRO_EDITION);
9094
add_test_script(&mut document);
9195
add_assert_macros(&mut document)?;
92-
add_allow_prebuilt_macros(&mut document)?;
96+
add_allow_prebuilt_macros(&mut document, use_snforge_std_compatibility)?;
9397

9498
if self.fork_config {
9599
add_fork_config(&mut document)?;
@@ -184,18 +188,30 @@ fn overwrite_or_copy_template_files(
184188
template_path: &Path,
185189
project_path: &Path,
186190
project_name: &str,
191+
use_snforge_std_compatibility: bool,
187192
) -> Result<()> {
188193
for entry in dir.entries() {
189194
let path_without_template_name = entry.path().strip_prefix(template_path)?;
190195
let destination = project_path.join(path_without_template_name);
191196
match entry {
192197
DirEntry::Dir(dir) => {
193198
fs::create_dir_all(&destination)?;
194-
overwrite_or_copy_template_files(dir, template_path, project_path, project_name)?;
199+
overwrite_or_copy_template_files(
200+
dir,
201+
template_path,
202+
project_path,
203+
project_name,
204+
use_snforge_std_compatibility,
205+
)?;
195206
}
196207
DirEntry::File(file) => {
197208
let contents = file.contents();
198209
let contents = replace_project_name(contents, project_name)?;
210+
let contents = if use_snforge_std_compatibility {
211+
replace_imports_snforge_std_with_compatibility(&contents)?
212+
} else {
213+
contents
214+
};
199215
fs::write(destination, contents)?;
200216
}
201217
}
@@ -204,6 +220,12 @@ fn overwrite_or_copy_template_files(
204220
Ok(())
205221
}
206222

223+
fn replace_imports_snforge_std_with_compatibility(contents: &[u8]) -> Result<Vec<u8>> {
224+
let contents = std::str::from_utf8(contents).context("UTF-8 error")?;
225+
let contents = contents.replace("snforge_std::", "snforge_std_compatibility::");
226+
Ok(contents.into_bytes())
227+
}
228+
207229
fn replace_project_name(contents: &[u8], project_name: &str) -> Result<Vec<u8>> {
208230
let contents = std::str::from_utf8(contents).context("UTF-8 error")?;
209231
let contents = contents.replace("{{ PROJECT_NAME }}", project_name);
@@ -251,7 +273,10 @@ fn add_assert_macros(document: &mut DocumentMut) -> Result<()> {
251273
Ok(())
252274
}
253275

254-
fn add_allow_prebuilt_macros(document: &mut DocumentMut) -> Result<()> {
276+
fn add_allow_prebuilt_macros(
277+
document: &mut DocumentMut,
278+
use_snforge_std_compatibility: bool,
279+
) -> Result<()> {
255280
let tool_section = document.entry("tool").or_insert(Item::Table(Table::new()));
256281
let tool_table = tool_section
257282
.as_table_mut()
@@ -261,7 +286,11 @@ fn add_allow_prebuilt_macros(document: &mut DocumentMut) -> Result<()> {
261286
let mut scarb_table = Table::new();
262287

263288
let mut allow_prebuilt_macros = Array::new();
264-
allow_prebuilt_macros.push("snforge_std");
289+
if use_snforge_std_compatibility {
290+
allow_prebuilt_macros.push("snforge_std_compatibility");
291+
} else {
292+
allow_prebuilt_macros.push("snforge_std");
293+
}
265294

266295
scarb_table.insert(
267296
"allow-prebuilt-plugins",
@@ -372,12 +401,21 @@ pub fn new(
372401
create_snfoundry_manifest(&snfoundry_manifest_path)?;
373402
}
374403

404+
// TODO: Check Scarb `2.12`
405+
let use_snforge_compatibility = scarb_version.build.is_empty();
406+
375407
let template_config = TemplateManifestConfig::try_from(&template)?;
376408
template_config.add_dependencies(&scarb_version, &scarb_manifest_path)?;
377-
template_config.update_config(&scarb_manifest_path)?;
409+
template_config.update_config(&scarb_manifest_path, use_snforge_compatibility)?;
378410

379411
let template_dir = get_template_dir(&template)?;
380-
overwrite_or_copy_template_files(&template_dir, template_dir.path(), &project_path, &name)?;
412+
overwrite_or_copy_template_files(
413+
&template_dir,
414+
template_dir.path(),
415+
&project_path,
416+
&name,
417+
use_snforge_compatibility,
418+
)?;
381419

382420
extend_gitignore(&project_path)?;
383421

crates/forge/src/scarb.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,14 @@ mod tests {
120120
use crate::scarb::config::ForkTarget;
121121
use assert_fs::TempDir;
122122
use assert_fs::fixture::{FileWriteStr, PathChild, PathCopy};
123-
use camino::Utf8PathBuf;
124123
use cheatnet::runtime_extensions::forge_config_extension::config::BlockId;
125124
use configuration::load_package_config;
126125
use forge_runner::forge_config::ForgeTrackedResource;
127126
use indoc::{formatdoc, indoc};
128127
use scarb_api::metadata::MetadataCommandExt;
129128
use scarb_metadata::PackageId;
130129
use std::env;
131-
use std::str::FromStr;
132-
use test_utils::tempdir_with_tool_versions;
130+
use test_utils::{get_snforge_std_entry, tempdir_with_tool_versions};
133131

134132
fn setup_package(package_name: &str) -> TempDir {
135133
let temp = tempdir_with_tool_versions().unwrap();
@@ -139,12 +137,7 @@ mod tests {
139137
)
140138
.unwrap();
141139

142-
let snforge_std_path = Utf8PathBuf::from_str("../../snforge_std")
143-
.unwrap()
144-
.canonicalize_utf8()
145-
.unwrap()
146-
.to_string()
147-
.replace('\\', "/");
140+
let snforge_std_entry = get_snforge_std_entry().unwrap();
148141

149142
let manifest_path = temp.child("Scarb.toml");
150143
manifest_path
@@ -156,7 +149,7 @@ mod tests {
156149
157150
[dependencies]
158151
starknet = "2.4.0"
159-
snforge_std = {{ path = "{}" }}
152+
{}
160153
161154
[[tool.snforge.fork]]
162155
name = "FIRST_FORK_NAME"
@@ -179,7 +172,7 @@ mod tests {
179172
block_id.tag = "latest"
180173
"#,
181174
package_name,
182-
snforge_std_path
175+
snforge_std_entry
183176
))
184177
.unwrap();
185178

crates/forge/test_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition.workspace = true
99
assert_non_exact_gas = []
1010

1111
[dependencies]
12+
walkdir.workspace = true
1213
anyhow.workspace = true
1314
shared.workspace = true
1415
blockifier.workspace = true

crates/forge/test_utils/src/lib.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
pub mod runner;
22
pub mod running_tests;
33

4-
use std::path::PathBuf;
5-
64
use anyhow::Result;
75
use assert_fs::fixture::PathCopy;
6+
use camino::Utf8PathBuf;
87
use project_root::get_project_root;
98
use scarb_api::ScarbCommand;
109
use semver::Version;
10+
use std::str::FromStr;
1111

1212
const DEFAULT_ASSERT_MACROS: Version = Version::new(0, 1, 0);
1313
const MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS: Version = Version::new(2, 8, 0);
1414

15-
pub fn get_local_snforge_std_absolute_path() -> Result<PathBuf> {
16-
Ok(get_project_root()?.canonicalize()?.join("snforge_std"))
17-
}
18-
1915
pub fn tempdir_with_tool_versions() -> Result<assert_fs::TempDir> {
2016
let project_root = get_project_root()?;
2117
let temp_dir = assert_fs::TempDir::new()?;
@@ -33,3 +29,35 @@ pub fn get_assert_macros_version() -> Result<Version> {
3329
};
3430
Ok(assert_macros_version)
3531
}
32+
33+
#[must_use]
34+
pub fn get_std_name() -> String {
35+
if use_snforge_std_compatibility() {
36+
"snforge_std_compatibility".to_string()
37+
} else {
38+
"snforge_std".to_string()
39+
}
40+
}
41+
42+
pub fn get_std_path() -> Result<String> {
43+
let name = get_std_name();
44+
Ok(Utf8PathBuf::from_str(&format!("../../{name}"))?
45+
.canonicalize_utf8()?
46+
.to_string())
47+
}
48+
49+
pub fn get_snforge_std_entry() -> Result<String> {
50+
let name = get_std_name();
51+
let path = get_std_path()?;
52+
53+
Ok(format!("{name} = {{ path = \"{path}\" }}"))
54+
}
55+
56+
#[must_use]
57+
pub fn use_snforge_std_compatibility() -> bool {
58+
let scarb_version_output = ScarbCommand::version()
59+
.run()
60+
.expect("Failed to get scarb version");
61+
// TODO: Replace with condition for `2.12`
62+
scarb_version_output.scarb.build.is_empty()
63+
}

crates/forge/test_utils/src/runner.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{get_assert_macros_version, tempdir_with_tool_versions};
1+
use crate::{
2+
get_assert_macros_version, get_std_name, get_std_path, tempdir_with_tool_versions,
3+
use_snforge_std_compatibility,
4+
};
25
use anyhow::{Context, Result, anyhow};
36
use assert_fs::{
47
TempDir,
@@ -27,6 +30,7 @@ use std::{
2730
process::{Command, Stdio},
2831
str::FromStr,
2932
};
33+
use walkdir::WalkDir;
3034

3135
/// Represents a dependency of a Cairo project
3236
#[derive(Debug, Clone)]
@@ -112,6 +116,18 @@ impl Contract {
112116
}
113117
}
114118

119+
pub fn replace_snforge_std_with_snforge_std_compatibility(dir: &Path) {
120+
let temp_dir_files = WalkDir::new(dir);
121+
for entry in temp_dir_files {
122+
let entry = entry.unwrap();
123+
if entry.path().is_file() {
124+
let content = fs::read_to_string(entry.path()).unwrap();
125+
let modified_content = content.replace("snforge_std", "snforge_std_compatibility");
126+
fs::write(entry.path(), modified_content).unwrap();
127+
}
128+
}
129+
}
130+
115131
#[derive(Debug)]
116132
pub struct TestCase {
117133
dir: TempDir,
@@ -129,31 +145,28 @@ impl<'a> TestCase {
129145
test_file.touch()?;
130146
test_file.write_str(test_code)?;
131147

132-
dir.child("src/lib.cairo").touch().unwrap();
148+
if use_snforge_std_compatibility() {
149+
replace_snforge_std_with_snforge_std_compatibility(test_file.path());
150+
}
133151

134-
let snforge_std_path = Utf8PathBuf::from_str("../../snforge_std")
135-
.unwrap()
136-
.canonicalize_utf8()
137-
.unwrap()
138-
.to_string()
139-
.replace('\\', "/");
152+
dir.child("src/lib.cairo").touch().unwrap();
140153

154+
let snforge_std_name = get_std_name();
155+
let snforge_std_path = get_std_path().unwrap();
141156
let assert_macros_version = get_assert_macros_version()?.to_string();
142157

143158
let scarb_toml_path = dir.child("Scarb.toml");
144159
scarb_toml_path.write_str(&formatdoc!(
145160
r#"
146-
[package]
147-
name = "test_package"
148-
version = "0.1.0"
149-
150-
[dependencies]
151-
starknet = "2.4.0"
152-
snforge_std = {{ path = "{}" }}
153-
assert_macros = "{}"
154-
"#,
155-
snforge_std_path,
156-
assert_macros_version
161+
[package]
162+
name = "test_package"
163+
version = "0.1.0"
164+
165+
[dependencies]
166+
starknet = "2.4.0"
167+
{snforge_std_name} = {{ path = "{snforge_std_path}" }}
168+
assert_macros = "{assert_macros_version}"
169+
"#
157170
))?;
158171

159172
Ok(Self {

crates/forge/tests/code_quality.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use camino::Utf8PathBuf;
22
use packages_validation::check_and_lint;
3+
use test_utils::use_snforge_std_compatibility;
34

45
#[test]
56
fn validate_snforge_std() {
@@ -8,5 +9,7 @@ fn validate_snforge_std() {
89
.unwrap()
910
.try_into()
1011
.unwrap();
11-
check_and_lint(&package_path);
12+
if !use_snforge_std_compatibility() {
13+
check_and_lint(&package_path);
14+
}
1215
}

0 commit comments

Comments
 (0)