mirror of
https://github.com/outbackdingo/ghorg.git
synced 2026-01-27 10:19:03 +00:00
Add ghorgignore filter test (#435)
This commit is contained in:
65
cmd/clone.go
65
cmd/clone.go
@@ -664,35 +664,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
|
||||
}
|
||||
}
|
||||
|
||||
// filter repos down based on ghorgignore if one exists
|
||||
_, err := os.Stat(configs.GhorgIgnoreLocation())
|
||||
if !os.IsNotExist(err) {
|
||||
// Open the file parse each line and remove cloneTargets containing
|
||||
toIgnore, err := readGhorgIgnore()
|
||||
if err != nil {
|
||||
colorlog.PrintErrorAndExit(fmt.Sprintf("Error parsing your ghorgignore, error: %v", err))
|
||||
}
|
||||
|
||||
colorlog.PrintInfo("Using ghorgignore, filtering repos down...")
|
||||
|
||||
filteredCloneTargets := []scm.Repo{}
|
||||
var flag bool
|
||||
for _, cloned := range cloneTargets {
|
||||
flag = false
|
||||
for _, ignore := range toIgnore {
|
||||
if strings.Contains(cloned.URL, ignore) {
|
||||
flag = true
|
||||
}
|
||||
}
|
||||
|
||||
if !flag {
|
||||
filteredCloneTargets = append(filteredCloneTargets, cloned)
|
||||
}
|
||||
}
|
||||
|
||||
cloneTargets = filteredCloneTargets
|
||||
|
||||
}
|
||||
cloneTargets = filterWithGhorgignore(cloneTargets)
|
||||
|
||||
totalResourcesToClone, reposToCloneCount, snippetToCloneCount, wikisToCloneCount := getCloneableInventory(cloneTargets)
|
||||
if os.Getenv("GHORG_CLONE_WIKI") == "true" && os.Getenv("GHORG_CLONE_SNIPPETS") == "true" {
|
||||
@@ -1227,3 +1199,38 @@ func setOutputDirName(argz []string) {
|
||||
outputDirName = outputDirName + "_backup"
|
||||
}
|
||||
}
|
||||
|
||||
// filter repos down based on ghorgignore if one exists
|
||||
func filterWithGhorgignore(cloneTargets []scm.Repo) []scm.Repo {
|
||||
|
||||
_, err := os.Stat(configs.GhorgIgnoreLocation())
|
||||
if !os.IsNotExist(err) {
|
||||
// Open the file parse each line and remove cloneTargets containing
|
||||
toIgnore, err := readGhorgIgnore()
|
||||
if err != nil {
|
||||
colorlog.PrintErrorAndExit(fmt.Sprintf("Error parsing your ghorgignore, error: %v", err))
|
||||
}
|
||||
|
||||
colorlog.PrintInfo("Using ghorgignore, filtering repos down...")
|
||||
|
||||
filteredCloneTargets := []scm.Repo{}
|
||||
var flag bool
|
||||
for _, cloned := range cloneTargets {
|
||||
flag = false
|
||||
for _, ignore := range toIgnore {
|
||||
if strings.Contains(cloned.URL, ignore) {
|
||||
flag = true
|
||||
}
|
||||
}
|
||||
|
||||
if !flag {
|
||||
filteredCloneTargets = append(filteredCloneTargets, cloned)
|
||||
}
|
||||
}
|
||||
|
||||
cloneTargets = filteredCloneTargets
|
||||
|
||||
}
|
||||
|
||||
return cloneTargets
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -327,3 +328,69 @@ func UnsetEnv(prefix string) (restore func()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_filterWithGhorgignore(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
cloneTargets []scm.Repo
|
||||
expectedResult []scm.Repo
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
{
|
||||
name: "filters out repo named 'shouldbeignored'",
|
||||
cloneTargets: []scm.Repo{
|
||||
{Name: "shouldbeignored", URL: "https://github.com/org/shouldbeignored"},
|
||||
{Name: "bar", URL: "https://github.com/org/bar"},
|
||||
},
|
||||
expectedResult: []scm.Repo{
|
||||
{Name: "bar", URL: "https://github.com/org/bar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "filters out repo named 'shouldbeignored'",
|
||||
cloneTargets: []scm.Repo{
|
||||
{Name: "foo", URL: "https://github.com/org/foo"},
|
||||
{Name: "shouldbeignored", URL: "https://github.com/org/shouldbeignored"},
|
||||
},
|
||||
expectedResult: []scm.Repo{
|
||||
{Name: "foo", URL: "https://github.com/org/foo"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tmpfile, err := createTempFileWithContent("shouldbeignored")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
os.Setenv("GHORG_IGNORE_PATH", tmpfile.Name())
|
||||
|
||||
got := filterWithGhorgignore(tt.cloneTargets)
|
||||
if !reflect.DeepEqual(got, tt.expectedResult) {
|
||||
t.Errorf("filterWithGhorgignore() = %v, want %v", got, tt.expectedResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// createTempFileWithContent will create
|
||||
func createTempFileWithContent(content string) (*os.File, error) {
|
||||
tmpfile, err := os.CreateTemp("", "ghorgtest")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := tmpfile.Write([]byte(content)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tmpfile, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user