From a53aa1d0b8e75e41eb345ec39f1e015b0faa61b6 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 27 Apr 2022 09:02:30 -0700 Subject: [PATCH] fix bug when no slaves are present --- jc/parsers/update_alt_q.py | 8 +++++--- .../generic/update-alternatives-query2.json | 1 + .../generic/update-alternatives-query2.out | 14 ++++++++++++++ tests/test_update_alt_q.py | 13 +++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/generic/update-alternatives-query2.json create mode 100644 tests/fixtures/generic/update-alternatives-query2.out diff --git a/jc/parsers/update_alt_q.py b/jc/parsers/update_alt_q.py index 625e15dd..41ac78a7 100644 --- a/jc/parsers/update_alt_q.py +++ b/jc/parsers/update_alt_q.py @@ -241,10 +241,12 @@ def parse( if not 'alternatives' in raw_output: raw_output['alternatives'] = [] - if slaves: - alt_obj['slaves'] = slaves + if alt_obj: + if slaves: + alt_obj['slaves'] = slaves + slaves = [] + raw_output['alternatives'].append(alt_obj) - slaves = [] alt_obj = {"alternative": line_list[1]} continue diff --git a/tests/fixtures/generic/update-alternatives-query2.json b/tests/fixtures/generic/update-alternatives-query2.json new file mode 100644 index 00000000..395fde29 --- /dev/null +++ b/tests/fixtures/generic/update-alternatives-query2.json @@ -0,0 +1 @@ +{"name":"php-fpm.sock","link":"/run/php/php-fpm.sock","status":"auto","best":"/run/php/php8.1-fpm.sock","value":"/run/php/php8.1-fpm.sock","alternatives":[{"alternative":"/run/php/php7.4-fpm.sock","priority":74},{"alternative":"/run/php/php8.0-fpm.sock","priority":80},{"alternative":"/run/php/php8.1-fpm.sock","priority":81}]} diff --git a/tests/fixtures/generic/update-alternatives-query2.out b/tests/fixtures/generic/update-alternatives-query2.out new file mode 100644 index 00000000..7c0c9212 --- /dev/null +++ b/tests/fixtures/generic/update-alternatives-query2.out @@ -0,0 +1,14 @@ +Name: php-fpm.sock +Link: /run/php/php-fpm.sock +Status: auto +Best: /run/php/php8.1-fpm.sock +Value: /run/php/php8.1-fpm.sock + +Alternative: /run/php/php7.4-fpm.sock +Priority: 74 + +Alternative: /run/php/php8.0-fpm.sock +Priority: 80 + +Alternative: /run/php/php8.1-fpm.sock +Priority: 81 diff --git a/tests/test_update_alt_q.py b/tests/test_update_alt_q.py index 932cd45f..7957f675 100644 --- a/tests/test_update_alt_q.py +++ b/tests/test_update_alt_q.py @@ -13,10 +13,17 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/update-alternatives-query.out'), 'r', encoding='utf-8') as f: self.update_alternatives_query = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/update-alternatives-query2.out'), 'r', encoding='utf-8') as f: + self.update_alternatives_query2 = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/update-alternatives-query.json'), 'r', encoding='utf-8') as f: self.update_alternatives_query_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/update-alternatives-query2.json'), 'r', encoding='utf-8') as f: + self.update_alternatives_query2_json = json.loads(f.read()) + + def test_update_alt_q_nodata(self): """ @@ -30,6 +37,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.update_alt_q.parse(self.update_alternatives_query, quiet=True), self.update_alternatives_query_json) + def test_update_alt_q_no_slaves(self): + """ + Test 'update-alternatives --query' with no slaves in output + """ + self.assertEqual(jc.parsers.update_alt_q.parse(self.update_alternatives_query2, quiet=True), self.update_alternatives_query2_json) + if __name__ == '__main__': unittest.main()