LCOV - code coverage report
Current view: top level - dfmodules/unittest - HDF5FileUtils_test.cxx (source / functions) Coverage Total Hit
Test: code.result Lines: 100.0 % 35 35
Test Date: 2025-12-21 13:07:08 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  * @file HDF5FileUtils_test.cxx Test application that tests and demonstrates
       3              :  * the functionality of the HDF5FileUtils class.
       4              :  *
       5              :  * This is part of the DUNE DAQ Application Framework, copyright 2020.
       6              :  * Licensing/copyright details are in the COPYING file that you should have
       7              :  * received with this code.
       8              :  */
       9              : 
      10              : #include "HDF5FileUtils.hpp"
      11              : 
      12              : #define BOOST_TEST_MODULE HDF5FileUtils_test // NOLINT
      13              : 
      14              : #include "boost/test/unit_test.hpp"
      15              : 
      16              : #include <filesystem>
      17              : #include <fstream>
      18              : #include <iostream>
      19              : #include <regex>
      20              : #include <string>
      21              : #include <vector>
      22              : 
      23              : using namespace dunedaq::dfmodules;
      24              : 
      25              : std::vector<std::string>
      26            2 : delete_files_matching_pattern(const std::string& path, const std::string& pattern)
      27              : {
      28            2 :   std::regex regex_search_pattern(pattern);
      29            2 :   std::vector<std::string> file_list;
      30        32541 :   for (const auto& entry : std::filesystem::directory_iterator(path)) {
      31        32539 :     if (std::regex_match(entry.path().filename().string(), regex_search_pattern)) {
      32            3 :       if (std::filesystem::remove(entry.path())) {
      33            3 :         file_list.push_back(entry.path());
      34              :       }
      35              :     }
      36            2 :   }
      37            2 :   return file_list;
      38            2 : }
      39              : 
      40              : void
      41            3 : touchFile(const std::string& filepath)
      42              : {
      43            3 :   std::fstream fs;
      44            3 :   fs.open(filepath, std::ios::out);
      45            3 :   fs.close();
      46            3 : }
      47              : 
      48              : BOOST_AUTO_TEST_SUITE(HDF5FileUtils_test)
      49              : 
      50            2 : BOOST_AUTO_TEST_CASE(GetFileList)
      51              : {
      52            1 :   std::string file_path(std::filesystem::temp_directory_path());
      53            1 :   std::string file_prefix = "kurt";
      54            1 :   std::string file_extension = ".tmp";
      55            1 :   std::string pid = std::to_string(getpid());
      56              : 
      57              :   // delete any pre-existing files so that we start with a clean slate
      58            1 :   std::string delete_pattern = file_prefix + ".*" + pid + ".*" + file_extension;
      59            1 :   delete_files_matching_pattern(file_path, delete_pattern);
      60              : 
      61              :   // create a few test files
      62            1 :   std::string fullPath;
      63            1 :   fullPath = file_path + "/" + file_prefix + "_1_" + pid + file_extension;
      64            1 :   touchFile(fullPath);
      65            1 :   fullPath = file_path + "/" + file_prefix + "_2_" + pid + file_extension;
      66            1 :   touchFile(fullPath);
      67            1 :   fullPath = file_path + "/" + file_prefix + "_3_" + pid + file_extension;
      68            1 :   touchFile(fullPath);
      69              : 
      70            1 :   std::string search_pattern = file_prefix + ".*" + pid + ".*" + file_extension;
      71            1 :   std::vector<std::string> file_list = HDF5FileUtils::get_files_matching_pattern(file_path, search_pattern);
      72            1 :   BOOST_REQUIRE_EQUAL(file_list.size(), 3);
      73              : 
      74            1 :   file_list = delete_files_matching_pattern(file_path, delete_pattern);
      75            1 :   BOOST_REQUIRE_EQUAL(file_list.size(), 3);
      76            1 : }
      77              : 
      78              : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1