Skip to content

Commit

Permalink
It's time for TDD!!! (#1236)
Browse files Browse the repository at this point in the history
* Ensure every pullrequest to master has some sort of test changes.
* Add configurable inclusion for paths that need tests.
* Quote the PR branch at checkout.

No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner authored May 2, 2022
1 parent a0bde93 commit f57a548
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci_tdd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: TDD
on:
pull_request:
branches:
- '*'
workflow_dispatch:

jobs:

validation:
runs-on: ubuntu-latest
steps:

- uses: actions/setup-python@v2
with:
python-version: "3.8"

- name: make the checkout in a sane way.
run: git clone https://github.com/${{ github.event.pull_request.user.login }}/galaxy_ng; cd galaxy_ng ; git checkout "${{ github.head_ref }}"

- name: Ensure changes were made to tests
env:
PY_COLORS: '1'
GITHUB_BASE_REF: ${{ github.base_ref }}
GITHUB_BRANCH: ${{ github.head_ref }}
GITHUB_USER: ${{ github.event.pull_request.user.login }}
GITHUB_CONTEXT: ${{ toJson(github) }}
run: cd galaxy_ng; python dev/common/tdd.py
111 changes: 111 additions & 0 deletions dev/common/tdd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env python3

import json
import logging
import os
import subprocess
import sys


def get_current_branch():
pid = subprocess.run(
'git branch --show-current',
shell=True,
stdout=subprocess.PIPE,
check=True
)
branch_name = pid.stdout.decode('utf-8')
branch_name = branch_name.strip()
return branch_name


def get_changed_files(pr_branch, target_branch="master"):
cmd = f'git diff --name-only {pr_branch}..{target_branch}'
pid = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, check=True)
filenames = pid.stdout.decode('utf-8')
filenames = filenames.split('\n')
filenames = [x.strip() for x in filenames if x.strip()]
return filenames


def verify_test_files_changed(changed_files):

# places where appplication source goes
app_paths = [
'galaxy_ng/'
]

# places where test code goes
test_paths = [
'galaxy_ng/tests'
]

def is_app_path(fn):
for ap in app_paths:
if fn.startswith(ap):
return True
return False

def is_test_path(fn):
for tp in test_paths:
if fn.startswith(tp):
return True
return False

# exit early if no non-test changed in the api code
app_changed = False
for cf in changed_files:
if is_app_path(cf) and not is_test_path(cf):
app_changed = True
if not app_changed:
return

# look for any changes to file in the tests dirs
tests_found = False
for cf in changed_files:
for tp in test_paths:
if cf.startswith(tp):
tests_found = True
break
if tests_found:
break

if not tests_found:
raise Exception('Tests should be added or modified with -every- PR!!!')


def main():
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

# these users should not be checked
skip_users = ['dependabot[bot]', 'patchback[bot]']

# the pr submitter
pr_user = os.environ.get("GITHUB_USER")
print(f'PR-USER: {pr_user}')

if pr_user in skip_users:
print(f'{pr_user} is not required to add tests')
sys.exit(0)

# merge branch ...
pr_branch = get_current_branch()
if not pr_branch:
pr_branch = json.loads(os.environ.get('GITHUB_CONTEXT'))['ref']
print(f'PR-BRANCH: {pr_branch}')

# branch the PR wants to change
target_branch = os.environ.get("GITHUB_BASE_REF", "master")
print(f'TARGET-BRANCH: {target_branch}')
if target_branch != 'master':
print('TDD is only enforced on the master branch')
sys.exit(0)

changed_files = get_changed_files(pr_branch, target_branch=target_branch)
for cf in changed_files:
print(f'modified file: {cf}')
verify_test_files_changed(changed_files)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions galaxy_ng/tests/integration/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions for AH tests."""

import json
import logging
import os
Expand Down

0 comments on commit f57a548

Please sign in to comment.