summaryrefslogtreecommitdiff
path: root/tests/sources/bzr.py
blob: 5a5878f88d010b426f0c5c9250d0fab99a314f5a (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import pytest
import subprocess

from buildstream import SourceError, LoadError, Consistency, PluginError

from tests.testutils.site import HAVE_BZR
from .fixture import Setup


DATA_DIR = os.path.join(
    os.path.dirname(os.path.realpath(__file__)),
    'bzr',
)


class BzrSetup(Setup):
    bzr_env = {"BZR_EMAIL": "Testy McTesterson <testy.mctesterson@example.com>"}

    def __init__(self, datafiles, bstfile, tmpdir):
        super().__init__(datafiles, bstfile, tmpdir)
        self.source.preflight()
        print("Host bzr is {}".format(self.source.host_bzr))

    def bzr_init(self, target_dir):
        self.source.call(['bzr', 'init', target_dir],
                         fail="Failed to initialize bzr branch at {}"
                              .format(target_dir),
                         env=self.bzr_env)

    def bzr_create(self, target_dir):
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        self.bzr_init(target_dir)

    def bzr_initrepo(self, target_dir):
        self.source.call(['bzr', 'init-repo', target_dir],
                         fail="Faile to init bzr repo at {}".format(target_dir),
                         env=self.bzr_env)

    def bzr_createrepo(self, target_dir):
        if not os.path.exist(target_dir):
            os.makedirs(target_dir)
        self.bzr_initrepo(target_dir)

    def bzr_commit(self, bzr_dir, filename):
        if not os.path.exists(bzr_dir):
            raise FileNotFoundError("{} doesn't exist!".format(bzr_dir))
        self.source.call(['bzr', 'add', filename], cwd=bzr_dir,
                         fail="Failed to add file {}".format(filename),
                         env=self.bzr_env)
        self.source.call(['bzr', 'commit', '--message="Add file {}"'.format(filename)],
                         cwd=bzr_dir,
                         fail="Failed to commit file {}".format(filename),
                         env=self.bzr_env)


# Test that the source can be parsed meaningfully.
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_create_source(tmpdir, datafiles):
    setup = Setup(datafiles, 'target.bst', tmpdir)
    assert(setup.source.get_kind() == 'bzr')
    assert(setup.source.url == 'http://www.example.com')
    assert(setup.source.get_ref() == 'foo')
    assert(setup.source.tracking == 'trunk')


# Test that without ref, consistency is set appropriately.
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic-no-ref'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_no_ref(tmpdir, datafiles):
    setup = Setup(datafiles, 'target.bst', tmpdir)
    assert(setup.source.get_consistency() == Consistency.INCONSISTENT)


# Test that with ref, consistency is resolved
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_consistency_resolved(tmpdir, datafiles):
    setup = BzrSetup(datafiles, 'target.bst', tmpdir)
    assert(setup.source.get_consistency() == Consistency.RESOLVED)


# Test that with ref and fetching, consistency is cached
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_consistency_cached(tmpdir, datafiles):
    setup = BzrSetup(datafiles, 'target.bst', tmpdir)
    repodir = os.path.join(str(datafiles), 'foo')
    branchdir = os.path.join(repodir, 'bar')
    setup.bzr_initrepo(repodir)
    setup.bzr_init(branchdir)
    setup.bzr_commit(branchdir, 'b')
    setup.bzr_commit(branchdir, 'c/d')
    setup.bzr_commit(branchdir, 'c/e')
    setup.source.fetch()

    found_consistency = setup.source.get_consistency()
    print("Consistency is {}".format(found_consistency))
    assert(found_consistency == Consistency.CACHED)


# Test that without track, consistency is set appropriately.
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic-no-track'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_no_track(tmpdir, datafiles):
    with pytest.raises(LoadError):
        setup = Setup(datafiles, 'target.bst', tmpdir)


# Test that when I fetch, it ends up in the cache.
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_fetch(tmpdir, datafiles):
    # Pretty long setup
    setup = BzrSetup(datafiles, 'target.bst', tmpdir)
    repodir = os.path.join(str(datafiles), 'foo')
    branchdir = os.path.join(repodir, 'bar')
    setup.bzr_initrepo(repodir)
    setup.bzr_init(branchdir)
    setup.bzr_commit(branchdir, 'b')
    setup.bzr_commit(branchdir, 'c/d')
    setup.bzr_commit(branchdir, 'c/e')

    # Fetch the branch
    setup.source.fetch()
    assert(os.path.isdir(setup.source._get_mirror_dir()))
    assert(os.path.isdir(setup.source._get_branch_dir()))
    assert(os.path.isdir(os.path.join(setup.source._get_mirror_dir(), ".bzr")))


# Test that staging fails without ref
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_stage_bad_ref(tmpdir, datafiles):
    # Pretty long setup
    setup = BzrSetup(datafiles, 'target-bad-ref.bst', tmpdir)
    repodir = os.path.join(str(datafiles), 'foo')
    branchdir = os.path.join(repodir, 'bar')
    setup.bzr_initrepo(repodir)
    setup.bzr_init(branchdir)
    setup.bzr_commit(branchdir, 'b')
    setup.bzr_commit(branchdir, 'c/d')
    setup.bzr_commit(branchdir, 'c/e')

    setup.source.fetch()
    stagedir = os.path.join(str(tmpdir), 'stage')

    with pytest.raises(PluginError):
        setup.source.stage(stagedir)


# Test that I can stage the repo successfully
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_stage(tmpdir, datafiles):
    # Pretty long setup
    setup = BzrSetup(datafiles, 'target.bst', tmpdir)
    repodir = os.path.join(str(datafiles), 'foo')
    branchdir = os.path.join(repodir, 'bar')
    setup.bzr_initrepo(repodir)
    setup.bzr_init(branchdir)
    setup.bzr_commit(branchdir, 'b')
    setup.bzr_commit(branchdir, 'c/d')
    setup.bzr_commit(branchdir, 'c/e')

    setup.source.fetch()
    stagedir = os.path.join(str(tmpdir), 'stage')
    setup.source.stage(stagedir)
    expected_files = ['.bzr', 'b', 'c/d', 'c/e']
    for f in expected_files:
        dstpath = os.path.join(stagedir, f)
        print(dstpath)
        assert(os.path.exists(dstpath))


# Test that I can track the branch
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
@pytest.mark.skipif(HAVE_BZR is False, reason="`bzr` is not available")
def test_track(tmpdir, datafiles):
    # Pretty long setup
    setup = BzrSetup(datafiles, 'target-bad-ref.bst', tmpdir)
    repodir = os.path.join(str(datafiles), 'foo')
    branchdir = os.path.join(repodir, 'bar')
    setup.bzr_initrepo(repodir)
    setup.bzr_init(branchdir)
    setup.bzr_commit(branchdir, 'b')
    setup.bzr_commit(branchdir, 'c/d')
    setup.bzr_commit(branchdir, 'c/e')

    generated_ref = setup.source.track()
    print("Found ref {}".format(generated_ref))
    assert(generated_ref == '3')