github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/testdata/fs/main.go (about)

     1  package fs
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  )
    10  
    11  func Main() {
    12  	testAdHoc()
    13  }
    14  
    15  func testAdHoc() {
    16  	// Ensure stat works, particularly mode.
    17  	for _, path := range []string{"sub", "/animals.txt", "animals.txt"} {
    18  		if stat, err := os.Stat(path); err != nil {
    19  			log.Panicln(err)
    20  		} else {
    21  			fmt.Println(path, "mode", stat.Mode())
    22  		}
    23  	}
    24  
    25  	// Read the full contents of the file using io.Reader
    26  	b, err := os.ReadFile("/animals.txt")
    27  	if err != nil {
    28  		log.Panicln(err)
    29  	}
    30  	fmt.Println("contents:", string(b))
    31  
    32  	// Re-open the same file to test io.ReaderAt
    33  	f, err := os.Open("/animals.txt")
    34  	if err != nil {
    35  		log.Panicln(err)
    36  	}
    37  	defer f.Close()
    38  
    39  	// Seek to an arbitrary position.
    40  	if _, err = f.Seek(4, io.SeekStart); err != nil {
    41  		log.Panicln(err)
    42  	}
    43  
    44  	b1 := make([]byte, len(b))
    45  	// We expect to revert to the original position on ReadAt zero.
    46  	if _, err = f.ReadAt(b1, 0); err != nil {
    47  		log.Panicln(err)
    48  	}
    49  
    50  	if !bytes.Equal(b, b1) {
    51  		log.Panicln("unexpected ReadAt contents: ", string(b1))
    52  	}
    53  
    54  	b, err = os.ReadFile("/empty.txt")
    55  	if err != nil {
    56  		log.Panicln(err)
    57  	}
    58  	fmt.Println("empty:" + string(b))
    59  }