def test_long_name(self): def check(uid, name, variant, exp_logdir): tst = self.DummyTest("test", TestID(uid, name, variant), base_logdir=self.tmpdir.name) self.assertEqual(os.path.basename(tst.workdir), exp_logdir) return tst max_length = path.get_max_file_name_length(self.tmpdir.name) # uid takes 2 chars, but still fits length = max_length - 2 check(1, "a" * length, None, "1-" + ("a" * length)) # uid and variant each takes 2 chars, but still fits length = max_length - 4 check(2, "a" * length, {"variant_id": 1}, "2-" + ("a" * length) + "_1") # uid and variant each takes 3 chars, but still fits length = max_length - 6 check(99, "a" * length, {"variant_id": 88}, "99-" + ("a" * length) + "_88") # name is one char over limit, so name must shrink length = max_length - 3 check(3, "a" * length, {"variant_id": 1}, "3-" + ('a' * (length - 1)) + "_1") # Shrink variant length = max_length - 2 check("a" * length, "whatever", {"variant_id": 99}, "a" * length + "_9") length = max_length - 1 check("a" * length, "whatever", {"variant_id": 99}, "a" * length + "_") # Impossible to store (uid does not fit self.assertRaises(RuntimeError, check, "a" * (max_length + 1), "whatever", {"variant_id": "else"}, None)
def string_to_safe_path(input_str): """ Convert string to a valid file/dir name. This takes a string that may contain characters that are not allowed on FAT (Windows) filesystems and/or ext3 (Linux) filesystems, and replaces them for safe (boring) underlines. It limits the size of the path to be under 255 chars, and make hidden paths (starting with ".") non-hidden by making them start with "_". :param input_str: String to be converted :return: String which is safe to pass as a file/dir name (on recent fs) """ max_length = path.get_max_file_name_length(input_str) if input_str.startswith("."): input_str = "_" + input_str[1:max_length] elif len(input_str) > max_length: input_str = input_str[:max_length] try: return input_str.translate(_FS_TRANSLATE) except TypeError: # Deal with incorrect encoding for bad_chr in FS_UNSAFE_CHARS: input_str = input_str.replace(bad_chr, "_") return input_str
def _max_name_length(self): """Returns the maximum length for test file names The maximum file name is filesystem dependent, and besides that, standard data directory based on test file names has a suffix (:data:`SUFFIX`) that has to be accounted for. """ max_length = utils_path.get_max_file_name_length(self.filename) return max_length - len(self.SUFFIX)
def test_uid_name_uid_too_large_digits(self): """ Tests that when the filesystem can not cope with the size of the Test ID, not even the test uid, an exception will be raised. """ name = 'test' max_length = path.get_max_file_name_length(name) over_limit = max_length + 1 test_id = TestID(1, name, no_digits=over_limit) self.assertRaises(RuntimeError, lambda: test_id.str_filesystem)
def test_safe_path(self): self.assertEqual(astring.string_to_safe_path('a<>:"/\\|\\?*b'), "a__________b") self.assertEqual(astring.string_to_safe_path('..'), "_.") name = " " * 300 max_length = path.get_max_file_name_length(name) self.assertEqual(len(astring.string_to_safe_path(" " * 300)), max_length) avocado = '\u0430\u0432\u043e\u043a\u0430\u0434\xff<>' self.assertEqual(astring.string_to_safe_path(avocado), f"{avocado[:-2]}__")
def test_uid_name_large_digits(self): """ Tests that when the filesystem can only cope with the size of the Test ID, that's the only thing that will be kept. """ uid = 1 name = 'test' max_length = path.get_max_file_name_length(name) test_id = TestID(uid, name, no_digits=max_length) self.assertEqual(test_id.uid, 1) self.assertEqual(test_id.str_uid, f'{uid:0{max_length}d}') self.assertEqual(test_id.str_filesystem, f'{uid:0{max_length}d}') self.assertIs(test_id.variant, None) self.assertIs(test_id.str_variant, '')
def test_uid_large_name(self): """ Tests that when the filesystem can not cope with the size of the Test ID, the name will be shortened. """ uid = 1 name = 'test_' * 51 # 255 characters test_id = TestID(uid, name) max_length = path.get_max_file_name_length(name) self.assertEqual(test_id.uid, 1) # 2 chars are taken by the uid and dash max_name_length = max_length - 2 self.assertEqual(test_id.str_filesystem, f'{uid}-{name[:max_name_length]}') self.assertIs(test_id.variant, None) self.assertIs(test_id.str_variant, "")
def test_uid_name_large_variant(self): """ Tests that when the filesystem can not cope with the size of the Test ID, and a variant name is present, the name will be removed. """ uid = 1 name = 'test' variant_id = 'fast_' * 51 # 255 characters variant = {'variant_id': variant_id} test_id = TestID(uid, name, variant=variant) max_length = path.get_max_file_name_length(name) # 2 chars are taken by the uid and dash max_name_length = max_length - 2 self.assertEqual(test_id.uid, 1) self.assertEqual(test_id.str_filesystem, f'{uid}_{variant_id[:max_name_length]}') self.assertIs(test_id.variant, variant_id) self.assertEqual(test_id.str_variant, f";{variant_id}")