Skip to content
Snippets Groups Projects
Commit efc11d88 authored by Nick Draper's avatar Nick Draper
Browse files

Use setup and teardown to create/remove temp directories

re #18885
parent 2bd97ade
No related branches found
No related tags found
No related merge requests found
...@@ -100,37 +100,86 @@ public: ...@@ -100,37 +100,86 @@ public:
} }
static void destroySuite(DownloadInstrumentTest *suite) { delete suite; } static void destroySuite(DownloadInstrumentTest *suite) { delete suite; }
void createDirectory(Poco::Path path)
{
Poco::File file(path);
if (file.createDirectory())
{
m_directoriesToRemove.push_back(file);
}
}
void removeDirectories()
{
for (auto directory : m_directoriesToRemove)
{
try {
directory.remove(true);
} catch (Poco::FileException &fe) {
std::cout << fe.what() << std::endl;
}
}
m_directoriesToRemove.clear();
}
void setUp() override {
const std::string TEST_SUFFIX = "TEMPORARY_unitTest";
m_originalInstDir =
Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
// change the local download directory by adding a unittest subdirectory
auto testDirectories = m_originalInstDir;
Poco::Path localDownloadPath(m_originalInstDir[0]);
localDownloadPath.pushDirectory(TEST_SUFFIX);
m_localInstDir = localDownloadPath.toString();
createDirectory(localDownloadPath);
testDirectories[0] = m_localInstDir;
// also if you move the instrument directory to one with less files then it
// will run faster as it does not need to checksum as many files
try {
Poco::Path installInstrumentPath(testDirectories.back());
installInstrumentPath.pushDirectory(TEST_SUFFIX);
createDirectory(installInstrumentPath);
testDirectories.back() = installInstrumentPath.toString();
} catch (Poco::FileException &) {
std::cout << "Failed to change instrument directory continuing without, fine, just slower\n";
}
Mantid::Kernel::ConfigService::Instance().setInstrumentDirectories(testDirectories);
auto test =
Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
}
void tearDown() override {
Mantid::Kernel::ConfigService::Instance().setInstrumentDirectories(m_originalInstDir);
removeDirectories();
}
void test_Init() { void test_Init() {
MockedDownloadInstrument alg; MockedDownloadInstrument alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize()) TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized()) TS_ASSERT(alg.isInitialized())
} }
// These tests create some files, but they entire directories are created and
// removed in setup and teardown
void test_exec() { void test_exec() {
std::string localInstDir =
Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
cleanupDiretory(localInstDir);
TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.", TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.",
runDownloadInstrument(), 2); runDownloadInstrument(), 2);
cleanupDiretory(localInstDir);
} }
void test_execOrphanedFile() { void test_execOrphanedFile() {
std::string localInstDir =
Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
cleanupDiretory(localInstDir);
// add an orphaned file // add an orphaned file
Poco::Path orphanedFilePath(localInstDir); Poco::Path orphanedFilePath(m_localInstDir);
orphanedFilePath.makeDirectory(); orphanedFilePath.makeDirectory();
orphanedFilePath.setFileName("Orphaned_Should_not_be_here.xml"); orphanedFilePath.setFileName("Orphaned_Should_not_be_here.xml");
std::ofstream file; std::ofstream file;
file.open(orphanedFilePath.toString().c_str()); file.open(orphanedFilePath.toString().c_str());
file.close(); file.close();
TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.", TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.",
runDownloadInstrument(), 2); runDownloadInstrument(), 2);
...@@ -138,8 +187,6 @@ public: ...@@ -138,8 +187,6 @@ public:
TSM_ASSERT("The orphaned file was not deleted", TSM_ASSERT("The orphaned file was not deleted",
orphanedFile.exists() == false); orphanedFile.exists() == false);
deleteFile(orphanedFilePath.toString());
cleanupDiretory(localInstDir);
} }
int runDownloadInstrument() { int runDownloadInstrument() {
...@@ -155,23 +202,9 @@ public: ...@@ -155,23 +202,9 @@ public:
return alg.getProperty("FileDownloadCount"); return alg.getProperty("FileDownloadCount");
} }
void cleanupDiretory(std::string dir) { std::string m_localInstDir;
Poco::Path path(dir); std::vector<std::string> m_originalInstDir;
path.makeDirectory(); std::vector<Poco::File> m_directoriesToRemove;
deleteFile(path.setFileName("github.json").toString());
deleteFile(path.setFileName("NewFile.xml").toString());
deleteFile(path.setFileName("UpdatableFile.xml").toString());
}
bool deleteFile(std::string filePath) {
Poco::File file(filePath);
if (file.exists()) {
file.remove();
return true;
} else {
return false;
}
}
}; };
#endif /* MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_ */ #endif /* MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_ */
...@@ -207,6 +207,8 @@ public: ...@@ -207,6 +207,8 @@ public:
void appendDataSearchSubDir(const std::string &subdir); void appendDataSearchSubDir(const std::string &subdir);
/// Get the list of user search paths /// Get the list of user search paths
const std::vector<std::string> &getUserSearchDirs() const; const std::vector<std::string> &getUserSearchDirs() const;
/// Sets instrument directories
void setInstrumentDirectories(const std::vector<std::string>& directories);
/// Get instrument search directory /// Get instrument search directory
const std::vector<std::string> &getInstrumentDirectories() const; const std::vector<std::string> &getInstrumentDirectories() const;
/// Get instrument search directory /// Get instrument search directory
......
...@@ -1621,6 +1621,15 @@ const std::vector<std::string> &ConfigServiceImpl::getUserSearchDirs() const { ...@@ -1621,6 +1621,15 @@ const std::vector<std::string> &ConfigServiceImpl::getUserSearchDirs() const {
return m_UserSearchDirs; return m_UserSearchDirs;
} }
/**
* Sets the search directories for XML instrument definition files (IDFs)
* @params directories An ordered list of paths for instrument searching
*/
void ConfigServiceImpl::setInstrumentDirectories(
const std::vector<std::string> &directories) {
m_InstrumentDirs = directories;
}
/** /**
* Return the search directories for XML instrument definition files (IDFs) * Return the search directories for XML instrument definition files (IDFs)
* @returns An ordered list of paths for instrument searching * @returns An ordered list of paths for instrument searching
......
...@@ -358,6 +358,25 @@ public: ...@@ -358,6 +358,25 @@ public:
} }
} }
void TestSetInstrumentDirectory() {
auto originalDirectories = ConfigService::Instance().getInstrumentDirectories();
std::vector<std::string> testDirectories;
testDirectories.push_back("Test Directory 1");
testDirectories.push_back("Test Directory 2");
ConfigService::Instance().setInstrumentDirectories(testDirectories);
auto readDirectories = ConfigService::Instance().getInstrumentDirectories();
TS_ASSERT_EQUALS(readDirectories.size(), testDirectories.size());
TS_ASSERT_EQUALS(readDirectories[0], testDirectories[0]);
TS_ASSERT_EQUALS(readDirectories[1], testDirectories[1]);
// Restore original settings
ConfigService::Instance().setInstrumentDirectories(originalDirectories);
readDirectories = ConfigService::Instance().getInstrumentDirectories();
TS_ASSERT_EQUALS(readDirectories.size(), originalDirectories.size());
TS_ASSERT_EQUALS(readDirectories[0], originalDirectories[0]);
}
void TestCustomProperty() { void TestCustomProperty() {
std::string countString = std::string countString =
ConfigService::Instance().getString("algorithms.retained"); ConfigService::Instance().getString("algorithms.retained");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment