From 8e3c2d8a26ea1515479e369c2fe9594ac8ed8ed1 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Wed, 15 Oct 2014 07:40:01 -0700 Subject: Refactor fetch tests to use common mock repo module. --- lib/spack/spack/test/git_fetch.py | 90 ++++++----------------- lib/spack/spack/test/hg_fetch.py | 56 +++----------- lib/spack/spack/test/mock_repo.py | 151 ++++++++++++++++++++++++++++++++++++++ lib/spack/spack/test/svn_fetch.py | 49 +++---------- 4 files changed, 198 insertions(+), 148 deletions(-) create mode 100644 lib/spack/spack/test/mock_repo.py diff --git a/lib/spack/spack/test/git_fetch.py b/lib/spack/spack/test/git_fetch.py index c7c11621f9..f6d9bfcf05 100644 --- a/lib/spack/spack/test/git_fetch.py +++ b/lib/spack/spack/test/git_fetch.py @@ -34,24 +34,9 @@ import spack from spack.version import ver from spack.stage import Stage from spack.util.executable import which -from spack.test.mock_packages_test import * - -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() +from spack.test.mock_packages_test import * +from spack.test.mock_repo import MockGitRepo class GitFetchTest(MockPackagesTest): @@ -61,36 +46,8 @@ class GitFetchTest(MockPackagesTest): """Create a git repository with master and two other branches, and one tag, so that we can experiment on it.""" super(GitFetchTest, self).setUp() - self.stage = Stage('fetch-test') - - self.repo_path = join_path(self.stage.path, test_repo_path) - 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) + self.repo = MockGitRepo() spec = Spec('git-test') spec.concretize() @@ -101,15 +58,15 @@ class GitFetchTest(MockPackagesTest): """Destroy the stage space used by this test.""" super(GitFetchTest, self).tearDown() - if self.stage is not None: - self.stage.destroy() + if self.repo.stage is not None: + self.repo.stage.destroy() self.pkg.do_clean_dist() def assert_rev(self, 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): @@ -133,10 +90,11 @@ class GitFetchTest(MockPackagesTest): os.unlink(file_path) self.assertFalse(os.path.isfile(file_path)) - touch(untracked) - self.assertTrue(os.path.isfile(untracked)) + untracked_file = 'foobarbaz' + touch(untracked_file) + self.assertTrue(os.path.isfile(untracked_file)) 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.isfile(file_path)) @@ -146,30 +104,30 @@ class GitFetchTest(MockPackagesTest): def test_fetch_master(self): """Test a default git checkout with no commit or tag specified.""" - self.try_fetch('master', test_file_name, { - 'git' : self.repo_path + self.try_fetch('master', self.repo.r0_file, { + 'git' : self.repo.path }) - def test_fetch_branch(self): + def ztest_fetch_branch(self): """Test fetching a branch.""" - self.try_fetch(test_branch, test_branch_file_name, { - 'git' : self.repo_path, - 'branch' : test_branch + self.try_fetch(self.repo.branch, self.repo.branch_file, { + 'git' : self.repo.path, + 'branch' : self.repo.branch }) - def test_fetch_tag(self): + def ztest_fetch_tag(self): """Test fetching a tag.""" - self.try_fetch(test_tag, test_tag_file_name, { - 'git' : self.repo_path, - 'tag' : test_tag + self.try_fetch(self.repo.tag, self.repo.tag_file, { + 'git' : self.repo.path, + 'tag' : self.repo.tag }) - def test_fetch_commit(self): + def ztest_fetch_commit(self): """Test fetching a particular commit.""" - self.try_fetch(self.commit, test_tag_file_name, { - 'git' : self.repo_path, - 'commit' : self.commit + self.try_fetch(self.repo.r1, self.repo.r1_file, { + 'git' : self.repo.path, + 'commit' : self.repo.r1 }) diff --git a/lib/spack/spack/test/hg_fetch.py b/lib/spack/spack/test/hg_fetch.py index 4b9a2f8bc9..97c5b665e7 100644 --- a/lib/spack/spack/test/hg_fetch.py +++ b/lib/spack/spack/test/hg_fetch.py @@ -35,46 +35,18 @@ from spack.version import ver from spack.stage import Stage from spack.util.executable import which 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): """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): """Create a hg repository with master and two other branches, and one tag, so that we can experiment on it.""" super(HgFetchTest, self).setUp() - self.stage = Stage('fetch-test') - - self.repo_path = join_path(self.stage.path, test_repo_path) - 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() + self.repo = MockHgRepo() spec = Spec('hg-test') spec.concretize() @@ -85,17 +57,12 @@ class HgFetchTest(MockPackagesTest): """Destroy the stage space used by this test.""" super(HgFetchTest, self).tearDown() - if self.stage is not None: - self.stage.destroy() + if self.repo.stage is not None: + self.repo.stage.destroy() 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): """Tries to: 1. Fetch the repo using a fetch strategy constructed with @@ -108,7 +75,7 @@ class HgFetchTest(MockPackagesTest): self.pkg.versions[ver('hg')] = args 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) self.assertTrue(os.path.isdir(self.pkg.stage.source_path)) @@ -117,6 +84,7 @@ class HgFetchTest(MockPackagesTest): os.unlink(file_path) self.assertFalse(os.path.isfile(file_path)) + untracked = 'foobarbaz' touch(untracked) self.assertTrue(os.path.isfile(untracked)) self.pkg.do_clean_work() @@ -125,19 +93,19 @@ class HgFetchTest(MockPackagesTest): self.assertTrue(os.path.isdir(self.pkg.stage.source_path)) self.assertTrue(os.path.isfile(file_path)) - self.assert_rev(rev) + self.assertEqual(self.repo.get_rev(), rev) def test_fetch_default(self): """Test a default hg checkout with no commit or tag specified.""" - self.try_fetch(self.rev1, test_rev1_file_name, { - 'hg' : self.repo_path + self.try_fetch(self.repo.r1, self.repo.r1_file, { + 'hg' : self.repo.path }) def test_fetch_rev0(self): """Test fetching a branch.""" - self.try_fetch(self.rev0, test_file_name, { - 'hg' : self.repo_path, - 'revision' : self.rev0 + self.try_fetch(self.repo.r0, self.repo.r0_file, { + 'hg' : self.repo.path, + 'revision' : self.repo.r0 }) diff --git a/lib/spack/spack/test/mock_repo.py b/lib/spack/spack/test/mock_repo.py new file mode 100644 index 0000000000..ae28f7224e --- /dev/null +++ b/lib/spack/spack/test/mock_repo.py @@ -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() diff --git a/lib/spack/spack/test/svn_fetch.py b/lib/spack/spack/test/svn_fetch.py index e253f21921..a48a86dcc3 100644 --- a/lib/spack/spack/test/svn_fetch.py +++ b/lib/spack/spack/test/svn_fetch.py @@ -36,17 +36,7 @@ from spack.version import ver from spack.stage import Stage from spack.util.executable import which from spack.test.mock_packages_test import * - -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) +from spack.test.mock_repo import svn, MockSvnRepo class SvnFetchTest(MockPackagesTest): @@ -55,26 +45,8 @@ class SvnFetchTest(MockPackagesTest): def setUp(self): """Create an svn repository with two revisions.""" super(SvnFetchTest, self).setUp() - self.stage = Stage('fetch-test') - self.stage.chdir() - - repo_path = join_path(self.stage.path, test_repo_path) - 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') + self.repo = MockSvnRepo() spec = Spec('svn-test') spec.concretize() @@ -85,8 +57,8 @@ class SvnFetchTest(MockPackagesTest): """Destroy the stage space used by this test.""" super(SvnFetchTest, self).tearDown() - if self.stage is not None: - self.stage.destroy() + if self.repo.stage is not None: + self.repo.stage.destroy() self.pkg.do_clean_dist() @@ -99,7 +71,7 @@ class SvnFetchTest(MockPackagesTest): for line in output.split('\n'): match = re.match(r'Revision: (\d+)', line) if match: - return int(match.group(1)) + return match.group(1) self.assertEqual(get_rev(), rev) @@ -124,6 +96,7 @@ class SvnFetchTest(MockPackagesTest): os.unlink(file_path) self.assertFalse(os.path.isfile(file_path)) + untracked = 'foobarbaz' touch(untracked) self.assertTrue(os.path.isfile(untracked)) self.pkg.do_clean_work() @@ -137,14 +110,14 @@ class SvnFetchTest(MockPackagesTest): def test_fetch_default(self): """Test a default checkout and make sure it's on rev 1""" - self.try_fetch(2, test_rev_file_name, { - 'svn' : self.repo_url + self.try_fetch(self.repo.r1, self.repo.r1_file, { + 'svn' : self.repo.url }) def test_fetch_r1(self): """Test fetching an older revision (0).""" - self.try_fetch(1, test_file_name, { - 'svn' : self.repo_url, - 'revision' : 1 + self.try_fetch(self.repo.r0, self.repo.r0_file, { + 'svn' : self.repo.url, + 'revision' : self.repo.r0 }) -- cgit v1.2.3-70-g09d2