Skip to content

Commit

Permalink
misc: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Oct 24, 2024
1 parent 1ec6869 commit 90ff84e
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 80 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ insta = { workspace = true, features = ["yaml", "glob"] }
rstest = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["rt"] }
which = "*"


[patch.crates-io]
Expand Down
1 change: 0 additions & 1 deletion crates/pixi_trampoline/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct Metadata {
fn read_metadata(current_exe: &Path) -> Metadata {
// the metadata file is next to the current executable, under the name of exe + ".json"
let metadata_path = current_exe.with_extension("json");
eprintln!("current exe is {:?}", current_exe);
let metadata_file = File::open(metadata_path).unwrap();
let metadata: Metadata = serde_json::from_reader(metadata_file).unwrap();
metadata
Expand Down
82 changes: 5 additions & 77 deletions src/global/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ pub const TRAMPOLINE_BIN: &[u8] =

#[cfg(target_arch = "aarch64")]
#[cfg(target_os = "windows")]
const TRAMPOLINE_BIN: &[u8] = include_bytes!(
pub const TRAMPOLINE_BIN: &[u8] = include_bytes!(
"../../crates/pixi_trampoline/trampolines/pixi-trampoline-aarch64-pc-windows-msvc.exe"
);

#[cfg(target_arch = "aarch64")]
#[cfg(target_os = "linux")]
const TRAMPOLINE_BIN: &[u8] = include_bytes!(
pub const TRAMPOLINE_BIN: &[u8] = include_bytes!(
"../../crates/pixi_trampoline/trampolines/pixi-trampoline-aarch64-unknown-linux-musl"
);

#[cfg(target_arch = "x86_64")]
#[cfg(target_os = "macos")]
const TRAMPOLINE_BIN: &[u8] =
pub const TRAMPOLINE_BIN: &[u8] =
include_bytes!("../../crates/pixi_trampoline/trampolines/pixi-trampoline-x86_64-apple-darwin");

#[cfg(target_arch = "x86_64")]
#[cfg(target_os = "windows")]
const TRAMPOLINE_BIN: &[u8] = include_bytes!(
pub const TRAMPOLINE_BIN: &[u8] = include_bytes!(
"../../crates/pixi_trampoline/trampolines/pixi-trampoline-x86_64-pc-windows-msvc.exe"
);

#[cfg(target_arch = "x86_64")]
#[cfg(target_os = "linux")]
const TRAMPOLINE_BIN: &[u8] = include_bytes!(
pub const TRAMPOLINE_BIN: &[u8] = include_bytes!(
"../../crates/pixi_trampoline/trampolines/pixi-trampoline-x86_64-unknown-linux-musl"
);

Expand Down Expand Up @@ -65,7 +65,6 @@ impl Trampoline {
#[cfg(test)]
mod tests {
use super::*;
use which::which;

#[test]
fn test_trampoline_creation() {
Expand All @@ -75,75 +74,4 @@ mod tests {
"Binary should not be empty"
);
}

#[test]
fn test_python_execution_using_trampoline() {
let python_script = format!(
r##"
# -*- coding: utf-8 -*-
import os
import sys
def main():
# Get the environment variable
env_var_value = os.getenv('TRAMPOLINE_TEST_ENV')
# Check if it's set to 'teapot'
assert env_var_value == "teapot"
if __name__ == "__main__":
main()
"##
);
// Locate an arbitrary python installation from PATH
let python_executable_path = which("python").unwrap();

let trampoline = Trampoline::new();

let env_dir = std::env::temp_dir();
let trampoline_test_script_path = env_dir.join("trampoline_test.py");

let trampoline_path = env_dir.join("trampoline");
// trampoline_script_path.push("trampoline_test.py");

std::fs::write(&trampoline_test_script_path, python_script)
.expect("Failed to write trampoline script");
std::fs::write(&trampoline_path, TRAMPOLINE_BIN)
.expect("Failed to write trampoline script");

// set permission
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let metadata = std::fs::metadata(&trampoline_path).expect("Failed to get metadata");
let mut permissions = metadata.permissions();
permissions.set_mode(0o755); // rwxr-xr-x
std::fs::set_permissions(&trampoline_path, permissions)
.expect("Failed to set permissions");
}

let mut current_env = std::env::vars().collect::<HashMap<String, String>>();
current_env.insert("TRAMPOLINE_TEST_ENV".to_string(), "teapot".to_string());

let metadata = ManifestMetadata {
exe: python_executable_path.clone(),
path: python_executable_path
.parent()
.unwrap()
.to_string_lossy()
.to_string(),
env: current_env,
};

let json_path = env_dir.join("trampoline.json");
// serde deser into json
let file = std::fs::File::create(&json_path).expect("Failed to create json file");
serde_json::to_writer(file, &metadata).expect("Failed to write metadata to json file");

// start the trampoline and pass an argument

let mut command = std::process::Command::new(trampoline_path);
command.arg(trampoline_test_script_path);
let status = command.status().expect("Failed to execute command");
eprintln!("status is {:?}", status);
}
}

0 comments on commit 90ff84e

Please sign in to comment.