Skip to content

Commit

Permalink
Added test that ensures no duplicate projects are returned
Browse files Browse the repository at this point in the history
  • Loading branch information
w-edd committed Mar 4, 2024
1 parent d5c2bbd commit de10d00
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/totmicro/atlantis-yaml-generator

go 1.21

require github.com/google/go-github v17.0.0+incompatible
require (
github.com/google/go-github v17.0.0+incompatible
github.com/spf13/afero v1.11.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -13,6 +16,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down Expand Up @@ -58,6 +60,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
12 changes: 10 additions & 2 deletions pkg/atlantis/atlantis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func (pf ProjectFolder) Hash() string {
return fmt.Sprintf("%s", pf.Path)
}

type OSFileSystem struct{}

func (OSFileSystem) Walk(root string, walkFn filepath.WalkFunc) error {
return filepath.Walk(root, walkFn)
}

// GenerateAtlantisYAML generates the atlantis.yaml file
func GenerateAtlantisYAML() error {

Expand All @@ -64,6 +70,7 @@ func GenerateAtlantisYAML() error {

// Scan folders to detect projects
projectFoldersList, err := scanProjectFolders(
OSFileSystem{},
config.GlobalConfig.Parameters["terraform-base-dir"],
config.GlobalConfig.Parameters["discovery-mode"],
config.GlobalConfig.Parameters["pattern-detector"],
Expand Down Expand Up @@ -125,9 +132,10 @@ func GenerateAtlantisYAML() error {
return nil
}

func scanProjectFolders(basePath, discoveryMode, patternDetector string) (projectFolders []ProjectFolder, err error) {
// Scans a folder for projects and returns a list of unique projects.
func scanProjectFolders(filesystem helpers.Walkable, basePath, discoveryMode, patternDetector string) (projectFolders []ProjectFolder, err error) {
uniques := helpers.NewSet()
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
err = filesystem.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil {
return err
}
Expand Down
40 changes: 34 additions & 6 deletions pkg/atlantis/atlantis_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package atlantis

import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/totmicro/atlantis-yaml-generator/pkg/config"
"github.com/totmicro/atlantis-yaml-generator/pkg/helpers"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"reflect"
"testing"
)

func TestApplyProjectFilter(t *testing.T) {
Expand Down Expand Up @@ -387,7 +387,11 @@ func TestScanProjectFolders(t *testing.T) {

for _, test := range tests {
t.Run(test.discoveryMode, func(t *testing.T) {
projectFolders, err := scanProjectFolders(test.basePath, test.discoveryMode, test.patternDetector)
projectFolders, err := scanProjectFolders(
OSFileSystem{},
test.basePath,
test.discoveryMode,
test.patternDetector)
if test.expectedError {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -720,3 +724,27 @@ func TestGenerateAtlantisYAML(t *testing.T) {
assert.NoError(t, err)

}

// Tests multiple project hits returns unique projects only.
// e.g. if we scan for *.tf the same project isn't hit twice.
func TestScanProjectFoldersUniques(t *testing.T) {
memFS := afero.NewMemMapFs()
fs := afero.Afero{Fs: memFS}
// Create directories and files
// project3 has multiple hits
afero.WriteFile(fs, "projects_root/project1/main.tf", []byte("content"), 0644)
afero.WriteFile(fs, "projects_root/project2/main.tf", []byte("content"), 0644)
afero.WriteFile(fs, "projects_root/project3/main.tf", []byte("content"), 0644)
afero.WriteFile(fs, "projects_root/project3/outputs.tf", []byte("content"), 0644)

// Use the fs (implementing Walkable) to call scanProjectFolders
projectFolders, err := scanProjectFolders(fs, "projects_root", "single-workspace", `.*\.tf`)
if err != nil {
t.Errorf("scanProjectFolders returned an error: %v", err)
}

// Verify that 3 project folders were returned
if len(projectFolders) != 3 {
t.Errorf("Expected 3 project folders, got %d. Projects %v", len(projectFolders), projectFolders)
}
}
8 changes: 7 additions & 1 deletion pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func MatchesPattern(pattern string, str string) bool {
return matched
}

// Set data structure, using map as the underlying storage
// Set data structure
// Keys are strings and elements must implement Hashable to calculate keys.
type Set struct {
Elements map[string]Hashable
}
Expand Down Expand Up @@ -108,6 +109,11 @@ func (s *Set) List() []Hashable {
return list
}

// Enables use of Set by requiring its elements to be hashable.
type Hashable interface {
Hash() string
}

type Walkable interface {
Walk(root string, walkFn filepath.WalkFunc) error
}

0 comments on commit de10d00

Please sign in to comment.