From a05d519cc3e7db58ed300929f4db1135a7ec7a02 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 17 Mar 2023 18:47:37 +0100 Subject: [PATCH] Fix test case urls (must use raw) and target directory resolution --- Cargo.lock | 3 +++ crates/ruff_benchmark/Cargo.toml | 3 +++ crates/ruff_benchmark/benches/linter.rs | 6 ++--- crates/ruff_benchmark/src/lib.rs | 29 ++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a8e7bbc9bbd72..bea0f1351772b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2041,7 +2041,10 @@ version = "0.0.0" dependencies = [ "criterion", "mimalloc", + "once_cell", "ruff", + "serde", + "serde_json", "tikv-jemallocator", "ureq", "url", diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index 0d4899b11e1e49..50d414281cebf1 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -17,7 +17,10 @@ name = "linter" harness = false [dependencies] +once_cell.workspace = true ruff.path = "../ruff" +serde.workspace = true +serde_json.workspace = true url = "2.3.1" ureq = "2.6.2" diff --git a/crates/ruff_benchmark/benches/linter.rs b/crates/ruff_benchmark/benches/linter.rs index 329d7a4681e21f..4afc5fbef06dcd 100644 --- a/crates/ruff_benchmark/benches/linter.rs +++ b/crates/ruff_benchmark/benches/linter.rs @@ -26,12 +26,12 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; fn create_test_cases() -> Result, TestFileDownloadError> { Ok(vec![ - TestCase::fast(TestFile::try_download("numpy/globals.py", "https://github.com/numpy/numpy/blob/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), + TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), TestCase::normal(TestFile::try_download( "pydantic/types.py", - "https://raw.githubusercontent.com/pydantic/pydantic/main/pydantic/types.py", + "https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py", )?), - TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://github.com/numpy/numpy/blob/main/numpy/ctypeslib.py")?), + TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?), TestCase::slow(TestFile::try_download( "large/dataset.py", "https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py", diff --git a/crates/ruff_benchmark/src/lib.rs b/crates/ruff_benchmark/src/lib.rs index af7eb91ae961fc..6ea1fcabe160cd 100644 --- a/crates/ruff_benchmark/src/lib.rs +++ b/crates/ruff_benchmark/src/lib.rs @@ -1,5 +1,6 @@ use std::fmt::{Display, Formatter}; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; +use std::process::Command; use url::Url; /// Relative size of a test case. Benchmarks can use it to configure the time for how long a benchmark should run to get stable results. @@ -58,7 +59,7 @@ impl TestCase { } pub fn path(&self) -> PathBuf { - Path::new("target").join(self.name()) + TARGET_DIR.join(self.name()) } } @@ -68,6 +69,28 @@ pub struct TestFile { code: String, } +static TARGET_DIR: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { + cargo_target_directory().unwrap_or_else(|| PathBuf::from("target")) +}); + +fn cargo_target_directory() -> Option { + #[derive(serde::Deserialize)] + struct Metadata { + target_directory: PathBuf, + } + + std::env::var_os("CARGO_TARGET_DIR") + .map(PathBuf::from) + .or_else(|| { + let output = Command::new(std::env::var_os("CARGO")?) + .args(["metadata", "--format-version", "1"]) + .output() + .ok()?; + let metadata: Metadata = serde_json::from_slice(&output.stdout).ok()?; + Some(metadata.target_directory) + }) +} + impl TestFile { pub fn new(name: String, code: String) -> Self { Self { name, code } @@ -77,7 +100,7 @@ impl TestFile { pub fn try_download(name: &str, url: &str) -> Result { let url = Url::parse(url)?; - let cached_filename = Path::new("target").join(name); + let cached_filename = TARGET_DIR.join(name); if let Ok(content) = std::fs::read_to_string(&cached_filename) { Ok(TestFile::new(name.to_string(), content))