moth/pkg/transpile/mothball_test.go

55 lines
1.0 KiB
Go
Raw Normal View History

2020-09-08 17:49:02 -06:00
package transpile
2020-09-04 18:28:23 -06:00
import (
"archive/zip"
2020-10-12 17:44:44 -06:00
"bytes"
2020-09-04 18:28:23 -06:00
"io/ioutil"
2020-09-14 13:40:55 -06:00
"os"
"path"
"runtime"
2020-09-04 18:28:23 -06:00
"testing"
"github.com/spf13/afero"
"github.com/spf13/afero/zipfs"
)
2020-09-14 13:40:55 -06:00
func TestMothballsMemFs(t *testing.T) {
static := NewFsCategory(newTestFs(), "cat1")
2020-10-12 17:44:44 -06:00
mb := new(bytes.Buffer)
if err := Mothball(static, mb); err != nil {
2020-09-14 13:40:55 -06:00
t.Error(err)
}
}
func TestMothballsOsFs(t *testing.T) {
_, testfn, _, _ := runtime.Caller(0)
os.Chdir(path.Dir(testfn))
2020-09-04 18:28:23 -06:00
fs := NewRecursiveBasePathFs(afero.NewOsFs(), "testdata")
static := NewFsCategory(fs, "static")
2020-10-12 17:44:44 -06:00
mb := new(bytes.Buffer)
err := Mothball(static, mb)
2020-09-04 18:28:23 -06:00
if err != nil {
t.Error(err)
2020-09-14 13:40:55 -06:00
return
2020-09-04 18:28:23 -06:00
}
2020-10-12 17:44:44 -06:00
mbReader := bytes.NewReader(mb.Bytes())
mbr, err := zip.NewReader(mbReader, int64(mb.Len()))
2020-09-04 18:28:23 -06:00
if err != nil {
t.Error(err)
}
zfs := zipfs.New(mbr)
if f, err := zfs.Open("puzzles.txt"); err != nil {
t.Error(err)
} else {
defer f.Close()
if buf, err := ioutil.ReadAll(f); err != nil {
t.Error(err)
2020-10-12 17:44:44 -06:00
} else if string(buf) != "1\n2\n3\n" {
2020-09-04 18:28:23 -06:00
t.Error("Bad puzzles.txt", string(buf))
}
}
}