Skip to content

Commit 9a6fdb9

Browse files
Merge pull request #20 from Chia-Network/20240904-m1-tests
20240904 m1 tests
2 parents 801c93c + 617a17b commit 9a6fdb9

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.github/workflows/m1-test.yml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Run tests on MacOS
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
release:
9+
types: [published]
10+
pull_request:
11+
branches:
12+
- '**'
13+
14+
concurrency:
15+
group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}--${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }}
16+
cancel-in-progress: true
17+
18+
permissions:
19+
id-token: write
20+
contents: read
21+
22+
jobs:
23+
build_wheels:
24+
name: Build wheel on Mac M1
25+
runs-on: [MacOS, ARM64]
26+
strategy:
27+
fail-fast: false
28+
29+
steps:
30+
- uses: Chia-Network/actions/clean-workspace@main
31+
32+
- name: Checkout code
33+
uses: actions/checkout@v3
34+
with:
35+
fetch-depth: 0
36+
37+
- name: Set up rust
38+
run: |
39+
curl https://static.rust-lang.org/rustup/dist/aarch64-apple-darwin/rustup-init.sha256 | awk '{print $1 " *rustup-init"}' > checksum.txt
40+
curl -O https://static.rust-lang.org/rustup/dist/aarch64-apple-darwin/rustup-init
41+
cat checksum.txt
42+
shasum -a 256 -c checksum.txt
43+
44+
- name: Install rust
45+
run: |
46+
chmod +x rustup-init
47+
./rustup-init -y || (echo "Rust is already installed. Exiting..." && exit 2)
48+
rm rustup-init checksum.txt
49+
50+
- name: Set up a venv for chia-blockchain
51+
run: |
52+
python3 -m venv ./test
53+
source ./test/bin/activate
54+
pip install chia-blockchain
55+
56+
- name: Run tests
57+
run: |
58+
source ./test/bin/activate
59+
cargo test --features=sim-tests

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tracing-subscriber = "0.3"
3333
ctor = "0.2.8"
3434
log = "0.4.22"
3535
env_logger = "0.11.3"
36+
exec = "0.3.1"
3637
pyo3 = { version = "0.20.0", features = ["auto-initialize"], optional = true }
3738

3839
[dev-dependencies]

src/simulator.rs

+24
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ fn to_spend_result(py: Python<'_>, spend_res: PyObject) -> PyResult<IncludeTrans
109109

110110
impl Default for Simulator {
111111
fn default() -> Self {
112+
// https://github.com/PyO3/pyo3/issues/1741
113+
#[cfg(target_os = "macos")]
114+
if let Ok(venv) = std::env::var("VIRTUAL_ENV") {
115+
Python::with_gil(|py| -> PyResult<_> {
116+
let version_info = py.version_info();
117+
let sys = py.import("sys").unwrap();
118+
let sys_path = sys.getattr("path").unwrap();
119+
sys_path
120+
.call_method1(
121+
"insert",
122+
(
123+
0,
124+
format!(
125+
"{}/lib/python{}.{}/site-packages",
126+
venv, version_info.major, version_info.minor
127+
),
128+
),
129+
)
130+
.unwrap();
131+
Ok(())
132+
})
133+
.unwrap();
134+
}
135+
112136
Python::with_gil(|py| -> PyResult<_> {
113137
let module = PyModule::from_code(
114138
py,

src/tests/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use exec::execvp;
2+
use std::ffi::OsString;
3+
14
pub mod calpoker;
25
pub mod channel_handler;
36
pub mod chialisp;
@@ -9,3 +12,31 @@ pub mod referee;
912
#[cfg(feature = "sim-tests")]
1013
pub mod simenv;
1114
pub mod standard_coin;
15+
16+
fn detect_run_as_python(args: &[String]) -> bool {
17+
args.iter().any(|x: &String| x == "-c")
18+
}
19+
20+
// Catch attempts on macos to run the test rig as 'python' and run python code in it.
21+
#[ctor::ctor]
22+
fn init() {
23+
let mut args = std::env::args();
24+
let args_vec: Vec<String> = args.collect();
25+
if detect_run_as_python(&args_vec) {
26+
let new_args: Vec<OsString> = args_vec
27+
.iter()
28+
.enumerate()
29+
.map(
30+
|(i, arg)| {
31+
if i == 0 {
32+
"python3".into()
33+
} else {
34+
arg.into()
35+
}
36+
},
37+
)
38+
.collect();
39+
let exec_err = execvp("python3", &new_args);
40+
eprintln!("Error Running: {:?}\n{:?}\n", new_args, exec_err);
41+
}
42+
}

0 commit comments

Comments
 (0)