Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ginkgo tests #94

Merged
merged 14 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ jobs:
with:
go-version: 1.18
- run: make test
name: Run Tests
name: Run tests and generate coverage report
shell: bash
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG_ID: ${{ secrets.OPENAI_ORG_ID }}
- uses: codecov/codecov-action@v3
with:
files: ./cover.out
fail_ci_if_error: true
- run: go vet -v ./...
build-all:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ last-processed.json
.copilot-ops.local.yaml

# development files
bin/
bin/

# coverage files should be generated within CI
cover.out
34 changes: 22 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ build:
@ echo ./copilot-ops -h "# run me!"
.PHONY: build

test: build lint
@ echo ▶️ go test
go clean -testcache ./...
go test -v ./...
@ echo ✅ go test
@ echo ▶️ go vet
go vet ./...
@ echo ✅ go vet
.PHONY: test


##@ Development

Expand All @@ -24,8 +14,11 @@ lint: golangci-lint ## Lint source code
$(GOLANGCILINT) run ./...
@ echo "✅ golangci-lint run"

# .PHONY: test
# test: lint ginkgo ## Run tests.
.PHONY: test
test: lint ginkgo ## Run tests.
@ echo "▶️ ginkgo test"
$(GINKGO) --coverprofile "cover.out" ./...
@ echo "✅ ginkgo test"

##@ Build Dependencies

Expand All @@ -42,5 +35,22 @@ GOLANGCILINT := $(LOCALBIN)/golangci-lint
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
golangci-lint: $(GOLANGCILINT) ## Download golangci-lint
$(GOLANGCILINT): $(LOCALBIN)
@ echo "▶️ Downloading golangci-lint"
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
@ echo "✅ Downloading golangci-lint"

.PHONY: ginkgo
GINKGO := $(LOCALBIN)/ginkgo
ginkgo: $(GINKGO) ## Download ginkgo
$(GINKGO): $(LOCALBIN)
@ echo "▶️ Downloading ginkgo@v2"
GOBIN=$(LOCALBIN) go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo
@ echo "✅ Downloaded ginkgo"


##@ Build Dependencies
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
@ echo "▶️ Local binary directory not present, creating..."
mkdir -p $(LOCALBIN)
@ echo "✅ Local binary directory created"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# copilot-ops

[![Go Report Card](https://goreportcard.com/badge/github.com/redhat-et/copilot-ops)](https://goreportcard.com/report/github.com/redhat-et/copilot-ops)

`copilot-ops` is a CLI tool that boosts up any "devops repo" to a ninja level of *Artificially Intelligent Ops Repo*.

## Requirements
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/redhat-et/copilot-ops
go 1.18

require (
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
)
Expand All @@ -20,6 +22,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -136,6 +137,10 @@ github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamh
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
Expand Down Expand Up @@ -249,6 +254,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -456,6 +463,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ func NewRootCmd() *cobra.Command {
// the root command shows the available subcommands
cmd := &cobra.Command{
Use: "copilot-ops",

Long: `copilot-ops is a workflow automation tool that proposes an intelligent patches on a repo,
using natural language AI engines (openai.com codex bring-your-own-token),
and can be used to implement github bots, editor extensions, and more.
`,

Example: ` copilot-ops generate --help`,

// Usage on every error is too noisy and makes it harder
Expand Down
68 changes: 68 additions & 0 deletions pkg/cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-et/copilot-ops/pkg/openai"
)

func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cmd Suite")
}

// OpenAITestServer Creates a mocked OpenAI server which can pretend to handle requests during testing.
func OpenAITestServer() *httptest.Server {
return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var resBytes []byte
switch r.URL.Path {
case "/v1/edits":
res := openai.EditResponse{
Response: openai.Response{
Choices: []openai.Choice{
{
Text: `
# @path/to/kubernetes.yaml
apiVersion: v1
kind: Pod
metadata:
name: cute-cats
spec:
priority: high
`,
Index: 0,
},
},
},
}
resBytes, _ = json.Marshal(res)
fmt.Fprint(w, string(resBytes))
return
case "/v1/completions":
res := openai.CompletionResponse{
ID: "completion id",
Response: openai.Response{
Choices: []openai.Choice{
{
Text: "choice 1",
Index: 0,
},
},
},
}
resBytes, _ = json.Marshal(res)
fmt.Fprintln(w, string(resBytes))
return
default:
// the endpoint doesn't exist
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
return
}
}))
}
17 changes: 17 additions & 0 deletions pkg/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

cmd "github.com/redhat-et/copilot-ops/pkg/cmd"
)

var _ = Describe("Root command", func() {
When("root command is created", func() {
It("contains edit and generate", func() {
rootCmd := cmd.NewRootCmd()
Expect(rootCmd).NotTo(BeNil())
})
})
})
6 changes: 4 additions & 2 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Config) Load() error {
viper.SetConfigName(".copilot-ops") // name of config file (without extension)

if err := viper.MergeInConfig(); err != nil {
var configFileNotFound *viper.ConfigFileNotFoundError
var configFileNotFound viper.ConfigFileNotFoundError
if ok := errors.As(err, &configFileNotFound); !ok {
return err // allow no config file
}
Expand All @@ -60,7 +60,7 @@ func (c *Config) Load() error {
viper.SetConfigName(".copilot-ops.local")

if err := viper.MergeInConfig(); err != nil {
var configFileNotFound *viper.ConfigFileNotFoundError
var configFileNotFound viper.ConfigFileNotFoundError
if ok := errors.As(err, &configFileNotFound); !ok {
return err // allow no config file
}
Expand All @@ -75,6 +75,8 @@ func (c *Config) Load() error {
return nil
}

// FindFileset Returns a fileset with the matching name,
// or nil if none exists.
func (c *Config) FindFileset(name string) *ConfigFilesets {
for _, fileset := range c.Filesets {
if fileset.Name == name {
Expand Down
42 changes: 42 additions & 0 deletions pkg/cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

cmd "github.com/redhat-et/copilot-ops/pkg/cmd"
)

var _ = Describe("Config", func() {
var config *cmd.Config
When("config is being loaded", func() {
BeforeEach(func() {
// generate an empty config
config = &cmd.Config{}
})
When("filesets are provided", func() {
BeforeEach(func() {
config.Filesets = []cmd.ConfigFilesets{
{
Name: "test",
Files: []string{"test.txt"},
},
}
})

It("finds the correct filesets", func() {
// config should find a fileset named "test"
Expect(config.FindFileset("test")).NotTo(BeNil())
// config should not find a fileset named "test2"
Expect(config.FindFileset("test2")).To(BeNil())
})

It("is case sensitive", func() {
// config should not find a fileset named "test"
Expect(config.FindFileset("test")).NotTo(BeNil())
// config should find a fileset named "TEST"
Expect(config.FindFileset("TEST")).To(BeNil())
})
})
})
})
26 changes: 18 additions & 8 deletions pkg/cmd/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ package cmd

// Define the names of flags used in commands.
const (
FlagRequest = "request"
FlagWrite = "write"
FlagPath = "path"
FlagFiles = "file"
FlagFilesets = "fileset"
FlagNTokens = "ntokens"
FlagNCompletions = "ncompletions"
FlagOutputType = "output"
FlagRequestFull = "request"
FlagRequestShort = "r"
FlagWriteFull = "write"
FlagWriteShort = "w"
FlagPathFull = "path"
FlagPathShort = "p"
FlagFilesFull = "file"
FlagFilesShort = "f"
FlagFilesetsFull = "fileset"
FlagFilesetsShort = "s"
FlagNTokensFull = "ntokens"
FlagNTokensShort = "n"
FlagNCompletionsFull = "ncompletions"
FlagNCompletionsShort = "c"
FlagOpenAIURLFull = "openai-url"
FlagOpenAIURLShort = "d"
FlagOutputTypeFull = "output"
FlagOutputTypeShort = "o"
)

// COMMAND Constants which define the names of commands used in the CLI.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewEditCmd() *cobra.Command {

// flag to add a file
cmd.Flags().StringP(
FlagFiles, "f", "",
FlagFilesFull, FlagFilesShort, "",
"File path to the document which should be edited.",
)

Expand Down
43 changes: 43 additions & 0 deletions pkg/cmd/edit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd_test

import (
"net/http/httptest"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-et/copilot-ops/pkg/cmd"
"github.com/spf13/cobra"
)

var _ = Describe("Edit", func() {
var c *cobra.Command

BeforeEach(func() {
// create command
c = cmd.NewEditCmd()
Expect(c).NotTo(BeNil())
})

When("OpenAI server exists", func() {
var ts *httptest.Server
BeforeEach(func() {
ts = OpenAITestServer()
})

JustBeforeEach(func() {
ts.Start()
err := c.Flags().Set(cmd.FlagOpenAIURLFull, ts.URL)
Expect(err).To(BeNil())
})

AfterEach(func() {
defer ts.Close()
})

It("works", func() {
err := cmd.RunEdit(c, []string{})
Expect(err).To(BeNil())
})

})
})
Loading