github.com/go-crzy/go-git-http-xfer@v1.4.1-0.20210704194527-d62541d1b6b5/addon/handler/archive/archive_test.go (about) 1 package archive 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "os/exec" 9 "path" 10 "strings" 11 "testing" 12 13 "github.com/go-crzy/go-git-http-xfer/githttpxfer" 14 ) 15 16 type ArchiveParams struct { 17 gitRootPath string 18 gitBinPath string 19 repoName string 20 head string 21 } 22 23 var ( 24 archiveParams *ArchiveParams 25 ) 26 27 func setupArchiveTest(t *testing.T) error { 28 29 gitBinPath, err := exec.LookPath("git") 30 if err != nil { 31 t.Log("git is not found. so skip git e2e test.") 32 return err 33 } 34 35 archiveParams = new(ArchiveParams) 36 37 gitRootPath, err := ioutil.TempDir("", "githttpxfer") 38 if err != nil { 39 t.Logf("get temp directory failed, err: %v", err) 40 return err 41 } 42 os.Chdir(gitRootPath) 43 archiveParams.gitRootPath = gitRootPath 44 archiveParams.gitBinPath = gitBinPath 45 archiveParams.repoName = "test.git" 46 archiveParams.head = "master" 47 48 return nil 49 } 50 51 func teardownArchiveTest() { 52 os.RemoveAll(archiveParams.gitRootPath) 53 } 54 55 func Test_it_should_download_archive_repository(t *testing.T) { 56 57 if err := setupArchiveTest(t); err != nil { 58 return 59 } 60 defer teardownArchiveTest() 61 62 ghx, err := githttpxfer.New(archiveParams.gitRootPath, archiveParams.gitBinPath) 63 if err != nil { 64 t.Errorf("An instance could not be created. %s", err.Error()) 65 return 66 } 67 68 ghx.Router.Add(githttpxfer.NewRoute( 69 Method, 70 Pattern, 71 New(ghx).Archive, 72 )) 73 74 ts := httptest.NewServer(ghx) 75 if ts == nil { 76 t.Error("test server is nil.") 77 } 78 defer ts.Close() 79 80 repoName := "archive_test.git" 81 absRepoPath := ghx.Git.GetAbsolutePath(repoName) 82 os.Mkdir(absRepoPath, os.ModeDir|os.ModePerm) 83 84 if _, err := execCmd(absRepoPath, "git", "init", "--bare", "--shared"); err != nil { 85 t.Errorf("execute command error: %s", err.Error()) 86 return 87 } 88 89 remoteRepoUrl := ts.URL + "/" + repoName 90 91 tempDir, _ := ioutil.TempDir("", "git-http-xfer") 92 dir := "archive_test" 93 destDir := path.Join(tempDir, dir) 94 95 if _, err := execCmd("", "git", "clone", remoteRepoUrl, destDir); err != nil { 96 t.Errorf("execute command error: %s", err.Error()) 97 return 98 } 99 100 if _, err := execCmd(destDir, "git", "config", "--global", "user.name", "John Smith"); err != nil { 101 t.Errorf("execute command error: %s", err.Error()) 102 return 103 } 104 105 if _, err := execCmd(destDir, "git", "config", "--global", "user.email", "js@example.com"); err != nil { 106 t.Errorf("execute command error: %s", err.Error()) 107 return 108 } 109 110 if _, err := execCmd(destDir, "touch", "README.txt"); err != nil { 111 t.Errorf("execute command error: %s", err.Error()) 112 return 113 } 114 115 if _, err := execCmd(destDir, "git", "add", "README.txt"); err != nil { 116 t.Errorf("execute command error: %s", err.Error()) 117 return 118 } 119 120 if _, err := execCmd(destDir, "git", "commit", "-m", "first commit"); err != nil { 121 t.Errorf("execute command error: %s", err.Error()) 122 return 123 } 124 125 output, err := ioutil.ReadFile(path.Join(destDir, ".git/HEAD")) 126 if err != nil { 127 t.Errorf("execute command error: %s", err.Error()) 128 return 129 } 130 131 archiveParams.head = strings.Join(strings.Split(strings.TrimSuffix(string(output), "\n"), "/")[2:], "/") 132 if archiveParams.head == "" { 133 t.Error("could not figure out HEAD") 134 return 135 } 136 137 if _, err := execCmd(destDir, "git", "push", "-u", "origin", archiveParams.head); err != nil { 138 t.Errorf("execute command error: %s", err.Error()) 139 return 140 } 141 142 if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl+"/archive/"+archiveParams.head+".zip"); err != nil { 143 t.Errorf("execute command error: %s", err.Error()) 144 return 145 } 146 147 if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl+"/archive/"+archiveParams.head+".tar"); err != nil { 148 t.Errorf("execute command error: %s", err.Error()) 149 return 150 } 151 152 } 153 154 func Test_it_should_fail_404_download_archive(t *testing.T) { 155 156 if err := setupArchiveTest(t); err != nil { 157 return 158 } 159 defer teardownArchiveTest() 160 161 ghx, err := githttpxfer.New(archiveParams.gitRootPath, archiveParams.gitBinPath) 162 if err != nil { 163 t.Errorf("An instance could not be created. %s", err.Error()) 164 return 165 } 166 167 ghx.Router.Add(githttpxfer.NewRoute( 168 Method, 169 Pattern, 170 New(ghx).Archive, 171 )) 172 173 ts := httptest.NewServer(ghx) 174 if ts == nil { 175 t.Log("test server is nil.") 176 t.FailNow() 177 } 178 defer ts.Close() 179 180 repoName := "archive_test.git" 181 remoteRepoUrl := ts.URL + "/" + repoName 182 183 client := ts.Client() 184 request, _ := http.NewRequest(http.MethodGet, remoteRepoUrl, nil) 185 response, err := client.Do(request) 186 if err != nil { 187 t.Errorf("Should not return %v", err) 188 } 189 if response.StatusCode != http.StatusNotFound { 190 t.Errorf( 191 "Status Code should be 404, current: %d", 192 response.StatusCode, 193 ) 194 } 195 } 196 197 func execCmd(dir string, name string, arg ...string) ([]byte, error) { 198 c := exec.Command(name, arg...) 199 c.Dir = dir 200 return c.CombinedOutput() 201 }