Skip to content

Commit

Permalink
tests: config: Fix assertions checks with pytest.raises
Browse files Browse the repository at this point in the history
Dedent assertion checks as they were currently skipped, and make sure
the correct error message is validated.

Signed-off-by: Pieter De Gendt <[email protected]>
  • Loading branch information
pdgendt committed Jan 7, 2025
1 parent af3fc7d commit 41f201b
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,18 @@ def test_delete_cmd_local():

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)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -l -d pytest.key', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
assert 'argument -d/--delete: not allowed with argument -l/--list' in err_msg
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -l -D pytest.key', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
assert 'argument -D/--delete-all: not allowed with argument -l/--list' in err_msg
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -d -D pytest.key', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
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 +487,34 @@ def test_config_precedence():
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'
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config pytest', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
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)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('-v config pytest.missing', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
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)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
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)
with pytest.raises(subprocess.CalledProcessError) as exc_info:
cmd('config -l pytest.foo', stderr=subprocess.STDOUT)
err_msg = exc_info.value.output.decode("utf-8")
assert '-l cannot be combined with name argument' in err_msg

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

Expand Down

0 comments on commit 41f201b

Please sign in to comment.