Skip to content

Commit 56166fc

Browse files
authored
Merge pull request #217 from AJIOB/AJIOB/non-root-cargo-manifest
Non-root cargo manifest file
2 parents b0828e7 + 6e59270 commit 56166fc

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

language_formatters_pre_commit_hooks/pretty_format_rust.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ def pretty_format_rust(argv: typing.Optional[typing.List[str]] = None) -> int:
1616
dest="autofix",
1717
help="Automatically fixes encountered not-pretty-formatted files",
1818
)
19+
parser.add_argument(
20+
"--manifest-path",
21+
default="Cargo.toml",
22+
dest="manifest_path",
23+
help="The cargo manifest file location (Default: %(default)s)",
24+
)
1925

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Cargo.lock
2+
/target/
3+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "untitled"
3+
version = "0.1.0"
4+
authors = ["Samuele Maci <[email protected]>"]
5+
6+
[[bin]]
7+
name = "testapp"
8+
path = "src/bin/src/main.rs"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello World!");
3+
}

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def run_autofix_test(
5757

5858
copyfile(not_pretty_formatted_path, not_pretty_formatted_tmp_path)
5959
with change_dir_context(tmpdir.strpath):
60-
parameters = extra_parameters + ["--autofix"] + [not_pretty_formatted_tmp_strpath]
60+
parameters = extra_parameters + ["--autofix", not_pretty_formatted_tmp_strpath]
6161
status_code = method(parameters)
6262
if status_code != 1:
6363
raise UnexpectedStatusCode(parameters=parameters, expected_status_code=1, actual_status_code=status_code)

tests/pretty_format_rust_test.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,60 @@ def undecorate_method():
3030
("pretty-formatted/src/main.rs", 0),
3131
("not-pretty-formatted/src/main.rs", 1),
3232
("not-pretty-formatted_fixed/src/main.rs", 0),
33+
("not-pretty-formatted_subdir/src/bin/src/main.rs", 1),
3334
),
3435
)
3536
def test_pretty_format_rust(undecorate_method, filename, expected_retval):
37+
manifest_root = filename.split("/")[0]
38+
manifest_root = os.path.abspath(manifest_root)
3639
filename = os.path.abspath(filename)
37-
x = os.path.dirname(os.path.dirname(filename))
38-
print(x)
39-
with change_dir_context(x):
40+
41+
with change_dir_context(manifest_root):
4042
assert undecorate_method([filename]) == expected_retval
4143

4244

45+
@pytest.mark.parametrize(
46+
("filename", "expected_retval"),
47+
(
48+
("invalid/src/main.rs", 1),
49+
("pretty-formatted/src/main.rs", 0),
50+
("not-pretty-formatted/src/main.rs", 1),
51+
("not-pretty-formatted_fixed/src/main.rs", 0),
52+
("not-pretty-formatted_subdir/src/bin/src/main.rs", 1),
53+
),
54+
)
55+
def test_pretty_format_rust_manifest(undecorate_method, filename, expected_retval):
56+
manifest_root = filename.split("/")[0]
57+
manifest_root = os.path.abspath(manifest_root)
58+
filename = os.path.abspath(filename)
59+
60+
manifest_file = os.path.join(manifest_root, "Cargo.toml")
61+
print(manifest_file)
62+
assert undecorate_method(["--manifest-path", manifest_file, filename]) == expected_retval
63+
64+
4365
def test_pretty_format_rust_autofix(tmpdir, undecorate_method):
4466
copyfile("not-pretty-formatted/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
4567
run_autofix_test(tmpdir, undecorate_method, "not-pretty-formatted/src/main.rs", "not-pretty-formatted_fixed/src/main.rs")
68+
69+
70+
@pytest.mark.xfail
71+
def test_pretty_format_rust_autofix_subdir_no_manifest_arg(tmpdir, undecorate_method):
72+
copyfile("not-pretty-formatted_subdir/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
73+
run_autofix_test(
74+
tmpdir.mkdir("src").mkdir("bin"),
75+
undecorate_method,
76+
"not-pretty-formatted_subdir/src/bin/src/main.rs",
77+
"not-pretty-formatted_fixed/src/main.rs",
78+
)
79+
80+
81+
def test_pretty_format_rust_autofix_subdir(tmpdir, undecorate_method):
82+
copyfile("not-pretty-formatted_subdir/Cargo.toml", tmpdir.join("Cargo.toml").strpath)
83+
run_autofix_test(
84+
tmpdir.mkdir("src").mkdir("bin"),
85+
undecorate_method,
86+
"not-pretty-formatted_subdir/src/bin/src/main.rs",
87+
"not-pretty-formatted_fixed/src/main.rs",
88+
["--manifest-path", "../../Cargo.toml"],
89+
)

0 commit comments

Comments
 (0)