From 73d9322813c1c675c1c22adab1e90a78ac4affef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Thu, 25 Feb 2016 10:39:46 +0100 Subject: [PATCH] linter: Test run on str, unicode, bytes and stream Previously it was not tested, and broke on Python 2 `unicode` inputs. --- tests/test_linter.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ yamllint/linter.py | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/test_linter.py diff --git a/tests/test_linter.py b/tests/test_linter.py new file mode 100644 index 0000000..8b223a5 --- /dev/null +++ b/tests/test_linter.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2016 Adrien Vergé +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import io + +import unittest + +from yamllint.config import YamlLintConfig +from yamllint import linter + + +class LinterTestCase(unittest.TestCase): + def fake_config(self): + return YamlLintConfig('extends: default') + + def test_run_on_string(self): + linter.run('test: document', self.fake_config()) + + def test_run_on_bytes(self): + linter.run(b'test: document', self.fake_config()) + + def test_run_on_unicode(self): + linter.run(u'test: document', self.fake_config()) + + def test_run_on_stream(self): + linter.run(io.StringIO(u'hello'), self.fake_config()) + + def test_run_on_int(self): + self.assertRaises(TypeError, linter.run, 42, self.fake_config()) + + def test_run_on_list(self): + self.assertRaises(TypeError, linter.run, + ['h', 'e', 'l', 'l', 'o'], self.fake_config()) diff --git a/yamllint/linter.py b/yamllint/linter.py index 65dc9e9..8af85a5 100644 --- a/yamllint/linter.py +++ b/yamllint/linter.py @@ -128,7 +128,7 @@ def run(input, conf): :param input: buffer, string or stream to read from :param conf: yamllint configuration object """ - if type(input) == str: + if type(input) in (type(b''), type(u'')): # compat with Python 2 & 3 return _run(input, conf) elif hasattr(input, 'read'): # Python 2's file or Python 3's io.IOBase # We need to have everything in memory to parse correctly