Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): resolving asv env explicitly #35

Merged
merged 21 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tokio = { version = "1.36.0", features = ["rt-multi-thread", "process"] }
tower-http = { version = "0.5.1", features = ["trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
serde_json = "1.0"

[target.'cfg(target_os = "linux")'.dependencies]
libsystemd = "0.7.0"
Expand All @@ -41,7 +42,6 @@ temp-env = "0.3.6"
test-temp-dir = "0.2.0"
# transitive deps we use directly
http = "1.0.0"
serde_json = "1.0"
hmac-sha256 = "1.1.7"
hex = "0.4.3"
tower = "0.4.13"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ All these currently assume you have a <samp>&lt;user></samp> login with sudo rig
micromamba run -n asv asv machine --yes
```

(use `micromamba activate asv` to make `asv` available in your PATH)

2. Update `LoadCredentialEncrypted` lines in <samp>benchmark.service</samp> using

```shell
Expand Down
5 changes: 3 additions & 2 deletions scripts/test.nu
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Source this file using `source test.nu`, then run e.g.:
# In one shell run:
#
# ```nushell
# cargo run -- --dry-run serve --secret-token "It's a Secret to Everybody"
# ```
#
# and in another shell:
# After installing `jaq` and `libgcrypt` in another nushell (activated via the `nu` command after installation), run the following:
#
# ```nushell
# source test.nu
# gh-hook http://localhost:3000/ (open ./src/fixtures/test.hook-pr-sync.json) --full --allow-errors
# ```

Expand Down
60 changes: 60 additions & 0 deletions src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ impl AsvCompare {
}
}

pub async fn resolve_env(wd: &Path) -> Result<Vec<String>> {
tracing::info!("Resolving Environments: {:?}", wd);
resolve_env_from_stdout(
Command::new("python")
.current_dir(wd)
.args(["-c", include_str!("resolve_env.py")]),
)
.await
}

async fn resolve_env_from_stdout(command: &mut Command) -> Result<Vec<String>> {
let stdout_env_specs_buffer = command.output().await?.stdout;
let stdout_env_specs = String::from_utf8(stdout_env_specs_buffer)?;
let parsed: Vec<String> = serde_json::from_str(&stdout_env_specs)?;
tracing::info!("Found environments: {:?}", parsed);
Ok(parsed)
}

async fn run_benchmark(repo: git2::Repository, on: &[String]) -> Result<PathBuf> {
let wd = {
let on = on.to_owned();
Expand All @@ -110,6 +128,10 @@ async fn run_benchmark(repo: git2::Repository, on: &[String]) -> Result<PathBuf>
tracing::info!("Running asv in {}", wd.display());
let mut command = asv_command(&wd);
command.arg("run"); // This skips even if benchmarks changed: .arg("--skip-existing-commits");
let env_specs = resolve_env(&wd).await?;
for env_spec in env_specs {
command.args(["-E", &env_spec]);
}
let mut child = if on.is_empty() {
command.spawn().context("failed to spawn `asv run`")?
} else {
Expand Down Expand Up @@ -173,3 +195,41 @@ fn fetch_configured_refs(repo: &git2::Repository, refs: &[String]) -> Result<Pat
}
Ok(wd)
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_resolve_env() {
let resolved_envs =
resolve_env_from_stdout(Command::new("echo").arg("[\"env0\", \"env1\", \"env2\"]"))
.await
.expect("Parsing unexpectedly failed for echo-ing a list of strings.");
assert_eq!(resolved_envs, vec!["env0", "env1", "env2"]);
}

#[tokio::test]
async fn test_resolve_env_empty_json() {
let resolved_envs = resolve_env_from_stdout(Command::new("echo").arg("[]"))
.await
.expect("Parsing unexpectedly failed for echo-ing an empty list.");
assert_eq!(resolved_envs.len(), 0);
}

#[tokio::test]
async fn test_resolve_env_crash_integer_list() {
let e = resolve_env_from_stdout(Command::new("echo").arg("[1, 2, 3]"))
.await
.expect_err("Integer list is not an expected type");
assert!(format!("{e:?}").contains("invalid type: integer `1`, expected a string"));
}

#[tokio::test]
async fn test_resolve_env_crash_bad_command() {
let e = resolve_env_from_stdout(&mut Command::new("echolllll"))
.await
.expect_err("echolllll should return an error");
assert!(format!("{e:?}").starts_with("No such file or directory (os error 2)"));
}
}
5 changes: 5 additions & 0 deletions src/resolve_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import asv
import json
conf = asv.config.Config.load("asv.conf.json")
env_names = [env.name for env in asv.environment.get_environments(conf, "")]
print(json.dumps(env_names))