Skip to content

Commit

Permalink
new fsys examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 14, 2023
1 parent c660c25 commit a59c823
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions pkg/fsys/fsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Clean(name string) {
}

// SaveTemp saves bytes to a named temporary file.
// The path to the file is returned.
func SaveTemp(name string, b ...byte) (string, error) {
_, path, err := save.Save(util.Temp(name), b...)
if err != nil {
Expand Down
65 changes: 39 additions & 26 deletions pkg/fsys/fsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,51 @@ import (
"github.com/bengarrett/retrotxtgo/pkg/internal/tmp"
)

const windows = "windows"

func ExampleTar() {
tmpTar := tmp.File("tar_test.tar")
tmpFile, err := fsys.SaveTemp(tmpTar, []byte("x")...)
func ExampleClean() {
path, err := fsys.SaveTemp("examplesave.txt", []byte("hello world")...)
if err != nil {
fsys.Clean(path)
log.Fatal(err)
}
defer os.Remove(tmpFile)
if err := fsys.Tar(tmpTar, tmpFile); err != nil {
log.Print(err)
return
}
f, err := os.Stat(tmpFile)
if err != nil {
log.Print(err)
return
}
fmt.Fprintf(os.Stdout, "%s, %d", f.Name(), f.Size())
fsys.Clean(path)
// Output:
}

func ExampleSaveTemp() {
file, _ := fsys.SaveTemp("example.txt", []byte("hello world")...)
defer os.Remove(file)
s, _ := os.Stat(file)
fmt.Printf("%s, %d", s.Name(), s.Size())
// Output:example.txt, 11
}

func ExampleTar() {
name := tmp.File("tar_test.tar")
file, _ := fsys.SaveTemp(name, []byte("x")...)
defer os.Remove(file)
_ = fsys.Tar(name, file)
s, _ := os.Stat(file)
fmt.Printf("%s, %d", s.Name(), s.Size())
// Output:tar_test.tar, 1536
}

func ExampleTouch() {
file, _ := fsys.Touch("example.txt")
defer os.Remove(file)
s, _ := os.Stat(file)
fmt.Printf("%s, %d", s.Name(), s.Size())
// Output:example.txt, 0
}

func ExampleWrite() {
file, _ := fsys.Touch("example.txt")
defer os.Remove(file)
_, _, _ = fsys.Write(file, []byte("hello world")...)
s, _ := os.Stat(file)
fmt.Printf("%s, %d", s.Name(), s.Size())
// Output:example.txt, 11
}

func BenchmarkReadLarge(_ *testing.B) {
large := mock.LargeExample()
if _, err := fsys.Read(large); err != nil {
Expand All @@ -53,16 +76,6 @@ func BenchmarkReadMega(_ *testing.B) {
fsys.Clean(mega)
}

func ExampleClean() {
path, err := fsys.SaveTemp("examplesave.txt", []byte("hello world")...)
if err != nil {
fsys.Clean(path)
log.Fatal(err)
}
fsys.Clean(path)
// Output:
}

func TestRead(t *testing.T) {
f := mock.FileExample("hello", 0)
large := mock.LargeExample()
Expand Down

0 comments on commit a59c823

Please sign in to comment.