Skip to content

Commit

Permalink
Restructure and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
relekang committed Jul 27, 2015
1 parent c3bad90 commit 4546e1e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 34 deletions.
4 changes: 2 additions & 2 deletions semantic_release/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
from semantic_release.git_helpers import commit_new_version

from semantic_release.helpers import (commit_new_version, get_current_version, get_new_version,
set_new_version)
from semantic_release.helpers import get_current_version, get_new_version, set_new_version
from semantic_release.history import evaluate_version_bump


Expand Down
15 changes: 15 additions & 0 deletions semantic_release/git_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from git import Repo
from invoke import run
from semantic_release.settings import load_config


def get_commit_log():
repo = Repo('.git')
for commit in repo.iter_commits():
yield commit.message


def commit_new_version(version):
add = run('git add {}'.format(load_config().get('version_variable').split(':')[0]), hide=True)
if add.ok:
run('git commit -m "{}"'.format(version), hide=True)
32 changes: 1 addition & 31 deletions semantic_release/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import configparser
import os
import re

import semver
from git import Repo
from invoke import run

DEFAULTS = {
'major_tag': ':boom:',
'minor_tag': ':sparkles:',
'patch_tag': ':bug:',
}
from semantic_release.settings import load_config


def get_current_version():
Expand All @@ -36,25 +28,3 @@ def set_new_version(new_version):
with open(filename, mode='w') as fw:
fw.write(content)
return True


def load_config():
config = configparser.ConfigParser()
with open(os.path.join(os.getcwd(), 'setup.cfg')) as f:
config.read_file(f)
settings = {}
settings.update(DEFAULTS)
settings.update(config._sections['semantic_release'])
return settings


def get_commit_log():
repo = Repo('.git')
for commit in repo.iter_commits():
yield commit.message


def commit_new_version(version):
add = run('git add {}'.format(load_config().get('version_variable').split(':')[0]), hide=True)
if add.ok:
run('git commit -m "{}"'.format(version), hide=True)
3 changes: 2 additions & 1 deletion semantic_release/history.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from semantic_release.helpers import get_commit_log, load_config
from semantic_release.git_helpers import get_commit_log
from semantic_release.helpers import load_config

LEVELS = {
1: 'patch',
Expand Down
20 changes: 20 additions & 0 deletions semantic_release/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import configparser
import os

DEFAULTS = {
'major_tag': ':boom:',
'minor_tag': ':sparkles:',
'patch_tag': ':bug:',
}


def load_config():
config = configparser.ConfigParser()
with open(os.path.join(os.getcwd(), 'setup.cfg')) as f:
config.read_file(f)
settings = {}
settings.update(DEFAULTS)
settings.update(config._sections['semantic_release'])
return settings


20 changes: 20 additions & 0 deletions tests/test_git_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest import TestCase, mock
from invoke.runner import Result
from semantic_release.git_helpers import get_commit_log, commit_new_version


class GetCommitLogTest(TestCase):
def test_first_commit_is_not_initial_commit(self):
self.assertNotEqual(next(get_commit_log()), 'Initial commit')


class CommitNewVersionTests(TestCase):
@mock.patch('semantic_release.git_helpers.run',
return_value=Result(stdout='', stderr='', pty='', exited=0))
def test_add_and_commit(self, mock_run):
commit_new_version('1.0.0')
self.assertEqual(
mock_run.call_args_list,
[mock.call('git add semantic_release/__init__.py', hide=True),
mock.call('git commit -m "1.0.0"', hide=True)]
)
13 changes: 13 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest import TestCase
from semantic_release.settings import load_config


class ConfigTests(TestCase):

def test_load_config(self):
config = load_config()
self.assertIn('version_variable', config)
self.assertIn('major_tag', config)
self.assertIn('minor_tag', config)
self.assertIn('patch_tag', config)

0 comments on commit 4546e1e

Please sign in to comment.