|
4 | 4 | #include <TFile.h>
|
5 | 5 | #include <TH1D.h>
|
6 | 6 | #include <TROOT.h>
|
| 7 | +#include <TSystem.h> |
7 | 8 | #include <ROOT/RError.hxx>
|
8 | 9 | #include <ROOT/RFile.hxx>
|
9 | 10 | #include <ROOT/TestSupport.hxx>
|
@@ -69,6 +70,40 @@ TEST(RFile, Open)
|
69 | 70 | EXPECT_THROW(file->Put("foo", foo), ROOT::RException);
|
70 | 71 | }
|
71 | 72 |
|
| 73 | +TEST(RFile, OpenInexistent) |
| 74 | +{ |
| 75 | + FileRaii fileGuard("does_not_exist.root"); |
| 76 | + |
| 77 | + // make sure that the file really does not exist, in case a previous test didn't clean it up. |
| 78 | + gSystem->Unlink(fileGuard.GetPath().c_str()); |
| 79 | + |
| 80 | + ROOT::TestSupport::CheckDiagsRAII diags; |
| 81 | + diags.optionalDiag(kSysError, "TFile::TFile", "", false); |
| 82 | + diags.optionalDiag(kError, "TFile::TFile", "", false); |
| 83 | + |
| 84 | + try { |
| 85 | + auto f = RFile::Open("does_not_exist.root"); |
| 86 | + FAIL() << "trying to open an inexistent file should throw"; |
| 87 | + } catch (const ROOT::RException &e) { |
| 88 | + EXPECT_THAT(e.what(), testing::HasSubstr("failed to open file")); |
| 89 | + } |
| 90 | + try { |
| 91 | + auto f = RFile::Update("/a/random/directory/that/definitely/does_not_exist.root"); |
| 92 | + FAIL() << "trying to update a file under an inexistent directory should throw"; |
| 93 | + } catch (const ROOT::RException &e) { |
| 94 | + EXPECT_THAT(e.what(), testing::HasSubstr("failed to open file")); |
| 95 | + } |
| 96 | + try { |
| 97 | + auto f = RFile::Recreate("/a/random/directory/that/definitely/does_not_exist.root"); |
| 98 | + FAIL() << "trying to create a file under an inexistent directory should throw"; |
| 99 | + } catch (const ROOT::RException &e) { |
| 100 | + EXPECT_THAT(e.what(), testing::HasSubstr("failed to open file")); |
| 101 | + } |
| 102 | + |
| 103 | + // This succeeds because Update creates the file if it doesn't exist. |
| 104 | + EXPECT_NO_THROW(RFile::Update("does_not_exist.root")); |
| 105 | +} |
| 106 | + |
72 | 107 | TEST(RFile, OpenForWriting)
|
73 | 108 | {
|
74 | 109 | FileRaii fileGuard("test_rfile_write.root");
|
|
0 commit comments