Salome HOME
829ee8654e37d72f29b58429df867d56871963e2
[tools/medcoupling.git] / src / ParaMEDMEMTest / ParaMEDMEMTest.cxx
1 // Copyright (C) 2007-2016  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 "TestInterpKernelUtils.hxx"
22 #include <cppunit/TestAssert.h>
23
24 #include <sstream>
25 #include <cmath>
26 #include <list>
27 #include <stdexcept>
28 #include <stdlib.h>
29
30 #ifndef WIN32
31 #include <unistd.h>
32 #endif
33
34
35
36 //================================================================================
37 /*!
38  * \brief Returns writable temporary directory
39  * \return full path to the temporary directory
40  */
41 //================================================================================
42
43 std::string ParaMEDMEMTest::getTmpDirectory()
44 {
45   std::string path;
46
47   std::list<std::string> dirs;
48   if ( getenv("TMP") )    dirs.push_back( getenv("TMP" ));
49   if ( getenv("TMPDIR") ) dirs.push_back( getenv("TMPDIR" ));
50   dirs.push_back( "/tmp" );
51
52   std::string tmpd = "";
53   for ( std::list<std::string>::iterator dir = dirs.begin(); dir != dirs.end() && tmpd == "" ; ++dir ) {
54     if ( access( dir->data(), W_OK ) == 0 ) {
55       tmpd = dir->data();
56     }
57   }
58
59   if ( tmpd == "" )
60     throw std::runtime_error("Can't find writable temporary directory. Set TMP environment variable");
61
62   return tmpd;
63 }
64
65 //================================================================================
66 /*!
67  * \brief Creates a copy of source file (if source file is specified)
68  * in the temporary directory and returns a path to the tmp file
69  *
70  * \param tmpfile name of the temporary file (without path)
71  * \param srcfile source file
72  * \return path to the temporary file
73  */
74 //================================================================================
75 std::string ParaMEDMEMTest::makeTmpFile( const std::string& tmpfile, const std::string& srcfile )
76 {
77   std::string tmpf = getTmpDirectory() + "/" + tmpfile;
78   if ( srcfile != "" ) {
79     std::string cmd  = "cp " + srcfile + " " + tmpf + " ; chmod +w " + tmpf;
80     system( cmd.c_str() );
81   }
82   return tmpf;
83 }
84
85
86 /*!
87  *  Tool to remove temporary files.
88  *  Allows automatique removal of temporary files in case of test failure.
89  */
90 ParaMEDMEMTest_TmpFilesRemover::~ParaMEDMEMTest_TmpFilesRemover()
91 {
92   std::set<std::string>::iterator it = myTmpFiles.begin();
93   for (; it != myTmpFiles.end(); it++) {
94     if (access((*it).data(), F_OK) == 0)
95       remove((*it).data());
96   }
97   myTmpFiles.clear();
98   //cout << "~ParaMEDMEMTest_TmpFilesRemover()" << endl;
99 }
100
101 bool ParaMEDMEMTest_TmpFilesRemover::Register(const std::string theTmpFile)
102 {
103   return (myTmpFiles.insert(theTmpFile)).second;
104 }