fix(tests): Refactor uv format tests (#15468)

Closes #15458

## Summary

Refactor uv format tests to reduce noise.
This commit is contained in:
Pepe Osca 2025-08-25 14:40:20 +02:00 committed by GitHub
parent 7f1a464216
commit 6e802873cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 106 deletions

View File

@ -20,13 +20,9 @@ fn format_project() -> Result<()> {
// Create an unformatted Python file // Create an unformatted Python file
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#" main_py.write_str(indoc! {r"
import sys x = 1
def hello(): "})?;
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;
uv_snapshot!(context.filters(), context.format(), @r" uv_snapshot!(context.filters(), context.format(), @r"
success: true success: true
@ -40,17 +36,9 @@ fn format_project() -> Result<()> {
// Check that the file was formatted // Check that the file was formatted
let formatted_content = fs_err::read_to_string(&main_py)?; let formatted_content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(formatted_content, @r#" assert_snapshot!(formatted_content, @r"
import sys x = 1
");
def hello():
print("Hello, World!")
if __name__ == "__main__":
hello()
"#);
Ok(()) Ok(())
} }
@ -70,13 +58,9 @@ fn format_from_project_root() -> Result<()> {
// Create an unformatted Python file // Create an unformatted Python file
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#" main_py.write_str(indoc! {r"
import sys x = 1
def hello(): "})?;
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;
let subdir = context.temp_dir.child("subdir"); let subdir = context.temp_dir.child("subdir");
fs_err::create_dir_all(&subdir)?; fs_err::create_dir_all(&subdir)?;
@ -94,17 +78,9 @@ fn format_from_project_root() -> Result<()> {
// Check that the file was formatted // Check that the file was formatted
let formatted_content = fs_err::read_to_string(&main_py)?; let formatted_content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(formatted_content, @r#" assert_snapshot!(formatted_content, @r"
import sys x = 1
");
def hello():
print("Hello, World!")
if __name__ == "__main__":
hello()
"#);
Ok(()) Ok(())
} }
@ -124,23 +100,15 @@ fn format_relative_project() -> Result<()> {
// Create an unformatted Python file in the relative project // Create an unformatted Python file in the relative project
let relative_project_main_py = context.temp_dir.child("project").child("main.py"); let relative_project_main_py = context.temp_dir.child("project").child("main.py");
relative_project_main_py.write_str(indoc! {r#" relative_project_main_py.write_str(indoc! {r"
import sys x = 1
def hello(): "})?;
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;
// Create another unformatted Python file in the root directory // Create another unformatted Python file in the root directory
let root_main_py = context.temp_dir.child("main.py"); let root_main_py = context.temp_dir.child("main.py");
root_main_py.write_str(indoc! {r#" root_main_py.write_str(indoc! {r"
import sys x = 1
def hello(): "})?;
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;
uv_snapshot!(context.filters(), context.format().arg("--project").arg("project"), @r" uv_snapshot!(context.filters(), context.format().arg("--project").arg("project"), @r"
success: true success: true
@ -154,27 +122,16 @@ fn format_relative_project() -> Result<()> {
// Check that the relative project file was formatted // Check that the relative project file was formatted
let relative_project_content = fs_err::read_to_string(&relative_project_main_py)?; let relative_project_content = fs_err::read_to_string(&relative_project_main_py)?;
assert_snapshot!(relative_project_content, @r#" assert_snapshot!(relative_project_content, @r"
import sys x = 1
");
def hello():
print("Hello, World!")
if __name__ == "__main__":
hello()
"#);
// Check that the root file was not formatted // Check that the root file was not formatted
let root_content = fs_err::read_to_string(&root_main_py)?; let root_content = fs_err::read_to_string(&root_main_py)?;
assert_snapshot!(root_content, @r#" assert_snapshot!(root_content, @r"
import sys x = 1
def hello(): ");
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#);
Ok(()) Ok(())
} }
@ -193,10 +150,9 @@ fn format_check() -> Result<()> {
// Create an unformatted Python file // Create an unformatted Python file
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#" main_py.write_str(indoc! {r"
def hello(): x = 1
print( "Hello, World!" ) "})?;
"#})?;
uv_snapshot!(context.filters(), context.format().arg("--check"), @r" uv_snapshot!(context.filters(), context.format().arg("--check"), @r"
success: false success: false
@ -211,10 +167,9 @@ fn format_check() -> Result<()> {
// Verify the file wasn't modified // Verify the file wasn't modified
let content = fs_err::read_to_string(&main_py)?; let content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(content, @r#" assert_snapshot!(content, @r"
def hello(): x = 1
print( "Hello, World!" ) ");
"#);
Ok(()) Ok(())
} }
@ -234,10 +189,9 @@ fn format_diff() -> Result<()> {
// Create an unformatted Python file // Create an unformatted Python file
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#" main_py.write_str(indoc! {r"
def hello(): x = 1
print( "Hello, World!" ) "})?;
"#})?;
uv_snapshot!(context.filters(), context.format().arg("--diff"), @r#" uv_snapshot!(context.filters(), context.format().arg("--diff"), @r#"
success: false success: false
@ -245,11 +199,9 @@ fn format_diff() -> Result<()> {
----- stdout ----- ----- stdout -----
--- main.py --- main.py
+++ main.py +++ main.py
@@ -1,2 +1,2 @@ @@ -1 +1 @@
-def hello(): -x = 1
- print( "Hello, World!" ) +x = 1
+def hello():
+ print("Hello, World!")
----- stderr ----- ----- stderr -----
@ -259,10 +211,9 @@ fn format_diff() -> Result<()> {
// Verify the file wasn't modified // Verify the file wasn't modified
let content = fs_err::read_to_string(&main_py)?; let content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(content, @r#" assert_snapshot!(content, @r"
def hello(): x = 1
print( "Hello, World!" ) ");
"#);
Ok(()) Ok(())
} }
@ -323,16 +274,14 @@ fn format_specific_files() -> Result<()> {
// Create multiple unformatted Python files // Create multiple unformatted Python files
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#" main_py.write_str(indoc! {r"
def main(): x = 1
print( "Main" ) "})?;
"#})?;
let utils_py = context.temp_dir.child("utils.py"); let utils_py = context.temp_dir.child("utils.py");
utils_py.write_str(indoc! {r#" utils_py.write_str(indoc! {r"
def utils(): x = 1
print( "utils" ) "})?;
"#})?;
uv_snapshot!(context.filters(), context.format().arg("--").arg("main.py"), @r" uv_snapshot!(context.filters(), context.format().arg("--").arg("main.py"), @r"
success: true success: true
@ -345,17 +294,15 @@ fn format_specific_files() -> Result<()> {
"); ");
let main_content = fs_err::read_to_string(&main_py)?; let main_content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(main_content, @r#" assert_snapshot!(main_content, @r"
def main(): x = 1
print("Main") ");
"#);
// Unchanged // Unchanged
let utils_content = fs_err::read_to_string(&utils_py)?; let utils_content = fs_err::read_to_string(&utils_py)?;
assert_snapshot!(utils_content, @r#" assert_snapshot!(utils_content, @r"
def utils(): x = 1
print( "utils" ) ");
"#);
Ok(()) Ok(())
} }
@ -375,7 +322,7 @@ fn format_version_option() -> Result<()> {
let main_py = context.temp_dir.child("main.py"); let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r" main_py.write_str(indoc! {r"
def hello(): pass x = 1
"})?; "})?;
// Run format with specific Ruff version // Run format with specific Ruff version