#15 container build: check git repo before submitting build
Merged 8 years ago by pbabinca. Opened 8 years ago by ttomecek.
ttomecek/rpkg cont-build-checks-repo  into  master

file modified
+17 -11
@@ -1670,6 +1670,17 @@ 

          return self.lookasidecache.remote_file_exists(pkg_name, filename,

                                                        checksum)

  

+     def check_repo(self, is_dirty=True, all_pushed=True):

+         if is_dirty:

+             if self.repo.is_dirty():

+                 raise rpkgError('%s has uncommitted changes.  Use git status '

+                                 'to see details' % self.path)

+         if all_pushed:

+             branch = self.repo.active_branch

+             full_branch = '%s/%s' % (self.branch_remote, self.branch_merge)

+             if self.repo.git.rev_list('%s...%s' % (full_branch, branch)):

+                 raise rpkgError('There are unpushed changes in your repo')

+ 

      def build(self, skip_tag=False, scratch=False, background=False,

                url=None, chain=None, arches=None, sets=False, nvr_check=True):

          """Initiate a build of the module.  Available options are:
@@ -1702,17 +1713,9 @@ 

          # construct the url

          if not url:

              # We don't have a url, so build from the latest commit

-             # Check to see if the tree is dirty

-             if self.repo.is_dirty():

-                 raise rpkgError('%s has uncommitted changes.  Use git status '

-                                 'to see details' % self.path)

-             # Need to check here to see if the local commit you want to build

-             # is pushed or not

-             branch = self.repo.active_branch

-             full_branch = '%s/%s' % (self.branch_remote, self.branch_merge)

-             if self.repo.git.rev_list('%s...%s' % (full_branch, branch)):

-                 raise rpkgError('There are unpushed changes in your repo')

- 

+             # Check to see if the tree is dirty and if all local commits

+             # are pushed

+             self.check_repo()

              url = self.anongiturl % {'module': self.module_name} + \

                  '?#%s' % self.commithash

          # Check to see if the target is valid
@@ -2408,6 +2411,9 @@ 

              if "buildContainer" not in self.kojisession.system.listMethods():

                  raise RuntimeError("Kojihub instance does not support buildContainer")

  

+             # check if repo is dirty and all commits are pushed

+             self.check_repo()

+ 

              build_target = self.kojisession.getBuildTarget(docker_target)

              if not build_target:

                  msg = "Unknown build target: %s" % docker_target

@@ -0,0 +1,70 @@ 

+ import os

+ import shutil

+ import subprocess

+ import tempfile

+ 

+ from pyrpkg.errors import rpkgError

+ 

+ from . import CommandTestCase

+ 

+ 

+ class CheckRepoCase(CommandTestCase):

+ 

+     def setUp(self):

+         super(CheckRepoCase, self).setUp()

+         self.dist = "master"

+         self.make_new_git(self.module)

+         moduledir = os.path.join(self.gitroot, self.module)

+ 

+         self.altpath = tempfile.mkdtemp(prefix='rpkg-tests.')

+         self.clonedir = os.path.join(self.altpath, self.module)

+         subprocess.check_call(['git', 'clone', 'file://%s' % moduledir],

+                               cwd=self.altpath, stdout=subprocess.PIPE,

+                               stderr=subprocess.PIPE)

+         import pyrpkg

+         self.cmd = pyrpkg.Commands(

+             self.clonedir, self.lookaside, self.lookasidehash,

+             self.lookaside_cgi, self.gitbaseurl,

+             self.anongiturl, self.branchre, self.kojiconfig,

+             self.build_client, self.user, self.dist,

+             self.target, self.quiet

+         )

+ 

+     def tearDown(self):

+         super(CheckRepoCase, self).tearDown()

+         # Drop the clone

+         shutil.rmtree(self.altpath)

+ 

+     def test_repo_is_dirty(self):

+         with open(os.path.join(self.clonedir, 'sources'), 'w') as fd:

+             fd.write("a")

+ 

+         with self.assertRaises(rpkgError) as cm:

+             self.cmd.check_repo(is_dirty=True, all_pushed=False)

+         exception = cm.exception

+         print exception.message

+         self.assertTrue("has uncommitted changes" in exception.message)

+ 

+     def test_repo_has_unpushed_changes(self):

+         with open(os.path.join(self.clonedir, 'sources'), 'w') as fd:

+             fd.write("a")

+         subprocess.check_call(

+             ['git', 'add', 'sources'],

+             cwd=self.clonedir

+         )

+         subprocess.check_call(

+             ['git', 'commit', '-m', 'commit sources'],

+             cwd=self.clonedir,

+         )

+ 

+         with self.assertRaises(rpkgError) as cm:

+             self.cmd.check_repo(is_dirty=False, all_pushed=True)

+         exception = cm.exception

+         print exception.message

+         self.assertTrue("There are unpushed changes in your repo" in exception.message)

+ 

+     def test_repo_is_clean(self):

+         self.cmd.check_repo(is_dirty=True, all_pushed=False)

+ 

+     def test_repo_has_everything_pushed(self):

+         self.cmd.check_repo(is_dirty=False, all_pushed=True)

no initial comment

If your test depends on this value set it in your test.

Please split the commit into two:
first one to move code to check_repo() with the call of the method from the original point. This way it will be obvious nothing has changed with the refactoring.
second one with the rest.