From 41131a7de0c720667c8605bf8e4cbf31991c494c Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:47:29 +0800 Subject: [PATCH] Allow `rye publish` working outside of project (#910) --- .gitignore | 1 + CHANGELOG.md | 2 ++ rye/src/cli/build.rs | 2 +- rye/src/cli/publish.rs | 14 +++++++------- rye/tests/test_publish.rs | 31 +++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 rye/tests/test_publish.rs diff --git a/.gitignore b/.gitignore index f9af4fa305..b2d5f03e4c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ site __pycache__ .idea token.txt +dist diff --git a/CHANGELOG.md b/CHANGELOG.md index b9c8f9b265..457c5a1d03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ _Unreleased_ +- Allow `rye publish` working outside of project. #910 + ## 0.30.0 Released on 2024-03-19 diff --git a/rye/src/cli/build.rs b/rye/src/cli/build.rs index c8634b9367..e944ffd0d1 100644 --- a/rye/src/cli/build.rs +++ b/rye/src/cli/build.rs @@ -13,7 +13,7 @@ use crate::utils::{get_venv_python_bin, CommandOutput, IoPathContext}; /// Builds a package for distribution. #[derive(Parser, Debug)] pub struct Args { - /// Build an sdist + /// Build a sdist #[arg(long)] sdist: bool, /// Build a wheel diff --git a/rye/src/cli/publish.rs b/rye/src/cli/publish.rs index 860952b4d7..4cc31b01f1 100644 --- a/rye/src/cli/publish.rs +++ b/rye/src/cli/publish.rs @@ -59,16 +59,17 @@ pub struct Args { pub fn execute(cmd: Args) -> Result<(), Error> { let output = CommandOutput::from_quiet_and_verbose(cmd.quiet, cmd.verbose); let venv = ensure_self_venv(output)?; - let project = PyProject::discover()?; - - if project.is_virtual() { - bail!("virtual packages cannot be published"); - } // Get the files to publish. let files = match cmd.dist { Some(paths) => paths, - None => vec![project.workspace_path().join("dist").join("*")], + None => { + let project = PyProject::discover()?; + if project.is_virtual() { + bail!("virtual packages cannot be published"); + } + vec![project.workspace_path().join("dist").join("*")] + } }; // a. Get token from arguments and offer encryption, then store in credentials file. @@ -115,7 +116,6 @@ pub fn execute(cmd: Args) -> Result<(), Error> { let maybe_encrypted = maybe_encrypt(&secret, cmd.yes)?; let maybe_encoded = maybe_encode(&secret, &maybe_encrypted); credentials[repository]["token"] = Item::Value(maybe_encoded.expose_secret().into()); - write_credentials(&credentials)?; secret } else if let Some(token) = credentials diff --git a/rye/tests/test_publish.rs b/rye/tests/test_publish.rs new file mode 100644 index 0000000000..a5d4ebffa5 --- /dev/null +++ b/rye/tests/test_publish.rs @@ -0,0 +1,31 @@ +use crate::common::{rye_cmd_snapshot, Space}; + +mod common; + +#[test] +fn test_publish_outside_project() { + let space = Space::new(); + space.init("my-project"); + + let status = space.rye_cmd().arg("build").status().unwrap(); + assert!(status.success()); + + // Publish outside the project. + // Since we provide a fake token, the failure is expected. + rye_cmd_snapshot!(space + .rye_cmd() + .arg("publish") + .arg("--yes") + .arg("--token") + .arg("fake-token") + .arg("--quiet") + .current_dir(space.project_path().parent().unwrap()) + .arg(space.project_path().join("dist").join("*")), @r###" + success: false + exit_code: 1 + ----- stdout ----- + + ----- stderr ----- + error: failed to publish files + "###); +}