simplify tests, remove readme injection

This commit is contained in:
2025-05-18 14:16:29 -04:00
parent c56d5058ec
commit 2827ce38d9
6 changed files with 50 additions and 55 deletions

View File

@@ -7,22 +7,36 @@ import (
"os"
"os/exec"
"testing"
"github.com/matryer/is"
)
// NOTE: Run these tests with `make test`
func must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}
func build() func() {
must(exec.Command("go", "build", "-o", "testdata/app", "testdata/app.go").CombinedOutput())
must(exec.Command("go", "build", "-o", "appify").CombinedOutput())
return func() {
os.RemoveAll("./appify")
os.RemoveAll("./testdata/app")
}
}
func Test(t *testing.T) {
is := is.New(t)
out, err := exec.Command("./appify",
cleanup := build()
defer cleanup()
out := must(exec.Command("./appify",
"-name", "Test",
"-icon", "testdata/machina-square.png",
"testdata/app").CombinedOutput()
"testdata/app").CombinedOutput())
t.Logf("%q", string(out))
is.NoErr(err)
defer os.RemoveAll("Test.app")
actualAppHash := filehash(t, "testdata/app")
actualAppHash := filehash("testdata/app")
type file struct {
path string
perm string
@@ -34,34 +48,37 @@ func Test(t *testing.T) {
{path: "Test.app/Contents/MacOS", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/MacOS/Test.app", perm: "-rwxr-xr-x", hash: actualAppHash},
{path: "Test.app/Contents/Info.plist", perm: "-rw-r--r--", hash: "0cd092b7b884e87617648dbdadb6a804"},
{path: "Test.app/Contents/README", perm: "-rw-r--r--", hash: "afeb10df47c7f189b848ae44a54e7e06"},
{path: "Test.app/Contents/Resources", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/Resources/icon.icns", perm: "-rw-r--r--", hash: "23bdc36475094ed8886f319811d3a182"},
} {
t.Run(f.path, func(t *testing.T) {
is := is.New(t)
info, err := os.Stat(f.path)
is.NoErr(err)
is.Equal(info.Mode().String(), f.perm) // perm
info := must(os.Stat(f.path))
if info.Mode().String() != f.perm {
t.Logf("%v != %v", info.Mode().String(), f.perm)
}
if f.hash != "" {
actual := filehash(t, f.path)
is.Equal(actual, f.hash) // hash
actual := filehash(f.path)
if actual != f.hash {
t.Logf("%v != %v", actual, f.hash)
}
}
})
}
}
func TestWithPListInjection(t *testing.T) {
is := is.New(t)
out, err := exec.Command("./appify",
cleanup := build()
defer cleanup()
out := must(exec.Command("./appify",
"-name", "Test",
"-icon", "testdata/machina-square.png",
"-plist", "testdata/additionalplist.xml",
"testdata/app").CombinedOutput()
"testdata/app").CombinedOutput())
t.Logf("%q", string(out))
is.NoErr(err)
defer os.RemoveAll("Test.app")
actualAppHash := filehash(t, "testdata/app")
actualAppHash := filehash("testdata/app")
type file struct {
path string
perm string
@@ -73,31 +90,29 @@ func TestWithPListInjection(t *testing.T) {
{path: "Test.app/Contents/MacOS", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/MacOS/Test.app", perm: "-rwxr-xr-x", hash: actualAppHash},
{path: "Test.app/Contents/Info.plist", perm: "-rw-r--r--", hash: "dd977257c7e77dc7739bb4ae70e5f697"},
{path: "Test.app/Contents/README", perm: "-rw-r--r--", hash: "afeb10df47c7f189b848ae44a54e7e06"},
{path: "Test.app/Contents/Resources", perm: "drwxr-xr-x"},
{path: "Test.app/Contents/Resources/icon.icns", perm: "-rw-r--r--", hash: "23bdc36475094ed8886f319811d3a182"},
} {
t.Run(f.path, func(t *testing.T) {
is := is.New(t)
info, err := os.Stat(f.path)
is.NoErr(err)
is.Equal(info.Mode().String(), f.perm) // perm
info := must(os.Stat(f.path))
if info.Mode().String() != f.perm {
t.Logf("%v != %v", info.Mode().String(), f.perm)
}
if f.hash != "" {
actual := filehash(t, f.path)
is.Equal(actual, f.hash) // hash
actual := filehash(f.path)
if actual != f.hash {
t.Logf("%v != %v", actual, f.hash)
}
}
})
}
}
// filehash gets an md5 hash of the file at path.
func filehash(t *testing.T, path string) string {
is := is.New(t)
f, err := os.Open(path)
is.NoErr(err)
func filehash(path string) string {
f := must(os.Open(path))
defer f.Close()
h := md5.New()
_, err = io.Copy(h, f)
is.NoErr(err)
must(io.Copy(h, f))
return fmt.Sprintf("%x", h.Sum(nil))
}