@@ -361,6 +361,39 @@ public async Task WriteFileThrow()
361361 Assert . Equal ( "old content" , fileContent ) ;
362362 Assert . Equal ( $ "File already exists: { Path . Combine ( TestFileContext . RootPath , "test.txt" ) } ", ex . Message ) ;
363363 }
364+ [ Fact ]
365+ public async Task WriteFileCreateDirectory ( )
366+ {
367+ var result = await File . Write (
368+ new WriteInput ( )
369+ {
370+ Content = "Created with a subdirectory" ,
371+ Path = Path . Combine ( TestFileContext . RootPath , "folder/Subdir/subdir.txt" )
372+ } ,
373+ new WriteOption ( )
374+ {
375+ CreateTargetDirectories = true
376+ } ) ;
377+ var fileContent = System . IO . File . ReadAllText ( result . Path ) ;
378+ Console . WriteLine ( result . Path ) ;
379+ Assert . Equal ( "Created with a subdirectory" , fileContent ) ;
380+ }
381+
382+ [ Fact ]
383+ public async Task WriteFileCreateDirectoryThrow ( )
384+ {
385+ var result = await Assert . ThrowsAsync < DirectoryNotFoundException > ( async ( ) => await File . Write (
386+ new WriteInput ( )
387+ {
388+ Content = "Task should throw" ,
389+ Path = Path . Combine ( TestFileContext . RootPath , "folder/Subdir/subdir.txt" )
390+ } ,
391+ new WriteOption ( )
392+ {
393+ CreateTargetDirectories = false
394+ } ) ) ;
395+
396+ }
364397
365398 [ Fact ]
366399 public async Task WriteFileBytesAppend ( )
@@ -430,6 +463,42 @@ public async Task WriteFileBytesThrow()
430463 Assert . Equal ( imageBytes , fileContentBytes ) ;
431464 }
432465
466+ [ Fact ]
467+ public async Task WriteFileBytesCreateDirectory ( )
468+ {
469+ var imageBytes = System . IO . File . ReadAllBytes ( BinaryTestFilePath ) ;
470+ TestFileContext . CreateBinaryFile ( "test.png" , new byte [ ] { 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 } ) ; // empty png
471+ var result = await File . WriteBytes (
472+ new WriteBytesInput ( )
473+ {
474+ ContentBytes = imageBytes ,
475+ Path = Path . Combine ( TestFileContext . RootPath , "folder/Subdir/test.png" )
476+ } ,
477+ new WriteBytesOption ( )
478+ {
479+ CreateTargetDirectories = true ,
480+ } ) ;
481+ var fileContentBytes = System . IO . File . ReadAllBytes ( result . Path ) ;
482+ Assert . Equal ( imageBytes . Length , fileContentBytes . Length ) ;
483+ Assert . Equal ( imageBytes , fileContentBytes ) ;
484+ }
485+ [ Fact ]
486+ public async Task WriteFileBytesCreateDirectoryThrow ( )
487+ {
488+ var imageBytes = System . IO . File . ReadAllBytes ( BinaryTestFilePath ) ;
489+ TestFileContext . CreateBinaryFile ( "test.png" , new byte [ ] { 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 } ) ; // empty png
490+ var result = await Assert . ThrowsAsync < DirectoryNotFoundException > ( async ( ) => await File . WriteBytes (
491+ new WriteBytesInput ( )
492+ {
493+ ContentBytes = imageBytes ,
494+ Path = Path . Combine ( TestFileContext . RootPath , "folder/Subdir/test.png" )
495+ } ,
496+ new WriteBytesOption ( )
497+ {
498+ CreateTargetDirectories = false ,
499+ } ) ) ;
500+ }
501+
433502 [ Fact ]
434503 public async Task ReadFileContent ( )
435504 {
0 commit comments