Commit 2957dd9b authored by Ayush Tiwari's avatar Ayush Tiwari

Clean up, add function for setting git config and follow convention

parent 0216d4b9
...@@ -35,8 +35,11 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -35,8 +35,11 @@ class GitCloneNonInformativeTests(unittest.TestCase):
self.dir = os.path.realpath(tempfile.mkdtemp()) self.dir = os.path.realpath(tempfile.mkdtemp())
self.parts_directory_path = os.path.join(self.dir, 'test_parts') self.parts_directory_path = os.path.join(self.dir, 'test_parts')
def setUpSubmoduleRepo(self): def setUpSubmoduleRepository(self):
# Create a main repo as well as a submodule repo """
Create a main repo as well as a submodule repository and add files and
commit then. Then add submodule to the main repository
"""
# Add files in both main as well as submodule repo # Add files in both main as well as submodule repo
subprocess.check_call('mkdir main_repo', cwd=self.dir, shell=True) subprocess.check_call('mkdir main_repo', cwd=self.dir, shell=True)
subprocess.check_call('mkdir submodule_repo', cwd=self.dir, shell=True) subprocess.check_call('mkdir submodule_repo', cwd=self.dir, shell=True)
...@@ -46,24 +49,26 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -46,24 +49,26 @@ class GitCloneNonInformativeTests(unittest.TestCase):
# Add files in submodule repo and initialize git in it # Add files in submodule repo and initialize git in it
subprocess.check_call('cd submodule_repo', cwd=self.dir, shell=True) subprocess.check_call('cd submodule_repo', cwd=self.dir, shell=True)
subprocess.check_call('git init', cwd=self.submodule_dir, shell=True) subprocess.check_call('git init', cwd=self.submodule_dir, shell=True)
self.setUpGitConfig(self.submodule_dir)
subprocess.check_call('touch file1.py', cwd=self.submodule_dir, shell=True) subprocess.check_call('touch file1.py', cwd=self.submodule_dir, shell=True)
self.git_add(self.submodule_dir) self.gitAdd(self.submodule_dir)
self.git_commit(self.submodule_dir, msg='Add file1 in submodule repo') self.gitCommit(self.submodule_dir, msg='Add file1 in submodule repo')
# Add files and folder in main repo and initialize git in it # Add files and folder in main repo and initialize git in it
subprocess.check_call('cd main_repo', cwd=self.dir, shell=True) subprocess.check_call('cd main_repo', cwd=self.dir, shell=True)
subprocess.check_call('git init', cwd=self.project_dir, shell=True) subprocess.check_call('git init', cwd=self.project_dir, shell=True)
self.setUpGitConfig(self.project_dir)
subprocess.check_call('mkdir dir1', cwd=self.project_dir, shell=True) subprocess.check_call('mkdir dir1', cwd=self.project_dir, shell=True)
subprocess.check_call('touch file1.py', cwd=self.project_dir, shell=True) subprocess.check_call('touch file1.py', cwd=self.project_dir, shell=True)
self.git_add(self.project_dir) self.gitAdd(self.project_dir)
self.git_commit(self.project_dir, msg='Add file and folder in main repo') self.gitCommit(self.project_dir, msg='Add file and folder in main repo')
# Add submodule to main repo # Add submodule to main repo
subprocess.check_call('cd dir1', cwd=self.project_dir, shell=True) subprocess.check_call('cd dir1', cwd=self.project_dir, shell=True)
submodule_dir_main_repo = os.path.join(self.project_dir, 'dir1') submodule_dir_main_repo = os.path.join(self.project_dir, 'dir1')
subprocess.check_call('git submodule add %s' % self.submodule_dir, subprocess.check_call('git submodule add %s' % self.submodule_dir,
cwd=submodule_dir_main_repo, shell=True) cwd=submodule_dir_main_repo, shell=True)
self.git_commit(self.project_dir, msg='Add submodule repo') self.gitCommit(self.project_dir, msg='Add submodule repo')
def tearDown(self): def tearDown(self):
shutil.rmtree(self.dir) shutil.rmtree(self.dir)
...@@ -79,7 +84,18 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -79,7 +84,18 @@ class GitCloneNonInformativeTests(unittest.TestCase):
os.chmod(path, mode) os.chmod(path, mode)
return path return path
def git_add(self, proj_dir, update=False): def setUpGitConfig(self, proj_dir):
"""
Setup user and email for given git repository
"""
subprocess.check_call("git config user.email 'test@example.com'",
cwd=proj_dir,
shell=True)
subprocess.check_call("git config user.name 'Test'",
cwd=proj_dir,
shell=True)
def gitAdd(self, proj_dir, update=False):
"""runs a 'git add .' in the provided git repo """runs a 'git add .' in the provided git repo
:param proj_dir: path to a git repo :param proj_dir: path to a git repo
:update: if True, will run 'git add -u' :update: if True, will run 'git add -u'
...@@ -91,16 +107,18 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -91,16 +107,18 @@ class GitCloneNonInformativeTests(unittest.TestCase):
# XXX: This is ugly and old. Change it before review # XXX: This is ugly and old. Change it before review
logging.error("'git add .' failed with '{}'".format(e.returncode)) logging.error("'git add .' failed with '{}'".format(e.returncode))
def git_commit(self, proj_dir, msg): def gitCommit(self, proj_dir, msg):
"""runs a 'git commit -m msg' in the provided git repo """runs a 'git commit -m msg' in the provided git repo
:param proj_dir: path to a git repo :param proj_dir: path to a git repo
""" """
try: try:
subprocess.check_call('git commit -m "{}"'.format(msg), cwd=proj_dir, shell=True) subprocess.check_call('git commit -m "{}"'.format(msg),
cwd=proj_dir,
shell=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
logging.error("'git commit' failed with '{}'".format(e.returncode)) logging.error("'git commit' failed with '{}'".format(e.returncode))
def get_repo_head_commit(self, proj_dir, branch='master'): def getRepositoryHeadCommit(self, proj_dir, branch='master'):
""" """
Returns the sha of HEAD of a git repo Returns the sha of HEAD of a git repo
""" """
...@@ -173,7 +191,7 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -173,7 +191,7 @@ class GitCloneNonInformativeTests(unittest.TestCase):
Main repo (M3) ---references---> Submodule(S2) Main repo (M3) ---references---> Submodule(S2)
Install should be M3+S2 Install should be M3+S2
""" """
self.setUpSubmoduleRepo() self.setUpSubmoduleRepository()
# STEP I: Clone repositories in status M1 and S1 (M1---->S1) # STEP I: Clone repositories in status M1 and S1 (M1---->S1)
recipe = self.makeGitCloneRecipe( recipe = self.makeGitCloneRecipe(
{ {
...@@ -189,16 +207,16 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -189,16 +207,16 @@ class GitCloneNonInformativeTests(unittest.TestCase):
self.assertTrue(os.listdir(submodule_repo_path)) self.assertTrue(os.listdir(submodule_repo_path))
# Get the head commit of the submodule repo # Get the head commit of the submodule repo
head_commit_submodule_after_clone = self.get_repo_head_commit( head_commit_submodule_after_clone = self.getRepositoryHeadCommit(
submodule_repo_path) submodule_repo_path)
# Update submodule repository and main repo separately # Update submodule repository and main repo separately
subprocess.check_call('touch file2.py', cwd=self.project_dir, shell=True) subprocess.check_call('touch file2.py', cwd=self.project_dir, shell=True)
self.git_add(self.project_dir) self.gitAdd(self.project_dir)
self.git_commit(self.project_dir, msg='Add file2 in main repo') self.gitCommit(self.project_dir, msg='Add file2 in main repo')
subprocess.check_call('touch file2.py', cwd=self.submodule_dir, shell=True) subprocess.check_call('touch file2.py', cwd=self.submodule_dir, shell=True)
self.git_add(self.submodule_dir) self.gitAdd(self.submodule_dir)
self.git_commit(self.submodule_dir, msg='Add file2 in submodule repo') self.gitCommit(self.submodule_dir, msg='Add file2 in submodule repo')
# STEP II: Clone repositories in status M2 and S2 (M2---->S1) # STEP II: Clone repositories in status M2 and S2 (M2---->S1)
# Update the recipe with new revision which still point to the submodule # Update the recipe with new revision which still point to the submodule
...@@ -206,7 +224,7 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -206,7 +224,7 @@ class GitCloneNonInformativeTests(unittest.TestCase):
recipe = self.makeGitCloneRecipe( recipe = self.makeGitCloneRecipe(
{ {
'repository': self.project_dir, 'repository': self.project_dir,
'revision': str(self.get_repo_head_commit(self.project_dir)) 'revision': str(self.getRepositoryHeadCommit(self.project_dir))
} }
) )
recipe.update() recipe.update()
...@@ -215,22 +233,22 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -215,22 +233,22 @@ class GitCloneNonInformativeTests(unittest.TestCase):
self.assertTrue(os.listdir(submodule_repo_path)) self.assertTrue(os.listdir(submodule_repo_path))
# Get the head commit of the submodule repo # Get the head commit of the submodule repo
head_commit_submodule_after_first_update = self.get_repo_head_commit( head_commit_submodule_after_first_update = self.getRepositoryHeadCommit(
submodule_repo_path) submodule_repo_path)
# Update main repo with changes of submodule # Update main repo with changes of submodule
submodule_dir_main_repo = os.path.join(self.project_dir, 'dir1', 'submodule_repo') submodule_dir_main_repo = os.path.join(self.project_dir, 'dir1', 'submodule_repo')
subprocess.check_call('git checkout master', cwd=submodule_dir_main_repo, shell=True) subprocess.check_call('git checkout master', cwd=submodule_dir_main_repo, shell=True)
subprocess.check_call('git pull', cwd=submodule_dir_main_repo, shell=True) subprocess.check_call('git pull', cwd=submodule_dir_main_repo, shell=True)
self.git_add(self.project_dir) self.gitAdd(self.project_dir)
self.git_commit(self.project_dir, msg='Update submodule version') self.gitCommit(self.project_dir, msg='Update submodule version')
# STEP II: Clone repositories in status M3 and S2 (M3---->S2) # STEP II: Clone repositories in status M3 and S2 (M3---->S2)
# Update the recipe with new revision which points to submodule new revision # Update the recipe with new revision which points to submodule new revision
recipe = self.makeGitCloneRecipe( recipe = self.makeGitCloneRecipe(
{ {
'repository': self.project_dir, 'repository': self.project_dir,
'revision': str(self.get_repo_head_commit(self.project_dir)) 'revision': str(self.getRepositoryHeadCommit(self.project_dir))
} }
) )
recipe.update() recipe.update()
...@@ -238,11 +256,11 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -238,11 +256,11 @@ class GitCloneNonInformativeTests(unittest.TestCase):
# Check if the submodule is not empty # Check if the submodule is not empty
self.assertTrue(os.listdir(submodule_repo_path)) self.assertTrue(os.listdir(submodule_repo_path))
# Get the head commit of the submodule repo # Get the head commit of the submodule repo
head_commit_submodule_after_second_update = self.get_repo_head_commit( head_commit_submodule_after_second_update = self.getRepositoryHeadCommit(
submodule_repo_path) submodule_repo_path)
# Check the HEAD of the submodule # Check the HEAD of the submodule
submodule_head_commit = self.get_repo_head_commit(self.submodule_dir) submodule_head_commit = self.getRepositoryHeadCommit(self.submodule_dir)
self.assertEqual(head_commit_submodule_after_clone, self.assertEqual(head_commit_submodule_after_clone,
head_commit_submodule_after_first_update) head_commit_submodule_after_first_update)
...@@ -288,7 +306,7 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -288,7 +306,7 @@ class GitCloneNonInformativeTests(unittest.TestCase):
self.test_ignore_ssl_certificate(ignore_ssl_certificate=False) self.test_ignore_ssl_certificate(ignore_ssl_certificate=False)
def test_clone_submodules_by_default(self, ignore_cloning_submodules=False): def test_clone_submodules_by_default(self, ignore_cloning_submodules=False):
self.setUpSubmoduleRepo() self.setUpSubmoduleRepository()
recipe = self.makeGitCloneRecipe( recipe = self.makeGitCloneRecipe(
{'repository': self.project_dir, {'repository': self.project_dir,
'ignore-cloning-submodules': str(ignore_cloning_submodules).lower()} 'ignore-cloning-submodules': str(ignore_cloning_submodules).lower()}
...@@ -313,7 +331,7 @@ class GitCloneNonInformativeTests(unittest.TestCase): ...@@ -313,7 +331,7 @@ class GitCloneNonInformativeTests(unittest.TestCase):
should udpate the main repo as well as submodule repo if the reference of should udpate the main repo as well as submodule repo if the reference of
submodule in main repo has been udpated. submodule in main repo has been udpated.
""" """
self.setUpSubmoduleRepo() self.setUpSubmoduleRepository()
recipe = self.makeGitCloneRecipe( recipe = self.makeGitCloneRecipe(
{'repository': self.project_dir} {'repository': self.project_dir}
) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment