All checks were successful
default / test artifact actions (push) Successful in 26s
178 lines
3.8 KiB
Go
178 lines
3.8 KiB
Go
package push
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"code.geekeey.de/actions/sdk"
|
|
)
|
|
|
|
func TestParseConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
env map[string]string
|
|
want *config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "minimal",
|
|
env: map[string]string{"name": "foo", "path": "dist"},
|
|
want: &config{Name: "foo", Path: "dist"},
|
|
},
|
|
{
|
|
name: "patterns",
|
|
env: map[string]string{"name": "foo", "path": "dist", "pattern": "**/*.go\n\n!main.go\n"},
|
|
want: &config{Name: "foo", Path: "dist", Patterns: []string{"**/*.go", "!main.go"}},
|
|
},
|
|
{
|
|
name: "missing name",
|
|
env: map[string]string{"path": "dist"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing path",
|
|
env: map[string]string{"name": "foo"},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := parseConfig(func(k string) string { return tc.env[k] })
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got %+v", got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
t.Errorf("expected %+v, got %+v", tc.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCreateArchive(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, "a.txt"), "a")
|
|
writeFile(t, filepath.Join(dir, "sub", "b.txt"), "b")
|
|
writeFile(t, filepath.Join(dir, "sub", "c.log"), "c")
|
|
|
|
rd, err := createArchive(dir, []string{"**/*.txt"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
entries := zipEntries(t, rd)
|
|
if want := []string{"a.txt", "sub/b.txt"}; !reflect.DeepEqual(entries, want) {
|
|
t.Errorf("expected entries %v, got %v", want, entries)
|
|
}
|
|
}
|
|
|
|
func TestCreateArchive_NotDirectory(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "file.txt")
|
|
writeFile(t, path, "x")
|
|
|
|
if _, err := createArchive(path, []string{"**/*"}); err == nil {
|
|
t.Fatal("expected error for non-directory path")
|
|
}
|
|
}
|
|
|
|
func TestArtifactURL(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
context *sdk.GitHubContext
|
|
id string
|
|
want string
|
|
}{
|
|
{
|
|
name: "full",
|
|
context: &sdk.GitHubContext{ServerURL: "https://code.geekeey.de", Repository: "actions/test", RunID: "1"},
|
|
id: "1234",
|
|
want: "https://code.geekeey.de/actions/test/actions/runs/1/artifacts/1234",
|
|
},
|
|
{
|
|
name: "trailing slash",
|
|
context: &sdk.GitHubContext{ServerURL: "https://code.geekeey.de/", Repository: "actions/test", RunID: "1"},
|
|
id: "1234",
|
|
want: "https://code.geekeey.de/actions/test/actions/runs/1/artifacts/1234",
|
|
},
|
|
{
|
|
name: "missing run id",
|
|
context: &sdk.GitHubContext{ServerURL: "https://code.geekeey.de", Repository: "actions/test"},
|
|
id: "1234",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "missing id",
|
|
context: &sdk.GitHubContext{ServerURL: "https://code.geekeey.de", Repository: "actions/test", RunID: "1"},
|
|
id: "",
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if got := artifactURL(tc.context, tc.id); got != tc.want {
|
|
t.Errorf("expected %q, got %q", tc.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func writeFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func zipEntries(t *testing.T, r io.Reader) []string {
|
|
t.Helper()
|
|
|
|
data, err := io.ReadAll(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var entries []string
|
|
for _, f := range zr.File {
|
|
if f.FileInfo().IsDir() {
|
|
continue
|
|
}
|
|
entries = append(entries, f.Name)
|
|
}
|
|
sort.Strings(entries)
|
|
return entries
|
|
}
|