Skip to content

Commit

Permalink
ginko tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostov6 committed Nov 10, 2021
1 parent d1a95c5 commit 1ca20c3
Show file tree
Hide file tree
Showing 13 changed files with 2,805 additions and 24 deletions.
7 changes: 4 additions & 3 deletions pkg/api/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ParseWithMetadata(b []byte, allTags []string, nTags int, targetBranch strin
err error
tags []string
)
if tags, err = getLastNVersions(allTags, nTags); err != nil {
if tags, err = GetLastNVersions(allTags, nTags); err != nil {
return nil, err
}
versionList := make([]string, 0)
Expand All @@ -93,7 +93,8 @@ func ParseWithMetadata(b []byte, allTags []string, nTags int, targetBranch strin
return Parse(b)
}

func getLastNVersions(tags []string, n int) ([]string, error) {
// GetLastNVersions returns only the last patch version for each major and minor version
func GetLastNVersions(tags []string, n int) ([]string, error) {
if n < 0 {
return nil, fmt.Errorf("n can't be negative")
} else if n == 0 {
Expand Down Expand Up @@ -133,7 +134,7 @@ func getLastNVersions(tags []string, n int) ([]string, error) {
}
}
if n > len(latestVersions) {
return nil, fmt.Errorf("number of tags is greater than the actual number of tags with latest patch:requested %d actual %d", n, len(latestVersions))
return nil, fmt.Errorf("number of tags is greater than the actual number of all tags: wanted - %d, actual - %d", n, len(latestVersions))
}
return latestVersions, nil
}
Expand Down
255 changes: 255 additions & 0 deletions pkg/api/parserGinkgo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package api_test

import (
"testing"

"github.com/gardener/docforge/pkg/api"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

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

var _ = Describe("Parser", func() {
Describe("Version processing", func() {
var (
tags []string
n int
outputTags []string
err error
)
JustBeforeEach(func() {
outputTags, err = api.GetLastNVersions(tags, n)
})
Context("given general use case input", func() {
BeforeEach(func() {
tags = []string{"v1.2.3", "v1.2.8", "v1.1.5", "v1.1.0", "v1.1.3", "v2.0.1", "v2.0.8", "v2.1.0", "v2.0.6"}
n = 4
})

It("should process them correctly", func() {
Expect(outputTags).To(Equal([]string{"v2.1.0", "v2.0.8", "v1.2.8", "v1.1.5"}))
Expect(err).NotTo(HaveOccurred())
})
})
Context("given versions without the v prefix", func() {
BeforeEach(func() {
tags = []string{"1.2.3", "1.2.8", "1.1.5", "1.1.0", "1.1.3", "2.0.1", "2.0.8", "2.1.0", "2.0.6"}
n = 4
})

It("should process them correctly", func() {
Expect(outputTags).To(Equal([]string{"2.1.0", "2.0.8", "1.2.8", "1.1.5"}))
Expect(err).NotTo(HaveOccurred())
})
})
Context("given a tag string with less versions as requested", func() {
BeforeEach(func() {
tags = []string{"v1.2.3", "v1.2.8", "v1.1.5", "v1.1.0", "v1.1.3", "v2.0.1", "v2.0.8", "v2.1.0", "v2.0.6"}
n = 5
})
It("should return the appropriate error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("number of tags is greater than the actual number of all tags: wanted - 5, actual - 4"))
})
})
Context("given a unparsable version", func() {
BeforeEach(func() {
tags = []string{"v1.2.3", "v1.2.8.0"}
n = 1
})
It("should return appropriate error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Error parsing version: v1.2.8.0"))
})
})

Context("given negative number", func() {
BeforeEach(func() {
tags = nil
n = -7
})

It("should throw error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("n can't be negative"))
})
})
Context("given empty version array", func() {
BeforeEach(func() {
tags = []string{}
n = 0
})
Context("and no num tags", func() {
BeforeEach(func() {
n = 0
})
It("should not return error", func() {
Expect(err).NotTo(HaveOccurred())
Expect(outputTags).To(Equal([]string{}))
})
})
Context("and some num tags", func() {
BeforeEach(func() {
n = 2
})
It("should return error that the number of tags is greater than the actual number of all tags", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("number of tags is greater than the actual number of all tags: wanted - 2, actual - 0"))
})
})
})
})
Describe("Parsing with metadata", func() {
var (
manifest []byte
tags []string
nVersions int
targetBranch string
url string
got *api.Documentation
err error
)
JustBeforeEach(func() {
v := map[string]int{}
vars := map[string]string{}

api.SetFlagsVariables(vars)
v[url] = len(tags)
api.SetNVersions(v, v)
got, err = api.ParseWithMetadata(manifest, tags, nVersions, targetBranch)
})

Context("given a general use case", func() {
BeforeEach(func() {
manifest = []byte(`structure:
- name: community
source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md
{{- $vers := Split .versions "," -}}
{{- range $i, $version := $vers -}}
{{- if eq $i 0 }}
- name: docs
{{- else }}
- name: {{$version}}
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md
{{- end }}`)
tags = []string{"v4.9", "v5.7", "v5.7.5", "v6.1", "v7.7"}
nVersions = 4
targetBranch = "master"
url = "https://github.com/Kostov6/documentation/blob/master/.docforge/test.yamls"
})

It("should work as expected", func() {
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(&api.Documentation{
Structure: []*api.Node{
&api.Node{
Name: "community",
Source: "https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "docs",
Source: "https://github.com/gardener/docforge/blob/master/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "v7.7",
Source: "https://github.com/gardener/docforge/blob/v7.7/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "v6.1",
Source: "https://github.com/gardener/docforge/blob/v6.1/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "v5.7.5",
Source: "https://github.com/gardener/docforge/blob/v5.7.5/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "v4.9",
Source: "https://github.com/gardener/docforge/blob/v4.9/integration-test/tested-doc/merge-test/testFile.md",
},
},
}))
})
Context("and no versions are wanted", func() {
BeforeEach(func() {
nVersions = 0
})
It("should only use target branch", func() {
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(&api.Documentation{
Structure: []*api.Node{
&api.Node{
Name: "community",
Source: "https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md",
},
&api.Node{
Name: "docs",
Source: "https://github.com/gardener/docforge/blob/master/integration-test/tested-doc/merge-test/testFile.md",
},
},
}))
})
})

Context("but more versions are requested than provided", func() {
BeforeEach(func() {
nVersions = 5
})
It("should return error that the number of tags is greater than the actual number of all tags", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("number of tags is greater than the actual number of all tags: wanted - 5, actual - 4"))

})
})

Context("but with broken yaml manifest", func() {
BeforeEach(func() {
manifest = []byte(`structure:
-name: community
source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md
{{- $vers := Split .versions "," -}}
{{- range $i, $version := $vers -}}
{{- if eq $i 0 }}
- name: docs
{{- else }}
- name: {{$version}}
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md
{{- end }}`)
})
It("should register the yaml error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("yaml: line 3: mapping values are not allowed in this context"))
})
})
})

Context("but with broken template format", func() {
BeforeEach(func() {
manifest = []byte(`structure:
- name: community
source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md
{{- $vers := Split .versions "," -}}
{{- range $i, $version := $vers -}}
{{- if eq $i 0 }}
- name: docs
{{- else }}
- name: {{$version}}
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md`)
})
It("should register the template error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("template: :11: unexpected EOF"))
})
})
})

})
8 changes: 2 additions & 6 deletions pkg/api/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func TestGetLastNVersions(t *testing.T) {
inputTags: []string{"v1.2.3", "v1.2.8", "v1.1.5", "v1.1.0", "v1.1.3", "v2.0.1", "v2.0.8", "v2.1.0", "v2.0.6"},
inputN: 5,
outputTags: nil,
err: fmt.Errorf("number of tags is greater than the actual number of tags with latest patch:requested %d actual %d", 5, 4),
err: fmt.Errorf("number of tags is greater than the actual number of all tags: wanted - %d, actual - %d", 5, 4),
}, {
inputTags: []string{"1.2.3", "1.2.8", "1.1.5", "1.1.0", "1.1.3", "2.0.1", "2.0.8", "2.1.0", "2.0.6"},
inputN: 4,
Expand All @@ -282,7 +282,7 @@ func TestGetLastNVersions(t *testing.T) {
},
}
for _, test := range tests {
result, resultErr := getLastNVersions(test.inputTags, test.inputN)
result, resultErr := GetLastNVersions(test.inputTags, test.inputN)

if !reflect.DeepEqual(result, test.outputTags) {
t.Errorf("Expected and actual result differ respectively: %s %s", test.outputTags, result)
Expand All @@ -299,7 +299,6 @@ func TestParseWithMetadata(t *testing.T) {
tags []string
nVersions int
b []byte
uri string
want *Documentation
err error
}{
Expand All @@ -318,7 +317,6 @@ func TestParseWithMetadata(t *testing.T) {
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md
{{- end }}`),
"https://github.com/Kostov6/documentation/blob/master/.docforge/test.yamls",
&Documentation{
Structure: []*Node{
&Node{
Expand Down Expand Up @@ -370,7 +368,6 @@ func TestParseWithMetadata(t *testing.T) {
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md
{{- end }}`),
"https://github.com/Kostov6/documentation/blob/master/.docforge/test.yamls",
&Documentation{
Structure: []*Node{
&Node{
Expand Down Expand Up @@ -412,7 +409,6 @@ func TestParseWithMetadata(t *testing.T) {
{{- end }}
source: https://github.com/gardener/docforge/blob/{{$version}}/integration-test/tested-doc/merge-test/testFile.md
{{- end }}`),
"https://github.com/Kostov6/documentation/blob/master/.docforge/test.yamls",
&Documentation{
Structure: []*Node{
&Node{
Expand Down
5 changes: 5 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package git

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

import (
"context"

Expand All @@ -12,12 +14,14 @@ import (
)

// Git interface defines gogit git API
//counterfeiter:generate . Git
type Git interface {
PlainOpen(path string) (Repository, error)
PlainCloneContext(ctx context.Context, path string, isBare bool, o *gogit.CloneOptions) (Repository, error)
}

// Repository interface defines gogit repository API
//counterfeiter:generate . Repository
type Repository interface {
FetchContext(ctx context.Context, o *gogit.FetchOptions) error
Worktree() (RepositoryWorktree, error)
Expand All @@ -26,6 +30,7 @@ type Repository interface {
}

// RepositoryWorktree interface defines gogit worktree API
//counterfeiter:generate . RepositoryWorktree
type RepositoryWorktree interface {
Checkout(opts *gogit.CheckoutOptions) error
}
Expand Down
Loading

0 comments on commit 1ca20c3

Please sign in to comment.