Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Cachyos/Scripts/WIP/emu/test_sanitize_filename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
import importlib.util
import unittest
import sys
from pathlib import Path
Comment on lines +1 to +5

# Dynamically import cia_3ds_decryptor.py
file_path = Path(__file__).parent / "cia_3ds_decryptor.py"
spec = importlib.util.spec_from_file_location("cia_3ds_decryptor", str(file_path))
if spec is None:
raise ImportError(f"Could not load {file_path}")
decryptor = importlib.util.module_from_spec(spec)
sys.modules["cia_3ds_decryptor"] = decryptor
spec.loader.exec_module(decryptor)

Check warning on line 14 in Cachyos/Scripts/WIP/emu/test_sanitize_filename.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Cachyos/Scripts/WIP/emu/test_sanitize_filename.py#L14

Item "None" of "Loader | None" has no attribute "exec_module". (union-attr)


class TestSanitizeFilename(unittest.TestCase):
def test_preservation_of_valid_characters(self):

Check notice on line 18 in Cachyos/Scripts/WIP/emu/test_sanitize_filename.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Cachyos/Scripts/WIP/emu/test_sanitize_filename.py#L18

Method name "test_preservation_of_valid_characters" doesn't conform to '[a-z_][a-z0-9_]{2,30}$' pattern
# a-z, A-Z, 0-9, -, _, ., and spaces
input_name = "test-FILE_123.3ds"
expected = "test-FILE_123.3ds"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)

def test_removal_of_invalid_characters(self):

Check notice on line 24 in Cachyos/Scripts/WIP/emu/test_sanitize_filename.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Cachyos/Scripts/WIP/emu/test_sanitize_filename.py#L24

Method name "test_removal_of_invalid_characters" doesn't conform to '[a-z_][a-z0-9_]{2,30}$' pattern
# !@#$%^&*()+={}[]|\:;"'<>,/? should be removed
input_name = "test!@#$ %^&*()_+= file.cia"
# VALID_CHARS = frozenset("-_abcdefghijklmnopqrstuvwxyz1234567890. ")
# "test", " ", "_", " ", "file.cia" are valid
expected = "test _ file.cia"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Incorrect expected value - the output should have double spaces where special characters were removed.

The characters !, @, #, $, %, ^, &, *, (, ), +, = are all removed, leaving two consecutive spaces between "test" and "" and between "" and "file.cia".

Current expected: "test _ file.cia"
Correct expected: "test _ file.cia"


def test_fallback_behavior(self):
# If all characters are removed, the original name is returned.
input_name = "!!!@@@###"
# All are invalid, so 'out' would be empty, returns original
expected = "!!!@@@###"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)

def test_mixed_case_preservation(self):
input_name = "MixedCaseFILENAME.3ds"
expected = "MixedCaseFILENAME.3ds"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)

def test_empty_string(self):
self.assertEqual(decryptor.sanitize_filename(""), "")
Comment on lines +18 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The tests can be consolidated into a single, data-driven method using subTest for improved maintainability and readability. This refactoring also allows for easily adding more test cases.

I've included new test cases for Unicode and emoji characters. The current sanitize_filename implementation appears to have a bug with characters outside the Latin-1 range (e.g., emoji) because it only considers range(256). The new test case for emoji will fail, highlighting this gap in functionality and test coverage.

Suggested change
def test_preservation_of_valid_characters(self):
# a-z, A-Z, 0-9, -, _, ., and spaces
input_name = "test-FILE_123.3ds"
expected = "test-FILE_123.3ds"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)
def test_removal_of_invalid_characters(self):
# !@#$%^&*()+={}[]|\:;"'<>,/? should be removed
input_name = "test!@#$ %^&*()_+= file.cia"
# VALID_CHARS = frozenset("-_abcdefghijklmnopqrstuvwxyz1234567890. ")
# "test", " ", "_", " ", "file.cia" are valid
expected = "test _ file.cia"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)
def test_fallback_behavior(self):
# If all characters are removed, the original name is returned.
input_name = "!!!@@@###"
# All are invalid, so 'out' would be empty, returns original
expected = "!!!@@@###"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)
def test_mixed_case_preservation(self):
input_name = "MixedCaseFILENAME.3ds"
expected = "MixedCaseFILENAME.3ds"
self.assertEqual(decryptor.sanitize_filename(input_name), expected)
def test_empty_string(self):
self.assertEqual(decryptor.sanitize_filename(""), "")
def test_all_scenarios(self):
scenarios = {
"preservation_of_valid_characters": ("test-FILE_123.3ds", "test-FILE_123.3ds"),
"removal_of_invalid_characters": ("test!@#$ %^&*()_+= file.cia", "test _ file.cia"),
"fallback_behavior": ("!!!@@@###", "!!!@@@###"),
"mixed_case_preservation": ("MixedCaseFILENAME.3ds", "MixedCaseFILENAME.3ds"),
"empty_string": ("", ""),
"unicode_chars_in_latin1": ("file-éà.txt", "file-.txt"),
"unicode_chars_outside_latin1": ("test👍.txt", "test.txt"),
}
for name, (input_name, expected) in scenarios.items():
with self.subTest(name):
self.assertEqual(decryptor.sanitize_filename(input_name), expected)


if __name__ == '__main__':

Check notice on line 47 in Cachyos/Scripts/WIP/emu/test_sanitize_filename.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Cachyos/Scripts/WIP/emu/test_sanitize_filename.py#L47

expected 2 blank lines after class or function definition, found 1 (E305)
unittest.main()
Comment on lines +11 to +48
Loading