Skip to content

Commit 4bce024

Browse files
feat(apollo_starknet_os_program): cairo file list test + fixer
1 parent 7053731 commit 4bce024

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

Cargo.lock

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

crates/apollo_starknet_os_program/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ description = "The source (Cairo) code of the Starknet OS."
88

99
[lints]
1010
workspace = true
11+
12+
[dependencies]
13+
apollo_infra_utils.workspace = true
14+
serde_json.workspace = true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
"starkware/starknet/common/new_syscalls.cairo",
3+
"starkware/starknet/core/aggregator/combine_blocks.cairo",
4+
"starkware/starknet/core/aggregator/main.cairo",
5+
"starkware/starknet/core/os/block_context.cairo",
6+
"starkware/starknet/core/os/builtins.cairo",
7+
"starkware/starknet/core/os/constants.cairo",
8+
"starkware/starknet/core/os/contract_address/contract_address.cairo",
9+
"starkware/starknet/core/os/contract_class/compiled_class.cairo",
10+
"starkware/starknet/core/os/contract_class/contract_class.cairo",
11+
"starkware/starknet/core/os/contract_class/deprecated_compiled_class.cairo",
12+
"starkware/starknet/core/os/data_availability/bls_field.cairo",
13+
"starkware/starknet/core/os/data_availability/commitment.cairo",
14+
"starkware/starknet/core/os/data_availability/compression.cairo",
15+
"starkware/starknet/core/os/execution/account_backward_compatibility.cairo",
16+
"starkware/starknet/core/os/execution/deprecated_execute_entry_point.cairo",
17+
"starkware/starknet/core/os/execution/deprecated_execute_syscalls.cairo",
18+
"starkware/starknet/core/os/execution/execute_entry_point.cairo",
19+
"starkware/starknet/core/os/execution/execute_syscalls.cairo",
20+
"starkware/starknet/core/os/execution/execute_transaction_utils.cairo",
21+
"starkware/starknet/core/os/execution/execute_transactions.cairo",
22+
"starkware/starknet/core/os/execution/revert.cairo",
23+
"starkware/starknet/core/os/os.cairo",
24+
"starkware/starknet/core/os/os_config/os_config.cairo",
25+
"starkware/starknet/core/os/output.cairo",
26+
"starkware/starknet/core/os/state/aliases.cairo",
27+
"starkware/starknet/core/os/state/aliases_test.cairo",
28+
"starkware/starknet/core/os/state/commitment.cairo",
29+
"starkware/starknet/core/os/state/output.cairo",
30+
"starkware/starknet/core/os/state/squash.cairo",
31+
"starkware/starknet/core/os/state/state.cairo",
32+
"starkware/starknet/core/os/transaction_hash/transaction_hash.cairo"
33+
]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::collections::HashSet;
2+
use std::fs::{DirEntry, File};
3+
use std::path::PathBuf;
4+
use std::sync::LazyLock;
5+
6+
use apollo_infra_utils::compile_time_cargo_manifest_dir;
7+
8+
static CAIRO_FILE_LIST_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
9+
PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo_files_list.json")
10+
});
11+
static CAIRO_FILE_LIST: LazyLock<Vec<String>> = LazyLock::new(|| {
12+
serde_json::from_reader(
13+
File::open(&*CAIRO_FILE_LIST_PATH)
14+
.unwrap_or_else(|error| panic!("Failed to open {CAIRO_FILE_LIST_PATH:?}: {error:?}.")),
15+
)
16+
.unwrap_or_else(|error| panic!("Failed to deserialize {CAIRO_FILE_LIST_PATH:?}: {error:?}."))
17+
});
18+
19+
/// Utility function for `get_cairo_file_list`.
20+
fn get_cairo_file_list_recursive(entry: DirEntry) -> Vec<String> {
21+
let file_type = entry.file_type().unwrap();
22+
if file_type.is_dir() {
23+
std::fs::read_dir(entry.path())
24+
.unwrap()
25+
.flat_map(|entry| get_cairo_file_list_recursive(entry.unwrap()))
26+
.collect()
27+
} else {
28+
assert!(file_type.is_file());
29+
if entry.path().extension().unwrap_or_default() == "cairo" {
30+
Vec::from_iter(std::iter::once(entry.path().to_str().unwrap().to_string()))
31+
} else {
32+
Vec::new()
33+
}
34+
}
35+
}
36+
37+
/// Find all files with a .cairo extension in the `src` directory.
38+
fn get_cairo_file_set() -> HashSet<String> {
39+
let base_path = PathBuf::from(compile_time_cargo_manifest_dir!()).join("src");
40+
let base_path_string = base_path.to_str().unwrap();
41+
std::fs::read_dir(base_path_string)
42+
.unwrap()
43+
.flat_map(|entry| get_cairo_file_list_recursive(entry.unwrap()))
44+
.map(|path| {
45+
assert!(path.starts_with(&base_path_string));
46+
path.strip_prefix(&base_path_string)
47+
.unwrap()
48+
.strip_prefix("/cairo/")
49+
.unwrap()
50+
.to_string()
51+
})
52+
.collect()
53+
}
54+
55+
/// Tests that the list of Cairo files in the `src` directory matches the actual set of cairo files
56+
/// in the crate.
57+
/// To fix this test, run the following command:
58+
/// ```bash
59+
/// FIX_OS_FILE_LIST=1 cargo test -p apollo_starknet_os_program test_cairo_file_list
60+
/// ```
61+
#[test]
62+
fn test_cairo_file_list() {
63+
let actual_files = get_cairo_file_set();
64+
let expected_files: HashSet<String> = HashSet::from_iter(CAIRO_FILE_LIST.iter().cloned());
65+
if std::env::var("FIX_OS_FILE_LIST").is_ok() {
66+
let mut actual_files_vec = Vec::from_iter(actual_files.iter());
67+
actual_files_vec.sort();
68+
std::fs::write(
69+
CAIRO_FILE_LIST_PATH.as_path(),
70+
serde_json::to_string_pretty(&actual_files_vec).unwrap(),
71+
)
72+
.expect("Failed to write the cairo file list.");
73+
} else {
74+
assert_eq!(actual_files, expected_files);
75+
}
76+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
#[cfg(test)]
2+
mod cairo_files_list_test;

0 commit comments

Comments
 (0)