Skip to content
Merged
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
3 changes: 3 additions & 0 deletions crates/uv/src/commands/project/format.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::Path;
use std::str::FromStr;

use anyhow::{Context, Result};
Expand All @@ -18,6 +19,7 @@ use crate::settings::NetworkSettings;

/// Run the formatter.
pub(crate) async fn format(
project_dir: &Path,
check: bool,
diff: bool,
extra_args: Vec<String>,
Expand Down Expand Up @@ -53,6 +55,7 @@ pub(crate) async fn format(
.context("Failed to install ruff {version}")?;

let mut command = Command::new(&ruff_path);
command.current_dir(project_dir);
command.arg("format");

if check {
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,7 @@ async fn run_project(
let cache = cache.init()?;

Box::pin(commands::format(
project_dir,
args.check,
args.diff,
args.extra_args,
Expand Down
69 changes: 69 additions & 0 deletions crates/uv/tests/it/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,75 @@ fn format_project() -> Result<()> {
Ok(())
}

#[test]
fn format_relative_project() -> Result<()> {
Copy link
Contributor Author

@jorgehermo9 jorgehermo9 Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired in https://github.com/astral-sh/uv/blob/35a8dd514ef22d4c71cab430c74af2c1b95fbfe3/crates/uv/tests/it/lock.rs#L25729

I'm not sure if the relative_project nomenclature applies here.

Also, there are a lot of duplicated code among these tests (mainly, the same pyproject.toml, and the formatted/unformatted code snippets). I preferred to maintain the previous style and copypaste the mock data. I'm not sure what approach do you usually follow here (to extract into constants/fixtures vs always copypaste)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably move them into a reusable snippet here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let context = TestContext::new_with_versions(&[]);

let pyproject_toml = context.temp_dir.child("project").child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"#})?;

// Create an unformatted Python file in the relative project
let relative_project_main_py = context.temp_dir.child("project").child("main.py");
relative_project_main_py.write_str(indoc! {r#"
import sys
def hello():
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;

// Create another unformatted Python file in the root directory
let root_main_py = context.temp_dir.child("main.py");
root_main_py.write_str(indoc! {r#"
import sys
def hello():
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;

uv_snapshot!(context.filters(), context.format().arg("--project").arg("project"), @r"
success: true
exit_code: 0
----- stdout -----
1 file reformatted

----- stderr -----
warning: `uv format` is experimental and may change without warning. Pass `--preview-features format` to disable this warning.
");

// Check that the relative project file was formatted
let relative_project_content = fs_err::read_to_string(&relative_project_main_py)?;
assert_snapshot!(relative_project_content, @r#"
import sys


def hello():
print("Hello, World!")


if __name__ == "__main__":
hello()
"#);

// Check that the root file was not formatted
let root_content = fs_err::read_to_string(&root_main_py)?;
assert_snapshot!(root_content, @r#"
import sys
def hello():
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#);
Ok(())
}

#[test]
fn format_check() -> Result<()> {
let context = TestContext::new_with_versions(&[]);
Expand Down
Loading