backport test framework directory creation fix (#1707)
backport test framework directory creation fix
This commit is contained in:
parent
1300204ff1
commit
b922357c48
|
@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#include "libtorrent/assert.hpp"
|
||||
#include "libtorrent/file.hpp"
|
||||
#include "libtorrent/aux_/escape_string.hpp"
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef WIN32
|
||||
|
@ -222,6 +223,53 @@ void print_usage(char const* executable)
|
|||
"by -l. If no test is specified, all tests are run\n", executable);
|
||||
}
|
||||
|
||||
void change_directory(std::string const& f, error_code& ec)
|
||||
{
|
||||
ec.clear();
|
||||
|
||||
#ifdef TORRENT_WINDOWS
|
||||
#if TORRENT_USE_WSTRING
|
||||
#define SetCurrentDirectory_ SetCurrentDirectoryW
|
||||
std::wstring n = convert_to_wstring(f);
|
||||
#else
|
||||
#define SetCurrentDirectory_ SetCurrentDirectoryA
|
||||
std::string const& n = convert_to_native(f);
|
||||
#endif // TORRENT_USE_WSTRING
|
||||
|
||||
if (SetCurrentDirectory_(n.c_str()) == 0)
|
||||
ec.assign(GetLastError(), system_category());
|
||||
#else
|
||||
std::string n = convert_to_native(f);
|
||||
int ret = ::chdir(n.c_str());
|
||||
if (ret != 0)
|
||||
ec.assign(errno, system_category());
|
||||
#endif
|
||||
}
|
||||
|
||||
struct unit_directory_guard
|
||||
{
|
||||
std::string dir;
|
||||
unit_directory_guard(std::string const& d) : dir(d) {}
|
||||
~unit_directory_guard()
|
||||
{
|
||||
error_code ec;
|
||||
std::string parent_dir = parent_path(dir);
|
||||
change_directory(parent_dir, ec); // windows will not allow to remove current dir, so let's change it to root
|
||||
if (ec)
|
||||
{
|
||||
TEST_ERROR("Failed to change directory: " + ec.message());
|
||||
return;
|
||||
}
|
||||
if (!keep_files)
|
||||
{
|
||||
error_code ec;
|
||||
remove_all(dir, ec);
|
||||
if (ec)
|
||||
TEST_ERROR("Failed to remove unit test directory: " + ec.message());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
EXPORT int main(int argc, char const* argv[])
|
||||
{
|
||||
char const* executable = argv[0];
|
||||
|
@ -321,24 +369,11 @@ EXPORT int main(int argc, char const* argv[])
|
|||
#else
|
||||
process_id = getpid();
|
||||
#endif
|
||||
std::string root_dir = current_working_directory();
|
||||
char dir[40];
|
||||
snprintf(dir, sizeof(dir), "test_tmp_%u", process_id);
|
||||
std::string test_dir = complete(dir);
|
||||
error_code ec;
|
||||
create_directory(test_dir, ec);
|
||||
if (ec)
|
||||
{
|
||||
fprintf(stderr, "Failed to create test directory: %s\n", ec.message().c_str());
|
||||
return 1;
|
||||
}
|
||||
#ifdef TORRENT_WINDOWS
|
||||
SetCurrentDirectoryA(dir);
|
||||
#else
|
||||
chdir(dir);
|
||||
#endif
|
||||
fprintf(stderr, "cwd = \"%s\"\n", test_dir.c_str());
|
||||
|
||||
int total_failures = 0;
|
||||
std::string unit_dir_prefix = combine_path(root_dir, dir);
|
||||
std::printf("cwd_prefix = \"%s\"\n", unit_dir_prefix.c_str());
|
||||
|
||||
if (_g_num_unit_tests == 0)
|
||||
{
|
||||
|
@ -355,6 +390,25 @@ EXPORT int main(int argc, char const* argv[])
|
|||
if (filter && tests_to_run.count(_g_unit_tests[i].name) == 0)
|
||||
continue;
|
||||
|
||||
std::string unit_dir = unit_dir_prefix;
|
||||
char i_str[40];
|
||||
snprintf(i_str, sizeof(i_str), "%u", i);
|
||||
unit_dir.append(i_str);
|
||||
error_code ec;
|
||||
create_directory(unit_dir, ec);
|
||||
if (ec)
|
||||
{
|
||||
std::printf("Failed to create unit test directory: %s\n", ec.message().c_str());
|
||||
return 1;
|
||||
}
|
||||
unit_directory_guard unit_dir_guard(unit_dir);
|
||||
change_directory(unit_dir, ec);
|
||||
if (ec)
|
||||
{
|
||||
std::printf("Failed to change unit test directory: %s\n", ec.message().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
unit_test_t& t = _g_unit_tests[i];
|
||||
|
||||
if (redirect_stdout)
|
||||
|
@ -422,7 +476,6 @@ EXPORT int main(int argc, char const* argv[])
|
|||
|
||||
t.num_failures = _g_test_failures;
|
||||
t.run = true;
|
||||
total_failures += _g_test_failures;
|
||||
++num_run;
|
||||
|
||||
if (redirect_stdout && t.output)
|
||||
|
@ -459,16 +512,8 @@ EXPORT int main(int argc, char const* argv[])
|
|||
if (redirect_stdout) fflush(stdout);
|
||||
if (redirect_stderr) fflush(stderr);
|
||||
|
||||
int ret = print_failures();
|
||||
#if !defined TORRENT_LOGGING
|
||||
if (ret == 0 && !keep_files)
|
||||
{
|
||||
remove_all(test_dir, ec);
|
||||
if (ec)
|
||||
fprintf(stderr, "failed to remove test dir: %s\n", ec.message().c_str());
|
||||
}
|
||||
#endif
|
||||
int total_num_failures = print_failures();
|
||||
|
||||
return total_failures ? 333 : 0;
|
||||
return total_num_failures ? 333 : 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
unit_test_t _g_unit_tests[1024];
|
||||
int _g_num_unit_tests = 0;
|
||||
int _g_test_failures = 0;
|
||||
int _g_test_failures = 0; // flushed at start of every unit
|
||||
int _g_test_idx = 0;
|
||||
|
||||
static std::vector<std::string> failure_strings;
|
||||
|
@ -65,6 +65,7 @@ int print_failures()
|
|||
}
|
||||
|
||||
fprintf(stderr, "\n\n");
|
||||
int total_num_failures = 0;
|
||||
|
||||
for (int i = 0; i < _g_num_unit_tests; ++i)
|
||||
{
|
||||
|
@ -77,6 +78,7 @@ int print_failures()
|
|||
}
|
||||
else
|
||||
{
|
||||
total_num_failures += _g_unit_tests[i].num_failures;
|
||||
fprintf(stderr, "\x1b[31m[%-*s] %d FAILURES\n"
|
||||
, longest_name
|
||||
, _g_unit_tests[i].name
|
||||
|
@ -86,8 +88,8 @@ int print_failures()
|
|||
|
||||
fprintf(stderr, "\x1b[0m");
|
||||
|
||||
if (_g_test_failures > 0)
|
||||
if (total_num_failures > 0)
|
||||
fprintf(stderr, "\n\n\x1b[41m == %d TEST(S) FAILED ==\x1b[0m\n\n\n", _g_test_failures);
|
||||
return _g_test_failures;
|
||||
return total_num_failures;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue