From 15ac5a9004885dcc276440c2a9ccd5460ddd8d31 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 21 Feb 2023 15:44:55 -0800 Subject: [PATCH] fix for crontab files with only shortcut entries --- jc/parsers/crontab.py | 5 ++++- jc/parsers/crontab_u.py | 5 ++++- .../fixtures/generic/crontab-no-normal-entries.json | 1 + tests/fixtures/generic/crontab-no-normal-entries.out | 2 ++ .../generic/crontab-u-no-normal-entries.json | 1 + .../fixtures/generic/crontab-u-no-normal-entries.out | 2 ++ tests/test_crontab.py | 12 ++++++++++++ tests/test_crontab_u.py | 11 +++++++++++ 8 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/generic/crontab-no-normal-entries.json create mode 100644 tests/fixtures/generic/crontab-no-normal-entries.out create mode 100644 tests/fixtures/generic/crontab-u-no-normal-entries.json create mode 100644 tests/fixtures/generic/crontab-u-no-normal-entries.out diff --git a/jc/parsers/crontab.py b/jc/parsers/crontab.py index 07f7da53..031bd8d5 100644 --- a/jc/parsers/crontab.py +++ b/jc/parsers/crontab.py @@ -174,7 +174,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.6' + version = '1.7' description = '`crontab` command and file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -273,6 +273,9 @@ def parse(data, raw=False, quiet=False): raw_output['schedule'] = cron_list # Add shortcut entries back in + if 'schedule' not in raw_output: + raw_output['schedule'] = [] + for item in shortcut_list: raw_output['schedule'].append(item) diff --git a/jc/parsers/crontab_u.py b/jc/parsers/crontab_u.py index 7fb5a938..b847c6ad 100644 --- a/jc/parsers/crontab_u.py +++ b/jc/parsers/crontab_u.py @@ -171,7 +171,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.7' + version = '1.8' description = '`crontab` file parser with user support' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -271,6 +271,9 @@ def parse(data, raw=False, quiet=False): raw_output['schedule'] = cron_list # Add shortcut entries back in + if 'schedule' not in raw_output: + raw_output['schedule'] = [] + for item in shortcut_list: raw_output['schedule'].append(item) diff --git a/tests/fixtures/generic/crontab-no-normal-entries.json b/tests/fixtures/generic/crontab-no-normal-entries.json new file mode 100644 index 00000000..ec0e89fb --- /dev/null +++ b/tests/fixtures/generic/crontab-no-normal-entries.json @@ -0,0 +1 @@ +{"variables":[],"schedule":[{"occurrence":"daily","command":"/bin/sh do_the_thing"}]} diff --git a/tests/fixtures/generic/crontab-no-normal-entries.out b/tests/fixtures/generic/crontab-no-normal-entries.out new file mode 100644 index 00000000..ac72d233 --- /dev/null +++ b/tests/fixtures/generic/crontab-no-normal-entries.out @@ -0,0 +1,2 @@ +#this is a test for the jc module +@daily /bin/sh do_the_thing diff --git a/tests/fixtures/generic/crontab-u-no-normal-entries.json b/tests/fixtures/generic/crontab-u-no-normal-entries.json new file mode 100644 index 00000000..250f49df --- /dev/null +++ b/tests/fixtures/generic/crontab-u-no-normal-entries.json @@ -0,0 +1 @@ +{"variables":[],"schedule":[{"occurrence":"daily","user":"root","command":"/bin/sh do_the_thing"}]} diff --git a/tests/fixtures/generic/crontab-u-no-normal-entries.out b/tests/fixtures/generic/crontab-u-no-normal-entries.out new file mode 100644 index 00000000..559adfe3 --- /dev/null +++ b/tests/fixtures/generic/crontab-u-no-normal-entries.out @@ -0,0 +1,2 @@ +#this is a test for the jc module +@daily root /bin/sh do_the_thing diff --git a/tests/test_crontab.py b/tests/test_crontab.py index ccb5e145..81ae4b49 100644 --- a/tests/test_crontab.py +++ b/tests/test_crontab.py @@ -12,10 +12,16 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab.out'), 'r', encoding='utf-8') as f: centos_7_7_crontab = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.out'), 'r', encoding='utf-8') as f: + generic_crontab_no_normal_entries = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab.json'), 'r', encoding='utf-8') as f: centos_7_7_crontab_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.json'), 'r', encoding='utf-8') as f: + generic_crontab_no_normal_entries_json = json.loads(f.read()) + def test_crontab_nodata(self): """ @@ -29,6 +35,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.crontab.parse(self.centos_7_7_crontab, quiet=True), self.centos_7_7_crontab_json) + def test_crontab_no_normal_entries(self): + """ + Test 'crontab' with no normal entries - only shortcuts + """ + self.assertEqual(jc.parsers.crontab.parse(self.generic_crontab_no_normal_entries, quiet=True), self.generic_crontab_no_normal_entries_json) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_crontab_u.py b/tests/test_crontab_u.py index 9e4c07b2..ad5429cd 100644 --- a/tests/test_crontab_u.py +++ b/tests/test_crontab_u.py @@ -18,6 +18,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/crontab-u.out'), 'r', encoding='utf-8') as f: debian10_crontab_u = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.out'), 'r', encoding='utf-8') as f: + generic_crontab_u_no_normal_entries = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/crontab-u.json'), 'r', encoding='utf-8') as f: ubuntu_18_4_crontab_u_json = json.loads(f.read()) @@ -28,6 +31,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/crontab-u.json'), 'r', encoding='utf-8') as f: debian10_crontab_u_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.json'), 'r', encoding='utf-8') as f: + generic_crontab_u_no_normal_entries_json = json.loads(f.read()) + def test_crontab_u_nodata(self): """ @@ -53,6 +59,11 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.crontab_u.parse(self.debian10_crontab_u, quiet=True), self.debian10_crontab_u_json) + def test_crontab_u_no_normal_entries(self): + """ + Test 'crontab' with no normal entries - only shortcut entries (has a user field) + """ + self.assertEqual(jc.parsers.crontab_u.parse(self.generic_crontab_u_no_normal_entries, quiet=True), self.generic_crontab_u_no_normal_entries_json) if __name__ == '__main__': unittest.main()