tests: project: Clean up tests a bit

Simplify the projects tests to make them easier to understand and work
with:

 - py.path (https://py.readthedocs.io/en/latest/path.html) functions
   like mkdir() return the resulting path, so operations can be chained

 - py.path paths have a write() method. Use it to write the manifest.
   Get rid of the separate file.

 - Switch to the following directory layout, which is simpler and makes
   the manifest easier to locate:

     <tmpdir>/repos/{kconfiglib,net-tools}
     <tmpdir>/west/.west_marker
     <tmpdir>/manifest.yml

   <tmpdir> is used directly as the working directory.

 - Add some more comments

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
This commit is contained in:
Ulf Magnusson 2018-11-12 22:51:12 +01:00 committed by Marti Bolivar
parent a5bc638bb0
commit 75f21d968d
2 changed files with 26 additions and 45 deletions

View File

@ -1,19 +0,0 @@
# Simple manifest template used for testing project commands.
#
# The local-tmpdir URL is rewritten to the temporary directory used
# when tests are run.
manifest:
defaults:
remote: local-tmpdir
revision: master
remotes:
- name: local-tmpdir
url: file://{tmpdir}
projects:
- name: net-tools
- name: Kconfiglib
revision: zephyr
path: sub/Kconfiglib

View File

@ -9,12 +9,6 @@ import pytest
from west.commands import project from west.commands import project
# Path to the template manifest used to construct a real one when
# running each test case.
MANIFEST_TEMPLATE_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'manifest.yml')
)
# Where the projects are cloned to # Where the projects are cloned to
NET_TOOLS_PATH = 'net-tools' NET_TOOLS_PATH = 'net-tools'
KCONFIGLIB_PATH = 'sub/Kconfiglib' KCONFIGLIB_PATH = 'sub/Kconfiglib'
@ -33,12 +27,8 @@ COMMAND_OBJECTS = (
def cmd(cmd): def cmd(cmd):
# We assume the manifest is in ../manifest.yml when tests are run. # We assume the manifest is in manifest.yml when tests are run.
manifest_path = os.path.abspath(os.path.join(os.path.dirname(os.getcwd()), cmd += ' -m manifest.yml'
'manifest.yml'))
# Add quotes so that we properly escape backslashes on Windows, which
# otherwise would be removed by shlex
cmd += ' -m "{}"'.format(manifest_path)
# cmd() takes the command as a string, which is less clunky to work with. # cmd() takes the command as a string, which is less clunky to work with.
# Split it according to shell rules. # Split it according to shell rules.
@ -61,12 +51,11 @@ def cmd(cmd):
@pytest.fixture @pytest.fixture
def clean_west_topdir(tmpdir): def clean_west_topdir(tmpdir):
# Initialize the repositories used for testing. # Initialize some repositories that we use as remotes, in repos/
repos = tmpdir.join('repositories') remote_repos_dir = tmpdir.mkdir('repos')
repos.mkdir()
git = shutil.which('git') git = shutil.which('git')
for path in ('net-tools', 'Kconfiglib'): for path in ('net-tools', 'Kconfiglib'):
fullpath = str(repos.join(path)) fullpath = str(remote_repos_dir.join(path))
subprocess.check_call([git, 'init', fullpath]) subprocess.check_call([git, 'init', fullpath])
# The repository gets user name and email set in case there is # The repository gets user name and email set in case there is
# no global default. # no global default.
@ -93,17 +82,28 @@ def clean_west_topdir(tmpdir):
if path == 'Kconfiglib': if path == 'Kconfiglib':
subprocess.check_call([git, 'branch', 'zephyr'], cwd=fullpath) subprocess.check_call([git, 'branch', 'zephyr'], cwd=fullpath)
# Create the per-tmpdir manifest file. # Create west/.west_topdir, to mark this directory as a West installation,
with open(MANIFEST_TEMPLATE_PATH, 'r') as src: # and a manifest.yml pointing to the repositories we created above
with open(str(tmpdir.join('manifest.yml')), 'w') as dst: tmpdir.mkdir('west').join('.west_topdir').ensure()
dst.write(src.read().format(tmpdir=str(repos))) tmpdir.join('manifest.yml').write('''
manifest:
defaults:
remote: repos
revision: master
# Initialize and change to the installation directory. remotes:
zephyrproject = tmpdir.join('zephyrproject') - name: repos
zephyrproject.mkdir() url: file://{}
zephyrproject.mkdir('west')
zephyrproject.join('west', '.west_topdir').ensure() projects:
zephyrproject.chdir() - name: net-tools
- name: Kconfiglib
revision: zephyr
path: sub/Kconfiglib
'''.format(remote_repos_dir))
# Switch to the top-level West installation directory
tmpdir.chdir()
def test_list(clean_west_topdir): def test_list(clean_west_topdir):