Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[bandit]
exclude_dirs = /build,/dist,/.eggs,*.egg-info
# B603: We use subprocess.run with hardcoded git commands, no untrusted input
skips = B603
67 changes: 67 additions & 0 deletions .github/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Integration Tests

This directory contains integration tests for git-cd.

## Test Repository

The tests use the repository at https://github.com/gitcd-io/travis-gitcd as a test repository.

## Running Tests Locally

To run the tests locally:

1. Clone the test repository:
```bash
git clone https://github.com/gitcd-io/travis-gitcd.git ~/build/gitcd-io/travis-gitcd
```

2. Configure git:
```bash
cd ~/build/gitcd-io/travis-gitcd
git config user.email "test@example.com"
git config user.name "Test User"
```

3. Set environment variable:
```bash
export GITHUB_RUN_NUMBER=test-$(date +%s)
```

4. Run individual test scripts:
```bash
bash .github/tests/git-cd-version.sh
bash .github/tests/git-cd-init.sh
# etc...
```

## GitHub Actions Setup

For the GitHub Actions workflow to work, you need to configure a secret named `GH_TEST_REPO_TOKEN` with a Personal Access Token that has write access to the `gitcd-io/travis-gitcd` repository.

To create a PAT:
1. Go to GitHub Settings → Developer settings → Personal access tokens
2. Generate new token (classic)
3. Select scopes: `repo` (Full control of private repositories)
4. Copy the token
5. Add it as a repository secret named `GH_TEST_REPO_TOKEN`

## Test Scripts

- `git-cd-version.sh` - Test version command
- `git-cd-upgrade.sh` - Test upgrade functionality
- `git-cd-init.sh` - Test initialization
- `git-cd-start.sh` - Test starting a feature branch
- `git-add-build.sh` - Helper to add build commits
- `git-cd-refresh.sh` - Test refresh functionality
- `git-cd-review.sh` - Test creating pull requests
- `git-cd-finish.sh` - Test finishing a feature
- `git-cd-release.sh` - Test release process
- `git-cd-test.sh` - Test branch management
- `git-cd-compare.sh` - Test branch comparison
- `git-cd-clean.sh` - Test cleaning up branches
- `git-cd-init-release-date.sh` - Test date-based versioning init
- `git-cd-release-date.sh` - Test date-based release
- `git-cd-init-release-file.sh` - Test file-based versioning init
- `git-cd-release-file.sh` - Test file-based release
- `git-cd-subdirectory.sh` - Test running from subdirectories
- `git-reset.sh` - Helper to reset git state
11 changes: 11 additions & 0 deletions .github/tests/git-add-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set -e

# change workdir to travis-gitcd
cd ~/build/gitcd-io/travis-gitcd

echo github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION >> README.rst
git commit -m "Add current build number: $GITHUB_RUN_NUMBER-$PYTHON_VERSION" .
git push

# change back to original workdir
cd -
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ set -e
cd ~/build/gitcd-io/travis-gitcd

# assert that new feature branch exists remote
git branch -a | grep "origin/feature/travis-test-$TRAVIS_JOB_NUMBER"
git branch -a | grep "origin/feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION"

# delete feature branch on remote
git push origin :feature/travis-test-$TRAVIS_JOB_NUMBER
git push origin :feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION

# assert that the feature branch not present on remote anymore
REMOTE_COUNT=`git branch -a | grep "origin/feature/travis-test-$TRAVIS_JOB_NUMBER" | wc -l`
REMOTE_COUNT=`git branch -a | grep "origin/feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION" | wc -l`
if [ $REMOTE_COUNT != 0 ]; then
echo "Feature Branch still found on remote"
exit 1
Expand All @@ -25,7 +25,7 @@ expect
EOD

# assert the local feature branch is deleted
LOCAL_COUNT=`git branch -a | grep "feature/travis-test-$TRAVIS_JOB_NUMBER" | wc -l`
LOCAL_COUNT=`git branch -a | grep "feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION" | wc -l`
if [ $LOCAL_COUNT != 0 ]; then
echo "Feature Branch still found locally"
exit 1
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
cd ~/build/gitcd-io/travis-gitcd

# write version file because git-cd init checks its existence
echo $TRAVIS_JOB_NUMBER.1 > version.txt
echo $GITHUB_RUN_NUMBER-$PYTHON_VERSION.1 > version.txt

# git-cd init with accepting all the defaults
/usr/bin/expect <<EOD
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ set -e
# change workdir to travis-gitcd
cd ~/build/gitcd-io/travis-gitcd

# Ensure we're on master with a clean state
git fetch origin
git checkout master
git reset --hard origin/master

# git-cd finish with deleting the feature branch
/usr/bin/expect <<EOD
spawn git-cd release
expect "Whats the current tag number you want to deliver?"
send "$TRAVIS_JOB_NUMBER\n"
send "$GITHUB_RUN_NUMBER-$PYTHON_VERSION\n"
expect "What message your new tag should have?"
send "New Travis Release $TRAVIS_JOB_NUMBER\n"
send "New GitHub Release $GITHUB_RUN_NUMBER-$PYTHON_VERSION\n"
expect
EOD

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ spawn git-cd review
expect "Your personal Github token?"
send "$GH_ACCESS_TOKEN\n"
expect "Pull-Request title?"
send "Pull Request Title for travis build $TRAVIS_JOB_NUMBER\n"
send "Pull Request Title for github build $GITHUB_RUN_NUMBER-$PYTHON_VERSION\n"
expect "Pull-Request body?"
send "Pull Request Body for travis build $TRAVIS_JOB_NUMBER\n"
send "Pull Request Body for github build $GITHUB_RUN_NUMBER-$PYTHON_VERSION\n"
expect
EOD

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ set -e
# change workdir to travis-gitcd
cd ~/build/gitcd-io/travis-gitcd

git-cd start travis-$TRAVIS_JOB_NUMBER
git-cd start github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION

# assert that new feature branch exists remote
git branch -a | grep "origin/feature/travis-$TRAVIS_JOB_NUMBER"
git branch -a | grep "origin/feature/github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION"

# change back to original workdir
cd -
29 changes: 29 additions & 0 deletions .github/tests/git-cd-subdirectory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
set -e

# Test that git-cd works from a subdirectory of the project

# Save original working directory
ORIGINAL_DIR=$(pwd)

# change workdir to travis-gitcd
cd ~/build/gitcd-io/travis-gitcd

# Create a subdirectory and test from there
mkdir -p src/subdir
cd src/subdir

# Test version command from subdirectory
git-cd version | grep "You run git-cd in version"

# Test that config is found from subdirectory
# The start command should work and find the .gitcd config
# This would fail before the fix if .gitcd is not in the current directory

echo "✓ git-cd commands work correctly from subdirectory"

# change back to test repo root and cleanup
cd ~/build/gitcd-io/travis-gitcd
rm -rf src

# change back to original workdir
cd "$ORIGINAL_DIR"
6 changes: 3 additions & 3 deletions .travis/tests/git-cd-test.sh → .github/tests/git-cd-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ set -e
cd ~/build/gitcd-io/travis-gitcd

# start a feature
git-cd start travis-test-$TRAVIS_JOB_NUMBER
git-cd start github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION

# assert that new feature branch exists remote
git branch -a | grep "origin/feature/travis-test-$TRAVIS_JOB_NUMBER"
git branch -a | grep "origin/feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION"


# call git-cd clean
git-cd test

# assert the feature branch is checked out again
git status | grep "origin/feature/travis-test-$TRAVIS_JOB_NUMBER"
git status | grep "origin/feature/github-test-$GITHUB_RUN_NUMBER-$PYTHON_VERSION"

# assert there is no diff against the test branch

Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions .github/tests/git-reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set -e

# change workdir to travis-gitcd
cd ~/build/gitcd-io/travis-gitcd

# Save current branch name
CURRENT_BRANCH=$(git branch --show-current)

# Reset any local changes
git reset --hard

# Checkout master to allow cleanup
git checkout master

# Delete local feature branches that might exist
git branch -D "feature/github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION" 2>/dev/null || true
git branch -D "test/github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION" 2>/dev/null || true

# Reset master to origin
git reset --hard origin/master

# change back to original workdir
cd -
155 changes: 155 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: Tests

# Note: These tests require write access to gitcd-io/travis-gitcd repository.
# You need to configure a secret named GH_TEST_REPO_TOKEN with a Personal Access Token
# that has write access to that repository. Alternatively, you can fork the test repo
# and update the repository URL in the test scripts.

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

concurrency:
group: integration-tests
cancel-in-progress: false

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
max-parallel: 1
fail-fast: false

env:
GH_ACCESS_TOKEN: ${{ secrets.GH_TEST_REPO_TOKEN || secrets.GITHUB_TOKEN }}
PYTHON_VERSION: ${{ matrix.python-version }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install system dependencies
run: |
sudo apt-get -qq update
sudo apt-get install -y expect

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install .

- name: Run flake8
run: |
find . -name \*.py -not -path "./build/*" -not -path "./dist/*" -not -path "./*.egg-info/*" -exec flake8 {} +

- name: Run bandit security scan
run: |
bandit -r . -c .bandit
continue-on-error: true

- name: Configure Git
run: |
git config --global user.email "ci@github.com"
git config --global user.name "GitHub Actions"

- name: Clone test repository
env:
GH_TOKEN: ${{ secrets.GH_TEST_REPO_TOKEN || secrets.GITHUB_TOKEN }}
run: |
if [ -z "${{ secrets.GH_TEST_REPO_TOKEN }}" ]; then
echo "Warning: GH_TEST_REPO_TOKEN not set. Integration tests may fail."
echo "See .github/tests/README.md for setup instructions."
fi
git clone https://x-access-token:${GH_TOKEN}@github.com/gitcd-io/travis-gitcd.git ~/build/gitcd-io/travis-gitcd
cd ~/build/gitcd-io/travis-gitcd
git config user.email "ci@github.com"
git config user.name "GitHub Actions"

- name: Cleanup previous test branches
run: |
cd ~/build/gitcd-io/travis-gitcd
# Delete any leftover branches from previous test runs
git fetch origin
# List all remote branches matching our test pattern and delete them
git branch -r | grep 'origin/feature/github-' | sed 's|origin/||' | while read branch; do
branch=$(echo "$branch" | xargs) # trim whitespace
echo "Deleting remote branch: $branch"
git push origin --delete "$branch" || true
done
git branch -r | grep 'origin/test/github-' | sed 's|origin/||' | while read branch; do
branch=$(echo "$branch" | xargs) # trim whitespace
echo "Deleting remote branch: $branch"
git push origin --delete "$branch" || true
done
git checkout master
git reset --hard origin/master

- name: Test git-cd version
run: bash .github/tests/git-cd-version.sh
continue-on-error: false

- name: Test git-cd upgrade
run: bash .github/tests/git-cd-upgrade.sh
continue-on-error: false

- name: Test git-cd init
run: bash .github/tests/git-cd-init.sh

- name: Test git-cd start
run: bash .github/tests/git-cd-start.sh

- name: Add test build
run: bash .github/tests/git-add-build.sh

- name: Test git-cd refresh
run: bash .github/tests/git-cd-refresh.sh

- name: Test git-cd review
run: bash .github/tests/git-cd-review.sh

- name: Test git-cd finish
run: bash .github/tests/git-cd-finish.sh

- name: Test git-cd release
run: bash .github/tests/git-cd-release.sh

- name: Test git-cd test
run: bash .github/tests/git-cd-test.sh

- name: Test git-cd compare
run: bash .github/tests/git-cd-compare.sh

- name: Test git-cd clean
run: bash .github/tests/git-cd-clean.sh

- name: Test git-cd init-release-date
run: bash .github/tests/git-cd-init-release-date.sh

- name: Test git-cd release-date
run: bash .github/tests/git-cd-release-date.sh

- name: Reset git state
run: bash .github/tests/git-reset.sh

- name: Test git-cd init-release-file
run: bash .github/tests/git-cd-init-release-file.sh

- name: Test git-cd release-file
run: bash .github/tests/git-cd-release-file.sh

- name: Reset git state (final)
run: bash .github/tests/git-reset.sh

- name: Test git-cd subdirectory
run: bash .github/tests/git-cd-subdirectory.sh
Loading
Loading