Salome HOME
update resources
[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 std::string ParaMEDMEMTest::getResourceFile( const std::string& filename )
34 {
35   std::string resourceFile = "";
36
37   if ( getenv("MEDTOOL_ROOT_DIR") ) {
38     // use MEDTOOL_ROOT_DIR env.var
39     resourceFile = getenv("MEDTOOL_ROOT_DIR");
40     resourceFile += "/share/resources/med/";
41   }
42   else {
43     resourceFile = get_current_dir_name();
44     resourceFile += "/../../resources/";
45   }
46
47   resourceFile += filename;
48   return resourceFile;
49 }
50
51
52 //================================================================================
53 /*!
54  * \brief Returns writable temporary directory
55  * \return full path to the temporary directory
56  */
57 //================================================================================
58
59 std::string ParaMEDMEMTest::getTmpDirectory()
60 {
61   std::string path;
62
63   std::list<std::string> dirs;
64   if ( getenv("TMP") )    dirs.push_back( getenv("TMP" ));
65   if ( getenv("TMPDIR") ) dirs.push_back( getenv("TMPDIR" ));
66   dirs.push_back( "/tmp" );
67
68   std::string tmpd = "";
69   for ( std::list<std::string>::iterator dir = dirs.begin(); dir != dirs.end() && tmpd == "" ; ++dir ) {
70     if ( access( dir->data(), W_OK ) == 0 ) {
71       tmpd = dir->data();
72     }
73   }
74
75   if ( tmpd == "" )
76     throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
77
78   return tmpd;
79 }
80
81 //================================================================================
82 /*!
83  * \brief Creates a copy of source file (if source file is specified)
84  * in the temporary directory and returns a path to the tmp file
85  *
86  * \param tmpfile name of the temporary file (without path)
87  * \param srcfile source file
88  * \return path to the temporary file
89  */
90 //================================================================================
91 std::string ParaMEDMEMTest::makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
92 {
93   std::string tmpf = getTmpDirectory() + "/" + tmpfile;
94   if ( srcfile != "" ) {
95     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
96     system( cmd.c_str() );
97   }
98   return tmpf;
99 }
100
101
102 /*!
103  *  Tool to remove temporary files.
104  *  Allows automatique removal of temporary files in case of test failure.
105  */
106 ParaMEDMEMTest_TmpFilesRemover::~ParaMEDMEMTest_TmpFilesRemover()
107 {
108   std::set<std::string>::iterator it = myTmpFiles.begin();
109   for (; it != myTmpFiles.end(); it++) {
110     if (access((*it).data(), F_OK) == 0)
111       remove((*it).data());
112   }
113   myTmpFiles.clear();
114   //cout << "~ParaMEDMEMTest_TmpFilesRemover()" << endl;
115 }
116
117 bool ParaMEDMEMTest_TmpFilesRemover::Register(const std::string theTmpFile)
118 {
119   return (myTmpFiles.insert(theTmpFile)).second;
120 }