From 1582151a0963541ce2a1cd76589c5dc3299f1e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 26 Jan 2022 13:38:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Fix=20a=20py2=20test=20failure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit str.rstrip won't work when we receive unicode instead. I'll celebrate the day I can finally kick py2 support from the source... --- tests/util/test_pip.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/util/test_pip.py b/tests/util/test_pip.py index ec4800868..912012bba 100644 --- a/tests/util/test_pip.py +++ b/tests/util/test_pip.py @@ -160,6 +160,13 @@ class PipUtilTest(unittest.TestCase): os.path.dirname(__file__), "_files", "pip_test_data" ) + def _get_lines(self, file): + with io.open(os.path.join(self._test_data, file), "r", encoding="utf-8") as f: + lines = list( + map(lambda x: x.rstrip, f.readlines()) + ) # not str.rstrip because in py2 it'll be unicode... + return lines + @ddt.data( ("already_installed_1.txt", True), ("already_installed_2.txt", True), @@ -167,9 +174,7 @@ class PipUtilTest(unittest.TestCase): ) @ddt.unpack def test_is_already_installed(self, file, expected): - with io.open(os.path.join(self._test_data, file), "r", encoding="utf-8") as f: - lines = list(map(str.rstrip, f.readlines())) - + lines = self._get_lines(file) actual = octoprint.util.pip.is_already_installed(lines) self.assertEqual(expected, actual) @@ -181,9 +186,7 @@ class PipUtilTest(unittest.TestCase): ) @ddt.unpack def test_is_egg_problem(self, file, expected): - with io.open(os.path.join(self._test_data, file), "r", encoding="utf-8") as f: - lines = list(map(str.rstrip, f.readlines())) - + lines = self._get_lines(file) actual = octoprint.util.pip.is_egg_problem(lines) self.assertEqual(expected, actual) @@ -195,9 +198,7 @@ class PipUtilTest(unittest.TestCase): ) @ddt.unpack def test_is_python_mismatch(self, file, expected): - with io.open(os.path.join(self._test_data, file), "r", encoding="utf-8") as f: - lines = list(map(str.rstrip, f.readlines())) - + lines = self._get_lines(file) actual = octoprint.util.pip.is_python_mismatch(lines) self.assertEqual(expected, actual) @@ -210,8 +211,6 @@ class PipUtilTest(unittest.TestCase): ) @ddt.unpack def test_get_result_line(self, file, expected): - with io.open(os.path.join(self._test_data, file), "r", encoding="utf-8") as f: - lines = list(map(str.rstrip, f.readlines())) - + lines = self._get_lines(file) actual = octoprint.util.pip.get_result_line(lines) self.assertEqual(expected, actual)