71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package pkgdir
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewAndZip(t *testing.T) {
|
|
folder := t.TempDir()
|
|
target, err := New(folder, "command", "pub-test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest, _ := os.ReadFile(filepath.Join(target, Manifest))
|
|
for _, part := range []string{"id: pub-test", "kind: command", "scope: public", "arguments: [input]"} {
|
|
if !strings.Contains(string(manifest), part) {
|
|
t.Errorf("Manifest ohne %q:\n%s", part, manifest)
|
|
}
|
|
}
|
|
os.WriteFile(filepath.Join(target, ".DS_Store"), []byte("x"), 0o644)
|
|
os.MkdirAll(filepath.Join(target, ".git"), 0o755)
|
|
os.WriteFile(filepath.Join(target, ".git", "HEAD"), []byte("x"), 0o644)
|
|
|
|
raw, err := Zip(target)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
z, _ := zip.NewReader(bytes.NewReader(raw), int64(len(raw)))
|
|
var names []string
|
|
for _, f := range z.File {
|
|
names = append(names, f.Name)
|
|
}
|
|
slices.Sort(names)
|
|
if !slices.Equal(names, []string{"COMMAND.md", "hugin.yaml"}) {
|
|
t.Errorf("ZIP enthält %v", names)
|
|
}
|
|
}
|
|
|
|
func TestNewValidatesInputs(t *testing.T) {
|
|
folder := t.TempDir()
|
|
if _, err := New(folder, "hook", "pub-x"); err == nil {
|
|
t.Error("Art hook angenommen")
|
|
}
|
|
if _, err := New(folder, "skill", "Pub_X"); err == nil {
|
|
t.Error("ungültige id angenommen")
|
|
}
|
|
New(folder, "skill", "pub-x")
|
|
if _, err := New(folder, "skill", "pub-x"); err == nil {
|
|
t.Error("vorhandener Ordner überschrieben")
|
|
}
|
|
}
|
|
|
|
func TestZipWithoutManifest(t *testing.T) {
|
|
if _, err := Zip(t.TempDir()); err == nil {
|
|
t.Error("Ordner ohne hugin.yaml angenommen")
|
|
}
|
|
}
|
|
|
|
func TestZipRejectsSymlink(t *testing.T) {
|
|
folder := t.TempDir()
|
|
os.WriteFile(filepath.Join(folder, Manifest), []byte("id: x\n"), 0o644)
|
|
os.Symlink("/etc/hosts", filepath.Join(folder, "SKILL.md"))
|
|
if _, err := Zip(folder); err == nil {
|
|
t.Error("Symlink angenommen")
|
|
}
|
|
}
|