github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/deployment/internal/repodiscovery/repodiscovery_test.go (about) 1 package repodiscovery 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 "reflect" 8 "testing" 9 10 "github.com/henvic/wedeploycli/inspector" 11 "gopkg.in/src-d/go-git.v4/plumbing" 12 ) 13 14 func TestMain(m *testing.M) { 15 cleanup() 16 setup() 17 18 ec := m.Run() 19 cleanup() 20 os.Exit(ec) 21 } 22 23 func cleanup() { 24 var list = []string{ 25 "project", 26 "project-with-git", 27 "service-with-git", 28 "service-with-git-init-only", 29 "service-with-git-no-upstream", 30 "service-without-git", 31 } 32 33 for _, mock := range list { 34 if err := os.RemoveAll(filepath.Join("mocks", mock)); err != nil { 35 panic(err) 36 } 37 } 38 } 39 40 func setup() { 41 if !existsDependency("tar") { 42 return 43 } 44 45 cmd := exec.Command("tar", "xjf", "mocks.tar.bz2") 46 cmd.Dir = "./mocks" 47 cmd.Stderr = os.Stderr 48 49 if err := cmd.Run(); err != nil { 50 panic(err) 51 } 52 } 53 54 type repoCase struct { 55 path string 56 repositories []Repository 57 repoless []string 58 err error 59 } 60 61 var repoCases = []repoCase{ 62 repoCase{ 63 path: "mocks/project", 64 repositories: []Repository{ 65 Repository{ 66 Services: []string{"service1"}, 67 Path: "service-with-git-1", 68 Commit: "53f46851d481468d34eef9c868eceaad8778e60c", 69 CommitAuthor: "Henrique Vicente", 70 CommitAuthorEmail: "henriquevicente@gmail.com", 71 CommitMessage: "Adding LCP.json.\n", 72 CommitDate: "2018-08-23 19:16:31 -0400 -0400", 73 Branch: "master", 74 CleanWorkingTree: true, 75 }, 76 Repository{ 77 Services: []string{"service2"}, 78 Path: "service-with-git-2", 79 Commit: "c17f3c056d0facfb7d4c04f32d6191deb9b9f95a", 80 CommitAuthor: "Henrique Vicente", 81 CommitAuthorEmail: "henriquevicente@gmail.com", 82 CommitMessage: "Adding service 2.\n", 83 CommitDate: "2018-08-23 19:37:40 -0400 -0400", 84 Branch: "master", 85 CleanWorkingTree: true, 86 }, 87 }, 88 repoless: []string{"nogit"}, 89 }, 90 repoCase{ 91 path: "mocks/project-with-git", 92 repositories: []Repository{ 93 Repository{ 94 Services: []string{"service", "nested"}, 95 Path: "", 96 Origin: "https://github.com/example/project-with-git", 97 Commit: "0bf3c655b40eb018978a31f208376c94b2529a08", 98 CommitAuthor: "Henrique Vicente", 99 CommitAuthorEmail: "henriquevicente@gmail.com", 100 CommitMessage: "Adding project.\n", 101 CommitDate: "2018-08-24 02:19:11 -0400 -0400", 102 Branch: "master", 103 }, 104 }, 105 }, 106 repoCase{ 107 path: "mocks/service-with-git", 108 repositories: []Repository{ 109 Repository{ 110 Services: []string{"withgit"}, 111 Path: "", 112 Origin: "https://github.com/example/service-with-git", 113 Commit: "e9bfabfb987a4594321a2b7045bd9abae21d69b3", 114 CommitAuthor: "Henrique Vicente", 115 CommitAuthorEmail: "henriquevicente@gmail.com", 116 CommitMessage: "Adding service.\n", 117 CommitDate: "2018-08-23 19:18:51 -0400 -0400", 118 Branch: "master", 119 CleanWorkingTree: true, 120 }, 121 }, 122 }, 123 repoCase{ 124 path: "mocks/service-with-git-no-upstream", 125 repositories: []Repository{ 126 Repository{ 127 Services: []string{"withgit"}, 128 Path: "", 129 Origin: "https://github.com/example/service-with-git", 130 Commit: "e9bfabfb987a4594321a2b7045bd9abae21d69b3", 131 CommitAuthor: "Henrique Vicente", 132 CommitAuthorEmail: "henriquevicente@gmail.com", 133 CommitMessage: "Adding service.\n", 134 CommitDate: "2018-08-23 19:18:51 -0400 -0400", 135 Branch: "master", 136 CleanWorkingTree: true, 137 }, 138 }, 139 }, 140 repoCase{ 141 path: "mocks/service-without-git", 142 repoless: []string{"without"}, 143 }, 144 repoCase{ 145 path: "mocks/service-with-git-init-only", 146 err: plumbing.ErrReferenceNotFound, 147 }, 148 } 149 150 func TestDiscover(t *testing.T) { 151 if !existsDependency("tar") { 152 t.Skip("tar not found in system") 153 } 154 155 for _, rc := range repoCases { 156 i := inspector.ContextOverview{} 157 158 if err := i.Load(rc.path); err != nil { 159 panic(err) 160 } 161 162 d := Discover{ 163 Path: rc.path, 164 Services: i.Services, 165 } 166 167 repositories, repoless, err := d.Run() 168 169 if !reflect.DeepEqual(repositories, rc.repositories) { 170 t.Errorf("Expected repositories to be %+v, got %+v instead", rc.repositories, repositories) 171 } 172 173 if !reflect.DeepEqual(repoless, rc.repoless) { 174 t.Errorf("Expected repoless to be %v, got %v instead", rc.repoless, repoless) 175 } 176 177 if err != rc.err { 178 t.Errorf("Expected error to be %v, got %v instead", rc.err, err) 179 } 180 } 181 } 182 183 func existsDependency(cmd string) bool { 184 _, err := exec.LookPath(cmd) 185 return err == nil 186 }