5
5
import logging
6
6
import os
7
7
import re
8
+ import hashlib
8
9
from dataclasses import dataclass , field
9
10
from functools import cached_property
10
11
from hashlib import sha256
@@ -94,6 +95,11 @@ def make_test_source_with_context(
94
95
return TestFile (file_name = file_name , source_dir = source_dir , dest_dir = dest_dir )
95
96
96
97
98
+ def tid_short_hash (tid : str ) -> str :
99
+ """Take the first 10 chars of the hash of the given `tid`."""
100
+ return hashlib .sha256 (tid .encode ()).hexdigest ()[:10 ]
101
+
102
+
97
103
@dataclass (frozen = True )
98
104
class TestCase :
99
105
"""A test case is an instance of test definition together with a set of Jinja variables used to render all
@@ -113,8 +119,11 @@ def tid(self) -> str:
113
119
"""Return the test id. Used as destination folder name for the generated test case.
114
120
The result is part of a full directory name of the test case. Therefore, the OS filesystem
115
121
directory separator is replaced with underscore.
122
+
123
+ Since the result is also used as a folder name, we restrict it's length to 255 characters.
124
+ This is because some filesystems complain if the name is longer that that.
116
125
"""
117
- return re .sub (
126
+ name = re .sub (
118
127
f"[{ os .sep } :]" ,
119
128
"_" ,
120
129
"_" .join (
@@ -124,6 +133,12 @@ def tid(self) -> str:
124
133
)
125
134
),
126
135
)
136
+ max_len = 255
137
+ if len (name ) > max_len :
138
+ name_hash = tid_short_hash (name )
139
+ return f"{ name [: max_len - len (name_hash ) - 1 ]} _{ name_hash } "
140
+ else :
141
+ return name
127
142
128
143
def expand (self , template_dir : str , target_dir : str , namespace : str ) -> None :
129
144
"""Expand test case This will create the target folder, copy files and render render templates."""
0 commit comments