diff --git a/language_formatters_pre_commit_hooks/pretty_format_rust.py b/language_formatters_pre_commit_hooks/pretty_format_rust.py index cf71ba3..c5cb6f7 100644 --- a/language_formatters_pre_commit_hooks/pretty_format_rust.py +++ b/language_formatters_pre_commit_hooks/pretty_format_rust.py @@ -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( @@ -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)))) diff --git a/test-data/pretty_format_rust/not-pretty-formatted_subdir/.gitignore b/test-data/pretty_format_rust/not-pretty-formatted_subdir/.gitignore new file mode 100644 index 0000000..1649ba3 --- /dev/null +++ b/test-data/pretty_format_rust/not-pretty-formatted_subdir/.gitignore @@ -0,0 +1,3 @@ +/Cargo.lock +/target/ + diff --git a/test-data/pretty_format_rust/not-pretty-formatted_subdir/Cargo.toml b/test-data/pretty_format_rust/not-pretty-formatted_subdir/Cargo.toml new file mode 100644 index 0000000..bf4ebda --- /dev/null +++ b/test-data/pretty_format_rust/not-pretty-formatted_subdir/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "untitled" +version = "0.1.0" +authors = ["Samuele Maci "] + +[[bin]] +name = "testapp" +path = "src/bin/src/main.rs" diff --git a/test-data/pretty_format_rust/not-pretty-formatted_subdir/src/bin/src/main.rs b/test-data/pretty_format_rust/not-pretty-formatted_subdir/src/bin/src/main.rs new file mode 100644 index 0000000..9cbc35a --- /dev/null +++ b/test-data/pretty_format_rust/not-pretty-formatted_subdir/src/bin/src/main.rs @@ -0,0 +1,3 @@ +fn main() { +println!("Hello World!"); +} diff --git a/tests/__init__.py b/tests/__init__.py index b0abb74..f79deca 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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) diff --git a/tests/pretty_format_rust_test.py b/tests/pretty_format_rust_test.py index eab73e4..2225ccc 100644 --- a/tests/pretty_format_rust_test.py +++ b/tests/pretty_format_rust_test.py @@ -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) + 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"], + )