Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule 602 (Don’t compare to empty string) find an error in shell script #1232

Closed
kazh000 opened this issue Jan 15, 2021 · 0 comments · Fixed by #1480
Closed

Rule 602 (Don’t compare to empty string) find an error in shell script #1232

kazh000 opened this issue Jan 15, 2021 · 0 comments · Fixed by #1480
Assignees
Labels
Milestone

Comments

@kazh000
Copy link

kazh000 commented Jan 15, 2021

Summary

Linter finds an error of the rule 602 in shell code

Issue Type
  • Bug Report
Ansible and Ansible Lint details
ansible 2.10.4
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.12 (default, Dec 18 2020, 06:04:50) [GCC 8.3.0]

ansible-lint 5.0.0a1.dev8
  • ansible installation method: pip
  • ansible-lint installation method: pip
OS / ENVIRONMENT

Run in docker. Dockerfile:

FROM python:3.6

WORKDIR /usr/src/app

RUN pip install git+https://github.com/ansible-community/ansible-lint "ansible"

RUN ansible  --version

RUN ansible-lint --version

RUN echo "---\n- hosts: localhost\n  gather_facts: no\n  tasks:\n\
  - name: Reproduce the bug in the rule 602\n    shell: |\n\
      var=\$(pwd)\n      if [[ \"\$var\" == \"\" ]]; then echo test; fi\n    changed_when

RUN cat test.yml

RUN ansible-lint --force-color --show-relpath test.yml
STEPS TO REPRODUCE
docker build --rm -f Dockerfile .
Desired Behaviour

No error message

Actual Behaviour

Please give some details of what is actually happening.
Include a minimum complete verifiable example with:

  • playbook
  • output of running ansible-lint
  • if you're getting a stack trace, output of
    ansible-playbook --syntax-check playbook
Step 1/9 : FROM python:3.6
 ---> bd4a91d81d7e
Step 2/9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 6b223c53a8f1
Step 3/9 : RUN pip install git+https://github.com/ansible-community/ansible-lint "ansible"
 ---> Using cache
 ---> 6af3b1194721
Step 4/9 : RUN ansible  --version
 ---> Running in c2b07e26d819
ansible 2.10.4
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.12 (default, Dec 18 2020, 06:04:50) [GCC 8.3.0]
Removing intermediate container c2b07e26d819
 ---> e319230a42c3
Step 5/9 : RUN ansible-lint --version
 ---> Running in 6130591acb07
ansible-lint 5.0.0a1.dev8
Removing intermediate container 6130591acb07
 ---> 5b4fde26df0d
Step 6/9 : RUN echo "---\n- hosts: localhost\n  gather_facts: no\n  tasks:\n  - name: Reproduce the bug in the rule 602\n    shell: |\n      var=\$(pwd)\n      if [[ \"\$var\" == \"\" ]]; then echo test; fi\n    changed_when: False" > test.yml
 ---> Running in 3d5ba620c413
Removing intermediate container 3d5ba620c413
 ---> 59509ff20919
Step 7/9 : RUN cat test.yml
 ---> Running in 736962797dc1
---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: Reproduce the bug in the rule 602
    shell: |
      var=$(pwd)
      if [[ "$var" == "" ]]; then echo test; fi
    changed_when: False
Removing intermediate container 736962797dc1
 ---> af9ed683e9e1
Step 8/9 : RUN ansible-lint --force-color --show-relpath test.yml
 ---> Running in 62d37ee100d5
WARNING  Listing 1 violation(s) that are fatal
602: Don't compare to empty string
test.yml:8       if [[ "$var" == "" ]]; then echo test; fi
You can skip specific rules or tags by adding them to your configuration file:
# .ansible-lint
warn_list:  # or 'skip_list' to silence them completely
  - '602'  # Don't compare to empty string
Finished with 1 failure(s), 0 warning(s) on 1 files.
The command '/bin/sh -c ansible-lint --force-color --show-relpath test.yml' returned a non-zero code: 2
@kazh000 kazh000 added the bug label Jan 15, 2021
@ssbarnea ssbarnea self-assigned this Mar 18, 2021
@ssbarnea ssbarnea added this to the 5.0.5 milestone Mar 18, 2021
ssbarnea added a commit that referenced this issue Mar 18, 2021
- rewrite matching to avoid line processing and trigger only on when
  blocks, highly reducing the chance of false-positives
- add additional pass test to avoid future regression
- refactor rule to use embedded tests

Fixes: #1232
ssbarnea added a commit that referenced this issue Mar 18, 2021
- rewrite matching to avoid line processing and trigger only on when
  blocks, highly reducing the chance of false-positives
- add additional pass test to avoid future regression
- refactor rule to use embedded tests

Fixes: #1232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants