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

Add autosync-before-run, --sync and --no-sync arguments to rye test and rye run (Regular sync mode) #1005

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__
.idea
token.txt
dist
**/*.rs.pending-snap
4 changes: 4 additions & 0 deletions docs/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ use-uv = false
# to `true` when uv is enabled and `false` otherwise.
autosync = true

# Enable or disable automatic `sync` ahead of `run` and `test`. This defaults
# to `true` when uv is enabled and `false` otherwise.
autosync-before-run = true

# Marks the managed .venv in a way that cloud based synchronization systems
# like Dropbox and iCloud Files will not upload it. This defaults to true
# as a .venv in cloud storage typically does not make sense. Set this to
Expand Down
28 changes: 23 additions & 5 deletions rye/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use anyhow::{bail, Context, Error};
use clap::Parser;
use console::style;

use crate::config::Config;
use crate::pyproject::{PyProject, Script};
use crate::sync::{sync, SyncOptions};
use crate::sync::{autosync, sync, SyncOptions};
use crate::tui::redirect_to_stderr;
use crate::utils::{exec_spawn, get_venv_python_bin, success_status, IoPathContext};
use crate::utils::{exec_spawn, get_venv_python_bin, success_status, CommandOutput, IoPathContext};

/// Runs a command installed into this package.
#[derive(Parser, Debug)]
Expand All @@ -26,6 +27,18 @@ pub struct Args {
/// Use this pyproject.toml file
#[arg(long, value_name = "PYPROJECT_TOML")]
pyproject: Option<PathBuf>,
/// Runs `sync` even if auto-sync is disabled.
#[arg(long)]
sync: bool,
/// Does not run `sync` even if auto-sync is enabled.
#[arg(long, conflicts_with = "sync")]
no_sync: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
/// Turns off all output.
#[arg(short, long, conflicts_with = "verbose")]
quiet: bool,
}

#[derive(Parser, Debug)]
Expand All @@ -36,11 +49,16 @@ enum Cmd {

pub fn execute(cmd: Args) -> Result<(), Error> {
let _guard = redirect_to_stderr(true);
let output = CommandOutput::from_quiet_and_verbose(cmd.quiet, cmd.verbose);
let pyproject = PyProject::load_or_discover(cmd.pyproject.as_deref())?;

// make sure we have the minimal virtualenv.
sync(SyncOptions::python_only().pyproject(cmd.pyproject))
.context("failed to sync ahead of run")?;
if (Config::current().autosync_before_run() && !cmd.no_sync) || cmd.sync {
autosync(&pyproject, output)?;
} else {
// make sure we have the minimal virtualenv.
sync(SyncOptions::python_only().pyproject(cmd.pyproject))
.context("failed to sync ahead of run")?;
}

if cmd.list || cmd.cmd.is_none() {
return list_scripts(&pyproject);
Expand Down
8 changes: 7 additions & 1 deletion rye/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub struct Args {
// Disable test output capture to stdout
#[arg(long = "no-capture", short = 's')]
no_capture: bool,
/// Runs `sync` even if auto-sync is disabled.
#[arg(long)]
sync: bool,
/// Does not run `sync` even if auto-sync is enabled.
#[arg(long, conflicts_with = "sync")]
no_sync: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -72,7 +78,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
if !pytest.is_file() {
let has_pytest = has_pytest_dependency(&projects)?;
if has_pytest {
if Config::current().autosync() {
if (Config::current().autosync_before_run() && !cmd.no_sync) || cmd.sync {
autosync(&projects[0], output)?;
} else {
bail!("pytest not installed but in dependencies. Run `rye sync`.")
Expand Down
11 changes: 10 additions & 1 deletion rye/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,23 @@ impl Config {
Ok(rv)
}

/// Enable autosync.
/// Enable autosync for `add` and `remove`.
pub fn autosync(&self) -> bool {
self.doc
.get("behavior")
.and_then(|x| x.get("autosync"))
.and_then(|x| x.as_bool())
.unwrap_or_else(|| self.use_uv())
}

/// Enable autosync for `run` and `test`.
pub fn autosync_before_run(&self) -> bool {
self.doc
.get("behavior")
.and_then(|x| x.get("autosync-before-run"))
.and_then(|x| x.as_bool())
.unwrap_or_else(|| self.use_uv())
}

/// Indicates if uv should be used instead of pip-tools.
pub fn use_uv(&self) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions rye/tests/test_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ fn test_dotenv() {
42 23

----- stderr -----
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);
}
24 changes: 24 additions & 0 deletions rye/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ fn test_init_lib() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

assert!(
Expand Down Expand Up @@ -89,6 +95,12 @@ fn test_init_default() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

assert!(
Expand Down Expand Up @@ -138,6 +150,12 @@ fn test_init_script() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

rye_cmd_snapshot!(space.rye_cmd().arg("run").arg("python").arg("-mmy_project"), @r###"
Expand All @@ -147,6 +165,12 @@ fn test_init_script() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Generating production lockfile: [TEMP_PATH]/project/requirements.lock
Generating dev lockfile: [TEMP_PATH]/project/requirements-dev.lock
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);
}

Expand Down