summaryrefslogtreecommitdiff
path: root/tests/context/context.py
blob: 371dd36f71b134d810ebd03b5d54aedfc3f5b9ea (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
import os
import pytest

from buildstream import InvocationContext
from buildstream import ContextError

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

# Simple fixture to create a PluginBase object that
# we use for loading plugins.
@pytest.fixture()
def context_fixture():
    return {
        'context' : InvocationContext('x86_64')
    }

#######################################
#        Test instantiation           #
#######################################
def test_context_create(context_fixture):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))
    assert(context.arch == 'x86_64')

#######################################
#     Test configuration loading      #
#######################################
def test_context_load(context_fixture):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))

    context.load()
    assert(context.sourcedir == '~/buildstream/sources')
    assert(context.builddir == '~/buildstream/build')
    assert(context.deploydir == '~/buildstream/deploy')
    assert(context.artifactdir == '~/buildstream/artifacts')
    assert(context.ccachedir == '~/buildstream/ccache')

# Test that values in a user specified config file
# override the defaults
@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_context_load_user_config(context_fixture, datafiles):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))

    conf_file = os.path.join(datafiles.dirname,
                             datafiles.basename,
                             'userconf.yaml')
    context.load(conf_file)

    assert(context.sourcedir == '~/pony')
    assert(context.builddir == '~/buildstream/build')
    assert(context.deploydir == '~/buildstream/deploy')
    assert(context.artifactdir == '~/buildstream/artifacts')
    assert(context.ccachedir == '~/buildstream/ccache')

#######################################
#          Test failure modes         #
#######################################

@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_context_load_missing_config(context_fixture, datafiles):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))

    conf_file = os.path.join(datafiles.dirname,
                             datafiles.basename,
                             'nonexistant.yaml')

    with pytest.raises(ContextError) as exc:
        context.load(conf_file)

@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_context_load_malformed_config(context_fixture, datafiles):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))

    conf_file = os.path.join(datafiles.dirname,
                             datafiles.basename,
                             'malformed.yaml')

    with pytest.raises(ContextError) as exc:
        context.load(conf_file)

@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_context_load_notdict_config(context_fixture, datafiles):
    context = context_fixture['context']
    assert(isinstance(context, InvocationContext))

    conf_file = os.path.join(datafiles.dirname,
                             datafiles.basename,
                             'notdict.yaml')

    with pytest.raises(ContextError) as exc:
        context.load(conf_file)