moth/cmd/mothd/zipfs_test.go

65 lines
938 B
Go
Raw Normal View History

2018-09-14 18:24:48 -06:00
package main
2018-05-09 21:52:52 -06:00
import (
"archive/zip"
"fmt"
2020-02-29 18:36:59 -07:00
"github.com/spf13/afero"
2020-02-29 22:37:22 -07:00
"io"
2018-05-09 21:52:52 -06:00
"testing"
)
func TestZipfs(t *testing.T) {
2020-02-29 18:36:59 -07:00
fs := new(afero.MemMapFs)
2020-02-29 22:37:22 -07:00
2020-02-29 18:36:59 -07:00
tf, err := fs.Create("/test.zip")
2018-05-09 21:52:52 -06:00
if err != nil {
t.Error(err)
return
}
2020-02-29 18:36:59 -07:00
defer fs.Remove(tf.Name())
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
w := zip.NewWriter(tf)
f, err := w.Create("moo.txt")
if err != nil {
t.Error(err)
return
}
// no Close method
_, err = fmt.Fprintln(f, "The cow goes moo")
//.Write([]byte("The cow goes moo"))
if err != nil {
t.Error(err)
return
}
w.Close()
tf.Close()
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
// Now read it in
2020-02-29 18:36:59 -07:00
mb, err := OpenZipfs(fs, tf.Name())
2018-05-09 21:52:52 -06:00
if err != nil {
t.Error(err)
return
}
cow, err := mb.Open("moo.txt")
if err != nil {
t.Error(err)
return
}
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
line := make([]byte, 200)
n, err := cow.Read(line)
if (err != nil) && (err != io.EOF) {
t.Error(err)
return
}
if string(line[:n]) != "The cow goes moo\n" {
t.Log(line)
t.Error("Contents didn't match")
return
}
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
}