diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index 53023ec71..4ada4dc01 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -5,27 +5,13 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import os import os.path import unittest +from contextlib import contextmanager from unittest import mock -import pytest from ddt import data, ddt, unpack from octoprint.filemanager.storage import LocalFileStorage, StorageError -try: - import pathvalidate # noqa: F401 - - HAS_PATHVALIDATE = True -except ImportError: - HAS_PATHVALIDATE = False - -pathvalidate_unavailable_only = pytest.mark.skipif( - HAS_PATHVALIDATE, reason="pathvalidate available" -) -pathvalidate_available_only = pytest.mark.skipif( - not HAS_PATHVALIDATE, reason="pathvalidate unavailable" -) - class FileWrapper: def __init__(self, filename): @@ -728,33 +714,29 @@ class LocalStorageTest(unittest.TestCase): self.assertEqual(0, len(gcode_metadata["links"])) self.assertEqual(1, len(stl_metadata["links"])) - @pathvalidate_unavailable_only @data( - ("some_file.gco", "some_file.gco"), + ("some_file.gco", "some_file.gco", False), + ("some file.gco", "some file.gco", False), + ( + "some_file with (parentheses) and ümläuts and digits 123.gco", + "some_file with (parentheses) and ümläuts and digits 123.gco", + False, + ), + ("pengüino pequeño.stl", "pengüino pequeño.stl", False), + ("some file.gco", "some_file.gco", True), ( "some_file with (parentheses) and ümläuts and digits 123.gco", "some_file_with_(parentheses)_and_umlauts_and_digits_123.gco", + True, ), - ("pengüino pequeño.stl", "penguino_pequeno.stl"), + ("pengüino pequeño.stl", "penguino_pequeno.stl", True), ) @unpack - def test_sanitize_name_pvu(self, input, expected): - actual = self.storage.sanitize_name(input) - self.assertEqual(expected, actual) - - @pathvalidate_available_only - @data( - ("some_file.gco", "some_file.gco"), - ( - "some_file with (parentheses) and ümläuts and digits 123.gco", - "some_file with (parentheses) and ümläuts and digits 123.gco", - ), - ("pengüino pequeño.stl", "pengüino pequeño.stl"), - ) - @unpack - def test_sanitize_name_pva(self, input, expected): - actual = self.storage.sanitize_name(input) + def test_sanitize_name(self, input, expected, really_universal): + with _set_really_universal(self.storage, really_universal): + actual = self.storage.sanitize_name(input) self.assertEqual(expected, actual) + self.storage._really_universal = False @data("some/folder/still/left.gco", "also\\no\\backslashes.gco") def test_sanitize_name_invalid(self, input): @@ -786,8 +768,38 @@ class LocalStorageTest(unittest.TestCase): except ValueError as e: self.assertTrue(e.args[0].startswith("path not contained in base folder: ")) - def _test_sanitize(self, input, expected_path, expected_name): - actual = self.storage.sanitize(input) + @data( + ("", "/", "", False), + ( + "some/folder/with/trailing/slash/", + "/some/folder/with/trailing/slash", + "", + False, + ), + (("some", "folder", ""), "/some/folder", "", False), + ("some/folder/and/some file.gco", "/some/folder/and", "some file.gco", False), + ( + ("some", "folder", "and", "some file.gco"), + "/some/folder/and", + "some file.gco", + False, + ), + ("some file.gco", "/", "some file.gco", False), + (("some file.gco",), "/", "some file.gco", False), + ("some/folder/and/some file.gco", "/some/folder/and", "some_file.gco", True), + ( + ("some", "folder", "and", "some file.gco"), + "/some/folder/and", + "some_file.gco", + True, + ), + ("some file.gco", "/", "some_file.gco", True), + (("some file.gco",), "/", "some_file.gco", True), + ) + @unpack + def test_sanitize(self, input, expected_path, expected_name, really_universal): + with _set_really_universal(self.storage, really_universal): + actual = self.storage.sanitize(input) self.assertTrue(isinstance(actual, tuple)) self.assertEqual(2, len(actual)) @@ -803,34 +815,6 @@ class LocalStorageTest(unittest.TestCase): self.assertEqual(expected_path, actual_path) self.assertEqual(expected_name, actual_name) - @pathvalidate_unavailable_only - @data( - ("some/folder/and/some file.gco", "/some/folder/and", "some_file.gco"), - (("some", "folder", "and", "some file.gco"), "/some/folder/and", "some_file.gco"), - ("some file.gco", "/", "some_file.gco"), - (("some file.gco",), "/", "some_file.gco"), - ("", "/", ""), - ("some/folder/with/trailing/slash/", "/some/folder/with/trailing/slash", ""), - (("some", "folder", ""), "/some/folder", ""), - ) - @unpack - def test_sanitize_pvu(self, input, expected_path, expected_name): - self._test_sanitize(input, expected_path, expected_name) - - @pathvalidate_available_only - @data( - ("some/folder/and/some file.gco", "/some/folder/and", "some file.gco"), - (("some", "folder", "and", "some file.gco"), "/some/folder/and", "some file.gco"), - ("some file.gco", "/", "some file.gco"), - (("some file.gco",), "/", "some file.gco"), - ("", "/", ""), - ("some/folder/with/trailing/slash/", "/some/folder/with/trailing/slash", ""), - (("some", "folder", ""), "/some/folder", ""), - ) - @unpack - def test_sanitize_pva(self, input, expected_path, expected_name): - self._test_sanitize(input, expected_path, expected_name) - def _add_and_verify_file( self, path, expected_path, file_object, links=None, overwrite=False, display=None ): @@ -923,3 +907,13 @@ class LocalStorageTest(unittest.TestCase): ) ) return sanitized_path + + +@contextmanager +def _set_really_universal(storage, value): + orig = storage._really_universal + try: + storage._really_universal = value + yield + finally: + storage._really_universal = orig diff --git a/tests/util/test_files.py b/tests/util/test_files.py index b5f41853a..5b2c09c77 100644 --- a/tests/util/test_files.py +++ b/tests/util/test_files.py @@ -3,38 +3,27 @@ __copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms import unittest -import pytest from ddt import data, ddt, unpack from octoprint.util.files import sanitize_filename -try: - import pathvalidate # noqa: F401 - - HAS_PATHVALIDATE = True -except ImportError: - HAS_PATHVALIDATE = False - -pathvalidate_unavailable_only = pytest.mark.skipif( - HAS_PATHVALIDATE, reason="pathvalidate available" -) -pathvalidate_available_only = pytest.mark.skipif( - not HAS_PATHVALIDATE, reason="pathvalidate unavailable" -) - @ddt class FilesUtilTest(unittest.TestCase): @data( - ("some_file.gcode", "some_file.gcode"), - ("NUL.gcode", "NUL_.gcode"), - ("LPT1", "LPT1_"), - (".test.gcode", "test.gcode"), - ("..test.gcode", "test.gcode"), + ("some_file.gcode", "some_file.gcode", False), + ("NUL.gcode", "NUL_.gcode", False), + ("LPT1", "LPT1_", False), + (".test.gcode", "test.gcode", False), + ("..test.gcode", "test.gcode", False), + ("file with space.gcode", "file with space.gcode", False), + ("Wölfe 🐺.gcode", "Wölfe 🐺.gcode", False), + ("file with space.gcode", "file_with_space.gcode", True), + ("Wölfe 🐺.gcode", "Wolfe_wolf.gcode", True), ) @unpack - def test_sanitize_filename(self, filename, expected): - actual = sanitize_filename(filename) + def test_sanitize_filename(self, filename, expected, really_universal): + actual = sanitize_filename(filename, really_universal=really_universal) self.assertEqual(actual, expected) @data("file/with/slash.gcode", "file\\with\\backslash.gcode") @@ -44,23 +33,3 @@ class FilesUtilTest(unittest.TestCase): self.fail("expected ValueError") except ValueError as ex: self.assertEqual(str(ex), "name must not contain / or \\") - - @pathvalidate_unavailable_only - @data( - ("file with space.gcode", "file_with_space.gcode"), - ("Wölfe 🐺.gcode", "Wolfe_wolf.gcode"), - ) - @unpack - def test_sanitize_filename_pvu(self, filename, expected): - actual = sanitize_filename(filename) - self.assertEqual(actual, expected) - - @pathvalidate_available_only - @data( - ("file with space.gcode", "file with space.gcode"), - ("Wölfe 🐺.gcode", "Wölfe 🐺.gcode"), - ) - @unpack - def test_sanitize_filename_pva(self, filename, expected): - actual = sanitize_filename(filename) - self.assertEqual(actual, expected)