From 26abe2148685ccc72891db601c4aa1185e45dc78 Mon Sep 17 00:00:00 2001 From: Eduard Braun Date: Sun, 27 Nov 2016 18:38:29 +0100 Subject: [PATCH] call `sanitizeOptions()` in `start()` to prevent a third-party breakage --- scour/scour.py | 10 ++++++---- testscour.py | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 23be0c5..a1b99cf 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -3523,10 +3523,10 @@ def scourString(in_string, options=None): # input is a filename # returns the minidom doc representation of the SVG def scourXmlFile(filename, options=None): - # we need to set infilename (otherwise relative references in the SVG won't work) - if options is None: - options = generateDefaultOptions() - options.infilename = filename + # sanitize options (take missing attributes from defaults, discard unknown attributes) + options = sanitizeOptions(options) + # we need to make sure infilename is set correctly (otherwise relative references in the SVG won't work) + options.ensure_value("infilename", filename) # open the file and scour it with open(filename, "rb") as f: @@ -3783,6 +3783,8 @@ def getReport(): def start(options, input, output): + # sanitize options (take missing attributes from defaults, discard unknown attributes) + options = sanitizeOptions(options) start = walltime() diff --git a/testscour.py b/testscour.py index 6098914..560f79b 100755 --- a/testscour.py +++ b/testscour.py @@ -30,7 +30,7 @@ import unittest import six from six.moves import map, range -from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, run +from scour.scour import makeWellFormed, parse_args, scourString, scourXmlFile, start, run from scour.svg_regex import svg_parser from scour import __version__ @@ -58,15 +58,45 @@ class ScourOptions: class EmptyOptions(unittest.TestCase): - def runTest(self): + MINIMAL_SVG = '\n' \ + '\n' + + def test_scourString(self): options = ScourOptions try: - scourXmlFile('unittests/ids-to-strip.svg', options) + scourString(self.MINIMAL_SVG, options) fail = False except: fail = True self.assertEqual(fail, False, - 'Exception when calling Scour with empty options object') + 'Exception when calling "scourString" with empty options object') + + def test_scourXmlFile(self): + options = ScourOptions + try: + scourXmlFile('unittests/minimal.svg', options) + fail = False + except: + fail = True + self.assertEqual(fail, False, + 'Exception when calling "scourXmlFile" with empty options object') + + def test_start(self): + options = ScourOptions + input = open('unittests/minimal.svg', 'rb') + output = open('testscour_temp.svg', 'wb') + + stdout_temp = sys.stdout + sys.stdout = None + try: + start(options, input, output) + fail = False + except: + fail = True + sys.stdout = stdout_temp + + self.assertEqual(fail, False, + 'Exception when calling "start" with empty options object') class InvalidOptions(unittest.TestCase):