11use async_std:: fs:: File ;
2- use async_std:: io:: { self , Read , Write } ;
2+ use async_std:: fs:: OpenOptions ;
3+ use async_std:: io:: { self , Read , SeekFrom , Write } ;
34use async_std:: path:: PathBuf ;
45use async_std:: sync:: Arc ;
56use async_std:: task:: { Context , Poll } ;
67use std:: pin:: Pin ;
8+ use std:: sync:: Mutex ;
79
810#[ macro_export]
911macro_rules! assert {
1012 ( $actual: expr, $expected_file: expr, $block: expr) => {
1113 task:: block_on( async {
1214 use async_std:: io:: prelude:: * ;
1315 $block. await . unwrap( ) ;
14- let mut actual = std :: string :: String :: from_utf8 ( $actual) . unwrap ( ) ;
16+ let mut actual = $actual. to_string ( ) . await ;
1517 let mut expected = std:: string:: String :: new( ) ;
1618 $expected_file. read_to_string( & mut expected) . await . unwrap( ) ;
1719 match expected. find( "{DATE}" ) {
@@ -38,11 +40,29 @@ pub async fn read_fixture(name: &str) -> TestFile {
3840 let file = File :: open ( directory. join ( path) )
3941 . await
4042 . expect ( "Reading fixture file didn't work" ) ;
41- TestFile ( Arc :: new ( file) )
43+ let temp = std:: env:: temp_dir ( ) . join ( "foo.txt" ) ;
44+ let temp = OpenOptions :: new ( )
45+ . read ( true )
46+ . write ( true )
47+ . open ( temp)
48+ . await
49+ . unwrap ( ) ;
50+ TestFile ( Arc :: new ( file) , Arc :: new ( Mutex :: new ( temp) ) )
4251}
4352
4453#[ derive( Clone ) ]
45- pub struct TestFile ( Arc < File > ) ;
54+ pub struct TestFile ( Arc < File > , Arc < Mutex < File > > ) ;
55+
56+ impl TestFile {
57+ pub async fn to_string ( self ) -> String {
58+ use async_std:: prelude:: * ;
59+ let mut buf = String :: new ( ) ;
60+ let mut file = self . 1 . lock ( ) . unwrap ( ) ;
61+ file. seek ( SeekFrom :: Start ( 0 ) ) . await . unwrap ( ) ;
62+ dbg ! ( file. read_to_string( & mut buf) . await . unwrap( ) ) ;
63+ buf
64+ }
65+ }
4666
4767impl Read for TestFile {
4868 fn poll_read (
@@ -56,14 +76,14 @@ impl Read for TestFile {
5676
5777impl Write for TestFile {
5878 fn poll_write ( self : Pin < & mut Self > , cx : & mut Context , buf : & [ u8 ] ) -> Poll < io:: Result < usize > > {
59- Pin :: new ( & mut & * self . 0 ) . poll_write ( cx, buf)
79+ Pin :: new ( & mut & * self . 1 . lock ( ) . unwrap ( ) ) . poll_write ( cx, buf)
6080 }
6181
6282 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
63- Pin :: new ( & mut & * self . 0 ) . poll_flush ( cx)
83+ Pin :: new ( & mut & * self . 1 . lock ( ) . unwrap ( ) ) . poll_flush ( cx)
6484 }
6585
6686 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < io:: Result < ( ) > > {
67- Pin :: new ( & mut & * self . 0 ) . poll_close ( cx)
87+ Pin :: new ( & mut & * self . 1 . lock ( ) . unwrap ( ) ) . poll_close ( cx)
6888 }
6989}
0 commit comments