Skip to content

Commit

Permalink
Added basic tests (astral-sh#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Feb 18, 2024
1 parent 78bf4d0 commit fe69262
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 1 deletion.
60 changes: 60 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions rye/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ winreg = "0.52.0"

[target."cfg(windows)".build-dependencies]
static_vcruntime = "2.0.0"

[dev-dependencies]
fslock = "0.2.1"
insta = { version = "1.34.0", features = ["filters"] }
insta-cmd = "0.4.0"
3 changes: 3 additions & 0 deletions rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::pyproject::{BuildSystem, DependencyKind, ExpandedSources, PyProject};
use crate::sources::PythonVersion;
use crate::sync::{sync, SyncOptions};
use crate::utils::{format_requirement, set_proxy_variables, CommandOutput};

const PACKAGE_FINDER_SCRIPT: &str = r#"
Expand Down Expand Up @@ -251,6 +252,8 @@ pub fn execute(cmd: Args) -> Result<(), Error> {

if !cmd.excluded {
if Config::current().use_uv() {
sync(SyncOptions::python_only().pyproject(None))
.context("failed to sync ahead of add")?;
resolve_requirements_with_uv(
&pyproject_toml,
&self_venv,
Expand Down
6 changes: 5 additions & 1 deletion rye/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::{BufWriter, Write};
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
use std::{fmt, fs};
use std::{env, fmt, fs};

use anyhow::{anyhow, bail, Context, Error};
use minijinja::render;
Expand Down Expand Up @@ -331,6 +331,10 @@ fn generate_lockfile(
} else if output == CommandOutput::Quiet {
cmd.arg("-q");
}
// this primarily exists for testing
if let Ok(dt) = env::var("__RYE_UV_EXCLUDE_NEWER") {
cmd.arg("--exclude-newer").arg(dt);
}
cmd
} else {
let mut cmd = Command::new(get_pip_compile(py_ver, output)?);
Expand Down
168 changes: 168 additions & 0 deletions rye/tests/common/mod.rs
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;
Loading

0 comments on commit fe69262

Please sign in to comment.