1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ParaMEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
33 //================================================================================
35 * \brief Get path to the resources file.
37 * When running 'make test' source file is taken from MED_SRC/resources folder.
38 * Otherwise, file is searched in ${MED_ROOT_DIR}/share/salome/resources/med folder.
40 * \param filename name of the resource file (should not include a path)
41 * \return full path to the resource file
43 //================================================================================
45 std::string ParaMEDMEMTest::getResourceFile( const std::string& filename )
47 std::string resourceFile = "";
49 if ( getenv("top_srcdir") ) {
50 // we are in 'make test' step
51 resourceFile = getenv("top_srcdir");
52 resourceFile += "/resources/";
54 else if ( getenv("MED_ROOT_DIR") ) {
55 // use MED_ROOT_DIR env.var
56 resourceFile = getenv("MED_ROOT_DIR");
57 resourceFile += "/share/salome/resources/med/";
59 resourceFile += filename;
64 //================================================================================
66 * \brief Returns writable temporary directory
67 * \return full path to the temporary directory
69 //================================================================================
71 std::string ParaMEDMEMTest::getTmpDirectory()
75 std::list<std::string> dirs;
76 if ( getenv("TMP") ) dirs.push_back( getenv("TMP" ));
77 if ( getenv("TMPDIR") ) dirs.push_back( getenv("TMPDIR" ));
78 dirs.push_back( "/tmp" );
80 std::string tmpd = "";
81 for ( std::list<std::string>::iterator dir = dirs.begin(); dir != dirs.end() && tmpd == "" ; ++dir ) {
82 if ( access( dir->data(), W_OK ) == 0 ) {
88 throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
93 //================================================================================
95 * \brief Creates a copy of source file (if source file is specified)
96 * in the temporary directory and returns a path to the tmp file
98 * \param tmpfile name of the temporary file (without path)
99 * \param srcfile source file
100 * \return path to the temporary file
102 //================================================================================
103 std::string ParaMEDMEMTest::makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
105 std::string tmpf = getTmpDirectory() + "/" + tmpfile;
106 if ( srcfile != "" ) {
107 std::string cmd = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
108 system( cmd.c_str() );
115 * Tool to remove temporary files.
116 * Allows automatique removal of temporary files in case of test failure.
118 ParaMEDMEMTest_TmpFilesRemover::~ParaMEDMEMTest_TmpFilesRemover()
120 std::set<std::string>::iterator it = myTmpFiles.begin();
121 for (; it != myTmpFiles.end(); it++) {
122 if (access((*it).data(), F_OK) == 0)
123 remove((*it).data());
126 //cout << "~ParaMEDMEMTest_TmpFilesRemover()" << endl;
129 bool ParaMEDMEMTest_TmpFilesRemover::Register(const std::string theTmpFile)
131 return (myTmpFiles.insert(theTmpFile)).second;