summaryrefslogtreecommitdiff
path: root/tests/examples/junctions.py
blob: 49e2ebbffe001e7c43fcc16bbd5ee3237cf9ffc9 (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
import os
import pytest

from tests.testutils import cli_integration as cli
from tests.testutils.integration import assert_contains
from tests.testutils.site import IS_LINUX

pytestmark = pytest.mark.integration

DATA_DIR = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), '..', '..', 'doc', 'examples', 'junctions'
)

JUNCTION_IMPORT_PATH = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), '..', '..', 'doc', 'examples', 'autotools'
)


def ammend_juntion_path_paths(tmpdir):
    # The junction element in the examples/junctions project uses a local source type.
    # It's "path:" must specify a relative path from the project's root directory.
    # For the hello-junction element to function during these tests, the copy of the junctions
    # project made in the buildstream/tmp/directory, "path:" must be ammended to be the relative
    # path to the autotools example from the temporary test directory.
    junction_element = os.path.join(tmpdir, "elements", "hello-junction.bst")
    junction_element_bst = ""
    junction_relative_path = os.path.relpath(JUNCTION_IMPORT_PATH, tmpdir)
    with open(junction_element, 'r') as f:
        junction_element_bst = f.read()
    ammended_element_bst = junction_element_bst.replace("../autotools", junction_relative_path)
    with open(junction_element, 'w') as f:
        f.write(ammended_element_bst)


# Check that the autotools project is where the junctions example expects and
# contains the hello.bst element.
@pytest.mark.datafiles(DATA_DIR)
def test_autotools_example_is_present(datafiles):
    autotools_path = JUNCTION_IMPORT_PATH
    assert os.path.exists(autotools_path)
    assert os.path.exists(os.path.join(autotools_path, "elements", "hello.bst"))


# Test that the project builds successfully
@pytest.mark.skipif(not IS_LINUX, reason='Only available on linux')
@pytest.mark.datafiles(DATA_DIR)
def test_build(cli, tmpdir, datafiles):
    project = os.path.join(datafiles.dirname, datafiles.basename)
    ammend_juntion_path_paths(str(tmpdir))

    result = cli.run(project=project, args=['build', 'callHello.bst'])
    result.assert_success()


# Test the callHello script works as expected.
@pytest.mark.skipif(not IS_LINUX, reason='Only available on linux')
@pytest.mark.datafiles(DATA_DIR)
def test_shell_call_hello(cli, tmpdir, datafiles):
    project = os.path.join(datafiles.dirname, datafiles.basename)
    ammend_juntion_path_paths(str(tmpdir))

    result = cli.run(project=project, args=['build', 'callHello.bst'])
    result.assert_success()

    result = cli.run(project=project, args=['shell', 'callHello.bst', '--', '/bin/sh', 'callHello.sh'])
    result.assert_success()
    assert result.output == 'Calling hello:\nHello World!\nThis is amhello 1.0.\n'


# Test opening a cross-junction workspace
@pytest.mark.skipif(not IS_LINUX, reason='Only available on linux')
@pytest.mark.datafiles(DATA_DIR)
def test_open_cross_junction_workspace(cli, tmpdir, datafiles):
    project = os.path.join(datafiles.dirname, datafiles.basename)
    workspace_dir = os.path.join(str(tmpdir), "workspace_hello_junction")
    ammend_juntion_path_paths(str(tmpdir))

    result = cli.run(project=project,
                     args=['workspace', 'open', 'hello-junction.bst:hello.bst', workspace_dir])
    result.assert_success()

    result = cli.run(project=project,
                     args=['workspace', 'close', '--remove-dir', 'hello-junction.bst:hello.bst'])
    result.assert_success()