If we are on py3 and thus can rely on pathvalidate, we'll now no longer slugify uploaded files names, but try to keep them as close to the original name as possible, including unicode and emoji and whatnot. This requires the underlying file system to support this, so just in case the old behaviour can be enforced by setting the config flag feature.enforceReallyUniversalFilenames to true. Closes #4078
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
|
|
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
|
|
__copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
|
|
|
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"),
|
|
("..text.gcode", "test.gcode"),
|
|
)
|
|
@unpack
|
|
def test_sanitize_filename(self, filename, expected):
|
|
actual = sanitize_filename(filename)
|
|
self.assertEqual(actual, expected)
|
|
|
|
@data("file/with/slash.gcode", "file\\with\\backslash.gcode")
|
|
def test_sanitize_filename_invalid(self, filename):
|
|
try:
|
|
sanitize_filename(filename)
|
|
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_face.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)
|