From 4450bbafc3100dcdc41c48618291ac83d846f0b2 Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Thu, 5 Nov 2009 19:14:37 -0800 Subject: [PATCH] pass explicit temp directory to test suites --- src/common/pcap-file-test-suite.cc | 10 ++--- src/core/test.cc | 63 ++++++++++++++++++++++++++++-- src/core/test.h | 22 +++++++++++ test.py | 14 ++++++- utils/test-runner.cc | 9 +++++ 5 files changed, 108 insertions(+), 10 deletions(-) diff --git a/src/common/pcap-file-test-suite.cc b/src/common/pcap-file-test-suite.cc index d71c19b31..e87b9bbe7 100644 --- a/src/common/pcap-file-test-suite.cc +++ b/src/common/pcap-file-test-suite.cc @@ -107,7 +107,7 @@ WriteModeCreateTestCase::DoSetup (void) std::stringstream filename; uint32_t n = rand (); filename << n; - m_testFilename = "/tmp/" + filename.str () + ".pcap"; + m_testFilename = GetTempDir () + filename.str () + ".pcap"; } void @@ -218,7 +218,7 @@ ReadModeCreateTestCase::DoSetup (void) std::stringstream filename; uint32_t n = rand (); filename << n; - m_testFilename = "/tmp/" + filename.str () + ".pcap"; + m_testFilename = GetTempDir () + filename.str () + ".pcap"; } void @@ -317,7 +317,7 @@ AppendModeCreateTestCase::DoSetup (void) std::stringstream filename; uint32_t n = rand (); filename << n; - m_testFilename = "/tmp/" + filename.str () + ".pcap"; + m_testFilename = GetTempDir () + filename.str () + ".pcap"; } void @@ -416,7 +416,7 @@ FileHeaderTestCase::DoSetup (void) std::stringstream filename; uint32_t n = rand (); filename << n; - m_testFilename = "/tmp/" + filename.str () + ".pcap"; + m_testFilename = GetTempDir () + filename.str () + ".pcap"; } void @@ -607,7 +607,7 @@ RecordHeaderTestCase::DoSetup (void) std::stringstream filename; uint32_t n = rand (); filename << n; - m_testFilename = "/tmp/" + filename.str () + ".pcap"; + m_testFilename = GetTempDir () + filename.str () + ".pcap"; } void diff --git a/src/core/test.cc b/src/core/test.cc index 77d833651..d9c46e45e 100644 --- a/src/core/test.cc +++ b/src/core/test.cc @@ -87,6 +87,7 @@ TestCase::TestCase (std::string name) m_continueOnFailure (false), m_detailsReported (false), m_basedir ("invalid"), + m_tempdir ("invalid"), m_ofs (0), m_error (false) { @@ -203,7 +204,14 @@ TestCase::GetName (void) void TestCase::SetBaseDir (std::string basedir) { - m_basedir = basedir; + if (basedir[basedir.length () - 1] != '/') + { + m_basedir = basedir + "/"; + } + else + { + m_basedir = basedir; + } } std::string @@ -212,13 +220,32 @@ TestCase::GetBaseDir (void) return m_basedir; } +void +TestCase::SetTempDir (std::string tempdir) +{ + if (tempdir[tempdir.length () - 1] != '/') + { + m_tempdir = tempdir + "/"; + } + else + { + m_tempdir = tempdir; + } +} + +std::string +TestCase::GetTempDir (void) +{ + return m_tempdir; +} + std::string TestCase::GetSourceDir (std::string file) { std::string::size_type relPathBegin = file.find_first_of ("/"); - NS_ABORT_MSG_IF (relPathBegin == std::string::npos, "TestCase::GetSrouceDir(): Internal Error"); + NS_ABORT_MSG_IF (relPathBegin == std::string::npos, "TestCase::GetSourceDir(): Internal Error"); std::string::size_type relPathEnd = file.find_last_of ("/"); - NS_ABORT_MSG_IF (relPathEnd == std::string::npos, "TestCase::GetSrouceDir(): Internal Error"); + NS_ABORT_MSG_IF (relPathEnd == std::string::npos, "TestCase::GetSourceDir(): Internal Error"); return GetBaseDir () + file.substr (relPathBegin, relPathEnd + 1 - relPathBegin); } @@ -353,6 +380,7 @@ TestSuite::TestSuite (std::string name, TestType type) : m_name (name), m_verbose (false), m_basedir ("invalid"), + m_tempdir ("invalid"), m_ofs (0), m_error (false), m_type (type) @@ -476,7 +504,14 @@ TestSuite::GetName (void) void TestSuite::SetBaseDir (std::string basedir) { - m_basedir = basedir; + if (basedir[basedir.length () - 1] != '/') + { + m_basedir = basedir + "/"; + } + else + { + m_basedir = basedir; + } } std::string @@ -485,6 +520,25 @@ TestSuite::GetBaseDir (void) return m_basedir; } +void +TestSuite::SetTempDir (std::string tempdir) +{ + if (tempdir[tempdir.length () - 1] != '/') + { + m_tempdir = tempdir + "/"; + } + else + { + m_tempdir = tempdir; + } +} + +std::string +TestSuite::GetTempDir (void) +{ + return m_tempdir; +} + void TestSuite::SetStream (std::ofstream *ofs) { @@ -589,6 +643,7 @@ TestSuite::DoRun (void) (*i)->SetVerbose (m_verbose); (*i)->SetContinueOnFailure (m_continueOnFailure); (*i)->SetBaseDir (m_basedir); + (*i)->SetTempDir (m_tempdir); (*i)->SetStream (m_ofs); // diff --git a/src/core/test.h b/src/core/test.h index 571578ff1..c19d023d8 100644 --- a/src/core/test.h +++ b/src/core/test.h @@ -662,6 +662,16 @@ public: */ std::string GetBaseDir (void); + /** + * \brief Set the temporary file directory (where to write temporary files). + */ + void SetTempDir (std::string dir); + + /** + * \brief Get the temporary file directory . + */ + std::string GetTempDir (void); + /** * \brief Get the source directory of the current source file. * @@ -830,6 +840,7 @@ private: bool m_continueOnFailure; bool m_detailsReported; std::string m_basedir; + std::string m_tempdir; std::ofstream *m_ofs; bool m_error; }; @@ -944,6 +955,16 @@ public: */ std::string GetBaseDir (void); + /** + * \brief Set the temporary file directory (where to write temporary files). + */ + void SetTempDir (std::string dir); + + /** + * \brief Get the temporary file directory. + */ + std::string GetTempDir (void); + /** * \brief Set the stream to which status and result messages will be written. * @@ -1064,6 +1085,7 @@ private: bool m_verbose; bool m_continueOnFailure; std::string m_basedir; + std::string m_tempdir; std::ofstream *m_ofs; bool m_error; TestType m_type; diff --git a/test.py b/test.py index 6a029e0b8..4804b6e64 100755 --- a/test.py +++ b/test.py @@ -616,6 +616,7 @@ class Job: self.shell_command = "" self.display_name = "" self.basedir = "" + self.tempdir = "" self.cwd = "" self.tmp_file_name = "" self.returncode = False @@ -673,6 +674,13 @@ class Job: def set_basedir(self, basedir): self.basedir = basedir + # + # This is the directory to which a running test suite should write any + # temporary files. + # + def set_tempdir(self, tempdir): + self.tempdir = tempdir + # # This is the current working directory that will be given to an executing # test as it is being run. It will be used for examples to tell them where @@ -766,7 +774,8 @@ class worker_thread(threading.Thread): # file name # (job.returncode, standard_out, standard_err, et) = run_job_synchronously(job.shell_command + - " --basedir=%s --out=%s" % (job.basedir, job.tmp_file_name), job.cwd, options.valgrind) + " --basedir=%s --tempdir=%s --out=%s" % (job.basedir, job.tempdir, job.tmp_file_name), + job.cwd, options.valgrind) job.set_elapsed_time(et) @@ -1004,6 +1013,7 @@ def run_tests(): job.set_tmp_file_name(os.path.join(testpy_output_dir, "%s.xml" % test)) job.set_cwd(os.getcwd()) job.set_basedir(os.getcwd()) + job.set_tempdir(testpy_output_dir) if (options.multiple): multiple = " --multiple" else: @@ -1074,6 +1084,7 @@ def run_tests(): job.set_tmp_file_name("") job.set_cwd(testpy_output_dir) job.set_basedir(os.getcwd()) + job.set_tempdir(testpy_output_dir) job.set_shell_command("examples/%s" % test) if options.valgrind and not eval(do_valgrind_run): @@ -1097,6 +1108,7 @@ def run_tests(): job.set_tmp_file_name("") job.set_cwd(testpy_output_dir) job.set_basedir(os.getcwd()) + job.set_tempdir(testpy_output_dir) job.set_shell_command("examples/%s" % options.example) if options.verbose: diff --git a/utils/test-runner.cc b/utils/test-runner.cc index 0c6973378..3c0d4da28 100644 --- a/utils/test-runner.cc +++ b/utils/test-runner.cc @@ -40,11 +40,13 @@ main (int argc, char *argv[]) bool doKinds = false; bool haveBasedir = false; + bool haveTempdir = false; bool haveOutfile = false; bool haveType = false; std::string suiteName; std::string basedir; + std::string tempdir; std::string outfileName; std::string typeName; @@ -96,6 +98,12 @@ main (int argc, char *argv[]) doSuite = true; } + if (arg.find ("--tempdir=") != std::string::npos) + { + tempdir = arg.substr (arg.find_first_of ("=") + 1, 9999); + haveTempdir = true; + } + if (arg.compare ("--verbose") == 0) { doVerbose = true; @@ -247,6 +255,7 @@ main (int argc, char *argv[]) if (doSuite == false || (doSuite == true && suiteName == testSuite->GetName ())) { testSuite->SetBaseDir (basedir); + testSuite->SetTempDir (tempdir); testSuite->SetStream (pofs); testSuite->SetVerbose (doVerbose); testSuite->SetContinueOnFailure (doMultiple);