Skip to content

Non-root cargo manifest file #217

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

Merged
merged 2 commits into from
Apr 12, 2024
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
10 changes: 8 additions & 2 deletions language_formatters_pre_commit_hooks/pretty_format_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ def pretty_format_rust(argv: typing.Optional[typing.List[str]] = None) -> int:
dest="autofix",
help="Automatically fixes encountered not-pretty-formatted files",
)
parser.add_argument(
"--manifest-path",
default="Cargo.toml",
dest="manifest_path",
help="The cargo manifest file location (Default: %(default)s)",
)

parser.add_argument("filenames", nargs="*", help="Filenames to fix")
args = parser.parse_args(argv)

# Check
status_code, output, _ = run_command("cargo", "fmt", "--", "--check", *args.filenames)
status_code, output, _ = run_command("cargo", "fmt", "--manifest-path", args.manifest_path, "--", "--check", *args.filenames)
not_well_formatted_files = sorted(line.split()[2] for line in output.splitlines() if line.startswith("Diff in "))
if not_well_formatted_files:
print(
Expand All @@ -31,7 +37,7 @@ def pretty_format_rust(argv: typing.Optional[typing.List[str]] = None) -> int:
),
)
if args.autofix:
run_command("cargo", "fmt", "--", *not_well_formatted_files)
run_command("cargo", "fmt", "--manifest-path", args.manifest_path, "--", *not_well_formatted_files)
elif status_code != 0:
print("Detected not valid rust source files among {}".format("\n".join(sorted(args.filenames))))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Cargo.lock
/target/

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "untitled"
version = "0.1.0"
authors = ["Samuele Maci <[email protected]>"]

[[bin]]
name = "testapp"
path = "src/bin/src/main.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello World!");
}
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run_autofix_test(

copyfile(not_pretty_formatted_path, not_pretty_formatted_tmp_path)
with change_dir_context(tmpdir.strpath):
parameters = extra_parameters + ["--autofix"] + [not_pretty_formatted_tmp_strpath]
parameters = extra_parameters + ["--autofix", not_pretty_formatted_tmp_strpath]
status_code = method(parameters)
if status_code != 1:
raise UnexpectedStatusCode(parameters=parameters, expected_status_code=1, actual_status_code=status_code)
Expand Down
50 changes: 47 additions & 3 deletions tests/pretty_format_rust_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,60 @@ def undecorate_method():
("pretty-formatted/src/main.rs", 0),
("not-pretty-formatted/src/main.rs", 1),
("not-pretty-formatted_fixed/src/main.rs", 0),
("not-pretty-formatted_subdir/src/bin/src/main.rs", 1),
),
)
def test_pretty_format_rust(undecorate_method, filename, expected_retval):
manifest_root = filename.split("/")[0]
manifest_root = os.path.abspath(manifest_root)
filename = os.path.abspath(filename)
x = os.path.dirname(os.path.dirname(filename))
print(x)
with change_dir_context(x):

with change_dir_context(manifest_root):
assert undecorate_method([filename]) == expected_retval


@pytest.mark.parametrize(
("filename", "expected_retval"),
(
("invalid/src/main.rs", 1),
("pretty-formatted/src/main.rs", 0),
("not-pretty-formatted/src/main.rs", 1),
("not-pretty-formatted_fixed/src/main.rs", 0),
("not-pretty-formatted_subdir/src/bin/src/main.rs", 1),
),
)
def test_pretty_format_rust_manifest(undecorate_method, filename, expected_retval):
manifest_root = filename.split("/")[0]
manifest_root = os.path.abspath(manifest_root)
filename = os.path.abspath(filename)

manifest_file = os.path.join(manifest_root, "Cargo.toml")
print(manifest_file)
Copy link
Owner

@macisamuele macisamuele Apr 2, 2024

Choose a reason for hiding this comment

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

please remove prints from tests

assert undecorate_method(["--manifest-path", manifest_file, filename]) == expected_retval


def test_pretty_format_rust_autofix(tmpdir, undecorate_method):
copyfile("not-pretty-formatted/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
run_autofix_test(tmpdir, undecorate_method, "not-pretty-formatted/src/main.rs", "not-pretty-formatted_fixed/src/main.rs")


@pytest.mark.xfail
def test_pretty_format_rust_autofix_subdir_no_manifest_arg(tmpdir, undecorate_method):
copyfile("not-pretty-formatted_subdir/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
run_autofix_test(
tmpdir.mkdir("src").mkdir("bin"),
undecorate_method,
"not-pretty-formatted_subdir/src/bin/src/main.rs",
"not-pretty-formatted_fixed/src/main.rs",
)


def test_pretty_format_rust_autofix_subdir(tmpdir, undecorate_method):
copyfile("not-pretty-formatted_subdir/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
run_autofix_test(
tmpdir.mkdir("src").mkdir("bin"),
undecorate_method,
"not-pretty-formatted_subdir/src/bin/src/main.rs",
"not-pretty-formatted_fixed/src/main.rs",
["--manifest-path", "../../Cargo.toml"],
)
Loading