Changeset b457e67 in mod_gnutls


Ignore:
Timestamp:
Dec 31, 2019, 11:26:25 AM (3 months ago)
Author:
Fiona Klute <fiona.klute@…>
Branches:
master
Children:
92cf138
Parents:
ac516aa
Message:

Open log files through argparse

The with block around main ensures they get closed even in case of
exceptions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/runtest.py

    rac516aa rb457e67  
    173173        # Run the test connections
    174174        with contextlib.ExitStack() as stack:
    175             log_file = None
    176             output_file = None
    177             if args.log_connection:
    178                 log_file = stack.enter_context(open(args.log_connection, 'w'))
    179             if args.log_responses:
    180                 output_file = stack.enter_context(open(args.log_responses, 'w'))
    181 
    182175            if plugin.run_connection:
    183176                plugin.run_connection(testname,
    184                                       conn_log=log_file,
    185                                       response_log=output_file)
     177                                      conn_log=args.log_connection,
     178                                      response_log=args.log_responses)
    186179            else:
    187180                test_conf = stack.enter_context(
     
    189182                run_test_conf(test_conf,
    190183                              float(os.environ.get('TEST_QUERY_TIMEOUT', 5.0)),
    191                               conn_log=log_file, response_log=output_file)
     184                              conn_log=args.log_connection,
     185                              response_log=args.log_responses)
    192186
    193187    # run extra checks the test's hooks.py might define
    194188    if plugin.post_check:
    195         log_file = None
    196         output_file = None
    197         with contextlib.ExitStack() as stack:
    198             # TODO: The log files should be created as temporary
    199             # files if needed by the plugin but not configured.
    200             if args.log_connection:
    201                 log_file = stack.enter_context(open(args.log_connection, 'r'))
    202             if args.log_responses:
    203                 output_file = stack.enter_context(open(args.log_responses, 'r'))
    204             plugin.post_check(conn_log=log_file, response_log=output_file)
     189        if args.log_connection:
     190            args.log_connection.seek(0)
     191        if args.log_responses:
     192            args.log_responses.seek(0)
     193        plugin.post_check(conn_log=args.log_connection,
     194                          response_log=args.log_responses)
    205195
    206196
     
    212202    parser.add_argument('--test-number', type=int,
    213203                        required=True, help='load YAML test configuration')
    214     parser.add_argument('--log-connection', type=str, default=None,
     204    # TODO: The log files should be created as temporary
     205    # files if needed by the plugin but not configured.
     206    parser.add_argument('--log-connection', type=argparse.FileType('w+'),
     207                        default=None,
    215208                        help='write connection log to this file')
    216     parser.add_argument('--log-responses', type=str, default=None,
     209    parser.add_argument('--log-responses', type=argparse.FileType('w+'),
     210                        default=None,
    217211                        help='write HTTP responses to this file')
    218212
     
    226220    args = parser.parse_args()
    227221
    228     main(args)
     222    with contextlib.ExitStack() as stack:
     223        if args.log_connection:
     224            stack.enter_context(contextlib.closing(args.log_connection))
     225        if args.log_responses:
     226            stack.enter_context(contextlib.closing(args.log_responses))
     227        main(args)
Note: See TracChangeset for help on using the changeset viewer.