From f47b363f716a693e6baa3bab5f4b5add30a8a6ed Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Fri, 26 Feb 2021 22:47:21 +0800 Subject: [PATCH] Add rustfmt for Rust (#1250) * Add linter for Rust - add 2015 edition - add 2018 edition * Add tap reports - add tap reports - fix good test case * 1 command=lesslayers Co-authored-by: Lukas Gravley --- .../test/rust_2015/reports/expected-RUST_2015.tap | 7 +++++++ .automation/test/rust_2015/rust_bad_1.rs | 7 +++++++ .automation/test/rust_2015/rust_good_1.rs | 8 ++++++++ .../test/rust_2018/reports/expected-RUST_2018.tap | 7 +++++++ .automation/test/rust_2018/rust_bad_1.rs | 7 +++++++ .automation/test/rust_2018/rust_good_1.rs | 8 ++++++++ Dockerfile | 9 +++++++++ README.md | 3 +++ lib/functions/buildFileList.sh | 10 ++++++++++ lib/linter.sh | 8 +++++++- 10 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .automation/test/rust_2015/reports/expected-RUST_2015.tap create mode 100644 .automation/test/rust_2015/rust_bad_1.rs create mode 100644 .automation/test/rust_2015/rust_good_1.rs create mode 100644 .automation/test/rust_2018/reports/expected-RUST_2018.tap create mode 100644 .automation/test/rust_2018/rust_bad_1.rs create mode 100644 .automation/test/rust_2018/rust_good_1.rs diff --git a/.automation/test/rust_2015/reports/expected-RUST_2015.tap b/.automation/test/rust_2015/reports/expected-RUST_2015.tap new file mode 100644 index 00000000..0dbd03c0 --- /dev/null +++ b/.automation/test/rust_2015/reports/expected-RUST_2015.tap @@ -0,0 +1,7 @@ +TAP version 13 +1..2 +not ok 1 - rust_bad_1.rs + --- + message: Diff in /tmp/lint/.automation/test/rust_2015/rust_bad_1.rs at line 1 \n // rustfmt-struct_field_align_threshold 50\n fn func() {\n- Ok(ServerInformation { name unwrap_message_string(items.get(0)),\n- vendor unwrap_message_string(items.get(1)),\n- version unwrap_message_string(items.get(2)),\n- spec_version unwrap_message_string(items.get(3)), });\n+ Ok(ServerInformation {\n+ name unwrap_message_string(items.get(0)),\n+ vendor unwrap_message_string(items.get(1)),\n+ version unwrap_message_string(items.get(2)),\n+ spec_version unwrap_message_string(items.get(3)),\n+ });\n }\n \n + ... +ok 2 - rust_good_1.rs diff --git a/.automation/test/rust_2015/rust_bad_1.rs b/.automation/test/rust_2015/rust_bad_1.rs new file mode 100644 index 00000000..9a3e93bd --- /dev/null +++ b/.automation/test/rust_2015/rust_bad_1.rs @@ -0,0 +1,7 @@ +// rustfmt-struct_field_align_threshold: 50 +fn func() { + Ok(ServerInformation { name: unwrap_message_string(items.get(0)), + vendor: unwrap_message_string(items.get(1)), + version: unwrap_message_string(items.get(2)), + spec_version: unwrap_message_string(items.get(3)), }); +} diff --git a/.automation/test/rust_2015/rust_good_1.rs b/.automation/test/rust_2015/rust_good_1.rs new file mode 100644 index 00000000..e1d5d6a9 --- /dev/null +++ b/.automation/test/rust_2015/rust_good_1.rs @@ -0,0 +1,8 @@ +fn func() { + Ok(ServerInformation { + name: unwrap_message_string(items.get(0)), + vendor: unwrap_message_string(items.get(1)), + version: unwrap_message_string(items.get(2)), + spec_version: unwrap_message_string(items.get(3)), + }); +} diff --git a/.automation/test/rust_2018/reports/expected-RUST_2018.tap b/.automation/test/rust_2018/reports/expected-RUST_2018.tap new file mode 100644 index 00000000..9ba97785 --- /dev/null +++ b/.automation/test/rust_2018/reports/expected-RUST_2018.tap @@ -0,0 +1,7 @@ +TAP version 13 +1..2 +not ok 1 - rust_bad_1.rs + --- + message: Diff in /tmp/lint/.automation/test/rust_2018/rust_bad_1.rs at line 1 \n // rustfmt-struct_field_align_threshold 50\n fn func() {\n- Ok(ServerInformation { name unwrap_message_string(items.get(0)),\n- vendor unwrap_message_string(items.get(1)),\n- version unwrap_message_string(items.get(2)),\n- spec_version unwrap_message_string(items.get(3)), });\n+ Ok(ServerInformation {\n+ name unwrap_message_string(items.get(0)),\n+ vendor unwrap_message_string(items.get(1)),\n+ version unwrap_message_string(items.get(2)),\n+ spec_version unwrap_message_string(items.get(3)),\n+ });\n }\n \n + ... +ok 2 - rust_good_1.rs diff --git a/.automation/test/rust_2018/rust_bad_1.rs b/.automation/test/rust_2018/rust_bad_1.rs new file mode 100644 index 00000000..9a3e93bd --- /dev/null +++ b/.automation/test/rust_2018/rust_bad_1.rs @@ -0,0 +1,7 @@ +// rustfmt-struct_field_align_threshold: 50 +fn func() { + Ok(ServerInformation { name: unwrap_message_string(items.get(0)), + vendor: unwrap_message_string(items.get(1)), + version: unwrap_message_string(items.get(2)), + spec_version: unwrap_message_string(items.get(3)), }); +} diff --git a/.automation/test/rust_2018/rust_good_1.rs b/.automation/test/rust_2018/rust_good_1.rs new file mode 100644 index 00000000..e1d5d6a9 --- /dev/null +++ b/.automation/test/rust_2018/rust_good_1.rs @@ -0,0 +1,8 @@ +fn func() { + Ok(ServerInformation { + name: unwrap_message_string(items.get(0)), + vendor: unwrap_message_string(items.get(1)), + version: unwrap_message_string(items.get(2)), + spec_version: unwrap_message_string(items.get(3)), + }); +} diff --git a/Dockerfile b/Dockerfile index cd9c46fc..b1d79dc5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -109,6 +109,7 @@ RUN apk add --no-cache \ R R-dev R-doc \ readline-dev \ ruby ruby-dev ruby-bundler ruby-rdoc \ + rustup \ zlib zlib-dev ######################################## @@ -141,6 +142,14 @@ ENV PATH="/node_modules/.bin:${PATH}" ############################## RUN bundle install +############################## +# Install rustfmt # +############################## +RUN ln -s /usr/bin/rustup-init /usr/bin/rustup \ + && rustup toolchain install stable-x86_64-unknown-linux-musl \ + && rustup component add rustfmt --toolchain=stable-x86_64-unknown-linux-musl \ + && ln -s /root/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/rustfmt /usr/bin/rustfmt + ################################### # Install DotNet and Dependencies # ################################### diff --git a/README.md b/README.md index a16f9153..88b3b8ae 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base | **R** | [lintr](https://github.com/jimhester/lintr) | | **Raku** | [Raku](https://raku.org) | | **Ruby** | [RuboCop](https://github.com/rubocop-hq/rubocop) | +| **Rust** | [Rustfmt](https://github.com/rust-lang/rustfmt) | | **Shell** | [Shellcheck](https://github.com/koalaman/shellcheck) / [executable bit check] / [shfmt](https://github.com/mvdan/sh) | | **Snakemake** | [snakefmt](https://github.com/snakemake/snakefmt/) / [snakemake --lint](https://snakemake.readthedocs.io/en/stable/snakefiles/writing_snakefiles.html#best-practices) | | **SQL** | [sql-lint](https://github.com/joereynolds/sql-lint) | @@ -284,6 +285,8 @@ But if you wish to select or exclude specific linters, we give you full control | **VALIDATE_R** | `true` | Flag to enable or disable the linting process of the R language. | | **VALIDATE_RAKU** | `true` | Flag to enable or disable the linting process of the Raku language. | | **VALIDATE_RUBY** | `true` | Flag to enable or disable the linting process of the Ruby language. | +| **VALIDATE_RUST_2015** | `true` | Flag to enable or disable the linting process of the Rust language. (edition: 2015) | +| **VALIDATE_RUST_2018** | `true` | Flag to enable or disable the linting process of Rust language. (edition: 2018) | | **VALIDATE_SHELL_SHFMT** | `true` | Flag to enable or disable the linting process of Shell scripts. (Utilizing: shfmt) | | **VALIDATE_SNAKEMAKE_LINT** | `true` | Flag to enable or disable the linting process of Snakefiles. (Utilizing: snakemake --lint) | | **VALIDATE_SNAKEMAKE_SNAKEFMT** | `true` | Flag to enable or disable the linting process of Snakefiles. (Utilizing: snakefmt) | diff --git a/lib/functions/buildFileList.sh b/lib/functions/buildFileList.sh index a11af7cf..2b1a84e0 100755 --- a/lib/functions/buildFileList.sh +++ b/lib/functions/buildFileList.sh @@ -640,6 +640,16 @@ function BuildFileList() { ################################ FILE_ARRAY_RUBY+=("${FILE}") + ###################### + # Get the RUST files # + ###################### + elif [ "${FILE_TYPE}" == "rs" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_RUST_2015+=("${FILE}") + FILE_ARRAY_RUST_2018+=("${FILE}") + ########################### # Get the SNAKEMAKE files # ########################### diff --git a/lib/linter.sh b/lib/linter.sh index b81799d9..845e5314 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -184,7 +184,9 @@ LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CLOUDFORMATION' 'CLOJURE' 'C 'DART' 'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GHERKIN' 'GO' 'GROOVY' 'HTML' 'JAVA' 'JAVASCRIPT_ES' "${JAVASCRIPT_STYLE_NAME}" 'JSCPD' 'JSON' 'JSX' 'KUBERNETES_KUBEVAL' 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' - 'PROTOBUF' 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'PYTHON_ISORT' 'R' 'RAKU' 'RUBY' 'SHELL_SHFMT' 'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' + 'PROTOBUF' 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'PYTHON_ISORT' + 'R' 'RAKU' 'RUBY' 'RUST_2015' 'RUST_2018' + 'SHELL_SHFMT' 'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' 'TEKTON' 'TERRAFORM' 'TERRAFORM_TERRASCAN' 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML') ############################## @@ -235,6 +237,8 @@ LINTER_NAMES_ARRAY['PYTHON_ISORT']="isort" LINTER_NAMES_ARRAY['R']="R" LINTER_NAMES_ARRAY['RAKU']="raku" LINTER_NAMES_ARRAY['RUBY']="rubocop" +LINTER_NAMES_ARRAY['RUST_2015']="rustfmt" +LINTER_NAMES_ARRAY['RUST_2018']="rustfmt" LINTER_NAMES_ARRAY['SHELL_SHFMT']="shfmt" LINTER_NAMES_ARRAY['SNAKEMAKE_LINT']="snakemake" LINTER_NAMES_ARRAY['SNAKEMAKE_SNAKEFMT']="snakefmt" @@ -804,6 +808,8 @@ LINTER_COMMANDS_ARRAY['PYTHON_ISORT']="isort --check --diff --sp ${PYTHON_ISORT_ LINTER_COMMANDS_ARRAY['R']="lintr" LINTER_COMMANDS_ARRAY['RAKU']="raku" LINTER_COMMANDS_ARRAY['RUBY']="rubocop -c ${RUBY_LINTER_RULES} --force-exclusion" +LINTER_COMMANDS_ARRAY['RUST_2015']="rustfmt --check --edition 2015" +LINTER_COMMANDS_ARRAY['RUST_2018']="rustfmt --check --edition 2018" LINTER_COMMANDS_ARRAY['SHELL_SHFMT']="shfmt -d" LINTER_COMMANDS_ARRAY['SNAKEMAKE_LINT']="snakemake --lint -s" LINTER_COMMANDS_ARRAY['SNAKEMAKE_SNAKEFMT']="snakefmt --config ${SNAKEMAKE_SNAKEFMT_LINTER_RULES} --check --compact-diff"