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

gracefully handle missing parent key #13170

Merged
merged 1 commit into from
Jun 8, 2024
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
gracefully handle missing parent key
  • Loading branch information
jertel committed Jun 8, 2024
commit f96b82b11203e7ca400f5dfa131556fc075799e2
2 changes: 1 addition & 1 deletion salt/manager/tools/sbin/so-yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def replace(args):

def getKeyValue(content, key):
pieces = key.split(".", 1)
if len(pieces) > 1:
if len(pieces) > 1 and pieces[0] in content:
return getKeyValue(content[pieces[0]], pieces[1])
return content.get(key, None)

Expand Down
11 changes: 11 additions & 0 deletions salt/manager/tools/sbin/so-yaml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ def test_get_missing(self):
self.assertEqual(result, 2)
self.assertEqual("", mock_stdout.getvalue())

def test_get_missing_parent(self):
with patch('sys.stdout', new=StringIO()) as mock_stdout:
filename = "/tmp/so-yaml_test-get.yaml"
file = open(filename, "w")
file.write("{key1: { child1: 123, child2: { deep1: 45 } }, key2: false, key3: [e,f,g]}")
file.close()

result = soyaml.get([filename, "key1.child3.deep3"])
self.assertEqual(result, 2)
self.assertEqual("", mock_stdout.getvalue())

def test_get_usage(self):
with patch('sys.exit', new=MagicMock()) as sysmock:
with patch('sys.stderr', new=StringIO()) as mock_stderr:
Expand Down
Loading