Skip to content

Commit

Permalink
Start driving validate behaviour from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caius committed Apr 21, 2024
1 parent 5593747 commit 2737272
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"bytes"
"io/ioutil"
"net/http"
"os"
"testing"

httpmock "github.com/jarcoal/httpmock"
assert "github.com/stretchr/testify/assert"
)

func TestValidate__FileExistsPathArgument__Response200(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

var outputBuffer bytes.Buffer

os.Chdir(t.TempDir())
os.Mkdir(".semaphore", 0755)
f, err := os.Create(".semaphore/semaphore.yml")
if err != nil {
t.Fatal(err)
}
f.WriteString("---\n")
f.Close()

requestBodySent := ""

httpmock.RegisterResponder("POST", "https://org.semaphoretext.xyz/api/v1alpha/yaml",
func(req *http.Request) (*http.Response, error) {
body, _ := ioutil.ReadAll(req.Body)

requestBodySent = string(body)

return httpmock.NewStringResponse(200, "{\"pipeline_id\": \"\",\"message\":\"YAML definition is valid.\"}"), nil
},
)

RootCmd.SetArgs([]string{"validate", ".semaphore/semaphore.yml"})
RootCmd.SetOutput(&outputBuffer)
RootCmd.Execute()

requestBodyExpected := "{\"yaml_definition\":\"---\\n\"}"

if requestBodyExpected != requestBodySent {
t.Errorf("Expected the API to receive YAML with: %s, got: %s", requestBodyExpected, requestBodySent)
}

assert.Equal(t, outputBuffer.String(), "")
}

func TestValidate__FileMissingPathArgument(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

os.Chdir(t.TempDir())

RootCmd.SetArgs([]string{"validate", "semaphore.yml"})
assert.Panics(t, func() {
RootCmd.Execute()
}, "exit 1")

// Can't check output as utils.CheckWithMessage() writes directly to stderr
}

0 comments on commit 2737272

Please sign in to comment.