Skip to content

Commit

Permalink
Unit tests (#133)
Browse files Browse the repository at this point in the history
* Resolve name conflict between a local module and the git package's cmd module

* Simple unit tests for SemVer 2.0 version numbers

* Contributing instructions (including how to run tests locally)
  • Loading branch information
dbeatty10 authored Jul 25, 2022
1 parent d82a8a2 commit 4df0fff
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 8 deletions.
71 changes: 71 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Contributing to this repo

## Setup

### Personal access token (PAT)

Follow [these](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) instructions to configurate a personal access token.

Only permissions needed:
- public_repo

Save the token to a secure location. Click the "Configure SSO" button and "Authorize" if applicable.

### Config

```shell
cp config.example.json config.json

# Add your GitHub username and token
$EDITOR config.json

# Export the JSON credentials into an environment variable
export CONFIG=$(<config.json)
```

## Virtual environment

### Install dependencies
Instructions for POSIX bash/zsh (see [here](https://docs.python.org/3/library/venv.html) for syntax for other shells):
```shell
python3 -m venv env
source env/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt -r dev-requirements.txt
source env/bin/activate
```

## Testing locally

```shell
PYTHONPATH=hubcap python -m pytest
```

## Run in test mode

```shell
export ENV=test
./cron.sh
```

### Optional configuration environment variables
```
# Default value is the `git-tmp` directory within the current working directory
# This directory will be deleted by default at the end of the run
export GIT_TMP=git-tmp
```

### Optional parameters
Preserve commits/build artifacts within the `$GIT_TMP` directory
```
export ENV=test
./cron.sh --no-cleanup
```

## Run in production mode

**WARNING:** Use with caution -- _will_ modify state.
```shell
export ENV=prod
./cron.sh
```
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "github_user",
"token": "pe4s0n@l-@cce$$-t0k3n"
},
"remote": "[email protected]:org/hub.getdbt.com.git",
"remote": "[email protected]:dbt-labs/hub.getdbt.com.git",
"push_branches": true,
"one_branch_per_repo": true
}
2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==22.6.0
pytest
Empty file added hubcap/__init__.py
Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion hubcap/hubcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import package
import release_carrier

from cmd import *
from git_helper import *
from records import *

# ==
Expand Down
8 changes: 4 additions & 4 deletions hubcap/records.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''Interface for objects useful to processing hub entries'''

import cmd
import git_helper
import hashlib
import json
import logging
Expand Down Expand Up @@ -71,7 +71,7 @@ def run(self, main_dir):
for tag in self.new_tags:
# go to repo dir to checkout tag and tag-commit specific package list
os.chdir(self.local_path_to_repo)
cmd.run_cmd(f'git checkout tags/{tag}')
git_helper.run_cmd(f'git checkout tags/{tag}')
packages = package.parse_pkgs(Path(os.getcwd()))

# return to hub and build spec
Expand All @@ -88,7 +88,7 @@ def run(self, main_dir):

msg = f'hubcap: Adding tag {tag} for {self.github_username}/{self.github_repo_name}'
logging.info(msg)
cmd.run_cmd('git add -A')
git_helper.run_cmd('git add -A')
subprocess.run(args=['git', 'commit', '-am', f'{msg}'], capture_output=True)

# if succesful return branchname
Expand All @@ -102,7 +102,7 @@ def cut_version_branch(self):

completed_subprocess = subprocess.run(['git', 'checkout', '-q', '-b', branch_name])
if completed_subprocess.returncode == 128:
run_cmd(f'git checkout -q {branch_name}')
git_helper.run_cmd(f'git checkout -q {branch_name}')

return branch_name

Expand Down
2 changes: 1 addition & 1 deletion hubcap/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests

from pathlib import Path
from cmd import *
from git_helper import *
from records import *

NOW = int(datetime.datetime.now().timestamp())
Expand Down
2 changes: 1 addition & 1 deletion hubcap/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import package

from cmd import *
from git_helper import *


def parse_semver_tag(tag):
Expand Down
Empty file added tests/__init__.py
Empty file.
111 changes: 111 additions & 0 deletions tests/test_version_regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pytest
from hubcap.version import is_valid_semver_tag


@pytest.fixture
def semver_20_valid():
"""Valid SemVer 2.0 versions"""
# Most test cases from https://regex101.com/r/Ly7O1x/3/
return [
# Begin test cases from https://regex101.com/r/Ly7O1x/3/
"0.0.4",
"1.2.3",
"10.20.30",
"1.1.2-prerelease+meta",
"1.1.2+meta",
"1.1.2+meta-valid",
"1.0.0-alpha",
"1.0.0-beta",
"1.0.0-alpha.beta",
"1.0.0-alpha.beta.1",
"1.0.0-alpha.1",
"1.0.0-alpha0.valid",
"1.0.0-alpha.0valid",
"1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
"1.0.0-rc.1+build.1",
"2.0.0-rc.1+build.123",
"1.2.3-beta",
"10.2.3-DEV-SNAPSHOT",
"1.2.3-SNAPSHOT-123",
"1.0.0",
"2.0.0",
"1.1.7",
"2.0.0+build.1848",
"2.0.1-alpha.1227",
"1.0.0-alpha+beta",
"1.2.3----RC-SNAPSHOT.12.9.1--.12+788",
"1.2.3----R-S.12.9.1--.12+meta",
"1.2.3----RC-SNAPSHOT.12.9.1--.12",
"1.0.0+0.build.1-rc.10000aaa-kk-0.1",
"99999999999999999999999.999999999999999999.99999999999999999",
"1.0.0-0A.is.legal",
# Begin custom test cases
"1.0.3-post1",
"0.8.0-a1",
"0.8.0-b2",
"0.8.0-rc3",
]


@pytest.fixture
def semver_20_invalid():
"""Invalid SemVer 2.0 versions"""
# Most test cases from https://regex101.com/r/Ly7O1x/3/
return [
# Begin test cases from https://regex101.com/r/Ly7O1x/3/
"1",
"1.2",
"1.2.3-0123",
"1.2.3-0123.0123",
"1.1.2+.123",
"+invalid",
"-invalid",
"-invalid+invalid",
"-invalid.01",
"alpha",
"alpha.beta",
"alpha.beta.1",
"alpha.1",
"alpha+beta",
"alpha_beta",
"alpha.",
"alpha..",
"beta",
"1.0.0-alpha_beta",
"-alpha.",
"1.0.0-alpha..",
"1.0.0-alpha..1",
"1.0.0-alpha...1",
"1.0.0-alpha....1",
"1.0.0-alpha.....1",
"1.0.0-alpha......1",
"1.0.0-alpha.......1",
"01.1.1",
"1.01.1",
"1.1.01",
"1.2",
"1.2.3.DEV",
"1.2-SNAPSHOT",
"1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788",
"1.2-RC-SNAPSHOT",
"-1.0.3-gamma+b7718",
"+justmeta",
"9.8.7+meta+meta",
"9.8.7-whatever+meta+meta",
"99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12",
# Begin custom test cases
"1.0.3.post1",
"0.8.0a1",
"0.8.0b2",
"0.8.0rc3",
]


def test_regex_valid(semver_20_valid):
for version in semver_20_valid:
assert is_valid_semver_tag(version)


def test_regex_invalid(semver_20_invalid):
for version in semver_20_invalid:
assert not is_valid_semver_tag(version)

0 comments on commit 4df0fff

Please sign in to comment.