From 461ed33d0c4a5a201cf3f4fca257587429bd1ef0 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 10 Jun 2024 21:41:03 +0100 Subject: [PATCH] enhancement: YAMLLINT using --strict causes error and incorrect error count (#3612) --- .automation/test/yaml/bad/yml_bad_01.yml | 44 +++++++++++++++++++ .automation/test/yaml/bad/yml_bad_02.yml | 28 ++++++++++++ .automation/test/yaml/bad/yml_bad_03.yml | 26 +++++++++++ .automation/test/yaml/bad/yml_bad_04.yml | 40 +++++++++++++++++ .automation/test/yaml/bad/yml_bad_05.yml | 23 ++++++++++ .automation/test/yaml/bad/yml_bad_06.yml | 25 +++++++++++ .automation/test/yaml/bad/yml_bad_07.yml | 27 ++++++++++++ .../{yml_fix_1.yml => fix/yml_fix_01.yml} | 0 .automation/test/yaml/good/yml_good_01.yml | 25 +++++++++++ .automation/test/yaml/good/yml_good_02.yml | 24 ++++++++++ .automation/test/yaml/good/yml_good_03.yml | 22 ++++++++++ .automation/test/yaml/good/yml_good_04.yml | 20 +++++++++ .automation/test/yaml/good/yml_good_05.yml | 21 +++++++++ .automation/test/yaml/good/yml_good_06.yml | 21 +++++++++ .automation/test/yaml/good/yml_good_07.yml | 27 ++++++++++++ .automation/test/yaml/yml_bad_1.yml | 17 ------- .automation/test/yaml/yml_bad_2.yml | 17 ------- .automation/test/yaml/yml_good_1.yml | 18 -------- CHANGELOG.md | 1 + .../yaml.megalinter-descriptor.yml | 4 +- 20 files changed, 377 insertions(+), 53 deletions(-) create mode 100644 .automation/test/yaml/bad/yml_bad_01.yml create mode 100644 .automation/test/yaml/bad/yml_bad_02.yml create mode 100644 .automation/test/yaml/bad/yml_bad_03.yml create mode 100644 .automation/test/yaml/bad/yml_bad_04.yml create mode 100644 .automation/test/yaml/bad/yml_bad_05.yml create mode 100644 .automation/test/yaml/bad/yml_bad_06.yml create mode 100644 .automation/test/yaml/bad/yml_bad_07.yml rename .automation/test/yaml/{yml_fix_1.yml => fix/yml_fix_01.yml} (100%) create mode 100644 .automation/test/yaml/good/yml_good_01.yml create mode 100644 .automation/test/yaml/good/yml_good_02.yml create mode 100644 .automation/test/yaml/good/yml_good_03.yml create mode 100644 .automation/test/yaml/good/yml_good_04.yml create mode 100644 .automation/test/yaml/good/yml_good_05.yml create mode 100644 .automation/test/yaml/good/yml_good_06.yml create mode 100644 .automation/test/yaml/good/yml_good_07.yml delete mode 100644 .automation/test/yaml/yml_bad_1.yml delete mode 100644 .automation/test/yaml/yml_bad_2.yml delete mode 100644 .automation/test/yaml/yml_good_1.yml diff --git a/.automation/test/yaml/bad/yml_bad_01.yml b/.automation/test/yaml/bad/yml_bad_01.yml new file mode 100644 index 00000000000..c0e41b90303 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_01.yml @@ -0,0 +1,44 @@ +##################### +##################### +## Azure Pipelines ## +##################### +##################### + +# Error: 6:1 [trailing-spaces] trailing spaces +trigger: main +# Warning: 8:1 [document-start] missing document start "---" + +pool: + vmImage: 'ubuntu-latest' +jobs: +- job: Build +# Error: trailing-spaces] trailing spaces + + steps: + - task: UseNode@1 + inputs: + versionSpec: 12.x + checkLatest: true + empty: +# Error: [indentation] wrong indentation: expected 2 but found 0 + +- job: Test + steps: + +# Error: [empty-lines] too many blank lines (3 > 2) + + + + - script: npm install +# Error: [indentation] wrong indentation: expected 4 but found 2 + - script: npm test + - script: echo "Testing!" + +# Error: [trailing-spaces] trailing spaces + + +# Error: [indentation] wrong indentation: expected 4 but found 2 + - script: npm test +# Error: [empty-lines] too many blank lines (1 > 0) + + diff --git a/.automation/test/yaml/bad/yml_bad_02.yml b/.automation/test/yaml/bad/yml_bad_02.yml new file mode 100644 index 00000000000..94543fb5f1e --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_02.yml @@ -0,0 +1,28 @@ +##################### +##################### +## GitHub Actions ## +##################### +##################### + +# Rule: document-start - Missing document start marker +name: CI +on: [push, pull_request] +jobs: + build: + runs-on: ubuntu-latest # Rule: trailing-spaces - Trailing spaces are disallowed + steps: + - uses: actions/checkout@v2 # Rule: indentation - Expected 6 spaces but found 5 + - name: Run a one-line script # Rule: syntax - Expected , but found '-' + # Rule: trailing-spaces - Trailing spaces are disallowed + run: echo Hello, world! + - name: Two-line script + run: | + echo Add other actions to build, + echo test + - name: Three-line script + run: | + echo Line 1 + echo Line 2 # Rule: indentation - Expected 6 spaces but found 5 + echo Line 3 # Rule: trailing-spaces - Trailing spaces are disallowed +# Rule: empty-lines - Too many blank lines (1 > 0) + diff --git a/.automation/test/yaml/bad/yml_bad_03.yml b/.automation/test/yaml/bad/yml_bad_03.yml new file mode 100644 index 00000000000..fde7cc54486 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_03.yml @@ -0,0 +1,26 @@ +##################### +##################### +##### GitLab CI ##### +##################### +##################### + +# Rule: document-start - Missing document start marker +stages: +# Rule: wrong indentation: expected at least 1 +- build +- test +build_job: + stage: build + script: + - echo "Building..." # Rule: trailing-spaces - Trailing spaces are disallowed + # Rule: syntax error: expected , but found '' (syntax) + - echo "Build complete!" # Rule: empty-lines - Empty lines in a block avoided +test_job: + stage: test + script: + - echo "Testing..." + - # Rule: empty-values - Empty values should be avoided + - echo "Test complete!" + - echo "Another test" +# Rule: empty-lines - Too many blank lines (1 > 0) + diff --git a/.automation/test/yaml/bad/yml_bad_04.yml b/.automation/test/yaml/bad/yml_bad_04.yml new file mode 100644 index 00000000000..86c0b087e77 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_04.yml @@ -0,0 +1,40 @@ +######################## +######################## +## Data Serialization ## +######################## +######################## + +# Rule: document-start - Missing document start marker +# Rule: empty-lines - Too many blank lines (4 > 2) + + + + +data: + - id: 1 + name: John Doe + - id: 2 + name: Jane Smith + - id: 3 + - &anchor + foo: bar + # Rule: anchors - found undeclared alias "unknown" + - <<: *unknown + extra: value + - &anchor Foo Bar + - &anchor [item 1, item 2] + - items: + - item1 + - item2 + name: Alice Johnson # Rule: indentation - Inconsistent indentation +address: + # Rule: trailing-spaces - Trailing spaces are disallowed + street: 123 Main St + city: Anytown + zip: 12345 + state: CA + country: USA + garage: { key1: 4, key2: 8 } +phone: +# Rule: empty-lines - Too many blank lines (1 > 0) + diff --git a/.automation/test/yaml/bad/yml_bad_05.yml b/.automation/test/yaml/bad/yml_bad_05.yml new file mode 100644 index 00000000000..e38f10d6942 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_05.yml @@ -0,0 +1,23 @@ +########################## +########################## +## Kubernetes Manifests ## +########################## +########################## + +# Rule: document-start - Missing document start marker +apiVersion: v1 +kind: Pod +metadata: + name: mypod +spec: + containers: + - name: mycontainer # Rule: indentation - Inconsistent indentation + image: myimage + # Rule: trailing-spaces - Trailing spaces are disallowed + ports: + - containerPort: 80 # Rule: indentation - Inconsistent indentation + - containerPort: 443 # Rule: indentation - Inconsistent indentation + - name: mysidecar + image: sidecarimage + ports: + - containerPort: 8080 diff --git a/.automation/test/yaml/bad/yml_bad_06.yml b/.automation/test/yaml/bad/yml_bad_06.yml new file mode 100644 index 00000000000..1f6d7752267 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_06.yml @@ -0,0 +1,25 @@ +####################### +####################### +## Ansible Playbooks ## +####################### +####################### + +# Rule: document-start - Missing document start marker +- hosts: localhost + tasks: + - name: Install package + # Rule: trailing-spaces - Trailing spaces are disallowed + apt: + name: nginx + state: present + # syntax error: expected , but found '' (syntax) + - name: Start service + service: # Rule: indentation - Inconsistent indentation + name: nginx # Rule: indentation - Inconsistent indentation + state: started # Rule: indentation - Inconsistent indentation +- hosts: all + tasks: + - name: Echo message + command: echo "Hello World" +# Rule: empty-lines - Too many blank lines (1 > 0) + diff --git a/.automation/test/yaml/bad/yml_bad_07.yml b/.automation/test/yaml/bad/yml_bad_07.yml new file mode 100644 index 00000000000..cb3d3301a75 --- /dev/null +++ b/.automation/test/yaml/bad/yml_bad_07.yml @@ -0,0 +1,27 @@ +######################## +######################## +## Configuration File ## +######################## +######################## + +# Rule: document-start - Missing document start marker +database: + host: localhost + port: 5432 + user: admin + password: admin +server: + host: 0.0.0.0 + port: 8080 + ssl: + enabled: true + certificate: /path/to/cert.pem + key: /path/to/key.pem +# Rule: trailing-spaces - Trailing spaces are disallowed +logging: + level: DEBUG # Rule: indentation - Inconsistent indentation + # syntax error: expected , but found '' (syntax) + file: /var/log/webapp.log +features: + enable_feature_x: True + enable_feature_y: false diff --git a/.automation/test/yaml/yml_fix_1.yml b/.automation/test/yaml/fix/yml_fix_01.yml similarity index 100% rename from .automation/test/yaml/yml_fix_1.yml rename to .automation/test/yaml/fix/yml_fix_01.yml diff --git a/.automation/test/yaml/good/yml_good_01.yml b/.automation/test/yaml/good/yml_good_01.yml new file mode 100644 index 00000000000..ec2ca13d1fd --- /dev/null +++ b/.automation/test/yaml/good/yml_good_01.yml @@ -0,0 +1,25 @@ +##################### +##################### +## Azure Pipelines ## +##################### +##################### +--- +trigger: + - main + +pool: + vmImage: "ubuntu-latest" + +jobs: + - job: Build + steps: + - task: UseNode@1 + inputs: + versionSpec: "12.x" + checkLatest: true + + - job: Test + steps: + - script: npm install + - script: npm test + - script: echo "Testing!" diff --git a/.automation/test/yaml/good/yml_good_02.yml b/.automation/test/yaml/good/yml_good_02.yml new file mode 100644 index 00000000000..f34e470cf6b --- /dev/null +++ b/.automation/test/yaml/good/yml_good_02.yml @@ -0,0 +1,24 @@ +##################### +##################### +## GitHub Actions ## +##################### +##################### +--- +name: CI +on: [push, pull_request] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Run a one-line script + run: echo Hello, world! + - name: Two-line script + run: | + echo Add other actions to build, + echo test, and deploy your project. + - name: Three-line script + run: | + echo Line 1 + echo Line 2 + echo Line 3 diff --git a/.automation/test/yaml/good/yml_good_03.yml b/.automation/test/yaml/good/yml_good_03.yml new file mode 100644 index 00000000000..36378a321b8 --- /dev/null +++ b/.automation/test/yaml/good/yml_good_03.yml @@ -0,0 +1,22 @@ +##################### +##################### +##### GitLab CI ##### +##################### +##################### +--- +stages: + - build + - test + +build_job: + stage: build + script: + - echo "Building..." + - echo "Build complete!" + +test_job: + stage: test + script: + - echo "Testing..." + - echo "Test complete!" + - echo "Another test" diff --git a/.automation/test/yaml/good/yml_good_04.yml b/.automation/test/yaml/good/yml_good_04.yml new file mode 100644 index 00000000000..73e94601f6e --- /dev/null +++ b/.automation/test/yaml/good/yml_good_04.yml @@ -0,0 +1,20 @@ +######################## +######################## +## Data Serialization ## +######################## +######################## +--- +data: + - id: 1 + name: John Doe + - id: 2 + name: Jane Smith + - id: 3 + name: Alice Johnson + +address: + street: 123 Main St + city: Anytown + zip: 12345 + state: CA + country: USA diff --git a/.automation/test/yaml/good/yml_good_05.yml b/.automation/test/yaml/good/yml_good_05.yml new file mode 100644 index 00000000000..017e1760d53 --- /dev/null +++ b/.automation/test/yaml/good/yml_good_05.yml @@ -0,0 +1,21 @@ +########################## +########################## +## Kubernetes Manifests ## +########################## +########################## +--- +apiVersion: v1 +kind: Pod +metadata: + name: mypod +spec: + containers: + - name: mycontainer + image: myimage + ports: + - containerPort: 80 + - containerPort: 443 + - name: mysidecar + image: sidecarimage + ports: + - containerPort: 8080 diff --git a/.automation/test/yaml/good/yml_good_06.yml b/.automation/test/yaml/good/yml_good_06.yml new file mode 100644 index 00000000000..94400b68ccc --- /dev/null +++ b/.automation/test/yaml/good/yml_good_06.yml @@ -0,0 +1,21 @@ +####################### +####################### +## Ansible Playbooks ## +####################### +####################### +--- +- hosts: localhost + tasks: + - name: Install package + apt: + name: nginx + state: present + - name: Start service + service: + name: nginx + state: started + +- hosts: all + tasks: + - name: Echo message + command: echo "Hello World" diff --git a/.automation/test/yaml/good/yml_good_07.yml b/.automation/test/yaml/good/yml_good_07.yml new file mode 100644 index 00000000000..6ca4b5721b4 --- /dev/null +++ b/.automation/test/yaml/good/yml_good_07.yml @@ -0,0 +1,27 @@ +######################## +######################## +## Configuration File ## +######################## +######################## +--- +database: + host: localhost + port: 5432 + user: admin + password: admin + +server: + host: 0.0.0.0 + port: 8080 + ssl: + enabled: true + certificate: /path/to/cert.pem + key: /path/to/key.pem + +logging: + level: DEBUG + file: /var/log/webapp.log + +features: + enable_feature_x: true + enable_feature_y: false diff --git a/.automation/test/yaml/yml_bad_1.yml b/.automation/test/yaml/yml_bad_1.yml deleted file mode 100644 index 11e35c0411e..00000000000 --- a/.automation/test/yaml/yml_bad_1.yml +++ /dev/null @@ -1,17 +0,0 @@ -##################### -##################### -## Heres some vars ## -##################### -##################### - -############ -# Env Vars # -############ -env: - browser: here: there : again "yep" - es6: 0 - jest: yes - -Here: there 'is' something going on - -something: "For 'Nothing'" 123 diff --git a/.automation/test/yaml/yml_bad_2.yml b/.automation/test/yaml/yml_bad_2.yml deleted file mode 100644 index 11e35c0411e..00000000000 --- a/.automation/test/yaml/yml_bad_2.yml +++ /dev/null @@ -1,17 +0,0 @@ -##################### -##################### -## Heres some vars ## -##################### -##################### - -############ -# Env Vars # -############ -env: - browser: here: there : again "yep" - es6: 0 - jest: yes - -Here: there 'is' something going on - -something: "For 'Nothing'" 123 diff --git a/.automation/test/yaml/yml_good_1.yml b/.automation/test/yaml/yml_good_1.yml deleted file mode 100644 index 30325016ed7..00000000000 --- a/.automation/test/yaml/yml_good_1.yml +++ /dev/null @@ -1,18 +0,0 @@ ---- -##################### -##################### -## Heres some vars ## -##################### -##################### - -############ -# Env Vars # -############ -env: - browser: true - es6: true - jest: true - -Here: there # good comment - -something: "For Nothing" diff --git a/CHANGELOG.md b/CHANGELOG.md index 9632aa9a978..73e09b037bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l - Fixes - [syft](https://github.com/anchore/syft) use `scan` instead of deprecated `packages` arg - [Powershell](https://github.com/PowerShell/PSScriptAnalyzer#readme) Error table truncation improvements + - [yamllint](https://github.com/adrienverge/yamllint) fix error/warning count to work with different log output formats - Doc diff --git a/megalinter/descriptors/yaml.megalinter-descriptor.yml b/megalinter/descriptors/yaml.megalinter-descriptor.yml index 77e3deb3aa5..444ede90925 100644 --- a/megalinter/descriptors/yaml.megalinter-descriptor.yml +++ b/megalinter/descriptors/yaml.megalinter-descriptor.yml @@ -74,10 +74,12 @@ linters: config_file_name: ".yamllint.yml" cli_lint_mode: list_of_files cli_lint_errors_count: regex_count - cli_lint_errors_regex: "[0-9]+:[0-9]+.*error" + cli_lint_errors_regex: "[0-9]+:[0-9]+:?\\s*(?:\\[?warning\\]?|\\[?error\\]?)|(?:::warning file=|::error file=)" examples: - "yamllint myfile.yaml" - "yamllint -c .yamllint.yml myfile.yaml" + - "yamllint --strict" + - "yamllint --no-warnings" install: pip: - yamllint