From 41f201bc25fa4f9377b4fdc4d558dac29080822d Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Sat, 4 Jan 2025 20:36:28 +0100 Subject: [PATCH] tests: config: Fix assertions checks with pytest.raises Dedent assertion checks as they were currently skipped, and make sure the correct error message is validated. Signed-off-by: Pieter De Gendt --- tests/test_config.py | 51 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index fb3a569a..202a4a14 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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 @@ -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
.\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() == ''