Skip to content

Commit

Permalink
Convert tests to gomega and sclevine/spec
Browse files Browse the repository at this point in the history
  • Loading branch information
garethjevans committed Mar 10, 2022
1 parent 8e18071 commit 74fe9d2
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 73 deletions.
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ require (
github.com/google/go-containerregistry v0.8.0
github.com/google/go-github/v32 v32.1.0
github.com/iancoleman/strcase v0.2.0
github.com/onsi/gomega v1.18.1
github.com/pelletier/go-toml v1.9.4
github.com/rakyll/statik v0.1.7
github.com/sclevine/spec v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
google.golang.org/api v0.71.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand All @@ -32,7 +33,6 @@ require (
github.com/antchfx/xmlquery v1.3.9 // indirect
github.com/antchfx/xpath v1.2.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.11.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.12+incompatible // indirect
github.com/docker/distribution v2.8.0+incompatible // indirect
github.com/docker/docker v20.10.12+incompatible // indirect
Expand All @@ -47,11 +47,9 @@ require (
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/klauspost/compress v1.14.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/onsi/gomega v1.18.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
Expand All @@ -67,4 +65,5 @@ require (
google.golang.org/genproto v0.0.0-20220304144024-325a89244dc8 // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
Expand Down
29 changes: 29 additions & 0 deletions octo/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package octo_test

import (
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"testing"
)

func TestUnit(t *testing.T) {
suite := spec.New("octo", spec.Report(report.Terminal{}))
suite("Tests", testTestGeneration)
suite.Run(t)
}
149 changes: 80 additions & 69 deletions octo/test_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package octo_test

import (
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/pipeline-builder/octo"
"github.com/paketo-buildpacks/pipeline-builder/octo/actions"
"github.com/stretchr/testify/assert"
"github.com/sclevine/spec"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
Expand All @@ -16,92 +33,86 @@ type jobs struct {
Jobs map[string]*actions.Job `yaml:jobs`
}

func TestContributeTest_Default(t *testing.T) {
dir, descriptor := setUp(t)
defer os.RemoveAll(dir)

err := ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte{}, 0644)
assert.NoError(t, err)

contribution, err := octo.ContributeTest(descriptor)
assert.NoError(t, err)

assert.Equal(t, ".github/workflows/tests.yml", contribution.Path)
t.Log(string(contribution.Content))

var workflow jobs
err = yaml.Unmarshal(contribution.Content, &workflow)
assert.NoError(t, err)

assert.Equal(t, 2, len(workflow.Jobs))
assert.NotNil(t, workflow.Jobs["unit"])
assert.Nil(t, workflow.Jobs["integration"])
func testTestGeneration(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

steps := workflow.Jobs["unit"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test ./...")
}
dir string
descriptor octo.Descriptor
)

context("Generate Tests", func() {
it.Before(func() {
var err error
dir, err = ioutil.TempDir("", "main-package")
Expect(err).To(Not(HaveOccurred()))

func TestContributeTest_IntegrationTests(t *testing.T) {
dir, descriptor := setUp(t)
defer os.RemoveAll(dir)
Expect(os.Mkdir(filepath.Join(dir, ".github"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"), []byte(`---
github:
username: ${{ secrets.JAVA_GITHUB_USERNAME }}
token: ${{ secrets.JAVA_GITHUB_TOKEN }}
err := ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte{}, 0644)
assert.NoError(t, err)
package:
repository: gcr.io/paketo-buildpacks/dummy
register: true
registry_token: ${{ secrets.JAVA_GITHUB_TOKEN }}
`), 0644)).To(Succeed())

err = os.Mkdir(filepath.Join(dir, "integration"), 0755)
assert.NoError(t, err)
descriptor, err = octo.NewDescriptor(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"))
Expect(err).To(Not(HaveOccurred()))

err = ioutil.WriteFile(filepath.Join(dir, "integration", "main.go"), []byte{}, 0644)
assert.NoError(t, err)
Expect(ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte{}, 0644)).To(Succeed())
})

contribution, err := octo.ContributeTest(descriptor)
assert.NoError(t, err)
it.After(func() {
Expect(os.RemoveAll(dir)).To(Succeed())
})

assert.Equal(t, ".github/workflows/tests.yml", contribution.Path)
t.Log(string(contribution.Content))
it("will contribute a unit test pipeline", func() {
contribution, err := octo.ContributeTest(descriptor)
Expect(err).To(Not(HaveOccurred()))

var workflow jobs
err = yaml.Unmarshal(contribution.Content, &workflow)
assert.NoError(t, err)
Expect(contribution.Path).To(Equal(".github/workflows/tests.yml"))

assert.Equal(t, 3, len(workflow.Jobs))
assert.NotNil(t, workflow.Jobs["unit"])
assert.NotNil(t, workflow.Jobs["integration"])
var workflow jobs
Expect(yaml.Unmarshal(contribution.Content, &workflow)).To(Succeed())

steps := workflow.Jobs["unit"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test -short ./...")
Expect(len(workflow.Jobs)).To(Equal(2))
Expect(workflow.Jobs["unit"]).To(Not(BeNil()))
Expect(workflow.Jobs["integration"]).To(BeNil())

steps = workflow.Jobs["integration"].Steps
assert.Contains(t, steps[len(steps) - 1].Run, "richgo test ./integration/...")
}
steps := workflow.Jobs["unit"].Steps
Expect(steps[len(steps) - 1].Run).Should(ContainSubstring("richgo test ./..."))
})

func setUp(t *testing.T) (string, octo.Descriptor ){
dir, err := ioutil.TempDir("", "main-package")
assert.NoError(t, err)
context("there are integration tests", func() {
it.Before(func() {
Expect(os.Mkdir(filepath.Join(dir, "integration"), 0755)).To(Succeed())
Expect( ioutil.WriteFile(filepath.Join(dir, "integration", "main.go"), []byte{}, 0644)).To(Succeed())
})

err = os.Mkdir(filepath.Join(dir, ".github"), 0755)
assert.NoError(t, err)
it("will contribute a unit test and an integration test pipeline", func() {
contribution, err := octo.ContributeTest(descriptor)
Expect(err).To(Not(HaveOccurred()))

err = ioutil.WriteFile(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"), []byte(`---
github:
username: ${{ secrets.JAVA_GITHUB_USERNAME }}
token: ${{ secrets.JAVA_GITHUB_TOKEN }}
Expect(contribution.Path).To(Equal(".github/workflows/tests.yml"))

package:
repository: gcr.io/paketo-buildpacks/dummy
register: true
registry_token: ${{ secrets.JAVA_GITHUB_TOKEN }}
`), 0644)
assert.NoError(t, err)
var workflow jobs
Expect(yaml.Unmarshal(contribution.Content, &workflow)).To(Succeed())

descriptor, err := octo.NewDescriptor(filepath.Join(dir, ".github", "pipeline-descriptor.yaml"))
assert.NoError(t, err)
Expect(len(workflow.Jobs)).To(Equal(3))
Expect(workflow.Jobs["unit"]).To(Not(BeNil()))
Expect(workflow.Jobs["integration"]).To(Not(BeNil()))

t.Logf("%+v", descriptor)
unitSteps := workflow.Jobs["unit"].Steps
Expect(unitSteps[len(unitSteps) - 1].Run).Should(ContainSubstring("richgo test ./... -run Unit"))

integrationSteps := workflow.Jobs["integration"].Steps
Expect(integrationSteps[len(integrationSteps) - 1].Run).Should(ContainSubstring("richgo test ./integration/... -run Integration"))
})
})

assert.NotNil(t, descriptor.Package)
})

return dir, descriptor
}
}

0 comments on commit 74fe9d2

Please sign in to comment.