| 24 | #define ASSERT_TF_OK(x) ASSERT_EQ(TF_OK, TF_GetCode(x)) |
| 25 | |
| 26 | TEST(TestEnv, TestDirHandling) { |
| 27 | TF_StringStream* tempdirs = TF_GetLocalTempDirectories(); |
| 28 | const char* tempdir; |
| 29 | bool found = false; |
| 30 | while (TF_StringStreamNext(tempdirs, &tempdir)) { |
| 31 | found = true; |
| 32 | |
| 33 | TF_Status* s = TF_NewStatus(); |
| 34 | |
| 35 | ::tensorflow::string dirpath = |
| 36 | ::tensorflow::io::JoinPath(tempdir, "somedir"); |
| 37 | TF_CreateDir(dirpath.c_str(), s); |
| 38 | ASSERT_TF_OK(s) << "TF_CreateDir failed for " << dirpath << ": " |
| 39 | << TF_Message(s); |
| 40 | |
| 41 | ::tensorflow::string filepath = |
| 42 | ::tensorflow::io::JoinPath(dirpath, "somefile.txt"); |
| 43 | TF_WritableFileHandle* handle; |
| 44 | TF_NewWritableFile(filepath.c_str(), &handle, s); |
| 45 | ASSERT_TF_OK(s) << "NewWritableFile failed for " << filepath << ": " |
| 46 | << TF_Message(s); |
| 47 | |
| 48 | const char* data = "Hello, world!\n"; |
| 49 | TF_AppendWritableFile(handle, data, strlen(data), s); |
| 50 | ASSERT_TF_OK(s) << "TF_AppendWritableFile failed to append data to file at " |
| 51 | << filepath << ": " << TF_Message(s); |
| 52 | |
| 53 | TF_CloseWritableFile(handle, s); |
| 54 | ASSERT_TF_OK(s) << "TF_CloseWritableFile failed to close handle to " |
| 55 | << filepath << ": " << TF_Message(s); |
| 56 | |
| 57 | TF_StringStream* children = TF_GetChildren(dirpath.c_str(), s); |
| 58 | ASSERT_TF_OK(s) << "TF_GetChildren failed for " << dirpath; |
| 59 | const char* childpath; |
| 60 | ASSERT_TRUE(TF_StringStreamNext(children, &childpath)); |
| 61 | ASSERT_EQ(::tensorflow::string(childpath), "somefile.txt"); |
| 62 | // There should only be one file in this directory. |
| 63 | ASSERT_FALSE(TF_StringStreamNext(children, &childpath)); |
| 64 | ASSERT_EQ(childpath, nullptr); |
| 65 | TF_StringStreamDone(children); |
| 66 | |
| 67 | TF_FileStatistics stats; |
| 68 | TF_FileStat(filepath.c_str(), &stats, s); |
| 69 | ASSERT_EQ(stats.length, strlen(data)); |
| 70 | ASSERT_FALSE(stats.is_directory); |
| 71 | ASSERT_GT(stats.mtime_nsec, 0); |
| 72 | |
| 73 | // Trying to delete a non-empty directory should fail. |
| 74 | TF_DeleteDir(dirpath.c_str(), s); |
| 75 | ASSERT_NE(TF_OK, TF_GetCode(s)) |
| 76 | << "TF_DeleteDir unexpectedly succeeded with a non-empty directory " |
| 77 | << dirpath; |
| 78 | |
| 79 | TF_DeleteFile(filepath.c_str(), s); |
| 80 | ASSERT_TF_OK(s) << "TF_DeleteFile failed for " << filepath << ": " |
| 81 | << TF_Message(s); |
| 82 | |
| 83 | // Now deleting the directory should work. |
nothing calls this directly
no test coverage detected