sdk/context_test.go

149 lines
3.9 KiB
Go

package sdk
import (
"os"
"reflect"
"testing"
)
func TestAction_Context(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Remove(f.Name()) })
if _, err := f.Write([]byte(`{"foo": "bar"}`)); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
eventPayloadPath := f.Name()
cases := []struct {
name string
env map[string]string
exp *GitHubContext
}{
{
name: "empty",
env: nil,
exp: &GitHubContext{
// Defaults
APIURL: "https://api.github.com",
ServerURL: "https://github.com",
GraphqlURL: "https://api.github.com/graphql",
event: map[string]any{},
},
},
{
name: "no_payload",
env: map[string]string{
"GITHUB_ACTION": "__repo-owner_name-of-action-repo",
"GITHUB_ACTION_PATH": "/path/to/action",
"GITHUB_ACTION_REPOSITORY": "repo-owner/name-of-action-repo",
"GITHUB_ACTIONS": "true",
"GITHUB_ACTOR": "sethvargo",
"GITHUB_API_URL": "https://foo.com",
"GITHUB_BASE_REF": "main",
"GITHUB_ENV": "/path/to/env",
"GITHUB_EVENT_NAME": "event_name",
"GITHUB_HEAD_REF": "headbranch",
"GITHUB_GRAPHQL_URL": "https://baz.com",
"GITHUB_JOB": "12",
"GITHUB_PATH": "/path/to/path",
"GITHUB_REF": "refs/tags/v1.0",
"GITHUB_REF_NAME": "v1.0",
"GITHUB_REF_PROTECTED": "true",
"GITHUB_REF_TYPE": "tag",
"GITHUB_REPOSITORY": "sethvargo/baz",
"GITHUB_REPOSITORY_OWNER": "sethvargo",
"GITHUB_RETENTION_DAYS": "90",
"GITHUB_RUN_ATTEMPT": "6",
"GITHUB_RUN_ID": "56",
"GITHUB_RUN_NUMBER": "34",
"GITHUB_SERVER_URL": "https://bar.com",
"GITHUB_SHA": "abcd1234",
"GITHUB_STEP_SUMMARY": "/path/to/summary",
"GITHUB_WORKFLOW": "test",
"GITHUB_WORKSPACE": "/path/to/workspace",
"GITHUB_TOKEN": "somerandomtoken",
},
exp: &GitHubContext{
Action: "__repo-owner_name-of-action-repo",
ActionPath: "/path/to/action",
ActionRepository: "repo-owner/name-of-action-repo",
Actions: "true",
Actor: "sethvargo",
APIURL: "https://foo.com",
BaseRef: "main",
Env: "/path/to/env",
EventName: "event_name",
// NOTE: No EventPath
GraphqlURL: "https://baz.com",
Job: "12",
HeadRef: "headbranch",
Path: "/path/to/path",
Ref: "refs/tags/v1.0",
RefName: "v1.0",
RefProtected: "true",
RefType: "tag",
Repository: "sethvargo/baz",
RepositoryOwner: "sethvargo",
RetentionDays: "90",
RunAttempt: "6",
RunID: "56",
RunNumber: "34",
ServerURL: "https://bar.com",
SHA: "abcd1234",
StepSummary: "/path/to/summary",
Workflow: "test",
Workspace: "/path/to/workspace",
Token: "somerandomtoken",
event: map[string]any{},
},
},
{
name: "payload",
env: map[string]string{
"GITHUB_EVENT_PATH": eventPayloadPath,
},
exp: &GitHubContext{
EventPath: eventPayloadPath,
// Defaults
APIURL: "https://api.github.com",
ServerURL: "https://github.com",
GraphqlURL: "https://api.github.com/graphql",
event: map[string]any{
"foo": "bar",
},
},
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
a := New()
a.env = func(s string) string { return tc.env[s] }
got := a.Context()
_, err := got.Event()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, tc.exp) {
t.Errorf("expected\n\n%#v\n\nto be\n\n%#v\n", got, tc.exp)
}
})
}
}