Salome HOME
Implementation of equivalences in MEDFileMesh.
[tools/medcoupling.git] / src / ParaMEDMEMTest / ParaMEDMEMTest.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ParaMEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include <sstream>
24 #include <cmath>
25 #include <list>
26 #include <stdexcept>
27 #include <stdlib.h>
28
29 #ifndef WIN32
30 #include <unistd.h>
31 #endif
32
33 //================================================================================
34 /*!
35  * \brief Get path to the resources file.
36  *
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.
39  * 
40  * \param filename name of the resource file (should not include a path)
41  * \return full path to the resource file
42  */
43 //================================================================================
44
45 std::string ParaMEDMEMTest::getResourceFile( const std::string& filename )
46 {
47   std::string resourceFile = "";
48
49   if ( getenv("top_srcdir") ) {
50     // we are in 'make test' step
51     resourceFile = getenv("top_srcdir");
52     resourceFile += "/resources/";
53   }
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/";
58   }
59   resourceFile += filename;
60   return resourceFile;
61 }
62
63
64 //================================================================================
65 /*!
66  * \brief Returns writable temporary directory
67  * \return full path to the temporary directory
68  */
69 //================================================================================
70
71 std::string ParaMEDMEMTest::getTmpDirectory()
72 {
73   std::string path;
74
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" );
79
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 ) {
83       tmpd = dir->data();
84     }
85   }
86
87   if ( tmpd == "" )
88     throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
89
90   return tmpd;
91 }
92
93 //================================================================================
94 /*!
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
97  *
98  * \param tmpfile name of the temporary file (without path)
99  * \param srcfile source file
100  * \return path to the temporary file
101  */
102 //================================================================================
103 std::string ParaMEDMEMTest::makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
104 {
105   std::string tmpf = getTmpDirectory() + "/" + tmpfile;
106   if ( srcfile != "" ) {
107     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
108     system( cmd.c_str() );
109   }
110   return tmpf;
111 }
112
113
114 /*!
115  *  Tool to remove temporary files.
116  *  Allows automatique removal of temporary files in case of test failure.
117  */
118 ParaMEDMEMTest_TmpFilesRemover::~ParaMEDMEMTest_TmpFilesRemover()
119 {
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());
124   }
125   myTmpFiles.clear();
126   //cout << "~ParaMEDMEMTest_TmpFilesRemover()" << endl;
127 }
128
129 bool ParaMEDMEMTest_TmpFilesRemover::Register(const std::string theTmpFile)
130 {
131   return (myTmpFiles.insert(theTmpFile)).second;
132 }