forked from astral-sh/rye
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
321 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
use std::ffi::OsStr; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
use insta_cmd::get_cargo_bin; | ||
use tempfile::TempDir; | ||
|
||
// Exclude any packages uploaded after this date. | ||
pub static EXCLUDE_NEWER: &str = "2023-11-18T12:00:00Z"; | ||
|
||
pub const INSTA_FILTERS: &[(&str, &str)] = &[ | ||
// general temp folders | ||
( | ||
r"(\b[A-Z]:)?[\\/].*?[\\/]\.rye-tests---[^\\/]+[\\/]", | ||
"[TEMP_PATH]/", | ||
), | ||
// macos temp folder | ||
(r"/var/folders/\S+?/T/\S+", "[TEMP_FILE]"), | ||
// linux temp folders | ||
(r"/tmp/\.tmp\S+", "[TEMP_FILE]"), | ||
// windows temp folders | ||
(r"\b[A-Z]:\\.*\\Local\\Temp\\\S+", "[TEMP_FILE]"), | ||
(r" in (\d+\.)?\d+(ms|s)\b", " in [EXECUTION_TIME]"), | ||
(r"\\([\w\d.])", "/$1"), | ||
(r"rye.exe", "rye"), | ||
]; | ||
|
||
fn marked_tempdir() -> TempDir { | ||
TempDir::with_prefix(".rye-tests---").unwrap() | ||
} | ||
|
||
fn bootstrap_test_rye() -> PathBuf { | ||
let home = get_cargo_bin("rye").parent().unwrap().join("rye-test-home"); | ||
fs::create_dir_all(&home).ok(); | ||
let lock_path = home.join("lock"); | ||
let mut lock = fslock::LockFile::open(&lock_path).unwrap(); | ||
lock.lock().unwrap(); | ||
|
||
// write config | ||
let config_file = home.join("config.toml"); | ||
if !config_file.is_file() { | ||
fs::write( | ||
home.join("config.toml"), | ||
r#" | ||
[behavior] | ||
use-uv = true | ||
[default] | ||
toolchain = "[email protected]" | ||
"#, | ||
) | ||
.unwrap(); | ||
} | ||
|
||
// fetch the most important interpreters | ||
for version in ["[email protected]", "[email protected]", "[email protected]"] { | ||
if home.join("py").join(version).is_dir() { | ||
continue; | ||
} | ||
let status = Command::new(get_bin()) | ||
.env("RYE_HOME", &home) | ||
.arg("fetch") | ||
.arg(version) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
// make a dummy project to bootstrap it | ||
if !home.join("self").is_dir() { | ||
let t = marked_tempdir(); | ||
Command::new(get_bin()) | ||
.env("RYE_HOME", &home) | ||
.current_dir(t.path()) | ||
.arg("init") | ||
.arg("--name=test-project") | ||
.status() | ||
.unwrap(); | ||
Command::new(get_bin()) | ||
.env("RYE_HOME", &home) | ||
.current_dir(t.path()) | ||
.arg("sync") | ||
.status() | ||
.unwrap(); | ||
} | ||
|
||
lock.unlock().unwrap(); | ||
|
||
home | ||
} | ||
|
||
pub fn get_bin() -> PathBuf { | ||
get_cargo_bin("rye") | ||
} | ||
|
||
pub struct Space { | ||
#[allow(unused)] | ||
tempdir: TempDir, | ||
rye_home: PathBuf, | ||
project_dir: PathBuf, | ||
} | ||
|
||
impl Space { | ||
pub fn new() -> Space { | ||
let tempdir = marked_tempdir(); | ||
let project_dir = tempdir.path().join("project"); | ||
let rye_home = bootstrap_test_rye(); | ||
fs::create_dir_all(&project_dir).unwrap(); | ||
Space { | ||
tempdir, | ||
project_dir, | ||
rye_home, | ||
} | ||
} | ||
|
||
pub fn cmd<S>(&self, cmd: S) -> Command | ||
where | ||
S: AsRef<OsStr>, | ||
{ | ||
let mut rv = Command::new(cmd); | ||
rv.env("RYE_HOME", self.rye_home().as_os_str()); | ||
rv.env("UV_CACHE_DIR", self.tempdir.path().join("uv-cache")); | ||
rv.env("__RYE_UV_EXCLUDE_NEWER", EXCLUDE_NEWER); | ||
rv.current_dir(self.project_path()); | ||
rv | ||
} | ||
|
||
pub fn rye_cmd(&self) -> Command { | ||
self.cmd(get_bin()) | ||
} | ||
|
||
pub fn init(&self, name: &str) { | ||
let status = self | ||
.cmd(get_bin()) | ||
.arg("init") | ||
.arg("--name") | ||
.arg(name) | ||
.arg("-q") | ||
.current_dir(self.project_path()) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
pub fn rye_home(&self) -> &Path { | ||
&self.rye_home | ||
} | ||
|
||
pub fn project_path(&self) -> &Path { | ||
&self.project_dir | ||
} | ||
} | ||
|
||
#[allow(unused_macros)] | ||
macro_rules! rye_cmd_snapshot { | ||
($cmd:expr, @$snapshot:literal) => {{ | ||
let mut settings = insta::Settings::clone_current(); | ||
for (matcher, replacement) in $crate::common::INSTA_FILTERS { | ||
settings.add_filter(matcher, *replacement); | ||
} | ||
let _guard = settings.bind_to_scope(); | ||
insta_cmd::assert_cmd_snapshot!($cmd, @$snapshot); | ||
}}; | ||
} | ||
|
||
#[allow(unused_imports)] | ||
pub(crate) use rye_cmd_snapshot; |
Oops, something went wrong.