2020-01-15 08:55:21 +08:00
|
|
|
# Copyright (c) 2019, 2020 Nordic Semiconductor ASA
|
2019-05-25 11:53:08 +08:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2019-02-18 07:48:40 +08:00
|
|
|
import os
|
2019-05-16 02:18:54 +08:00
|
|
|
import platform
|
2019-02-18 07:48:40 +08:00
|
|
|
import shlex
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
|
2019-03-08 14:43:50 +08:00
|
|
|
from west import configuration as config
|
2019-02-18 07:48:40 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
GIT = shutil.which('git')
|
2019-10-02 06:47:36 +08:00
|
|
|
|
|
|
|
# If you change this, keep the docstring in repos_tmpdir() updated also.
|
2019-05-15 03:46:08 +08:00
|
|
|
MANIFEST_TEMPLATE = '''\
|
|
|
|
manifest:
|
|
|
|
defaults:
|
|
|
|
remote: test-local
|
|
|
|
|
|
|
|
remotes:
|
|
|
|
- name: test-local
|
|
|
|
url-base: THE_URL_BASE
|
|
|
|
|
|
|
|
projects:
|
|
|
|
- name: Kconfiglib
|
|
|
|
revision: zephyr
|
|
|
|
path: subdir/Kconfiglib
|
2020-01-14 03:34:40 +08:00
|
|
|
- name: tagged_repo
|
|
|
|
revision: v1.0
|
2019-05-15 03:46:08 +08:00
|
|
|
- name: net-tools
|
2019-10-02 06:47:36 +08:00
|
|
|
clone-depth: 1
|
2019-05-15 03:46:08 +08:00
|
|
|
west-commands: scripts/west-commands.yml
|
|
|
|
self:
|
|
|
|
path: zephyr
|
|
|
|
'''
|
2019-02-18 07:48:40 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Test fixtures
|
|
|
|
#
|
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def _session_repos():
|
|
|
|
'''Just a helper, do not use directly.'''
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
# It saves time to create repositories once at session scope, then
|
|
|
|
# clone the results as needed in per-test fixtures.
|
|
|
|
session_repos = os.path.join(os.environ['TOXTEMPDIR'], 'session_repos')
|
|
|
|
print('initializing session repositories in', session_repos)
|
|
|
|
shutil.rmtree(session_repos, ignore_errors=True)
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
# Create the repositories.
|
|
|
|
rp = {} # individual repository paths
|
2020-01-14 03:34:40 +08:00
|
|
|
for repo in 'Kconfiglib', 'tagged_repo', 'net-tools', 'zephyr':
|
2019-05-15 03:46:08 +08:00
|
|
|
path = os.path.join(session_repos, repo)
|
2019-02-18 07:48:40 +08:00
|
|
|
rp[repo] = path
|
|
|
|
create_repo(path)
|
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
# Initialize the "zephyr" repository.
|
|
|
|
# The caller needs to add west.yml with the right url-base.
|
|
|
|
add_commit(rp['zephyr'], 'base zephyr commit',
|
|
|
|
files={'CODEOWNERS': '',
|
|
|
|
'include/header.h': '#pragma once\n',
|
|
|
|
'subsys/bluetooth/code.c': 'void foo(void) {}\n'})
|
2019-02-18 07:48:40 +08:00
|
|
|
|
|
|
|
# Initialize the Kconfiglib repository.
|
2020-01-15 11:22:06 +08:00
|
|
|
create_branch(rp['Kconfiglib'], 'zephyr', checkout=True)
|
2019-02-18 07:48:40 +08:00
|
|
|
add_commit(rp['Kconfiglib'], 'test kconfiglib commit',
|
|
|
|
files={'kconfiglib.py': 'print("hello world kconfiglib")\n'})
|
|
|
|
|
2020-01-14 03:34:40 +08:00
|
|
|
# Initialize the tagged_repo repository.
|
|
|
|
add_commit(rp['tagged_repo'], 'tagged_repo commit',
|
|
|
|
files={'test.txt': 'hello world'})
|
|
|
|
add_tag(rp['tagged_repo'], 'v1.0')
|
|
|
|
|
2019-02-18 07:48:40 +08:00
|
|
|
# Initialize the net-tools repository.
|
|
|
|
add_commit(rp['net-tools'], 'test net-tools commit',
|
|
|
|
files={'qemu-script.sh': 'echo hello world net-tools\n',
|
|
|
|
'scripts/west-commands.yml': textwrap.dedent('''\
|
|
|
|
west-commands:
|
|
|
|
- file: scripts/test.py
|
|
|
|
commands:
|
2019-09-06 23:00:40 +08:00
|
|
|
- name: test-extension
|
|
|
|
class: TestExtension
|
|
|
|
help: test-extension-help
|
2019-02-18 07:48:40 +08:00
|
|
|
'''),
|
|
|
|
'scripts/test.py': textwrap.dedent('''\
|
|
|
|
from west.commands import WestCommand
|
2019-09-06 23:00:40 +08:00
|
|
|
class TestExtension(WestCommand):
|
2019-02-18 07:48:40 +08:00
|
|
|
def __init__(self):
|
2019-09-06 23:00:40 +08:00
|
|
|
super().__init__('test-extension',
|
|
|
|
'test-extension-help',
|
|
|
|
'')
|
2019-02-18 07:48:40 +08:00
|
|
|
def do_add_parser(self, parser_adder):
|
|
|
|
parser = parser_adder.add_parser(self.name)
|
|
|
|
return parser
|
|
|
|
def do_run(self, args, ignored):
|
|
|
|
print('Testing test command 1')
|
|
|
|
'''),
|
|
|
|
})
|
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
# Return the top-level temporary directory. Don't clean it up on
|
|
|
|
# teardown, so the contents can be inspected post-portem.
|
|
|
|
print('finished initializing session repositories')
|
|
|
|
return session_repos
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def repos_tmpdir(tmpdir, _session_repos):
|
2019-05-25 07:10:03 +08:00
|
|
|
'''Fixture for tmpdir with "remote" repositories.
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2020-01-15 08:23:38 +08:00
|
|
|
These can then be used to bootstrap a workspace and run
|
2019-05-15 03:46:08 +08:00
|
|
|
project-related commands on it with predictable results.
|
|
|
|
|
|
|
|
Switches directory to, and returns, the top level tmpdir -- NOT
|
|
|
|
the subdirectory containing the repositories themselves.
|
|
|
|
|
|
|
|
Initializes placeholder upstream repositories in tmpdir with the
|
|
|
|
following contents:
|
|
|
|
|
|
|
|
repos/
|
|
|
|
├── Kconfiglib (branch: zephyr)
|
|
|
|
│ └── kconfiglib.py
|
2020-01-14 03:34:40 +08:00
|
|
|
├── tagged_repo (branch: master, tag: v1.0)
|
|
|
|
│ └── test.txt
|
2019-05-15 03:46:08 +08:00
|
|
|
├── net-tools (branch: master)
|
|
|
|
│ └── qemu-script.sh
|
|
|
|
└── zephyr (branch: master)
|
|
|
|
├── CODEOWNERS
|
|
|
|
├── west.yml
|
|
|
|
├── include
|
|
|
|
│ └── header.h
|
|
|
|
└── subsys
|
|
|
|
└── bluetooth
|
|
|
|
└── code.c
|
|
|
|
|
|
|
|
The contents of west.yml are:
|
|
|
|
|
|
|
|
manifest:
|
|
|
|
defaults:
|
|
|
|
remote: test-local
|
|
|
|
remotes:
|
|
|
|
- name: test-local
|
2019-10-02 06:47:36 +08:00
|
|
|
url-base: file://<tmpdir>/repos
|
2019-05-15 03:46:08 +08:00
|
|
|
projects:
|
|
|
|
- name: Kconfiglib
|
|
|
|
revision: zephyr
|
|
|
|
path: subdir/Kconfiglib
|
2020-01-14 03:34:40 +08:00
|
|
|
- name: tagged_repo
|
|
|
|
revision: v1.0
|
2019-05-15 03:46:08 +08:00
|
|
|
- name: net-tools
|
2019-10-02 06:47:36 +08:00
|
|
|
clone-depth: 1
|
2019-05-15 03:46:08 +08:00
|
|
|
west-commands: scripts/west-commands.yml
|
|
|
|
self:
|
|
|
|
path: zephyr
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2019-05-15 03:46:08 +08:00
|
|
|
'''
|
2020-01-14 03:34:40 +08:00
|
|
|
kconfiglib, tagged_repo, net_tools, zephyr = [
|
|
|
|
os.path.join(_session_repos, x) for x in
|
|
|
|
['Kconfiglib', 'tagged_repo', 'net-tools', 'zephyr']]
|
2019-05-15 03:46:08 +08:00
|
|
|
repos = tmpdir.mkdir('repos')
|
|
|
|
repos.chdir()
|
2020-01-14 03:34:40 +08:00
|
|
|
for r in [kconfiglib, tagged_repo, net_tools, zephyr]:
|
2019-05-15 03:46:08 +08:00
|
|
|
subprocess.check_call([GIT, 'clone', r])
|
|
|
|
|
|
|
|
manifest = MANIFEST_TEMPLATE.replace('THE_URL_BASE',
|
|
|
|
str(tmpdir.join('repos')))
|
|
|
|
add_commit(str(repos.join('zephyr')), 'add manifest',
|
|
|
|
files={'west.yml': manifest})
|
|
|
|
return tmpdir
|
2019-02-18 07:48:40 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def west_init_tmpdir(repos_tmpdir):
|
|
|
|
'''Fixture for a tmpdir with 'remote' repositories and 'west init' run.
|
|
|
|
|
|
|
|
Uses the remote repositories from the repos_tmpdir fixture to
|
2020-01-15 08:23:38 +08:00
|
|
|
create a west workspace using the system bootstrapper's init
|
2019-05-14 23:24:13 +08:00
|
|
|
command.
|
2019-02-18 07:48:40 +08:00
|
|
|
|
2020-01-15 08:23:38 +08:00
|
|
|
The contents of the west workspace aren't checked at all.
|
2019-02-18 07:48:40 +08:00
|
|
|
This is left up to the test cases.
|
|
|
|
|
|
|
|
The directory that 'west init' created is returned as a
|
|
|
|
py.path.local, with the current working directory set there.'''
|
2020-01-15 08:23:38 +08:00
|
|
|
west_tmpdir = repos_tmpdir / 'workspace'
|
2020-01-15 08:59:33 +08:00
|
|
|
manifest = repos_tmpdir / 'repos' / 'zephyr'
|
|
|
|
cmd(f'init -m "{manifest}" "{west_tmpdir}"')
|
2019-02-18 07:48:40 +08:00
|
|
|
west_tmpdir.chdir()
|
|
|
|
config.read_config()
|
|
|
|
return west_tmpdir
|
|
|
|
|
|
|
|
#
|
|
|
|
# Helper functions
|
|
|
|
#
|
|
|
|
|
|
|
|
def check_output(*args, **kwargs):
|
|
|
|
# Like subprocess.check_output, but returns a string in the
|
|
|
|
# default encoding instead of a byte array.
|
|
|
|
try:
|
|
|
|
out_bytes = subprocess.check_output(*args, **kwargs)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print('*** check_output: nonzero return code', e.returncode,
|
|
|
|
file=sys.stderr)
|
|
|
|
print('cwd =', os.getcwd(), 'args =', args,
|
|
|
|
'kwargs =', kwargs, file=sys.stderr)
|
|
|
|
print('subprocess output:', file=sys.stderr)
|
|
|
|
print(e.output.decode(), file=sys.stderr)
|
|
|
|
raise
|
|
|
|
return out_bytes.decode(sys.getdefaultencoding())
|
|
|
|
|
2019-05-25 11:49:20 +08:00
|
|
|
def cmd(cmd, cwd=None, stderr=None, env=None):
|
2019-02-18 07:48:40 +08:00
|
|
|
# Run a west command in a directory (cwd defaults to os.getcwd()).
|
|
|
|
#
|
2019-05-16 02:18:54 +08:00
|
|
|
# This helper takes the command as a string.
|
2019-02-18 07:48:40 +08:00
|
|
|
#
|
|
|
|
# This helper relies on the test environment to ensure that the
|
|
|
|
# 'west' executable is a bootstrapper installed from the current
|
|
|
|
# west source code.
|
|
|
|
#
|
|
|
|
# stdout from cmd is captured and returned. The command is run in
|
|
|
|
# a python subprocess so that program-level setup and teardown
|
|
|
|
# happen fresh.
|
2019-05-16 02:18:54 +08:00
|
|
|
cmd = 'west ' + cmd
|
|
|
|
if platform.system() != 'Windows':
|
|
|
|
cmd = shlex.split(cmd)
|
|
|
|
print('running:', cmd)
|
2019-05-25 11:49:20 +08:00
|
|
|
if env:
|
|
|
|
print('with non-default environment:')
|
|
|
|
for k in env:
|
|
|
|
if k not in os.environ or env[k] != os.environ[k]:
|
2020-01-15 08:59:33 +08:00
|
|
|
print(f'\t{k}={env[k]}')
|
2019-05-25 11:49:20 +08:00
|
|
|
for k in os.environ:
|
|
|
|
if k not in env:
|
2020-01-15 08:59:33 +08:00
|
|
|
print(f'\t{k}: deleted, was: {os.environ[k]}')
|
2019-02-18 07:48:40 +08:00
|
|
|
try:
|
2019-05-25 11:49:20 +08:00
|
|
|
return check_output(cmd, cwd=cwd, stderr=stderr, env=env)
|
2019-02-18 07:48:40 +08:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print('cmd: west:', shutil.which('west'), file=sys.stderr)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def create_repo(path):
|
|
|
|
# Initializes a Git repository in 'path', and adds an initial commit to it
|
|
|
|
|
|
|
|
subprocess.check_call([GIT, 'init', path])
|
|
|
|
|
|
|
|
config_repo(path)
|
|
|
|
add_commit(path, 'initial')
|
|
|
|
|
|
|
|
|
|
|
|
def config_repo(path):
|
|
|
|
# Set name and email. This avoids a "Please tell me who you are" error when
|
|
|
|
# there's no global default.
|
|
|
|
subprocess.check_call([GIT, 'config', 'user.name', 'West Test'], cwd=path)
|
|
|
|
subprocess.check_call([GIT, 'config', 'user.email',
|
|
|
|
'west-test@example.com'],
|
|
|
|
cwd=path)
|
|
|
|
|
2020-01-15 11:22:06 +08:00
|
|
|
def create_branch(path, branch, checkout=False):
|
|
|
|
subprocess.check_call([GIT, 'branch', branch], cwd=path)
|
|
|
|
if checkout:
|
|
|
|
checkout_branch(path, branch)
|
|
|
|
|
|
|
|
def checkout_branch(path, branch, detach=False):
|
|
|
|
detach = ['--detach'] if detach else []
|
|
|
|
subprocess.check_call([GIT, 'checkout', branch] + detach,
|
|
|
|
cwd=path)
|
2019-02-18 07:48:40 +08:00
|
|
|
|
|
|
|
def add_commit(repo, msg, files=None, reconfigure=True):
|
|
|
|
# Adds a commit with message 'msg' to the repo in 'repo'
|
|
|
|
#
|
|
|
|
# If 'files' is given, it must be a dictionary mapping files to
|
|
|
|
# edit to the contents they should contain in the new
|
|
|
|
# commit. Otherwise, the commit will be empty.
|
|
|
|
#
|
|
|
|
# If 'reconfigure' is True, the user.name and user.email git
|
|
|
|
# configuration variables will be set in 'repo' using config_repo().
|
|
|
|
repo = str(repo)
|
|
|
|
|
|
|
|
if reconfigure:
|
|
|
|
config_repo(repo)
|
|
|
|
|
|
|
|
# Edit any files as specified by the user and add them to the index.
|
|
|
|
if files:
|
|
|
|
for path, contents in files.items():
|
|
|
|
dirname, basename = os.path.dirname(path), os.path.basename(path)
|
|
|
|
fulldir = os.path.join(repo, dirname)
|
|
|
|
if not os.path.isdir(fulldir):
|
|
|
|
# Allow any errors (like trying to create a directory
|
|
|
|
# where a file already exists) to propagate up.
|
|
|
|
os.makedirs(fulldir)
|
|
|
|
with open(os.path.join(fulldir, basename), 'w') as f:
|
|
|
|
f.write(contents)
|
|
|
|
subprocess.check_call([GIT, 'add', path], cwd=repo)
|
|
|
|
|
|
|
|
# The extra '--no-xxx' flags are for convenience when testing
|
|
|
|
# on developer workstations, which may have global git
|
|
|
|
# configuration to sign commits, etc.
|
|
|
|
#
|
|
|
|
# We don't want any of that, as it could require user
|
|
|
|
# intervention or fail in environments where Git isn't
|
|
|
|
# configured.
|
|
|
|
subprocess.check_call(
|
|
|
|
[GIT, 'commit', '-a', '--allow-empty', '-m', msg, '--no-verify',
|
|
|
|
'--no-gpg-sign', '--no-post-rewrite'], cwd=repo)
|
2019-03-20 23:57:27 +08:00
|
|
|
|
2020-01-14 03:34:40 +08:00
|
|
|
def add_tag(repo, tag, commit='HEAD', msg=None):
|
|
|
|
if msg is None:
|
|
|
|
msg = 'tag ' + tag
|
|
|
|
|
|
|
|
# Override tag.gpgSign with --no-sign, in case the test
|
|
|
|
# environment has that set to true.
|
|
|
|
subprocess.check_call([GIT, 'tag', '-m', msg, '--no-sign', tag, commit],
|
|
|
|
cwd=repo)
|
2019-03-20 23:57:27 +08:00
|
|
|
|
|
|
|
def rev_parse(repo, revision):
|
|
|
|
out = subprocess.check_output([GIT, 'rev-parse', revision], cwd=repo)
|
|
|
|
return out.decode(sys.getdefaultencoding())
|