Repository: astral-sh/uv
File: crates/uv/tests/it/format.rs (Rust)

Code before:
    ----- 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 file was formatted
    let formatted_content = fs_err::read_to_string(&main_py)?;
    assert_snapshot!(formatted_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(&[]);

    let pyproject_toml = context.temp_dir.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
    let main_py = context.temp_dir.child("main.py");
    main_py.write_str(indoc! {r#"
        def   hello():
            print(  "Hello, World!"  )
    "#})?;

    uv_snapshot!(context.filters(), context.format().arg("--check"), @r"
    success: false
    exit_code: 1
    ----- stdout -----
    Would reformat: main.py
    1 file would be reformatted


Review comment (refactor):
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)

Code after:
    ----- 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 file was formatted
    let formatted_content = fs_err::read_to_string(&main_py)?;
    assert_snapshot!(formatted_content, @r#"
    import sys


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


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

    Ok(())
}

#[test]
fn format_relative_project() -> Result<()> {
    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