diff --git a/libtransmission/libtransmission-test.c b/libtransmission/libtransmission-test.c index 2890df786..e8218342e 100644 --- a/libtransmission/libtransmission-test.c +++ b/libtransmission/libtransmission-test.c @@ -122,10 +122,9 @@ runTests (const testFunc * const tests, int numTests) #include "variant.h" -tr_session * session = NULL; -char * sandbox = NULL; -char * downloadDir = NULL; -char * blocklistDir = NULL; +/*** +**** +***/ static char* tr_getcwd (void) @@ -148,6 +147,15 @@ tr_getcwd (void) return tr_strdup (buf); } +char * +libtest_sandbox_create (void) +{ + const char * path = tr_getcwd (); + char * sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL); + tr_mkdtemp (sandbox); + return sandbox; +} + static void rm_rf (const char * killme) { @@ -179,6 +187,16 @@ rm_rf (const char * killme) } } +void +libtest_sandbox_destroy (const char * sandbox) +{ + rm_rf (sandbox); +} + +/*** +**** +***/ + #define MEM_K 1024 #define MEM_B_STR "B" #define MEM_K_STR "KiB" @@ -217,10 +235,7 @@ libttest_session_init (tr_variant * settings) if (settings == NULL) settings = &local_settings; - path = tr_getcwd (); - sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL); - tr_mkdtemp (sandbox); - tr_free (path); + sandbox = libtest_sandbox_create (); if (!formatters_inited) { @@ -275,15 +290,15 @@ libttest_session_init (tr_variant * settings) void libttest_session_close (tr_session * session) { - char * path; + char * sandbox; - path = tr_strdup (tr_sessionGetConfigDir (session)); + sandbox = tr_strdup (tr_sessionGetConfigDir (session)); tr_sessionClose (session); tr_logFreeQueue (tr_logGetQueue ()); session = NULL; - rm_rf (path); - tr_free (path); + libtest_sandbox_destroy (sandbox); + tr_free (sandbox); } /*** @@ -415,3 +430,27 @@ libttest_blockingTorrentVerify (tr_torrent * tor) while (!done) tr_wait_msec (10); } + +void +libtest_create_file_with_contents (const char * path, const char * str) +{ + FILE * fp; + char * dir; + const int tmperr = errno; + + dir = tr_dirname (path); + errno = 0; + tr_mkdirp (dir, 0700); + assert (errno == 0); + tr_free (dir); + + tr_remove (path); + fp = fopen (path, "wb"); + fprintf (fp, "%s", str); + fclose (fp); + + sync (); + + errno = tmperr; +} + diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h index d82c50593..70c21073c 100644 --- a/libtransmission/libtransmission-test.h +++ b/libtransmission/libtransmission-test.h @@ -83,6 +83,13 @@ tr_torrent * libttest_zero_torrent_init (tr_session * session); void libttest_blockingTorrentVerify (tr_torrent * tor); +void libtest_create_file_with_contents (const char * path, const char * str); + +char* libtest_sandbox_create (void); +void libtest_sandbox_destroy (const char * sandbox); + + + #endif /* !LIBTRANSMISSION_TEST_H */ diff --git a/libtransmission/rename-test.c b/libtransmission/rename-test.c index 96fbd5b0c..14a38aa3b 100644 --- a/libtransmission/rename-test.c +++ b/libtransmission/rename-test.c @@ -92,34 +92,11 @@ torrentRenameAndWait (tr_torrent * tor, **** ***/ -static void -create_file_with_contents (const char * path, const char * str) -{ - FILE * fp; - char * dir; - const int tmperr = errno; - - dir = tr_dirname (path); - errno = 0; - tr_mkdirp (dir, 0700); - assert (errno == 0); - tr_free (dir); - - tr_remove (path); - fp = fopen (path, "wb"); - fprintf (fp, "%s", str); - fclose (fp); - - sync (); - - errno = tmperr; -} - static void create_single_file_torrent_contents (const char * top) { char * path = tr_buildPath (top, "hello-world.txt", NULL); - create_file_with_contents (path, "hello, world!\n"); + libtest_create_file_with_contents (path, "hello, world!\n"); tr_free (path); } @@ -264,19 +241,19 @@ create_multifile_torrent_contents (const char * top) char * path; path = tr_buildPath (top, "Felidae", "Felinae", "Acinonyx", "Cheetah", "Chester", NULL); - create_file_with_contents (path, "It ain't easy bein' cheesy.\n"); + libtest_create_file_with_contents (path, "It ain't easy bein' cheesy.\n"); tr_free (path); path = tr_buildPath (top, "Felidae", "Pantherinae", "Panthera", "Tiger", "Tony", NULL); - create_file_with_contents (path, "They’re Grrrrreat!\n"); + libtest_create_file_with_contents (path, "They’re Grrrrreat!\n"); tr_free (path); path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Kyphi", NULL); - create_file_with_contents (path, "Inquisitive\n"); + libtest_create_file_with_contents (path, "Inquisitive\n"); tr_free (path); path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Saffron", NULL); - create_file_with_contents (path, "Tough\n"); + libtest_create_file_with_contents (path, "Tough\n"); tr_free (path); sync ();