summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/git_fetch.py
blob: 244680b5d0599d6de7cfefeacb4a5237476d8b0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
##############################################################################
# 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://github.com/llnl/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 os
import unittest
import shutil
import tempfile

from llnl.util.filesystem import *

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 *
from spack.test.mock_repo import MockGitRepo


class GitFetchTest(MockPackagesTest):
    """Tests fetching from a dummy git repository."""

    def setUp(self):
        """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.repo = MockGitRepo()

        spec = Spec('git-test')
        spec.concretize()
        self.pkg = spack.db.get(spec, new=True)


    def tearDown(self):
        """Destroy the stage space used by this test."""
        super(GitFetchTest, self).tearDown()

        if self.repo.stage is not None:
            self.repo.stage.destroy()

        self.pkg.do_clean()


    def assert_rev(self, rev):
        """Check that the current git revision is equal to the supplied rev."""
        self.assertEqual(self.repo.rev_hash('HEAD'), self.repo.rev_hash(rev))


    def try_fetch(self, rev, test_file, args):
        """Tries to:
           1. Fetch the repo using a fetch strategy constructed with
              supplied args.
           2. Check if the test_file is in the checked out repository.
           3. Assert that the repository is at the revision supplied.
           4. Add and remove some files, then reset the repo, and
              ensure it's all there again.
        """
        self.pkg.versions[ver('git')] = args

        self.pkg.do_stage()
        self.assert_rev(rev)

        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.isfile(file_path))

        os.unlink(file_path)
        self.assertFalse(os.path.isfile(file_path))

        untracked_file = 'foobarbaz'
        touch(untracked_file)
        self.assertTrue(os.path.isfile(untracked_file))
        self.pkg.do_restage()
        self.assertFalse(os.path.isfile(untracked_file))

        self.assertTrue(os.path.isdir(self.pkg.stage.source_path))
        self.assertTrue(os.path.isfile(file_path))

        self.assert_rev(rev)


    def test_fetch_master(self):
        """Test a default git checkout with no commit or tag specified."""
        self.try_fetch('master', self.repo.r0_file, {
            'git' : self.repo.path
        })


    def test_fetch_branch(self):
        """Test fetching a branch."""
        self.try_fetch(self.repo.branch, self.repo.branch_file, {
            'git'    : self.repo.path,
            'branch' : self.repo.branch
        })


    def test_fetch_tag(self):
        """Test fetching a tag."""
        self.try_fetch(self.repo.tag, self.repo.tag_file, {
            'git' : self.repo.path,
            'tag' : self.repo.tag
        })


    def test_fetch_commit(self):
        """Test fetching a particular commit."""
        self.try_fetch(self.repo.r1, self.repo.r1_file, {
            'git'    : self.repo.path,
            'commit' : self.repo.r1
        })