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 shebang functionality to tests #1784

Merged
merged 1 commit into from
Feb 29, 2024
Merged

Conversation

gunnarku
Copy link
Contributor

Tests that contain if __name__ == "__main__": now have a shebang line and execute permission.

Why are these changes needed?

The main reason for these changes is increased developer productivity and shortening the validation cycle. This functionality enables executing tests directly from the shell. We use the shebang functionality and execute permissions. The end result looks like this:

$ ./test_cache.py
========================================== test session starts ==========================================
platform darwin -- Python 3.11.7, pytest-7.4.4, pluggy-1.4.0
....
collected 4 items

test_cache.py ....                                                                                [100%]

=========================================== 4 passed in 0.46s ===========================================

This is the blunt logic (there probably is a more elegant way to accomplish this ;-)) that I used to make the updates on macOS:

find . -name '*.py' | xargs grep -r __main__ -l | xargs sed -i.tmp '1 i\'$'\n''#!/usr/bin/env python3 -m pytest'$'\n'''
find . -name '*.py' | xargs grep -r __main__ -l | xargs sed -i.tmp '2 i\'$'\n'''$'\n'''
find . -name '*.py' | xargs grep -r __main__ -l | xargs chmod +x
find . -name '*.tmp' | xargs rm

Discussion points:

  • Should all tests have the if __name__ == "__main__": part to make running them easier? Currently they don't, potential follow-up work here.
  • If yes, should pre-commit hook check for it?

Related issue number

N/A

Checks

@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 48.16%. Comparing base (8ec1c3e) to head (08560ae).

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1784       +/-   ##
===========================================
+ Coverage   37.05%   48.16%   +11.10%     
===========================================
  Files          62       62               
  Lines        6499     6499               
  Branches     1438     1561      +123     
===========================================
+ Hits         2408     3130      +722     
+ Misses       3898     3120      -778     
- Partials      193      249       +56     
Flag Coverage Δ
unittests 48.08% <ø> (+11.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ekzhu
Copy link
Collaborator

ekzhu commented Feb 28, 2024

What is a concrete benefit of making these test files executable other than we don't need to type python ?

Copy link
Member

@jackgerrits jackgerrits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nifty, I like it!

@gunnarku
Copy link
Contributor Author

What is a concrete benefit of making these test files executable other than we don't need to type python ?

@ekzhu : The main benefit derives from a simple empirical observation: if running test cases is easy, then contributors tend to run tests more often, add new test cases, and perceive that the validation system helps them to be more effective.

A secondary purpose is to enable those contributors who use Unix-based operating systems (or even WSL on Windows) to use the workflow they are used to.

@gunnarku
Copy link
Contributor Author

This is nifty, I like it!

@jackgerrits : Thank you! Shebang for the win! ;-)

Tests that contain `if __name__ == "__main__":` now have a shebang line
and execute permission.
@sonichi sonichi added this pull request to the merge queue Feb 29, 2024
Merged via the queue into microsoft:main with commit b8ceb86 Feb 29, 2024
46 of 57 checks passed
whiskyboy pushed a commit to whiskyboy/autogen that referenced this pull request Apr 17, 2024
Tests that contain `if __name__ == "__main__":` now have a shebang line
and execute permission.
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.

5 participants