Skip to content

Commit

Permalink
tests: rework passive tests into native go tests
Browse files Browse the repository at this point in the history
this commit re-works the basic.t tests and administers them using go's
native testing suite.

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa authored and ldelossa committed Sep 11, 2020
1 parent 42e5d23 commit a8c961f
Show file tree
Hide file tree
Showing 11 changed files with 1,271 additions and 21 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: CI

on: [push, pull_request]

jobs:
tests:
name: Tests
runs-on: ubuntu-latest
container: docker.io/library/golang:${{ matrix.go }}
strategy:
matrix:
go: ['1.13', '1.14']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: add gox
run: go install github.com/mitchellh/gox
- name: make binaries
run: make all
- name: perform tests
run: go test -v ./...
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ all:
rm -rf dist
mkdir -p dist
gox -ldflags="-w -s" -output="dist/github.com/go-jira/jira-{{.OS}}-{{.Arch}}" -osarch="darwin/amd64 linux/386 linux/amd64 windows/386 windows/amd64" ./...
_t/test_binaries.sh

install:
${MAKE} GOBIN=$$HOME/bin build
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/coryb/oreo v0.0.0-20180804211640-3e1b88fc08f1
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/google/go-cmp v0.5.2
github.com/google/uuid v1.1.1 // indirect
github.com/guelfey/go.dbus v0.0.0-20131113121618-f6a3a2366cc3 // indirect
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/guelfey/go.dbus v0.0.0-20131113121618-f6a3a2366cc3 h1:fngCxKbvZdctIsWj2hYijhAt4iK0JXSSA78B36xP0yI=
Expand Down Expand Up @@ -77,6 +79,7 @@ golang.org/x/net v0.0.0-20171102191033-01c190206fbd h1:CLQSRrSDQMOMkogMxky7XOkER
golang.org/x/net v0.0.0-20171102191033-01c190206fbd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20180727230415-bd9dbc187b6e h1:3dQ4fR8k5KugjVKO0oqSd1odxuk2yaE2CIfxWP2WarQ=
golang.org/x/sys v0.0.0-20180727230415-bd9dbc187b6e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/AlecAivazis/survey.v1 v1.6.1 h1:HyWkjKGBpzhNxrpaKRLDqoa4L1f4cMVBNU4bnVmU8Mw=
gopkg.in/AlecAivazis/survey.v1 v1.6.1/go.mod h1:2Ehl7OqkBl3Xb8VmC4oFW2bItAhnUfzIjrOzwRxCrOU=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
Expand Down
49 changes: 49 additions & 0 deletions test/.jira.d/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
config:
stop: true
endpoint: https://go-jira.atlassian.net
password-source: stdin
user: gojira
login: [email protected]

project: BASIC

queries:
todo: >-
resolution = unresolved {{if .project}}AND project = '{{.project}}'{{end}} AND status = 'To Do'
custom-commands:
- name: env
help: print the JIRA environment variables available to custom commands
script: |-
env | sort | grep JIRA
- name: print-project
help: print the name of the configured project
script: "echo $JIRA_PROJECT"
- name: jira-path
help: print the path the jira command that is running this alias
script: |-
echo {{jira}}
- name: mine
help: display issues assigned to me
script: |-
if [ -n "$JIRA_PROJECT" ]; then
# if `project: ...` configured just list the issues for current project
{{jira}} list --template table --query "resolution = unresolved and assignee=currentuser() and project = $JIRA_PROJECT ORDER BY priority asc, created"
else
# otherwise list issues for all project
{{jira}} list --template table --query "resolution = unresolved and assignee=currentuser() ORDER BY priority asc, created"
fi
- name: argtest
help: testing passing args
script: |-
echo {{args.ARG}}
args:
- name: ARG
help: string to echo for testing
- name: opttest
help: testing passing option flags
script: |-
echo {{options.OPT}}
options:
- name: OPT
help: string to echo for testing
1 change: 1 addition & 0 deletions test/.jira.d/list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template: list
56 changes: 56 additions & 0 deletions test/apilogin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
)

// withApiLogin is a hack to provide an api token on every command, this means keyring
// and gpg is not necessary to run the testing suite.
//
// a buffer containing stdout will be returned to the caller if no error is encountered.
// this still expects a config file is in a parent where the test runs for project
// and endpoint details.
func withApiLogin(login string, token string, cmd *exec.Cmd) (bytes.Buffer, error) {
var buf bytes.Buffer

cmd.Args = append(cmd.Args, "--login", login)

diag := fmt.Sprintf("--- running command: %+v ---\n", cmd.Args)
io.WriteString(os.Stdout, diag)

// write to stdout and also to our buffer
out := io.MultiWriter(&buf, os.Stdout)
cmd.Stdout = out

e := io.MultiWriter(&buf, os.Stderr)
cmd.Stderr = e

in, err := cmd.StdinPipe()
if err != nil {
return buf, err
}

err = cmd.Start()
if err != nil {
return buf, err
}

_, err = io.WriteString(in, token)
if err != nil {
return buf, err
}
in.Close()

err = cmd.Wait()
if err != nil {
return buf, err
}

diag = fmt.Sprintf("--- finished command: %+v ---\n\n", cmd.Args)
io.WriteString(os.Stdout, diag)
return buf, nil
}
98 changes: 98 additions & 0 deletions test/checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package test

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func checkDiff(t *testing.T, buf bytes.Buffer, expect string, formats ...interface{}) {
expect = fmt.Sprintf(expect, formats...)
if !cmp.Equal(expect, buf.String()) {
t.Fatal(
cmp.Diff(
buf.String(),
expect,
),
)
}
}

func checkCreateIssue(t *testing.T, buf bytes.Buffer, endpoint string) string {
out := strings.Split(buf.String(), " ")
if len(out) < 3 {
t.Fatalf("unexpected split count on create output: %v", buf.String())
}
issue := out[1]
expect := fmt.Sprintf("OK %s %s/browse/%s\n", issue, endpoint, issue)
if !cmp.Equal(expect, buf.String()) {
t.Fatal(
cmp.Diff(
buf.String(),
expect,
),
)
}
return issue
}

func checkEditIssue(t *testing.T, buf bytes.Buffer, issue, endpoint string) {
out := strings.Split(buf.String(), " ")
if len(out) < 3 {
t.Fatalf("unexpected split count on create output: %v", buf.String())
}
editedIssue := out[1]
expect := fmt.Sprintf("OK %s %s/browse/%s\n", issue, endpoint, issue)
if !cmp.Equal(expect, buf.String()) {
t.Fatal(
cmp.Diff(
buf.String(),
expect,
),
)
}
if !cmp.Equal(editedIssue, issue) {
t.Fatal(
cmp.Diff(
editedIssue,
issue,
),
)
}
}

func checkIssueInOutput(t *testing.T, buf bytes.Buffer, issue string) {
if !strings.Contains(buf.String(), issue) {
t.Fatalf("issue %s not located in stdout: %s", issue, buf.String())
}
}

func checkIssueNotInOutput(t *testing.T, buf bytes.Buffer, issue string) {
if strings.Contains(buf.String(), issue) {
t.Fatalf("issue %s not located in stdout: %s", issue, buf.String())
}
}

func checkBlockIssue(t *testing.T, buf bytes.Buffer, issue, blocker, endpoint string) {
checkDualIssues(t, buf, blocker, issue, endpoint)
}

func checkDupIssue(t *testing.T, buf bytes.Buffer, issue, duplicate, endpoint string) {
checkDualIssues(t, buf, issue, duplicate, endpoint)
}

func checkDualIssues(t *testing.T, buf bytes.Buffer, first, second, endpoint string) {
lines := strings.Split(buf.String(), "\n")
if len(lines) < 2 {
t.Fatalf("unexpected split count on create output: %v", buf.String())
}

testBuf := bytes.NewBuffer([]byte(lines[0] + "\n"))
checkEditIssue(t, *testBuf, first, endpoint)

testBuf = bytes.NewBuffer([]byte(lines[1] + "\n"))
checkEditIssue(t, *testBuf, second, endpoint)
}
Loading

0 comments on commit a8c961f

Please sign in to comment.