Skip to content

Commit

Permalink
tests/*server.py: close log file after each log line
Browse files Browse the repository at this point in the history
Make sure the log file is not locked once a test has
finished and align with the behavior of our logmsg.

Rename curl_test_data.py to be a general util.py.

Bug: curl#6058
Closes curl#6206
  • Loading branch information
mback2k committed Nov 14, 2020
1 parent 03822c3 commit 63047fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tests/dictserver.py
Expand Up @@ -35,6 +35,7 @@
except ImportError: # Python 3
import socketserver

from util import ClosingFileHandler

log = logging.getLogger(__name__)
HOST = "localhost"
Expand Down Expand Up @@ -138,7 +139,7 @@ def setup_logging(options):

# Write out to a logfile
if options.logfile:
handler = logging.FileHandler(options.logfile, mode="w")
handler = ClosingFileHandler(options.logfile)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
root_logger.addHandler(handler)
Expand Down
4 changes: 3 additions & 1 deletion tests/negtelnetserver.py
Expand Up @@ -32,6 +32,8 @@
else:
import SocketServer as socketserver

from util import ClosingFileHandler

log = logging.getLogger(__name__)
HOST = "localhost"
IDENT = "NTEL"
Expand Down Expand Up @@ -313,7 +315,7 @@ def setup_logging(options):

# Write out to a logfile
if options.logfile:
handler = logging.FileHandler(options.logfile, mode="w")
handler = ClosingFileHandler(options.logfile)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
root_logger.addHandler(handler)
Expand Down
6 changes: 3 additions & 3 deletions tests/smbserver.py
Expand Up @@ -34,7 +34,7 @@
import ConfigParser as configparser

# Import our curl test data helper
import curl_test_data
from util import TestData, ClosingFileHandler

# impacket needs to be installed in the Python environment
try:
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(self,
config_parser=config_parser)

# Set up a test data object so we can get test data later.
self.ctd = curl_test_data.TestData(test_data_directory)
self.ctd = TestData(test_data_directory)

# Override smbComNtCreateAndX so we can pretend to have files which
# don't exist.
Expand Down Expand Up @@ -353,7 +353,7 @@ def setup_logging(options):

# Write out to a logfile
if options.logfile:
handler = logging.FileHandler(options.logfile, mode="w")
handler = ClosingFileHandler(options.logfile)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
root_logger.addHandler(handler)
Expand Down
15 changes: 14 additions & 1 deletion tests/curl_test_data.py → tests/util.py
Expand Up @@ -19,7 +19,7 @@
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
"""Module for extracting test data from the test data folder"""
"""Module for extracting test data from the test data folder and other utils"""

from __future__ import (absolute_import, division, print_function,
unicode_literals)
Expand All @@ -33,6 +33,19 @@
REPLY_DATA = re.compile("<reply>[ \t\n\r]*<data[^<]*>(.*?)</data>", re.MULTILINE | re.DOTALL)


class ClosingFileHandler(logging.StreamHandler):
def __init__(self, filename):
super(ClosingFileHandler, self).__init__()
self.filename = os.path.abspath(filename)
self.setStream(None)

def emit(self, record):
with open(self.filename, "a") as fp:
self.setStream(fp)
super(ClosingFileHandler, self).emit(record)
self.setStream(None)


class TestData(object):
def __init__(self, data_folder):
self.data_folder = data_folder
Expand Down

0 comments on commit 63047fe

Please sign in to comment.