From e3208e5d01376a47250ce0339581f2fa7f828599 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Thu, 16 Jul 2020 07:29:18 -0400 Subject: [PATCH] Simplify config file writing in test functions The config_and_cursor fixture yields a function object which can be called to provide a desired configuration (in either dict or string form) to be written into the temporary config file. This eliminates the need for the test function to explicitly write to the config file and then flush it before the test is executed. The function object returns the configfile and cursorfile objects, as the fixture previously provided, so that the test function can manipulate them when needed. Signed-off-by: Kevin P. Fleming --- tests/cli/test_main.py | 133 ++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 81 deletions(-) diff --git a/tests/cli/test_main.py b/tests/cli/test_main.py index 7f8a758..ae5561b 100644 --- a/tests/cli/test_main.py +++ b/tests/cli/test_main.py @@ -44,9 +44,16 @@ def config_and_cursor(tmp_path): with NamedTemporaryFile(mode='wt', dir=tmp_path) as configfile: with NamedTemporaryFile(mode='w+t', dir=tmp_path) as cursorfile: - configfile.write('cursor-file: {0}\n'.format(cursorfile.name)) - configfile.flush() - yield (configfile, cursorfile) + def write_config(config): + configfile.write('cursor-file: {0}\n'.format(cursorfile.name)) + if isinstance(config, dict): + configfile.write(yaml.dump(config)) + elif config: + configfile.write(config) + configfile.flush() + return (configfile, cursorfile) + + yield write_config @pytest.fixture @@ -88,7 +95,7 @@ def test_normal_run(self, capsys, config_and_cursor, missing_or_empty_cursor): 'MESSAGE': 'message2'}) .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -104,7 +111,7 @@ def test_dry_run(self, config_and_cursor): 'MESSAGE': 'message'}) .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, '--dry-run']) cli.run() assert not cursorfile.read() @@ -122,7 +129,7 @@ def test_this_boot(self, config_and_cursor): 'MESSAGE': 'message'}) .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cursorfile.write(final_cursor) cursorfile.flush() cli = CLI(args=['--conf', configfile.name, '-b']) @@ -140,12 +147,12 @@ def test_log_level(self, config_and_cursor, missing_or_empty_cursor): .should_receive('get_next') .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, '-p', 'err']) cli.run() def test_reset(self, config_and_cursor): - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) with cursorfile: # force the cursorfile context to be exited at the right time cli = CLI(args=['--conf', configfile.name, 'reset']) cli.run() @@ -169,12 +176,10 @@ def test_stats(self, capsys, config_and_cursor, missing_or_empty_cursor): 'MESSAGE': 'include'}) .and_return({})) - (configfile, cursorfile) = config_and_cursor - configfile.write(""" + (configfile, cursorfile) = config_and_cursor(""" exclusions: - MESSAGE: [exclude] """) - configfile.flush() cli = CLI(args=['--conf', configfile.name, 'stats']) cli.run() @@ -215,7 +220,7 @@ def test_debrief(self, capsys, config_and_cursor, missing_or_empty_cursor): expectation.and_return({}) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, 'debrief']) cli.run() @@ -239,7 +244,7 @@ def test_debrief_no_input(self, capsys, config_and_cursor, missing_or_empty_curs .should_receive('get_next') .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, 'debrief']) cli.run() @@ -255,12 +260,10 @@ def test_exclusions_yaml(self, capsys, config_and_cursor, missing_or_empty_curso 'MESSAGE': 'message'}) .and_return({})) - (configfile, cursorfile) = config_and_cursor - configfile.write(""" + (configfile, cursorfile) = config_and_cursor(""" exclusions: - MESSAGE: [1] """) - configfile.flush() cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -283,14 +286,12 @@ def test_inclusions_yaml(self, config_and_cursor, missing_or_empty_cursor): .should_receive('add_disjunction') .replace_with(watcher.watch_call('add_disjunction'))) - (configfile, cursorfile) = config_and_cursor - configfile.write(""" + (configfile, cursorfile) = config_and_cursor(""" inclusions: - PRIORITY: [0, 1, 2, 3] - PRIORITY: [4, 5, 6] _SYSTEMD_UNIT: [myservice.service] """) - configfile.flush() cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -328,7 +329,7 @@ def test_multiple_output_formats_cli(self, capsys, config_and_cursor, missing_or .and_return(entry) .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, '-o', 'cat,cat,json']) cli.run() @@ -355,14 +356,12 @@ def test_multiple_output_formats_conf(self, capsys, config_and_cursor, missing_o .and_return(entry) .and_return({})) - (configfile, cursorfile) = config_and_cursor - configfile.write(""" + (configfile, cursorfile) = config_and_cursor(""" output: - cat - cat - json """) - configfile.flush() cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -395,7 +394,7 @@ def test_formatter_filter(self, capsys, config_and_cursor, missing_or_empty_curs .and_return(entry) .and_return({})) - (configfile, cursorfile) = config_and_cursor + (configfile, cursorfile) = config_and_cursor(None) cli = CLI(args=['--conf', configfile.name, '-p', 'err', '-o', 'login']) cli.run() @@ -434,14 +433,12 @@ def test(self, config_and_cursor, missing_or_empty_cursor): input=entry['OUTPUT']) .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'output': 'test', 'email': { 'command': self.TEST_COMMAND, - }, - })) - configfile.flush() + } + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -461,14 +458,12 @@ def test_dry_run(self, capsys, config_and_cursor): .should_receive('run') .never()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'output': 'test', 'email': { 'command': self.TEST_COMMAND, }, - })) - configfile.flush() + }) cli = CLI(args=['--dry-run', '--conf', configfile.name]) cli.run() @@ -490,14 +485,12 @@ def test_allow_empty(self, config_and_cursor, missing_or_empty_cursor): input=EMAIL_SUPPRESS_EMPTY_TEXT) .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'command': self.TEST_COMMAND, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -510,13 +503,11 @@ def test_suppress_empty(self, config_and_cursor, missing_or_empty_cursor): .should_receive('run') .never()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'command': self.TEST_COMMAND, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -569,8 +560,7 @@ def test(self, capsys, config_and_cursor, missing_or_empty_cursor): .should_receive('send_message') .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'output': 'test', 'email': { 'smtp': { @@ -578,8 +568,7 @@ def test(self, capsys, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -599,8 +588,7 @@ def test_dry_run(self, capsys, config_and_cursor): .should_receive('SMTP') .never()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'output': 'test', 'email': { 'smtp': { @@ -608,8 +596,7 @@ def test_dry_run(self, capsys, config_and_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--dry-run', '--conf', configfile.name]) cli.run() @@ -641,8 +628,7 @@ def test_allow_empty(self, config_and_cursor, missing_or_empty_cursor): .should_receive('send_message') .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -650,8 +636,7 @@ def test_allow_empty(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -664,16 +649,14 @@ def test_suppress_empty(self, config_and_cursor, missing_or_empty_cursor): .should_receive('SMTP') .never()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'smtp': { 'from': 'F', 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -691,8 +674,7 @@ def test_host(self, config_and_cursor, missing_or_empty_cursor): .should_receive('send_message') .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -701,8 +683,7 @@ def test_host(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -720,8 +701,7 @@ def test_port(self, config_and_cursor, missing_or_empty_cursor): .should_receive('send_message') .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -731,8 +711,7 @@ def test_port(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -752,8 +731,7 @@ def test_starttls(self, config_and_cursor, missing_or_empty_cursor): .once() .ordered()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -762,8 +740,7 @@ def test_starttls(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -783,8 +760,7 @@ def test_user(self, config_and_cursor, missing_or_empty_cursor): .once() .ordered()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -793,8 +769,7 @@ def test_user(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -814,8 +789,7 @@ def test_password(self, config_and_cursor, missing_or_empty_cursor): .once() .ordered()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -825,8 +799,7 @@ def test_password(self, config_and_cursor, missing_or_empty_cursor): 'to': 'T', }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run() @@ -855,8 +828,7 @@ def test_subject(self, config_and_cursor, missing_or_empty_cursor): .should_receive('send_message') .once()) - (configfile, cursorfile) = config_and_cursor - configfile.write(yaml.dump({ + (configfile, cursorfile) = config_and_cursor({ 'email': { 'suppress_empty': False, 'smtp': { @@ -865,7 +837,6 @@ def test_subject(self, config_and_cursor, missing_or_empty_cursor): 'subject': self.TEST_SUBJECT, }, }, - })) - configfile.flush() + }) cli = CLI(args=['--conf', configfile.name]) cli.run()