Skip to content

Commit 18c6e26

Browse files
committed
Using test names as sandbox dirs is now optional
Given that test names can be long _and_ some platforms insist upon a short maximum path length (eg, Windows), we may not want to indiscriminately use path names as the sandbox directory name. However, this has high utility in some situations, so allow it as an opt-in. By default, we'll just use an 8 hexadigit "random" string. (We'll use the rand(3) mechanism, which is not be particularly random, but since the sandbox directories are sequestered in a unique tempdir, a clar process need only attempt to avoid collisions between itself, not with other processes.)
1 parent 13931e5 commit 18c6e26

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Diff for: clar/sandbox.h

+38-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ static void clar_tempdir_init(void)
187187
if (chdir(_clar_tempdir) != 0)
188188
clar_abort("Failed to change into tempdir '%s': %s.\n",
189189
_clar_tempdir, strerror(errno));
190+
191+
#if !defined(CLAR_SANDBOX_TEST_NAMES) && defined(_WIN32)
192+
srand(clock() ^ (unsigned int)time(NULL) ^ GetCurrentProcessId() ^ GetCurrentThreadId());
193+
#elif !defined(CLAR_SANDBOX_TEST_NAMES)
194+
srand(clock() ^ time(NULL) ^ (getpid() << 16));
195+
#endif
190196
}
191197

192198
static void append(char *dst, const char *src)
@@ -208,9 +214,20 @@ static void append(char *dst, const char *src)
208214

209215
static int clar_sandbox_create(const char *suite_name, const char *test_name)
210216
{
217+
#ifndef CLAR_SANDBOX_TEST_NAMES
218+
char alpha[] = "0123456789abcdef";
219+
int num = rand();
220+
#endif
221+
211222
cl_assert(_clar_sandbox[0] == '\0');
212223

213-
cl_assert(strlen(_clar_tempdir) + strlen(suite_name) + strlen(test_name) + 2 < CLAR_MAX_PATH);
224+
/*
225+
* We may want to use test names as sandbox directory names for
226+
* readability, _however_ on platforms with restrictions for short
227+
* file / folder names (eg, Windows), this may be too long.
228+
*/
229+
#ifdef CLAR_SANDBOX_TEST_NAMES
230+
cl_assert(strlen(_clar_tempdir) + strlen(suite_name) + strlen(test_name) + 3 < CLAR_MAX_PATH);
214231

215232
strcpy(_clar_sandbox, _clar_tempdir);
216233
_clar_sandbox[_clar_tempdir_len] = '/';
@@ -219,6 +236,26 @@ static int clar_sandbox_create(const char *suite_name, const char *test_name)
219236
append(_clar_sandbox, suite_name);
220237
append(_clar_sandbox, "__");
221238
append(_clar_sandbox, test_name);
239+
#else
240+
((void)suite_name);
241+
((void)test_name);
242+
((void)append);
243+
244+
cl_assert(strlen(_clar_tempdir) + 9 < CLAR_MAX_PATH);
245+
246+
strcpy(_clar_sandbox, _clar_tempdir);
247+
_clar_sandbox[_clar_tempdir_len] = '/';
248+
249+
_clar_sandbox[_clar_tempdir_len + 1] = alpha[(num & 0xf0000000) >> 28];
250+
_clar_sandbox[_clar_tempdir_len + 2] = alpha[(num & 0x0f000000) >> 24];
251+
_clar_sandbox[_clar_tempdir_len + 3] = alpha[(num & 0x00f00000) >> 20];
252+
_clar_sandbox[_clar_tempdir_len + 4] = alpha[(num & 0x000f0000) >> 16];
253+
_clar_sandbox[_clar_tempdir_len + 5] = alpha[(num & 0x0000f000) >> 12];
254+
_clar_sandbox[_clar_tempdir_len + 6] = alpha[(num & 0x00000f00) >> 8];
255+
_clar_sandbox[_clar_tempdir_len + 7] = alpha[(num & 0x000000f0) >> 4];
256+
_clar_sandbox[_clar_tempdir_len + 8] = alpha[(num & 0x0000000f) >> 0];
257+
_clar_sandbox[_clar_tempdir_len + 9] = '\0';
258+
#endif
222259

223260
if (mkdir(_clar_sandbox, 0700) != 0)
224261
return -1;

0 commit comments

Comments
 (0)