File: test_transitive_includes.py

package info (click to toggle)
kas 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: python: 5,745; sh: 1,095; makefile: 210
file content (111 lines) | stat: -rw-r--r-- 4,715 bytes parent folder | download | duplicates (2)
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
# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2024
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
from pathlib import Path
import shutil
import subprocess
import yaml
import pytest
from kas import kas
from kas.includehandler import IncludeException


def test_transitive_includes(monkeykas, tmpdir, capsys):
    """
    Check if a reference to a repo that itself is specified in a
    remote repo is correctly resolved.
    """
    repos = ['main', 'foo', 'bar']
    dirs = {}
    for r in repos:
        dirs[r] = Path(tmpdir / r)
        os.mkdir(dirs[r])

    # create main repo config
    with open('tests/test_transitive_includes/main.yml', 'rb') as fds:
        config_top = yaml.safe_load(fds)
    config_top['repos']['foo']['url'] = str(dirs['foo'])
    with open(dirs['main'] / 'main.yml', 'w') as config_file:
        yaml.dump(config_top, config_file)
    shutil.copy('tests/test_transitive_includes/oe-init-build-env',
                dirs['main'])

    # create foo repo and config
    with open('tests/test_transitive_includes/foo.yml', 'rb') as fds:
        config_foo = yaml.safe_load(fds)
    config_foo['repos']['bar']['url'] = str(dirs['bar'])
    with open(dirs['foo'] / 'foo.yml', 'w') as config_file:
        yaml.dump(config_foo, config_file)
    subprocess.check_call(['git', 'init'], cwd=dirs['foo'])
    subprocess.check_call(['git', 'config', 'user.email', 'test'],
                          cwd=dirs['foo'])
    subprocess.check_call(['git', 'config', 'user.name', 'test'],
                          cwd=dirs['foo'])
    subprocess.check_call(['git', 'checkout', '-b', 'kas'], cwd=dirs['foo'])
    subprocess.check_call(['git', 'add', 'foo.yml'], cwd=dirs['foo'])
    subprocess.check_call(['git', 'commit', '-m', 'init'], cwd=dirs['foo'])

    # create bar repo
    shutil.copy('tests/test_transitive_includes/bar.yml', dirs['bar'])
    subprocess.check_call(['git', 'init'], cwd=dirs['bar'])
    subprocess.check_call(['git', 'config', 'user.email', 'test'],
                          cwd=dirs['bar'])
    subprocess.check_call(['git', 'config', 'user.name', 'test'],
                          cwd=dirs['bar'])
    subprocess.check_call(['git', 'checkout', '-b', 'kas'], cwd=dirs['bar'])
    subprocess.check_call(['git', 'add', 'bar.yml'], cwd=dirs['bar'])
    subprocess.check_call(['git', 'commit', '-m', 'init'], cwd=dirs['bar'])

    monkeykas.chdir(dirs['main'])
    kas.kas(['dump', 'main.yml'])
    config_final = yaml.safe_load(capsys.readouterr().out)
    assert config_final['env']['KAS_REPO_BAR'] == "1"


@pytest.mark.dirsfromenv
def test_include_from_submodule(monkeykas, tmpdir):
    tmpdir.mkdir('main')
    tmpdir.mkdir('sub')
    for s, t in [('sub_repo.yml', 'sub'),
                 ('super_repo.yml', 'main'),
                 ('oe-init-build-env', 'main')]:
        shutil.copy(f'tests/test_transitive_includes/{s}', tmpdir / t)

    monkeykas.chdir(tmpdir / 'sub')
    subprocess.check_call(['git', 'init', '-b', 'main'])
    subprocess.check_call(['git', 'add', 'sub_repo.yml'])
    subprocess.check_call(['git', 'commit', '-m', 'init'])

    monkeykas.chdir(tmpdir / 'main')
    subprocess.check_call(['git', 'init', '-b', 'main'])

    # clone a repo as non-submodule
    subprocess.check_call(['git', 'clone', '../sub', 'sub'])
    with pytest.raises(IncludeException):
        kas.kas(['checkout', 'super_repo.yml:sub/sub_repo.yml'])

    # now add the repo as submodule
    shutil.rmtree('sub')
    subprocess.check_call(['git', '-c', 'protocol.file.allow=always',
                          'submodule', 'add', '../sub', 'sub'])
    kas.kas(['-l', 'debug', 'checkout', 'super_repo.yml:sub/sub_repo.yml'])