Refactor fetch tests to use common mock repo module.

This commit is contained in:
Todd Gamblin 2014-10-15 07:40:01 -07:00
parent fbd7e96680
commit 8e3c2d8a26
4 changed files with 198 additions and 148 deletions

View file

@ -34,24 +34,9 @@
from spack.version import ver from spack.version import ver
from spack.stage import Stage from spack.stage import Stage
from spack.util.executable import which from spack.util.executable import which
from spack.test.mock_packages_test import * from spack.test.mock_packages_test import *
from spack.test.mock_repo import MockGitRepo
test_repo_path = 'test-repo'
test_file_name = 'test-file.txt'
test_branch = 'test-branch'
test_branch_file_name = 'branch-test-file'
test_tag_branch = 'test-tag-branch'
test_tag = 'test-tag'
test_tag_file_name = 'tag-test-file'
untracked = 'foobarbaz'
git = which('git', required=True)
def rev_hash(rev):
return git('rev-parse', rev, return_output=True).strip()
class GitFetchTest(MockPackagesTest): class GitFetchTest(MockPackagesTest):
@ -61,36 +46,8 @@ def setUp(self):
"""Create a git repository with master and two other branches, """Create a git repository with master and two other branches,
and one tag, so that we can experiment on it.""" and one tag, so that we can experiment on it."""
super(GitFetchTest, self).setUp() super(GitFetchTest, self).setUp()
self.stage = Stage('fetch-test')
self.repo_path = join_path(self.stage.path, test_repo_path) self.repo = MockGitRepo()
mkdirp(self.repo_path)
self.test_file = join_path(self.repo_path, test_file_name)
touch(self.test_file)
with working_dir(self.repo_path):
git('init')
git('add', self.test_file)
git('commit', '-m', 'testing')
git('branch', test_branch)
git('branch', test_tag_branch)
git('checkout', test_branch)
touch(test_branch_file_name)
git('add', test_branch_file_name)
git('commit', '-m' 'branch test')
git('checkout', test_tag_branch)
touch(test_tag_file_name)
git('add', test_tag_file_name)
git('commit', '-m' 'tag test')
git('tag', test_tag)
git('checkout', 'master')
self.commit = rev_hash(test_tag)
spec = Spec('git-test') spec = Spec('git-test')
spec.concretize() spec.concretize()
@ -101,15 +58,15 @@ def tearDown(self):
"""Destroy the stage space used by this test.""" """Destroy the stage space used by this test."""
super(GitFetchTest, self).tearDown() super(GitFetchTest, self).tearDown()
if self.stage is not None: if self.repo.stage is not None:
self.stage.destroy() self.repo.stage.destroy()
self.pkg.do_clean_dist() self.pkg.do_clean_dist()
def assert_rev(self, rev): def assert_rev(self, rev):
"""Check that the current git revision is equal to the supplied rev.""" """Check that the current git revision is equal to the supplied rev."""
self.assertEqual(rev_hash('HEAD'), rev_hash(rev)) self.assertEqual(self.repo.rev_hash('HEAD'), self.repo.rev_hash(rev))
def try_fetch(self, rev, test_file, args): def try_fetch(self, rev, test_file, args):
@ -133,10 +90,11 @@ def try_fetch(self, rev, test_file, args):
os.unlink(file_path) os.unlink(file_path)
self.assertFalse(os.path.isfile(file_path)) self.assertFalse(os.path.isfile(file_path))
touch(untracked) untracked_file = 'foobarbaz'
self.assertTrue(os.path.isfile(untracked)) touch(untracked_file)
self.assertTrue(os.path.isfile(untracked_file))
self.pkg.do_clean_work() self.pkg.do_clean_work()
self.assertFalse(os.path.isfile(untracked)) self.assertFalse(os.path.isfile(untracked_file))
self.assertTrue(os.path.isdir(self.pkg.stage.source_path)) self.assertTrue(os.path.isdir(self.pkg.stage.source_path))
self.assertTrue(os.path.isfile(file_path)) self.assertTrue(os.path.isfile(file_path))
@ -146,30 +104,30 @@ def try_fetch(self, rev, test_file, args):
def test_fetch_master(self): def test_fetch_master(self):
"""Test a default git checkout with no commit or tag specified.""" """Test a default git checkout with no commit or tag specified."""
self.try_fetch('master', test_file_name, { self.try_fetch('master', self.repo.r0_file, {
'git' : self.repo_path 'git' : self.repo.path
}) })
def test_fetch_branch(self): def ztest_fetch_branch(self):
"""Test fetching a branch.""" """Test fetching a branch."""
self.try_fetch(test_branch, test_branch_file_name, { self.try_fetch(self.repo.branch, self.repo.branch_file, {
'git' : self.repo_path, 'git' : self.repo.path,
'branch' : test_branch 'branch' : self.repo.branch
}) })
def test_fetch_tag(self): def ztest_fetch_tag(self):
"""Test fetching a tag.""" """Test fetching a tag."""
self.try_fetch(test_tag, test_tag_file_name, { self.try_fetch(self.repo.tag, self.repo.tag_file, {
'git' : self.repo_path, 'git' : self.repo.path,
'tag' : test_tag 'tag' : self.repo.tag
}) })
def test_fetch_commit(self): def ztest_fetch_commit(self):
"""Test fetching a particular commit.""" """Test fetching a particular commit."""
self.try_fetch(self.commit, test_tag_file_name, { self.try_fetch(self.repo.r1, self.repo.r1_file, {
'git' : self.repo_path, 'git' : self.repo.path,
'commit' : self.commit 'commit' : self.repo.r1
}) })

View file

@ -35,46 +35,18 @@
from spack.stage import Stage from spack.stage import Stage
from spack.util.executable import which from spack.util.executable import which
from spack.test.mock_packages_test import * from spack.test.mock_packages_test import *
from spack.test.mock_repo import MockHgRepo
test_repo_path = 'test-repo'
test_file_name = 'test-file.txt'
test_rev1_file_name = 'test-file2.txt'
untracked = 'foobarbaz'
hg = which('hg', required=True)
class HgFetchTest(MockPackagesTest): class HgFetchTest(MockPackagesTest):
"""Tests fetching from a dummy hg repository.""" """Tests fetching from a dummy hg repository."""
def get_rev(self):
"""Get current mercurial revision."""
return hg('id', '-i', return_output=True).strip()
def setUp(self): def setUp(self):
"""Create a hg repository with master and two other branches, """Create a hg repository with master and two other branches,
and one tag, so that we can experiment on it.""" and one tag, so that we can experiment on it."""
super(HgFetchTest, self).setUp() super(HgFetchTest, self).setUp()
self.stage = Stage('fetch-test')
self.repo_path = join_path(self.stage.path, test_repo_path) self.repo = MockHgRepo()
mkdirp(self.repo_path)
test_file = join_path(self.repo_path, test_file_name)
test_file_rev1 = join_path(self.repo_path, test_rev1_file_name)
with working_dir(self.repo_path):
hg('init')
touch(test_file)
hg('add', test_file)
hg('commit', '-m', 'revision 0', '-u', 'test')
self.rev0 = self.get_rev()
touch(test_file_rev1)
hg('add', test_file_rev1)
hg('commit', '-m' 'revision 1', '-u', 'test')
self.rev1 = self.get_rev()
spec = Spec('hg-test') spec = Spec('hg-test')
spec.concretize() spec.concretize()
@ -85,17 +57,12 @@ def tearDown(self):
"""Destroy the stage space used by this test.""" """Destroy the stage space used by this test."""
super(HgFetchTest, self).tearDown() super(HgFetchTest, self).tearDown()
if self.stage is not None: if self.repo.stage is not None:
self.stage.destroy() self.repo.stage.destroy()
self.pkg.do_clean_dist() self.pkg.do_clean_dist()
def assert_rev(self, rev):
"""Check that the current hg revision is equal to the supplied rev."""
self.assertEqual(self.get_rev(), rev)
def try_fetch(self, rev, test_file, args): def try_fetch(self, rev, test_file, args):
"""Tries to: """Tries to:
1. Fetch the repo using a fetch strategy constructed with 1. Fetch the repo using a fetch strategy constructed with
@ -108,7 +75,7 @@ def try_fetch(self, rev, test_file, args):
self.pkg.versions[ver('hg')] = args self.pkg.versions[ver('hg')] = args
self.pkg.do_stage() self.pkg.do_stage()
self.assert_rev(rev) self.assertEqual(self.repo.get_rev(), rev)
file_path = join_path(self.pkg.stage.source_path, test_file) file_path = join_path(self.pkg.stage.source_path, test_file)
self.assertTrue(os.path.isdir(self.pkg.stage.source_path)) self.assertTrue(os.path.isdir(self.pkg.stage.source_path))
@ -117,6 +84,7 @@ def try_fetch(self, rev, test_file, args):
os.unlink(file_path) os.unlink(file_path)
self.assertFalse(os.path.isfile(file_path)) self.assertFalse(os.path.isfile(file_path))
untracked = 'foobarbaz'
touch(untracked) touch(untracked)
self.assertTrue(os.path.isfile(untracked)) self.assertTrue(os.path.isfile(untracked))
self.pkg.do_clean_work() self.pkg.do_clean_work()
@ -125,19 +93,19 @@ def try_fetch(self, rev, test_file, args):
self.assertTrue(os.path.isdir(self.pkg.stage.source_path)) self.assertTrue(os.path.isdir(self.pkg.stage.source_path))
self.assertTrue(os.path.isfile(file_path)) self.assertTrue(os.path.isfile(file_path))
self.assert_rev(rev) self.assertEqual(self.repo.get_rev(), rev)
def test_fetch_default(self): def test_fetch_default(self):
"""Test a default hg checkout with no commit or tag specified.""" """Test a default hg checkout with no commit or tag specified."""
self.try_fetch(self.rev1, test_rev1_file_name, { self.try_fetch(self.repo.r1, self.repo.r1_file, {
'hg' : self.repo_path 'hg' : self.repo.path
}) })
def test_fetch_rev0(self): def test_fetch_rev0(self):
"""Test fetching a branch.""" """Test fetching a branch."""
self.try_fetch(self.rev0, test_file_name, { self.try_fetch(self.repo.r0, self.repo.r0_file, {
'hg' : self.repo_path, 'hg' : self.repo.path,
'revision' : self.rev0 'revision' : self.repo.r0
}) })

View file

@ -0,0 +1,151 @@
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import shutil
from llnl.util.filesystem import *
import spack
from spack.version import ver
from spack.stage import Stage
from spack.util.executable import which
class MockRepo(object):
def __init__(self, stage_name, repo_name):
"""This creates a stage and a repo directory within the stage."""
# Stage where this repo has been created
self.stage = Stage(stage_name)
# Full path to the repo within the stage.
self.path = join_path(self.stage.path, 'mock-git-repo')
mkdirp(self.path)
# Name for rev0 & rev1 files in the repo to be
self.r0_file = 'r0_file'
self.r1_file = 'r1_file'
#
# VCS Systems used by mock repo code.
#
git = which('git', required=True)
svn = which('svn', required=True)
svnadmin = which('svnadmin', required=True)
hg = which('hg', required=True)
class MockGitRepo(MockRepo):
def __init__(self):
super(MockGitRepo, self).__init__('mock-git-stage', 'mock-git-repo')
with working_dir(self.path):
git('init')
# r0 is just the first commit
touch(self.r0_file)
git('add', self.r0_file)
git('commit', '-m', 'mock-git-repo r0')
self.branch = 'test-branch'
self.branch_file = 'branch_file'
git('branch', self.branch)
self.tag_branch = 'tag-branch'
self.tag_file = 'tag_file'
git('branch', self.tag_branch)
# Check out first branch
git('checkout', self.branch)
touch(self.branch_file)
git('add', self.branch_file)
git('commit', '-m' 'r1 test branch')
# Check out a second branch and tag it
git('checkout', self.tag_branch)
touch(self.tag_file)
git('add', self.tag_file)
git('commit', '-m' 'tag test branch')
self.tag = 'test-tag'
git('tag', self.tag)
git('checkout', 'master')
# R1 test is the same as test for branch
self.r1 = self.rev_hash(self.branch)
self.r1_file = self.branch_file
def rev_hash(self, rev):
return git('rev-parse', rev, return_output=True).strip()
class MockSvnRepo(MockRepo):
def __init__(self):
super(MockSvnRepo, self).__init__('mock-svn-stage', 'mock-svn-repo')
with working_dir(self.stage.path):
svnadmin('create', self.path)
self.url = 'file://' + self.path
tmp_path = join_path(self.stage.path, 'tmp-path')
mkdirp(tmp_path)
with working_dir(tmp_path):
touch(self.r0_file)
svn('import', tmp_path, self.url, '-m', 'Initial import r0')
shutil.rmtree(tmp_path)
svn('checkout', self.url, tmp_path)
with working_dir(tmp_path):
touch(self.r1_file)
svn('add', self.r1_file)
svn('ci', '-m', 'second revision r1')
shutil.rmtree(tmp_path)
self.r0 = '1'
self.r1 = '2'
class MockHgRepo(MockRepo):
def __init__(self):
super(MockHgRepo, self).__init__('mock-hg-stage', 'mock-hg-repo')
with working_dir(self.path):
hg('init')
touch(self.r0_file)
hg('add', self.r0_file)
hg('commit', '-m', 'revision 0', '-u', 'test')
self.r0 = self.get_rev()
touch(self.r1_file)
hg('add', self.r1_file)
hg('commit', '-m' 'revision 1', '-u', 'test')
self.r1 = self.get_rev()
def get_rev(self):
"""Get current mercurial revision."""
return hg('id', '-i', return_output=True).strip()

View file

@ -36,17 +36,7 @@
from spack.stage import Stage from spack.stage import Stage
from spack.util.executable import which from spack.util.executable import which
from spack.test.mock_packages_test import * from spack.test.mock_packages_test import *
from spack.test.mock_repo import svn, MockSvnRepo
test_repo_path = 'test-repo'
test_import_path = 'test-import'
test_file_name = 'test-file.txt'
test_rev_file_name = 'test-rev-file'
untracked = 'foobarbaz'
svn = which('svn', required=True)
svnadmin = which('svnadmin', required=True)
class SvnFetchTest(MockPackagesTest): class SvnFetchTest(MockPackagesTest):
@ -55,26 +45,8 @@ class SvnFetchTest(MockPackagesTest):
def setUp(self): def setUp(self):
"""Create an svn repository with two revisions.""" """Create an svn repository with two revisions."""
super(SvnFetchTest, self).setUp() super(SvnFetchTest, self).setUp()
self.stage = Stage('fetch-test')
self.stage.chdir()
repo_path = join_path(self.stage.path, test_repo_path) self.repo = MockSvnRepo()
svnadmin('create', repo_path)
self.repo_url = 'file://' + repo_path
self.import_path = join_path(self.stage.path, test_import_path)
mkdirp(self.import_path)
with working_dir(self.import_path):
touch(test_file_name)
svn('import', self.import_path, self.repo_url, '-m', 'Initial import')
shutil.rmtree(self.import_path)
svn('checkout', self.repo_url, self.import_path)
with working_dir(self.import_path):
touch(test_rev_file_name)
svn('add', test_rev_file_name)
svn('ci', '-m', 'second revision')
spec = Spec('svn-test') spec = Spec('svn-test')
spec.concretize() spec.concretize()
@ -85,8 +57,8 @@ def tearDown(self):
"""Destroy the stage space used by this test.""" """Destroy the stage space used by this test."""
super(SvnFetchTest, self).tearDown() super(SvnFetchTest, self).tearDown()
if self.stage is not None: if self.repo.stage is not None:
self.stage.destroy() self.repo.stage.destroy()
self.pkg.do_clean_dist() self.pkg.do_clean_dist()
@ -99,7 +71,7 @@ def get_rev():
for line in output.split('\n'): for line in output.split('\n'):
match = re.match(r'Revision: (\d+)', line) match = re.match(r'Revision: (\d+)', line)
if match: if match:
return int(match.group(1)) return match.group(1)
self.assertEqual(get_rev(), rev) self.assertEqual(get_rev(), rev)
@ -124,6 +96,7 @@ def try_fetch(self, rev, test_file, args):
os.unlink(file_path) os.unlink(file_path)
self.assertFalse(os.path.isfile(file_path)) self.assertFalse(os.path.isfile(file_path))
untracked = 'foobarbaz'
touch(untracked) touch(untracked)
self.assertTrue(os.path.isfile(untracked)) self.assertTrue(os.path.isfile(untracked))
self.pkg.do_clean_work() self.pkg.do_clean_work()
@ -137,14 +110,14 @@ def try_fetch(self, rev, test_file, args):
def test_fetch_default(self): def test_fetch_default(self):
"""Test a default checkout and make sure it's on rev 1""" """Test a default checkout and make sure it's on rev 1"""
self.try_fetch(2, test_rev_file_name, { self.try_fetch(self.repo.r1, self.repo.r1_file, {
'svn' : self.repo_url 'svn' : self.repo.url
}) })
def test_fetch_r1(self): def test_fetch_r1(self):
"""Test fetching an older revision (0).""" """Test fetching an older revision (0)."""
self.try_fetch(1, test_file_name, { self.try_fetch(self.repo.r0, self.repo.r0_file, {
'svn' : self.repo_url, 'svn' : self.repo.url,
'revision' : 1 'revision' : self.repo.r0
}) })