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

Add skip configuration to assert_used #633

Merged
merged 3 commits into from Nov 30, 2020

Conversation

wilbertom
Copy link
Contributor

@wilbertom wilbertom commented Aug 12, 2020

Adding this configuration allows the user to skip the assert_used check
against some files. This is useful because asserts are very common
in test files when using pytest.

Specifying this configuration:

assert_used:
  skips: ['test.py$', '^test']

would skip all asserts against a test file.

Resolves #346

Thank you for the awesome project and your time!

@wilbertom
Copy link
Contributor Author

Fixing the failed checks now.

@wilbertom
Copy link
Contributor Author

@azrdev sorry for the late reply. I completely missed this notification.

I would like to edit this PR to use fnmatch instead of regular expressions. They're much more intuitive for this:

assert_used:
  skips: ['*_test.py', 'test_*.py']

I was not aware of this module when I wrote this PR.

@wilbertom wilbertom force-pushed the assert-used-skip-functionality branch from 887a76b to 1bdd08e Compare November 24, 2020 06:49
@wilbertom
Copy link
Contributor Author

OK, the checks are passing.

As I said, I would prefer using fnmatch before this gets merged because it would be a breaking change.

Is there a way to run the unit/functional tests locally? I'm trying with pytest but a lot seem to be failing. Do we have docs on doing that?

I would like to implement the same thing for random tests. While I agree that it's good to warn about it not being cryptographically secure, in tests randomly choosing data can be useful. I know this would be best served as a separate PR but like to be able to run the unit tests before then.

@lukehinds
Copy link
Member

@wilbertom see CI for how to run tests (we use tox):

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r test-requirements.txt
pip install tox
- name: Run tox
run: tox -e py38

@wilbertom
Copy link
Contributor Author

@lukehinds perfect. Thank you.

Adding this configuration allows the user to skip the assert_used
against some files. This is useful because asserts are very common
in test files when using pytest.

Specifying this configuration:

```
assert_used:
  skips: ['*_test.py', 'test_*.py']
```

would skip all asserts against a test file.

Resolves PyCQA#346
@wilbertom wilbertom force-pushed the assert-used-skip-functionality branch from b9efd37 to d76f506 Compare November 25, 2020 00:26
@wilbertom
Copy link
Contributor Author

This is ready for a review to be merged. I replaced re with fnmatch and added documentation.

@lukehinds lukehinds self-requested a review November 30, 2020 10:48
@lukehinds lukehinds merged commit 24db07e into PyCQA:master Nov 30, 2020
@ericwb ericwb added this to the Release 1.6.3 milestone Dec 6, 2020
@copdips
Copy link

copdips commented Mar 8, 2021

@wilbertom
Thx for this PR, could you please provide a full config example ?
I tried .bandit and .bandit.yml, none of them works, I think my config file is not good, and search in the online docs, I can not find an full exmaple.

assert_used:
   skips: ['*_test.py', 'test_*.py']

@copdips
Copy link

copdips commented Mar 9, 2021

@wilbertom
Thx for this PR, could you please provide a full config example ?
I tried .bandit and .bandit.yml, none of them works, I think my config file is not good, and search in the online docs, I can not find an full exmaple.

assert_used:
   skips: ['*_test.py', 'test_*.py']

I find the solution, the skips is applied on the file path not the file name

@arthurio
Copy link

arthurio commented May 26, 2021

@copdips Could you elaborate? I can't get it to work either.

Ok I figured it out... You need both an ini file for the targets and a yaml file for the configuration...

# .bandit
[bandit]
targets: src,tests
# bandit.yaml
assert_used:
   skips: ['tests/**']

Then:

$ bandit --ini .bandit -c bandit.yaml -r
[main]	INFO	Using command line arg for excluded paths
[main]	INFO	Using ini file for selected targets
[main]	INFO	Using command line arg for recursive scan
[main]	INFO	Using command line arg for aggregate output type
[main]	INFO	Using command line arg for max code lines output for issue
[main]	INFO	Using command line arg for severity level
[main]	INFO	Using command line arg for confidence level
[main]	INFO	Using command line arg for output format
[main]	INFO	Using command line arg for output file
[main]	INFO	profile include tests: None
[main]	INFO	profile exclude tests: None
[main]	INFO	cli include tests: None
[main]	INFO	cli exclude tests: None
[main]	INFO	using config: bandit.yaml
[main]	INFO	running on Python 3.9.1
Run started:2021-05-26 06:12:07.353758

Test results:
	No issues identified.

Code scanned:
	Total lines of code: 70
	Total lines skipped (#nosec): 8

Run metrics:
	Total issues (by severity):
		Undefined: 0.0
		Low: 0.0
		Medium: 0.0
		High: 0.0
	Total issues (by confidence):
		Undefined: 0.0
		Low: 0.0
		Medium: 0.0
		High: 0.0
Files skipped (0):

mikespallino pushed a commit to mikespallino/bandit that referenced this pull request Aug 25, 2021
* Add skip configuration to assert_used

Adding this configuration allows the user to skip the assert_used
against some files. This is useful because asserts are very common
in test files when using pytest.

Specifying this configuration:

```
assert_used:
  skips: ['*_test.py', 'test_*.py']
```

would skip all asserts against a test file.

Resolves PyCQA#346

* Document assert test skipping

Co-authored-by: Luke Hinds <7058938+lukehinds@users.noreply.github.com>
mikespallino pushed a commit to mikespallino/bandit that referenced this pull request Jan 7, 2022
* Add skip configuration to assert_used

Adding this configuration allows the user to skip the assert_used
against some files. This is useful because asserts are very common
in test files when using pytest.

Specifying this configuration:

```
assert_used:
  skips: ['*_test.py', 'test_*.py']
```

would skip all asserts against a test file.

Resolves PyCQA#346

* Document assert test skipping

Co-authored-by: Luke Hinds <7058938+lukehinds@users.noreply.github.com>
@sshishov
Copy link

How to put everything inside on pyproject.toml file? We are not using ini or yaml configurations, therefore for us the fix is not working.

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

Successfully merging this pull request may close these issues.

One-liner in bandit config to skip B101 assert_used in files matching a filter
7 participants