@@ -327,5 +327,70 @@ TEST_F(FilesystemTest, Chdir) {
327
327
(*std::move (f)).Close ().Check ();
328
328
}
329
329
330
+ TEST_F (FilesystemTest, WriteStream) {
331
+ std::string content_str = " 0123456789" ;
332
+ auto write = dir_.OpenWriteOnly (" test" , CreationOptions::CreateNew);
333
+ ASSERT_THAT (write, IsSuccess (_));
334
+ {
335
+ llvm::raw_fd_ostream os = write->WriteStream ();
336
+ os << content_str;
337
+ EXPECT_FALSE (os.has_error ()) << os.error ();
338
+ }
339
+ (*std::move (write)).Close ().Check ();
340
+
341
+ EXPECT_THAT (dir_.ReadFileToString (" test" ), IsSuccess (Eq (content_str)));
342
+ }
343
+
344
+ TEST_F (FilesystemTest, Rename) {
345
+ // Rename a file within a directory.
346
+ ASSERT_THAT (dir_.WriteFileFromString (" file1" , " content1" ), IsSuccess (_));
347
+ EXPECT_THAT (dir_.Rename (" file1" , dir_, " file2" ), IsSuccess (_));
348
+ EXPECT_THAT (dir_.ReadFileToString (" file2" ), IsSuccess (Eq (" content1" )));
349
+ auto read_missing = dir_.ReadFileToString (" file1" );
350
+ EXPECT_FALSE (read_missing.ok ());
351
+ EXPECT_TRUE (read_missing.error ().no_entity ());
352
+
353
+ // Rename a file between two directories.
354
+ auto d1 = *dir_.CreateDirectories (" subdir1" );
355
+ EXPECT_THAT (dir_.Rename (" file2" , d1, " file1" ), IsSuccess (_));
356
+ EXPECT_THAT (d1.ReadFileToString (" file1" ), IsSuccess (Eq (" content1" )));
357
+ auto d2 = *dir_.CreateDirectories (" subdir2" );
358
+ EXPECT_THAT (d1.Rename (" file1" , d2, " file1" ), IsSuccess (_));
359
+ EXPECT_THAT (d2.ReadFileToString (" file1" ), IsSuccess (Eq (" content1" )));
360
+ // Close the first directory.
361
+ d1 = Filesystem::Dir ();
362
+ EXPECT_THAT (dir_.Rmdir (" subdir1" ), IsSuccess (_))
363
+ << " Directory should have bene empty!" ;
364
+
365
+ // Rename directories.
366
+ ASSERT_THAT (dir_.ReadFileToString (std::filesystem::path (" subdir2" ) / " file1" ),
367
+ IsSuccess (Eq (" content1" )));
368
+ EXPECT_THAT (dir_.Rename (" subdir2" , dir_, " subdir1" ), IsSuccess (_));
369
+ EXPECT_THAT (dir_.ReadFileToString (std::filesystem::path (" subdir1" ) / " file1" ),
370
+ IsSuccess (Eq (" content1" )));
371
+
372
+ // The open directory `d2` should survive the rename and point at the same
373
+ // directory.
374
+ EXPECT_THAT (d2.ReadFileToString (" file1" ), IsSuccess (Eq (" content1" )));
375
+ EXPECT_THAT (d2.WriteFileFromString (" file2" , " content2" ), IsSuccess (_));
376
+ EXPECT_THAT (dir_.ReadFileToString (std::filesystem::path (" subdir1" ) / " file2" ),
377
+ IsSuccess (Eq (" content2" )));
378
+
379
+ // Rename over an existing file.
380
+ EXPECT_THAT (d2.Rename (" file2" , d2, " file1" ), IsSuccess (_));
381
+ EXPECT_THAT (d2.ReadFileToString (" file1" ), IsSuccess (Eq (" content2" )));
382
+
383
+ // Test error calls as well.
384
+ auto result = dir_.Rename (" missing1" , dir_, " missing2" );
385
+ EXPECT_TRUE (result.error ().no_entity ()) << result.error ();
386
+ result = d2.Rename (" file1" , dir_,
387
+ std::filesystem::path (" missing_subdir" ) / " file2" );
388
+ EXPECT_TRUE (result.error ().no_entity ()) << result.error ();
389
+ // Note that `d2` was renamed `subdir1` above, which is why this creates
390
+ // infinite subdirectories.
391
+ result = dir_.Rename (" subdir1" , d2, " infinite_subdirs" );
392
+ EXPECT_THAT (result.error ().unix_errnum (), EINVAL) << result.error ();
393
+ }
394
+
330
395
} // namespace
331
396
} // namespace Carbon::Filesystem
0 commit comments