-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start driving validate behaviour from tests
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |