Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: config: Fix assertions checks with pytest.raises #777

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, 2020 Nordic Semiconductor ASA

Check notice on line 1 in tests/conftest.py

View workflow job for this annotation

GitHub Actions / Check file tests/conftest.py

Unformatted file

Consider running 'ruff format tests/conftest.py' See https://github.com/zephyrproject-rtos/west/actions/runs/12668379712 for more details
#
# SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -351,6 +351,15 @@
print('cmd: west:', shutil.which('west'), file=sys.stderr)
raise


def cmd_raises(cmd_str_or_list, expected_exception_type, cwd=None, env=None):
# Similar to 'cmd' but an expected exception is caught.
# Returns the output together with stderr data
with pytest.raises(expected_exception_type) as exc_info:
cmd(cmd_str_or_list, stderr=subprocess.STDOUT, cwd=cwd, env=env)
return exc_info.value.output.decode("utf-8")


def create_workspace(workspace_dir, and_git=True):
# Manually create a bare-bones west workspace inside
# workspace_dir. The manifest.path config option is 'mp'. The
Expand Down
49 changes: 18 additions & 31 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, Nordic Semiconductor ASA

Check notice on line 1 in tests/test_config.py

View workflow job for this annotation

GitHub Actions / Check file tests/test_config.py

Unformatted file

Consider running 'ruff format tests/test_config.py' See https://github.com/zephyrproject-rtos/west/actions/runs/12668379712 for more details
#
# SPDX-License-Identifier: Apache-2.0

Expand All @@ -9,7 +9,7 @@
from typing import Any, Optional

import pytest
from conftest import cmd
from conftest import cmd, cmd_raises

from west import configuration as config
from west.util import PathType
Expand Down Expand Up @@ -253,18 +253,12 @@
'-DCONF_FILE=foo.conf -DEXTRA_CFLAGS=\'-Wextra -g0\' -DFOO=BAR'

def test_append_novalue():
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -a pytest.foo', stderr=subprocess.STDOUT)
# Get the output into a variable to simplify pytest error messages
err_msg = exc_info.value.output.decode("utf-8")
err_msg = cmd_raises('config -a pytest.foo', subprocess.CalledProcessError)
assert '-a requires both name and value' in err_msg

def test_append_notfound():
update_testcfg('pytest', 'key', 'val', configfile=LOCAL)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -a pytest.foo bar', stderr=subprocess.STDOUT)
# Get the output into a variable to simplify pytest error messages
err_msg = exc_info.value.output.decode("utf-8")
err_msg = cmd_raises('config -a pytest.foo bar', subprocess.CalledProcessError)
assert 'option pytest.foo not found in the local configuration file' in err_msg


Expand Down Expand Up @@ -446,15 +440,12 @@

def test_delete_cmd_error():
# Verify illegal combinations of flags error out.
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config -l -d pytest.key')
assert '-l cannot be combined with -d or -D' in str(e)
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config -l -D pytest.key')
assert '-l cannot be combined with -d or -D' in str(e)
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config -d -D pytest.key')
assert '-d cannot be combined with -D' in str(e)
err_msg = cmd_raises('config -l -d pytest.key', subprocess.CalledProcessError)
assert 'argument -d/--delete: not allowed with argument -l/--list' in err_msg
err_msg = cmd_raises('config -l -D pytest.key', subprocess.CalledProcessError)
assert 'argument -D/--delete-all: not allowed with argument -l/--list' in err_msg
err_msg = cmd_raises('config -d -D pytest.key', subprocess.CalledProcessError)
assert 'argument -D/--delete-all: not allowed with argument -d/--delete' in err_msg

def test_default_config():
# Writing to a value without a config destination should default
Expand Down Expand Up @@ -484,30 +475,26 @@
assert cfg(f=LOCAL)['pytest']['precedence'] == 'local'

def test_config_missing_key():
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config pytest')
assert str(e) == 'west config: error: missing key, please invoke ' \
'as: west config <section>.<key>\n'
err_msg = cmd_raises('config pytest', subprocess.CalledProcessError)
assert 'invalid configuration option "pytest"; expected "section.key" format' in err_msg


def test_unset_config():
# Getting unset configuration options should raise an error.
# With verbose output, the exact missing option should be printed.
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('-v config pytest.missing')
assert 'pytest.missing is unset' in str(e)
err_msg = cmd_raises('-v config pytest.missing', subprocess.CalledProcessError)
assert 'pytest.missing is unset' in err_msg

def test_no_args():
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config')
assert 'missing argument name' in str(e)
err_msg = cmd_raises('config', subprocess.CalledProcessError)
assert 'missing argument name' in err_msg

def test_list():
def sorted_list(other_args=''):
return list(sorted(cmd('config -l ' + other_args).splitlines()))

with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config -l pytest.foo')
assert '-l cannot be combined with name argument' in str(e)
err_msg = cmd_raises('config -l pytest.foo', subprocess.CalledProcessError)
assert '-l cannot be combined with name argument' in err_msg

assert cmd('config -l').strip() == ''

Expand Down
Loading