feat: add initial helper methods
This commit is contained in:
parent
81d80fcced
commit
76a93c0d8c
6 changed files with 1165 additions and 0 deletions
443
action_test.go
Normal file
443
action_test.go
Normal file
|
@ -0,0 +1,443 @@
|
|||
package sdk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newFakeGetenvFunc(t *testing.T, wantKey, v string) func(string) string {
|
||||
return func(gotKey string) string {
|
||||
if gotKey != wantKey {
|
||||
t.Errorf("expected call GetenvFunc(%q) to be GetenvFunc(%q)", gotKey, wantKey)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_WithFieldsSlice(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a = a.WithFieldsSlice("line=100", "file=app.js")
|
||||
a.Debugf("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::debug file=app.js,line=100::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_WithFieldsSlice_Panic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
want := `"no-equals" is not a proper k=v pair!`
|
||||
if got := recover(); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a = a.WithFieldsSlice("no-equals")
|
||||
a.Debugf("fail: %s", "thing")
|
||||
}
|
||||
|
||||
func TestAction_WithFieldsMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a = a.WithFieldsMap(map[string]string{"line": "100", "file": "app.js"})
|
||||
a.Debugf("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::debug file=app.js,line=100::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_GetInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "INPUT_FOO", "bar")
|
||||
|
||||
if got, want := a.GetInput("foo"), "bar"; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_IssueCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.IssueCommand(&Command{
|
||||
Name: "foo",
|
||||
Message: "bar",
|
||||
})
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::foo::bar"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_IssueFileCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_FOO", file.Name())
|
||||
|
||||
a.IssueFileCommand(&Command{
|
||||
Name: "foo",
|
||||
Message: "bar",
|
||||
})
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the message to be written to the env file
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp env file: %s", err)
|
||||
}
|
||||
|
||||
if got, want := string(data), "bar"; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_AddMask(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.AddMask("foobar")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::add-mask::foobar"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_AddMatcher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.AddMatcher("foobar.json")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::add-matcher::foobar.json"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_RemoveMatcher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.RemoveMatcher("foobar")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::remove-matcher owner=foobar::"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_Group(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.Group("mygroup")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::group::mygroup"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_EndGroup(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.EndGroup()
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::endgroup::"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_Debugf(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.Debugf("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::debug::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_Noticef(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.Noticef("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::notice::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_Warningf(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.Warningf("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::warning::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_Errorf(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
|
||||
a.Errorf("fail: %s", "thing")
|
||||
|
||||
if got, want := b.String(), fmt.Sprintln("::error::fail: thing"); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_AddPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// expect a file command to be issued when env file is set.
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
var b bytes.Buffer
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_PATH", file.Name())
|
||||
|
||||
a.AddPath("/custom/bin")
|
||||
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the message to be written to the file.
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp env file: %s", err)
|
||||
}
|
||||
|
||||
if got, want := string(data), "/custom/bin"; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_SaveState(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var b bytes.Buffer
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_STATE", file.Name())
|
||||
|
||||
a.SaveState("key", "value")
|
||||
a.SaveState("key2", "value2")
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the command to be written to the file.
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp env file: %s", err)
|
||||
}
|
||||
|
||||
want := fmt.Sprintf("key<<%s\nvalue\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
want += fmt.Sprintf("key2<<%s\nvalue2\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
if got := string(data); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_AddStepSummary(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// expectations for env file env commands
|
||||
var b bytes.Buffer
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_STEP_SUMMARY", file.Name())
|
||||
|
||||
a.AddStepSummary(`
|
||||
## This is
|
||||
|
||||
some markdown
|
||||
`)
|
||||
a.AddStepSummary(`
|
||||
- content
|
||||
`)
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the command to be written to the file.
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp summary file: %s", err)
|
||||
}
|
||||
|
||||
want := "\n## This is\n\nsome markdown\n\n- content\n"
|
||||
if got := string(data); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_SetEnv(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// expectations for env file env commands
|
||||
var b bytes.Buffer
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_ENV", file.Name())
|
||||
|
||||
a.SetEnv("key", "value")
|
||||
a.SetEnv("key2", "value2")
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the command to be written to the file.
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp env file: %s", err)
|
||||
}
|
||||
|
||||
want := fmt.Sprintf("key<<%s\nvalue\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
want += fmt.Sprintf("key2<<%s\nvalue2\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
if got := string(data); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAction_SetOutput(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// expectations for env file env commands
|
||||
var b bytes.Buffer
|
||||
file, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a temp env file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
a := New()
|
||||
a.w = &b
|
||||
a.env = newFakeGetenvFunc(t, "GITHUB_OUTPUT", file.Name())
|
||||
|
||||
a.SetOutput("key", "value")
|
||||
a.SetOutput("key2", "value2")
|
||||
|
||||
// expect an empty stdout buffer
|
||||
if got, want := b.String(), ""; got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
|
||||
// expect the command to be written to the file.
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read temp env file: %s", err)
|
||||
}
|
||||
|
||||
want := fmt.Sprintf("key<<%s\nvalue\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
want += fmt.Sprintf("key2<<%s\nvalue2\n%s", multiLineFileDelim, multiLineFileDelim)
|
||||
if got := string(data); got != want {
|
||||
t.Errorf("expected %q to be %q", got, want)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue