From f7a3a1491b9562d95905771bba83be3ce880318b Mon Sep 17 00:00:00 2001 From: Alan Zimmerman Date: Wed, 19 Jun 2024 09:34:37 -0700 Subject: [PATCH] Provide test example of using lints from config Summary: We are able to specify a limited set of lints from a config file. Provide an end-to-end test example of how to use this. Reviewed By: robertoaloi Differential Revision: D58779864 fbshipit-source-id: 6f3e185ee021a0752bb5b9108de236919aed25ec --- crates/elp/src/bin/main.rs | 31 +++++++++++++++++++ .../test/lint/from_config/app_b/src/app_b.erl | 5 +++ .../linter/parse_elp_lint_adhoc_output.stdout | 16 ++++++++++ test_projects/linter/elp_lint_adhoc.toml | 15 +++++++++ 4 files changed, 67 insertions(+) create mode 100644 crates/elp/src/resources/test/lint/from_config/app_b/src/app_b.erl create mode 100644 crates/elp/src/resources/test/linter/parse_elp_lint_adhoc_output.stdout create mode 100644 test_projects/linter/elp_lint_adhoc.toml diff --git a/crates/elp/src/bin/main.rs b/crates/elp/src/bin/main.rs index 41e926608b..db5145d0a4 100644 --- a/crates/elp/src/bin/main.rs +++ b/crates/elp/src/bin/main.rs @@ -1121,6 +1121,37 @@ mod tests { .expect("bad test"); } + #[test_case(false ; "rebar")] + #[test_case(true ; "buck")] + fn lint_custom_ad_hoc_lints(buck: bool) { + let tmp_dir = TempDir::new().expect("Could not create temporary directory"); + let tmp_path = tmp_dir.path(); + fs::create_dir_all(tmp_path).expect("Could not create temporary directory path"); + check_lint_fix( + args_vec![ + "lint", + "--experimental", + "--config-file", + "../../test_projects/linter/elp_lint_adhoc.toml", + "--module", + "app_b", + "--apply-fix", + "--to", + tmp_path, + ], + "linter", + expect_file!("../resources/test/linter/parse_elp_lint_adhoc_output.stdout"), + 0, + buck, + None, + tmp_path, + Path::new("../resources/test/lint/from_config"), + &[("app_b/src/app_b.erl", "app_b.erl")], + false, + ) + .expect("bad test"); + } + #[test_case(false ; "rebar")] #[test_case(true ; "buck")] fn lint_diagnostic_ignore(buck: bool) { diff --git a/crates/elp/src/resources/test/lint/from_config/app_b/src/app_b.erl b/crates/elp/src/resources/test/lint/from_config/app_b/src/app_b.erl new file mode 100644 index 0000000000..ab929e1e33 --- /dev/null +++ b/crates/elp/src/resources/test/lint/from_config/app_b/src/app_b.erl @@ -0,0 +1,5 @@ +-module(app_b). +-export([application_env_error/0]). + +application_env_error() -> + ok. diff --git a/crates/elp/src/resources/test/linter/parse_elp_lint_adhoc_output.stdout b/crates/elp/src/resources/test/linter/parse_elp_lint_adhoc_output.stdout new file mode 100644 index 0000000000..801eb5a9b5 --- /dev/null +++ b/crates/elp/src/resources/test/linter/parse_elp_lint_adhoc_output.stdout @@ -0,0 +1,16 @@ +module specified: app_b +Diagnostics reported in 1 modules: + app_b: 1 + 4:4-4:34::[WeakWarning] [ad-hoc: application:get_env/2] 'application:get_env/2' called +--------------------------------------------- + +Applying fix in module 'app_b' for + 4:4-4:34::[WeakWarning] [ad-hoc: application:get_env/2] 'application:get_env/2' called +@@ -1,5 +1,5 @@ + -module(app_b). + -export([application_env_error/0]). + + application_env_error() -> +- application:get_env(misc, key). ++ ok. + diff --git a/test_projects/linter/elp_lint_adhoc.toml b/test_projects/linter/elp_lint_adhoc.toml new file mode 100644 index 0000000000..f797edfceb --- /dev/null +++ b/test_projects/linter/elp_lint_adhoc.toml @@ -0,0 +1,15 @@ +enabled_lints = ["ad-hoc: application:get_env/2"] +# disabled_lints = [] + +[[ad_hoc_lints.lints]] +type = "ReplaceCall" + +[ad_hoc_lints.lints.matcher] +type = "MFA" +module = "application" +name = "get_env" +arity = 2 + +[ad_hoc_lints.lints.action] +action = "Replace" +type = "UseOk"