mirror of
https://github.com/gabrie30/ghorg.git
synced 2025-08-11 00:37:10 +02:00
327 lines
6.7 KiB
Go
327 lines
6.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gabrie30/ghorg/scm"
|
|
)
|
|
|
|
func TestShouldLowerRegularString(t *testing.T) {
|
|
|
|
upperName := "RepoName"
|
|
parseParentFolder([]string{upperName})
|
|
|
|
if parentFolder != "reponame" {
|
|
t.Errorf("Wrong folder name, expected: %s, got: %s", upperName, parentFolder)
|
|
}
|
|
}
|
|
|
|
func TestShouldNotChangeLowerCasedRegularString(t *testing.T) {
|
|
|
|
lowerName := "repo_name"
|
|
parseParentFolder([]string{lowerName})
|
|
|
|
if parentFolder != "repo_name" {
|
|
t.Errorf("Wrong folder name, expected: %s, got: %s", lowerName, parentFolder)
|
|
}
|
|
}
|
|
|
|
func TestReplaceDashWithUnderscore(t *testing.T) {
|
|
|
|
want := "repo-name"
|
|
lowerName := "repo-name"
|
|
parseParentFolder([]string{lowerName})
|
|
|
|
if parentFolder != want {
|
|
t.Errorf("Wrong folder name, expected: %s, got: %s", want, parentFolder)
|
|
}
|
|
}
|
|
|
|
func TestShouldNotChangeNonLettersString(t *testing.T) {
|
|
|
|
numberName := "1234567_8"
|
|
parseParentFolder([]string{numberName})
|
|
|
|
if parentFolder != "1234567_8" {
|
|
t.Errorf("Wrong folder name, expected: %s, got: %s", numberName, parentFolder)
|
|
}
|
|
}
|
|
|
|
type MockGitClient struct{}
|
|
|
|
func NewMockGit() MockGitClient {
|
|
return MockGitClient{}
|
|
}
|
|
|
|
func (g MockGitClient) Clone(repo scm.Repo) error {
|
|
_, err := ioutil.TempDir(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), repo.Name)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) SetOrigin(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) Checkout(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) Clean(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) UpdateRemote(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) Pull(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) Reset(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func (g MockGitClient) FetchAll(repo scm.Repo) error {
|
|
return nil
|
|
}
|
|
|
|
func TestInitialClone(t *testing.T) {
|
|
defer UnsetEnv("GHORG_")()
|
|
dir, err := ioutil.TempDir("", "ghorg_test_initial")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", dir)
|
|
os.Setenv("GHORG_CONCURRENCY", "1")
|
|
var testRepos = []scm.Repo{
|
|
{
|
|
Name: "testRepoOne",
|
|
},
|
|
{
|
|
Name: "testRepoTwo",
|
|
},
|
|
}
|
|
|
|
mockGit := NewMockGit()
|
|
CloneAllRepos(mockGit, testRepos)
|
|
got, _ := os.ReadDir(dir)
|
|
expected := len(testRepos)
|
|
if len(got) != expected {
|
|
t.Errorf("Wrong number of repos in clone, expected: %v, got: %v", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestMatchPrefix(t *testing.T) {
|
|
defer UnsetEnv("GHORG_")()
|
|
dir, err := ioutil.TempDir("", "ghorg_test_match_prefix")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", dir)
|
|
os.Setenv("GHORG_CONCURRENCY", "1")
|
|
os.Setenv("GHORG_MATCH_PREFIX", "test")
|
|
var testRepos = []scm.Repo{
|
|
{
|
|
Name: "testRepoOne",
|
|
},
|
|
{
|
|
Name: "testRepoTwo",
|
|
},
|
|
{
|
|
Name: "testRepoThree",
|
|
},
|
|
{
|
|
Name: "nottestRepoTwo",
|
|
},
|
|
{
|
|
Name: "nottestRepoThree",
|
|
},
|
|
}
|
|
|
|
mockGit := NewMockGit()
|
|
CloneAllRepos(mockGit, testRepos)
|
|
got, _ := os.ReadDir(dir)
|
|
expected := 3
|
|
if len(got) != expected {
|
|
t.Errorf("Wrong number of repos in clone, expected: %v, got: %v", expected, len(got))
|
|
}
|
|
}
|
|
|
|
func TestExcludeMatchPrefix(t *testing.T) {
|
|
defer UnsetEnv("GHORG_")()
|
|
dir, err := ioutil.TempDir("", "ghorg_test_exclude_match_prefix")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", dir)
|
|
os.Setenv("GHORG_CONCURRENCY", "1")
|
|
os.Setenv("GHORG_EXCLUDE_MATCH_PREFIX", "test")
|
|
var testRepos = []scm.Repo{
|
|
{
|
|
Name: "testRepoOne",
|
|
},
|
|
{
|
|
Name: "testRepoTwo",
|
|
},
|
|
{
|
|
Name: "testRepoThree",
|
|
},
|
|
{
|
|
Name: "nottestRepoTwo",
|
|
},
|
|
{
|
|
Name: "nottestRepoThree",
|
|
},
|
|
}
|
|
|
|
mockGit := NewMockGit()
|
|
CloneAllRepos(mockGit, testRepos)
|
|
got, _ := os.ReadDir(dir)
|
|
expected := 2
|
|
if len(got) != expected {
|
|
t.Errorf("Wrong number of repos in clone, expected: %v, got: %v", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestMatchRegex(t *testing.T) {
|
|
defer UnsetEnv("GHORG_")()
|
|
dir, err := ioutil.TempDir("", "ghorg_test_match_regex")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", dir)
|
|
os.Setenv("GHORG_CONCURRENCY", "1")
|
|
os.Setenv("GHORG_MATCH_REGEX", "^test-")
|
|
var testRepos = []scm.Repo{
|
|
{
|
|
Name: "test-RepoOne",
|
|
},
|
|
{
|
|
Name: "test-RepoTwo",
|
|
},
|
|
{
|
|
Name: "test-RepoThree",
|
|
},
|
|
{
|
|
Name: "nottestRepoTwo",
|
|
},
|
|
{
|
|
Name: "nottestRepoThree",
|
|
},
|
|
}
|
|
|
|
mockGit := NewMockGit()
|
|
CloneAllRepos(mockGit, testRepos)
|
|
got, _ := os.ReadDir(dir)
|
|
expected := 3
|
|
if len(got) != expected {
|
|
t.Errorf("Wrong number of repos in clone, expected: %v, got: %v", expected, got)
|
|
}
|
|
}
|
|
|
|
func TestExcludeMatchRegex(t *testing.T) {
|
|
defer UnsetEnv("GHORG_")()
|
|
testDescriptor := "ghorg_test_exclude_match_regex"
|
|
dir, err := ioutil.TempDir("", testDescriptor)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", dir)
|
|
os.Setenv("GHORG_CONCURRENCY", "1")
|
|
os.Setenv("GHORG_OUTPUT_DIR", testDescriptor)
|
|
os.Setenv("GHORG_EXCLUDE_MATCH_REGEX", "^test-")
|
|
var testRepos = []scm.Repo{
|
|
{
|
|
Name: "test-RepoOne",
|
|
},
|
|
{
|
|
Name: "test-RepoTwo",
|
|
},
|
|
{
|
|
Name: "test-RepoThree",
|
|
},
|
|
{
|
|
Name: "nottestRepoTwo",
|
|
},
|
|
{
|
|
Name: "nottestRepoThree",
|
|
},
|
|
}
|
|
|
|
mockGit := NewMockGit()
|
|
CloneAllRepos(mockGit, testRepos)
|
|
got, _ := os.ReadDir(dir)
|
|
expected := 2
|
|
if len(got) != expected {
|
|
t.Errorf("Wrong number of repos in clone, expected: %v, got: %v", expected, got)
|
|
}
|
|
}
|
|
|
|
// UnsetEnv unsets all envars having prefix and returns a function
|
|
// that restores the env. Any newly added envars having prefix are
|
|
// also unset by restore. It is idiomatic to use with a defer.
|
|
//
|
|
// Note that modifying the env may have unpredictable results when
|
|
// tests are run with t.Parallel.
|
|
func UnsetEnv(prefix string) (restore func()) {
|
|
before := map[string]string{}
|
|
|
|
for _, e := range os.Environ() {
|
|
if !strings.HasPrefix(e, prefix) {
|
|
continue
|
|
}
|
|
|
|
parts := strings.SplitN(e, "=", 2)
|
|
before[parts[0]] = parts[1]
|
|
|
|
os.Unsetenv(parts[0])
|
|
}
|
|
|
|
return func() {
|
|
after := map[string]string{}
|
|
|
|
for _, e := range os.Environ() {
|
|
if !strings.HasPrefix(e, prefix) {
|
|
continue
|
|
}
|
|
|
|
parts := strings.SplitN(e, "=", 2)
|
|
after[parts[0]] = parts[1]
|
|
|
|
// Check if the envar previously existed
|
|
v, ok := before[parts[0]]
|
|
if !ok {
|
|
// This is a newly added envar with prefix, zap it
|
|
os.Unsetenv(parts[0])
|
|
continue
|
|
}
|
|
|
|
if parts[1] != v {
|
|
// If the envar value has changed, set it back
|
|
os.Setenv(parts[0], v)
|
|
}
|
|
}
|
|
|
|
// Still need to check if there have been any deleted envars
|
|
for k, v := range before {
|
|
if _, ok := after[k]; !ok {
|
|
// k is not present in after, so we set it.
|
|
os.Setenv(k, v)
|
|
}
|
|
}
|
|
}
|
|
}
|