Skip to content

Commit

Permalink
Enhancement: Add support for google-java-format (#1969)
Browse files Browse the repository at this point in the history
* Add support for google-java-format

* adding tests and array

* fix comma

* Update linter.sh

* get it in order

* fix hava version

Co-authored-by: Admiral Awkbar <admiralawkbar@github.com>
  • Loading branch information
guotongfei and admiralAwkbar committed Sep 28, 2021
1 parent ec410c9 commit 1afc7f8
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 14 deletions.
19 changes: 19 additions & 0 deletions .automation/test/google_java_format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Google Java Format Test Cases

This folder holds the test cases for **Google Java format**.

## Additional Docs

No Additional information is needed for this test case.

## Good Test Cases

The test cases denoted: `LANGUAGE_good_FILE.EXTENSION` are all valid, and should pass successfully when linted.

- **Note:** They are linted utilizing the default linter rules.

## Bad Test Cases

The test cases denoted: `LANGUAGE_bad_FILE.EXTENSION` are **NOT** valid, and should trigger errors when linted.

- **Note:** They are linted utilizing the default linter rules.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class java_bad_1 // first letter of classname is not capitalized
{

public static void main(String args[])
{
System.out.println("Hello, World");
}
}
12 changes: 12 additions & 0 deletions .automation/test/google_java_format/google_java_format_bad_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Application {

/**
* main.
*
* @param args
*/
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Represents a good Java file.
*/
public static class JavaGood {
// Prints "Hello, World" to the terminal window.
private void helloWorld() {
System.out.println("Hello, World");
}
}
13 changes: 13 additions & 0 deletions .automation/test/google_java_format/google_java_format_good_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@SuppressWarnings("checkstyle:hideutilityclassconstructor")
public class Application {

/**
* main.
*
* @param args
*/
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}

}
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ RUN apk add --no-cache \
make \
musl-dev \
npm nodejs-current \
openjdk8-jre \
openjdk11-jre \
openssl-dev \
perl perl-dev \
py3-setuptools python3-dev \
Expand Down Expand Up @@ -287,6 +287,14 @@ RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repo
| cut -d '"' -f 4) \
&& curl --retry 5 --retry-delay 5 -sSL "$CHECKSTYLE_LATEST" \
--output /usr/bin/checkstyle \
##############################
# Install google-java-format #
##############################
&& GOOGLE_JAVA_FORMAT_VERSION=$(curl -s https://github.com/google/google-java-format/releases/latest \
| cut -d '"' -f 2 | cut -d '/' -f 8 | sed -e 's/v//g') \
&& curl --retry 5 --retry-delay 5 -sSL \
"https://github.com/google/google-java-format/releases/download/v$GOOGLE_JAVA_FORMAT_VERSION/google-java-format-$GOOGLE_JAVA_FORMAT_VERSION-all-deps.jar" \
--output /usr/bin/google-java-format \
#################################
# Install luacheck and luarocks #
#################################
Expand Down
10 changes: 9 additions & 1 deletion Dockerfile-slim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ RUN apk add --no-cache \
make \
musl-dev \
npm nodejs-current \
openjdk8-jre \
openjdk11-jre \
openssl-dev \
perl perl-dev \
py3-setuptools python3-dev \
Expand Down Expand Up @@ -225,6 +225,14 @@ RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repo
| cut -d '"' -f 4) \
&& curl --retry 5 --retry-delay 5 -sSL "$CHECKSTYLE_LATEST" \
--output /usr/bin/checkstyle \
##############################
# Install google-java-format #
##############################
&& GOOGLE_JAVA_FORMAT_VERSION=$(curl -s https://github.com/google/google-java-format/releases/latest \
| cut -d '"' -f 2 | cut -d '/' -f 8 | sed -e 's/v//g') \
&& curl --retry 5 --retry-delay 5 -sSL \
"https://github.com/google/google-java-format/releases/download/v$GOOGLE_JAVA_FORMAT_VERSION/google-java-format-$GOOGLE_JAVA_FORMAT_VERSION-all-deps.jar" \
--output /usr/bin/google-java-format \
#################################
# Install luacheck and luarocks #
#################################
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base
| **Golang** | [golangci-lint](https://github.com/golangci/golangci-lint) |
| **Groovy** | [npm-groovy-lint](https://github.com/nvuillam/npm-groovy-lint) |
| **HTML** | [HTMLHint](https://github.com/htmlhint/HTMLHint) |
| **Java** | [checkstyle](https://checkstyle.org) |
| **Java** | [checkstyle](https://checkstyle.org) / [google-java-format](https://github.com/google/google-java-format) |
| **JavaScript** | [eslint](https://eslint.org/) / [standard js](https://standardjs.com/) |
| **JSON** | [eslint-plugin-json](https://www.npmjs.com/package/eslint-plugin-json) |
| **JSONC** | [eslint-plugin-jsonc](https://www.npmjs.com/package/eslint-plugin-jsonc) |
Expand Down Expand Up @@ -337,9 +337,10 @@ But if you wish to select or exclude specific linters, we give you full control
| **VALIDATE_GITLEAKS** | `true` | Flag to enable or disable the linting process of the secrets. |
| **VALIDATE_GHERKIN** | `true` | Flag to enable or disable the linting process of the Gherkin language. |
| **VALIDATE_GO** | `true` | Flag to enable or disable the linting process of the Golang language. |
| **VALIDATE_GOOGLE_JAVA_FORMAT** | `true` | Flag to enable or disable the linting process of the Java language. (Utilizing: google-java-format) |
| **VALIDATE_GROOVY** | `true` | Flag to enable or disable the linting process of the language. |
| **VALIDATE_HTML** | `true` | Flag to enable or disable the linting process of the HTML language. |
| **VALIDATE_JAVA** | `true` | Flag to enable or disable the linting process of the language. |
| **VALIDATE_JAVA** | `true` | Flag to enable or disable the linting process of the Java language. (Utilizing: checkstyle) |
| **VALIDATE_JAVASCRIPT_ES** | `true` | Flag to enable or disable the linting process of the Javascript language. (Utilizing: eslint) |
| **VALIDATE_JAVASCRIPT_STANDARD** | `true` | Flag to enable or disable the linting process of the Javascript language. (Utilizing: standard) |
| **VALIDATE_JSCPD** | `true` | Flag to enable or disable the JSCPD. |
Expand Down
1 change: 1 addition & 0 deletions lib/functions/buildFileList.sh
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ function BuildFileList() {
# Append the file to the array #
################################
FILE_ARRAY_JAVA+=("${FILE}")
FILE_ARRAY_GOOGLE_JAVA_FORMAT+=("${FILE}")

############################
# Get the JavaScript files #
Expand Down
2 changes: 1 addition & 1 deletion lib/functions/linterVersions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ BuildLinterVersions() {
GET_VERSION_CMD="$("${LINTER}" -v 2>&1)"
elif [[ ${LINTER} == "terrascan" ]]; then
GET_VERSION_CMD="$("${LINTER}" version 2>&1)"
elif [[ ${LINTER} == "checkstyle" ]]; then
elif [[ ${LINTER} == "checkstyle" ]] || [[ ${LINTER} == "google-java-format" ]]; then
GET_VERSION_CMD="$(java -jar "/usr/bin/${LINTER}" --version 2>&1)"
elif [[ ${LINTER} == "clippy" ]]; then
GET_VERSION_CMD="$(cargo-clippy --version 2>&1)"
Expand Down
20 changes: 11 additions & 9 deletions lib/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ fi
LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CLANG_FORMAT'
'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'CPP' 'CSHARP' 'CSS' 'DART'
'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GITHUB_ACTIONS'
'GITLEAKS' 'GHERKIN' 'GO' 'GROOVY' 'HTML' 'JAVA' 'JAVASCRIPT_ES'
"${JAVASCRIPT_STYLE_NAME}" 'JSCPD' 'JSON' 'JSONC' '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' 'PYTHON_MYPY' 'R' 'RAKU' 'RUBY'
'RUST_2015' 'RUST_2018' 'RUST_CLIPPY' 'SHELL_SHFMT' 'SNAKEMAKE_LINT'
'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' 'SQLFLUFF' 'TEKTON' 'TERRAFORM_TFLINT'
'TERRAFORM_TERRASCAN' 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD'
'XML' 'YAML')
'GITLEAKS' 'GHERKIN' 'GO' 'GOOGLE_JAVA_FORMAT' 'GROOVY' 'HTML' 'JAVA'
'JAVASCRIPT_ES' "${JAVASCRIPT_STYLE_NAME}" 'JSCPD' 'JSON' 'JSONC' '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' 'PYTHON_MYPY'
'R' 'RAKU' 'RUBY' 'RUST_2015' 'RUST_2018' 'RUST_CLIPPY' 'SHELL_SHFMT'
'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' 'SQLFLUFF' 'TEKTON'
'TERRAFORM_TFLINT' 'TERRAFORM_TERRASCAN' 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES'
'TYPESCRIPT_STANDARD' 'XML' 'YAML')

##############################
# Linter command names array #
Expand All @@ -236,6 +236,7 @@ LINTER_NAMES_ARRAY['GITHUB_ACTIONS']="actionlint"
LINTER_NAMES_ARRAY['GITLEAKS']="gitleaks"
LINTER_NAMES_ARRAY['GHERKIN']="gherkin-lint"
LINTER_NAMES_ARRAY['GO']="golangci-lint"
LINTER_NAMES_ARRAY['GOOGLE_JAVA_FORMAT']="google-java-format"
LINTER_NAMES_ARRAY['GROOVY']="npm-groovy-lint"
LINTER_NAMES_ARRAY['HTML']="htmlhint"
LINTER_NAMES_ARRAY['JAVA']="checkstyle"
Expand Down Expand Up @@ -837,6 +838,7 @@ LINTER_COMMANDS_ARRAY['GITHUB_ACTIONS']="actionlint -config-file ${GITHUB_ACTION
LINTER_COMMANDS_ARRAY['GITLEAKS']="gitleaks -q -c ${GITLEAKS_LINTER_RULES} --no-git -p"
LINTER_COMMANDS_ARRAY['GHERKIN']="gherkin-lint -c ${GHERKIN_LINTER_RULES}"
LINTER_COMMANDS_ARRAY['GO']="golangci-lint run -c ${GO_LINTER_RULES}"
LINTER_COMMANDS_ARRAY['GOOGLE_JAVA_FORMAT']="java -jar /usr/bin/google-java-format"
LINTER_COMMANDS_ARRAY['GROOVY']="npm-groovy-lint -c ${GROOVY_LINTER_RULES} --failon warning"
LINTER_COMMANDS_ARRAY['HTML']="htmlhint --config ${HTML_LINTER_RULES}"
LINTER_COMMANDS_ARRAY['JAVA']="java -jar /usr/bin/checkstyle -c ${JAVA_LINTER_RULES}"
Expand Down
1 change: 1 addition & 0 deletions test/inspec/super-linter/controls/super_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
{ linter_name: "clang-format"},
{ linter_name: "cfn-lint"},
{ linter_name: "checkstyle", version_command: "java -jar /usr/bin/checkstyle --version"},
{ linter_name: "google-java-format", version_command: "java -jar /usr/bin/google-java-format --version"},
{ linter_name: "chktex"},
{ linter_name: "clippy", linter_command: "clippy", version_command: "cargo-clippy --version"},
{ linter_name: "clj-kondo"},
Expand Down

0 comments on commit 1afc7f8

Please sign in to comment.