Skip to content

Commit

Permalink
Adding jig report command
Browse files Browse the repository at this point in the history
  • Loading branch information
robmadole committed Jan 19, 2014
1 parent 12f9a92 commit 9844694
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
bin
cover
docs/build
dist

.coverage
.jig
Expand Down
4 changes: 2 additions & 2 deletions salt/roots/salt/docs/jigops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ Python 2.6:

::

vagrant@raring64:~$ /envs/python2.6/bin/python script/test
vagrant@raring64:/vagrant$ /envs/python2.6/bin/python script/test

Python 2.7:

::

vagrant@raring64:~$ /envs/python2.7/bin/python script/test
vagrant@raring64:/vagrant$ /envs/python2.7/bin/python script/test

Coverage report
---------------
Expand Down
13 changes: 13 additions & 0 deletions src/jig/commands/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ def _hint(hint):
$ jig init [directory]
""")

GIT_REV_LIST_FORMAT_ERROR = _hint(
u"""
The revision range is not in a valid format.
Use "REV_A..REV_B" to specify the revisions that Jig should operate against.
""")

GIT_REV_LIST_MISSING = _hint(
u"""
The revision specified is formatted correctly but one of both of the revisions
could not be found.
""")

ALREADY_INITIALIZED = _hint(
u"""
You are initializing a Git repository for use with Jig but it
Expand Down
36 changes: 36 additions & 0 deletions src/jig/commands/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import argparse

from jig.commands.base import BaseCommand
from jig.runner import Runner

_parser = argparse.ArgumentParser(
description='Run plugins on a revision range',
usage='jig check [-h] [-p PLUGIN] [--revrange REVISION_RANGE] [PATH]')

_parser.add_argument(
'--plugin', '-p',
help='Only run this specific named plugin')
_parser.add_argument(
'--rev-range', dest='rev_range', default='HEAD^1..HEAD',
help='Git revision range to run the plugins against')
_parser.add_argument(
'path', nargs='?', default='.',
help='Path to the Git repository')


class Command(BaseCommand):
parser = _parser

def process(self, argv):
path = argv.path
rev_range = argv.rev_range

# Make the runner use our view
runner = Runner(view=self.view)

runner.main(
path,
plugin=argv.plugin,
rev_range=rev_range,
interactive=False
)
4 changes: 2 additions & 2 deletions src/jig/commands/runnow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jig.runner import Runner

_parser = argparse.ArgumentParser(
description='Run all plugins and show the results',
description='Run plugins on staged changes and show the results',
usage='jig runnow [-h] [-p PLUGIN] [PATH]')

_parser.add_argument(
Expand All @@ -24,4 +24,4 @@ def process(self, argv):
# Make the runner use our view
runner = Runner(view=self.view)

runner.fromhook(path, plugin=argv.plugin, interactive=False)
runner.main(path, plugin=argv.plugin, interactive=False)
117 changes: 117 additions & 0 deletions src/jig/commands/tests/test_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# coding=utf-8
from unittest import SkipTest

from jig.exc import ForcedExit
from jig.tests.testcase import (
CommandTestCase, PluginTestCase, result_with_hint)
from jig.plugins import set_jigconfig
from jig.output import ATTENTION
from jig.commands import report


class TestReportCommand(CommandTestCase, PluginTestCase):

"""
Test the report command.
"""
command = report.Command

def setUp(self):
super(TestReportCommand, self).setUp()

self._add_plugin(self.jigconfig, 'plugin01')
set_jigconfig(self.gitrepodir, config=self.jigconfig)

# Create a few commits
self.commit(self.gitrepodir, 'a.txt', 'a')
self.commit(self.gitrepodir, 'b.txt', 'b')
self.commit(self.gitrepodir, 'c.txt', 'c')

def test_cannot_resolve_revision_range(self):
"""
Given a range that is invalid it notifies the user.
"""
with self.assertRaises(ForcedExit) as ec:
self.run_command('--rev-range FOO..BAR {}'.format(self.gitrepodir))

self.assertEqual(1, ec.exception.message)

self.assertTrue(self.error)

def test_reports_one_commit(self):
"""
With a range indicating one commit it reports on that one.
"""
with self.assertRaises(SystemExit) as ec:
self.run_command('--rev-range HEAD^1..HEAD {}'.format(self.gitrepodir))

self.assertEqual(0, ec.exception.message)

self.assertResults(u"""
▾ plugin01
⚠ line 1: c.txt
c is +
{0} Jig ran 1 plugin
Info 0 Warn 1 Stop 0
""".format(ATTENTION), self.output)

def test_defaults_latest_commit(self):
"""
Without a revision range it uses HEAD^1..HEAD.
"""
with self.assertRaises(SystemExit) as ec:
self.run_command('{}'.format(self.gitrepodir))

self.assertResults(u"""
▾ plugin01
⚠ line 1: c.txt
c is +
{0} Jig ran 1 plugin
Info 0 Warn 1 Stop 0
""".format(ATTENTION), self.output)

def test_reports_two_commits(self):
"""
With a range indicating two commits it reports on both.
"""
with self.assertRaises(SystemExit) as ec:
self.run_command('--rev-range HEAD~2..HEAD {}'.format(self.gitrepodir))

self.assertEqual(0, ec.exception.message)

self.assertResults(u"""
▾ plugin01
⚠ line 1: c.txt
c is +
⚠ line 1: b.txt
b is +
{0} Jig ran 1 plugin
Info 0 Warn 2 Stop 0
""".format(ATTENTION), self.output)

def test_reports_only_one_plugin(self):
"""
If a plugin is given it will only report on that plugin.
"""
with self.assertRaises(SystemExit) as ec:
self.run_command('--plugin plugin01 --rev-range HEAD^1..HEAD {}'.format(self.gitrepodir))

self.assertEqual(0, ec.exception.message)

self.assertResults(u"""
▾ plugin01
⚠ line 1: c.txt
c is +
{0} Jig ran 1 plugin
Info 0 Warn 1 Stop 0
""".format(ATTENTION), self.output)
2 changes: 1 addition & 1 deletion src/jig/commands/tests/test_runnow.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_changes(self):

def test_specific_plugin_installed(self):
"""
A specific plugin can be ran but it's not installed.
A specific plugin can be ran if it's installed.
"""
self._add_plugin(self.jigconfig, 'plugin01')
set_jigconfig(self.gitrepodir, config=self.jigconfig)
Expand Down
18 changes: 18 additions & 0 deletions src/jig/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ class GitCloneError(JigException):
pass


class GitRevListFormatError(JigException):

"""
The given Git revision list is not in a valid format.
"""
hint = 'GIT_REV_LIST_FORMAT_ERROR'


class GitRevListMissing(JigException):

"""
The revision lists could not be found for a given Git repository.
"""
hint = 'GIT_REV_LIST_MISSING'


class AlreadyInitialized(JigException):

"""
Expand Down
33 changes: 31 additions & 2 deletions src/jig/gitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
from textwrap import dedent

import git
from git.exc import GitCommandError
from git.exc import GitCommandError, BadObject
import gitdb
import async
import smmap

from jig.exc import NotGitRepo, PreCommitExists, GitCloneError
from jig.exc import (
NotGitRepo, PreCommitExists, GitCloneError, GitRevListFormatError,
GitRevListMissing)
from jig.conf import JIG_DIR_NAME

# Dependencies to make jig run
Expand Down Expand Up @@ -159,3 +161,30 @@ def remote_has_updates(repository):
is_tracking_newer = True

return is_different and is_tracking_newer


def parse_rev_range(repository, rev_range):
"""
Convert revision range to two :class:`git.objects.commit.Commit` objects.
:param string repository: path to the Git repository
:param string rev_range: Double dot-separated revision range, like "FOO..BAR"
:returns: the two commits representing the range
:rtype: tuple
"""
rev_pair = rev_range.split('..')

if len(rev_pair) != 2 or not all(rev_pair):
raise GitRevListFormatError(rev_range)

rev_a, rev_b = rev_pair

try:
repo = git.Repo(repository)

commit_a = repo.commit(rev_a)
commit_b = repo.commit(rev_b)

return commit_a, commit_b
except (BadObject, GitCommandError):
raise GitRevListMissing(rev_range)
Loading

0 comments on commit 9844694

Please sign in to comment.