Skip to content

Commit 088e24f

Browse files
committed
[*_test.go] Use ioutil.TempDir for all relevant tests
Closes #84 All tests now work using temporary folders from io/ioutil
1 parent 5683eb9 commit 088e24f

File tree

4 files changed

+65
-36
lines changed

4 files changed

+65
-36
lines changed

internal/web/results_test.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"github.com/G-Node/gin-valid/internal/config"
99
"github.com/gorilla/mux"
10+
"io/ioutil"
1011
"net/http"
1112
"net/http/httptest"
1213
"os"
@@ -22,18 +23,19 @@ func TestResultsUnsupportedV2(t *testing.T) {
2223
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
2324
r, _ := http.NewRequest("GET", filepath.Join("/results/wtf", username, "/", reponame, "/", id), bytes.NewReader(body))
2425
w := httptest.NewRecorder()
26+
resultfldr, _ := ioutil.TempDir("", "results")
2527
srvcfg := config.Read()
2628
srvcfg.Settings.Validators = append(srvcfg.Settings.Validators, "wtf")
29+
srvcfg.Dir.Result = resultfldr
2730
config.Set(srvcfg)
28-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id), 0755)
29-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id, srvcfg.Label.ResultsFile))
31+
os.MkdirAll(filepath.Join(resultfldr, "nix", username, reponame, id), 0755)
32+
f, _ := os.Create(filepath.Join(resultfldr, "nix", username, reponame, id, srvcfg.Label.ResultsFile))
3033
defer f.Close()
3134
f.WriteString(content)
3235
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
3336
sig.Write(body)
3437
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
3538
router.ServeHTTP(w, r)
36-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id))
3739
srvcfg.Settings.Validators = srvcfg.Settings.Validators[:len(srvcfg.Settings.Validators)-1]
3840
config.Set(srvcfg)
3941
status := w.Code
@@ -50,16 +52,18 @@ func TestResultsODML(t *testing.T) {
5052
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
5153
r, _ := http.NewRequest("GET", filepath.Join("/results/odml", username, "/", reponame, "/", id), bytes.NewReader(body))
5254
w := httptest.NewRecorder()
55+
resultfldr, _ := ioutil.TempDir("", "results")
5356
srvcfg := config.Read()
54-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id), 0755)
55-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id, srvcfg.Label.ResultsFile))
57+
srvcfg.Dir.Result = resultfldr
58+
config.Set(srvcfg)
59+
os.MkdirAll(filepath.Join(resultfldr, "odml", username, reponame, id), 0755)
60+
f, _ := os.Create(filepath.Join(resultfldr, "odml", username, reponame, id, srvcfg.Label.ResultsFile))
5661
defer f.Close()
5762
f.WriteString(content)
5863
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
5964
sig.Write(body)
6065
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
6166
router.ServeHTTP(w, r)
62-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id))
6367
status := w.Code
6468
if status != http.StatusOK {
6569
t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status)
@@ -74,16 +78,18 @@ func TestResultsNIX(t *testing.T) {
7478
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
7579
r, _ := http.NewRequest("GET", filepath.Join("/results/nix", username, "/", reponame, "/", id), bytes.NewReader(body))
7680
w := httptest.NewRecorder()
81+
resultfldr, _ := ioutil.TempDir("", "results")
7782
srvcfg := config.Read()
78-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id), 0755)
79-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id, srvcfg.Label.ResultsFile))
83+
srvcfg.Dir.Result = resultfldr
84+
config.Set(srvcfg)
85+
os.MkdirAll(filepath.Join(resultfldr, "nix", username, reponame, id), 0755)
86+
f, _ := os.Create(filepath.Join(resultfldr, "nix", username, reponame, id, srvcfg.Label.ResultsFile))
8087
defer f.Close()
8188
f.WriteString(content)
8289
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
8390
sig.Write(body)
8491
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
8592
router.ServeHTTP(w, r)
86-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id))
8793
status := w.Code
8894
if status != http.StatusOK {
8995
t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status)
@@ -98,16 +104,18 @@ func TestResultsInJSON(t *testing.T) {
98104
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
99105
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
100106
w := httptest.NewRecorder()
107+
resultfldr, _ := ioutil.TempDir("", "results")
101108
srvcfg := config.Read()
102-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755)
103-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
109+
srvcfg.Dir.Result = resultfldr
110+
config.Set(srvcfg)
111+
os.MkdirAll(filepath.Join(resultfldr, "bids", username, reponame, id), 0755)
112+
f, _ := os.Create(filepath.Join(resultfldr, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
104113
defer f.Close()
105114
f.WriteString(content)
106115
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
107116
sig.Write(body)
108117
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
109118
router.ServeHTTP(w, r)
110-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id))
111119
status := w.Code
112120
if status != http.StatusOK {
113121
t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status)
@@ -122,16 +130,18 @@ func TestResultsInProgress(t *testing.T) {
122130
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
123131
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
124132
w := httptest.NewRecorder()
133+
resultfldr, _ := ioutil.TempDir("", "results")
125134
srvcfg := config.Read()
126-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755)
127-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
135+
srvcfg.Dir.Result = resultfldr
136+
config.Set(srvcfg)
137+
os.MkdirAll(filepath.Join(resultfldr, "bids", username, reponame, id), 0755)
138+
f, _ := os.Create(filepath.Join(resultfldr, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
128139
defer f.Close()
129140
f.WriteString(content)
130141
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
131142
sig.Write(body)
132143
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
133144
router.ServeHTTP(w, r)
134-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id))
135145
status := w.Code
136146
if status != http.StatusOK {
137147
t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status)
@@ -146,16 +156,18 @@ func TestResultsSomeResults(t *testing.T) {
146156
router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET")
147157
r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body))
148158
w := httptest.NewRecorder()
159+
resultfldr, _ := ioutil.TempDir("", "results")
149160
srvcfg := config.Read()
150-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755)
151-
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
161+
srvcfg.Dir.Result = resultfldr
162+
config.Set(srvcfg)
163+
os.MkdirAll(filepath.Join(resultfldr, "bids", username, reponame, id), 0755)
164+
f, _ := os.Create(filepath.Join(resultfldr, "bids", username, reponame, id, srvcfg.Label.ResultsFile))
152165
defer f.Close()
153166
f.WriteString(content)
154167
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
155168
sig.Write(body)
156169
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
157170
router.ServeHTTP(w, r)
158-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id))
159171
status := w.Code
160172
if status != http.StatusOK {
161173
t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status)

internal/web/status_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"github.com/G-Node/gin-valid/internal/config"
99
"github.com/gorilla/mux"
10+
"io/ioutil"
1011
"net/http"
1112
"net/http/httptest"
1213
"os"
@@ -21,16 +22,17 @@ func TestStatusOK(t *testing.T) {
2122
router.HandleFunc("/status/{validator}/{user}/{repo}", Status).Methods("GET")
2223
r, _ := http.NewRequest("GET", filepath.Join("/status/bids", username, reponame), bytes.NewReader(body))
2324
w := httptest.NewRecorder()
25+
resultfldr, _ := ioutil.TempDir("", "results")
2426
srvcfg := config.Read()
27+
srvcfg.Dir.Result = resultfldr
2528
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
2629
sig.Write(body)
2730
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
28-
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder), 0755)
31+
os.MkdirAll(filepath.Join(resultfldr, "bids", username, reponame, srvcfg.Label.ResultsFolder), 0755)
2932
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder, srvcfg.Label.ResultsBadge))
3033
defer f.Close()
3134
f.WriteString(content)
3235
router.ServeHTTP(w, r)
33-
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder))
3436
status := w.Code
3537
if status != http.StatusOK {
3638
t.Fatalf(`Status(w http.ResponseWriter, r *http.Request) status code = %v`, status)

internal/web/user_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/G-Node/gin-valid/internal/config"
66
"github.com/G-Node/gin-valid/internal/resources/templates"
77
"github.com/gorilla/mux"
8+
"io/ioutil"
89
"net/http"
910
"net/http/httptest"
1011
"net/url"
@@ -38,27 +39,28 @@ func TestUserDoLoginFailed(t *testing.T) {
3839
}
3940

4041
func TestUserDoLoginOK(t *testing.T) {
42+
tokens, _ := ioutil.TempDir("", "tokens")
4143
srvcfg := config.Read()
4244
srvcfg.GINAddresses.WebURL = weburl
4345
srvcfg.GINAddresses.GitURL = giturl
46+
srvcfg.Dir.Tokens = tokens
4447
config.Set(srvcfg)
45-
pth, _ := filepath.Abs(srvcfg.Dir.Tokens)
46-
tokendir := filepath.Join(pth, "by-sessionid")
48+
tokendir := filepath.Join(tokens, "by-sessionid")
4749
os.MkdirAll(tokendir, 0755)
4850
sessionid, err := doLogin(username, password)
49-
os.RemoveAll(tokendir)
5051
if sessionid == "" || err != nil {
5152
t.Fatalf(`doLogin(username, password) = %q, %v`, sessionid, err)
5253
}
5354
}
5455

5556
func TestUserLoginPost(t *testing.T) {
57+
tokens, _ := ioutil.TempDir("", "tokens")
5658
srvcfg := config.Read()
5759
srvcfg.GINAddresses.WebURL = weburl
5860
srvcfg.GINAddresses.GitURL = giturl
61+
srvcfg.Dir.Tokens = tokens
5962
config.Set(srvcfg)
60-
pth, _ := filepath.Abs(srvcfg.Dir.Tokens)
61-
tokendir := filepath.Join(pth, "by-sessionid")
63+
tokendir := filepath.Join(tokens, "by-sessionid")
6264
os.MkdirAll(tokendir, 0755)
6365
v := make(url.Values)
6466
v.Set("username", username)
@@ -69,7 +71,6 @@ func TestUserLoginPost(t *testing.T) {
6971
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
7072
w := httptest.NewRecorder()
7173
router.ServeHTTP(w, r)
72-
os.RemoveAll(tokendir)
7374
status := w.Code
7475
if status != http.StatusFound {
7576
t.Fatalf(`LoginPost(w http.ResponseWriter, r *http.Request) status code = %v`, status)

internal/web/validate_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/G-Node/gin-valid/internal/config"
1111
"github.com/G-Node/gin-valid/internal/resources/templates"
1212
"github.com/gorilla/mux"
13+
"io/ioutil"
1314
"net/http"
1415
"net/http/httptest"
1516
"os"
@@ -79,17 +80,20 @@ func TestValidateBadgeFail(t *testing.T) { //TODO
7980
router := mux.NewRouter()
8081
router.HandleFunc("/validate/{validator}/{user}/{repo}", Validate).Methods("POST")
8182
srvcfg := config.Read()
82-
srvcfg.Dir.Tokens = "."
83-
os.Mkdir("tmp", 0755)
84-
srvcfg.Dir.Temp = "./tmp"
83+
resultfldr, _ := ioutil.TempDir("", "results")
84+
tempfldr, _ := ioutil.TempDir("", "temp")
85+
tokenfldr, _ := ioutil.TempDir("", "token")
8586
srvcfg.GINAddresses.WebURL = weburl
8687
srvcfg.GINAddresses.GitURL = giturl
88+
srvcfg.Dir.Result = resultfldr
89+
srvcfg.Dir.Temp = tempfldr
90+
srvcfg.Dir.Tokens = tokenfldr
8791
config.Set(srvcfg)
8892
var tok gweb.UserToken
8993
tok.Username = username
9094
tok.Token = token
9195
saveToken(tok)
92-
os.Mkdir(filepath.Join(srvcfg.Dir.Tokens, "by-repo"), 0755)
96+
os.Mkdir(filepath.Join(tokenfldr, "by-repo"), 0755)
9397
linkToRepo(username, filepath.Join(username, "/", reponame))
9498
r, err := http.NewRequest("POST", filepath.Join("/validate/bids/", username, "/", reponame), bytes.NewReader(body))
9599
if err != nil {
@@ -99,10 +103,8 @@ func TestValidateBadgeFail(t *testing.T) { //TODO
99103
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
100104
sig.Write(body)
101105
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
102-
os.Mkdir("tmp", 0755)
103106
router.ServeHTTP(w, r)
104107
time.Sleep(5 * time.Second) //TODO HACK
105-
os.RemoveAll(filepath.Join(srvcfg.Dir.Tokens, "by-repo"))
106108
status := w.Code
107109
if status != http.StatusOK {
108110
t.Fatalf(`Validate(w http.ResponseWriter, r *http.Request) status code = %v`, status)
@@ -172,13 +174,20 @@ func TestValidateTMPFail(t *testing.T) {
172174
router := mux.NewRouter()
173175
router.HandleFunc("/validate/{validator}/{user}/{repo}", Validate).Methods("POST")
174176
srvcfg := config.Read()
175-
srvcfg.Dir.Tokens = "."
177+
resultfldr, _ := ioutil.TempDir("", "results")
178+
tempfldr, _ := ioutil.TempDir("", "temp")
179+
tokenfldr, _ := ioutil.TempDir("", "token")
180+
srvcfg.GINAddresses.WebURL = weburl
181+
srvcfg.GINAddresses.GitURL = giturl
182+
srvcfg.Dir.Result = resultfldr
183+
srvcfg.Dir.Temp = tempfldr
184+
srvcfg.Dir.Tokens = tokenfldr
176185
config.Set(srvcfg)
177186
var tok gweb.UserToken
178187
tok.Username = username
179188
tok.Token = token
180189
saveToken(tok)
181-
os.Mkdir(filepath.Join(srvcfg.Dir.Tokens, "by-repo"), 0755)
190+
os.Mkdir(filepath.Join(tokenfldr, "by-repo"), 0755)
182191
linkToRepo(username, filepath.Join(username, "/", reponame))
183192
r, _ := http.NewRequest("POST", filepath.Join("/validate/bids/", username, "/", reponame), bytes.NewReader(body))
184193
w := httptest.NewRecorder()
@@ -198,14 +207,19 @@ func TestValidateRepoDoesNotExists(t *testing.T) {
198207
body := []byte("{}")
199208
router := mux.NewRouter()
200209
router.HandleFunc("/validate/{validator}/{user}/{repo}", Validate).Methods("POST")
210+
resultfldr, _ := ioutil.TempDir("", "results")
211+
tempfldr, _ := ioutil.TempDir("", "temp")
212+
tokenfldr, _ := ioutil.TempDir("", "token")
201213
srvcfg := config.Read()
202-
srvcfg.Dir.Tokens = "."
214+
srvcfg.Dir.Result = resultfldr
215+
srvcfg.Dir.Temp = tempfldr
216+
srvcfg.Dir.Tokens = tokenfldr
203217
config.Set(srvcfg)
204218
var tok gweb.UserToken
205219
tok.Username = username
206220
tok.Token = token2
207221
saveToken(tok)
208-
os.Mkdir(filepath.Join(srvcfg.Dir.Tokens, "by-repo"), 0755)
222+
os.Mkdir(filepath.Join(tokenfldr, "by-repo"), 0755)
209223
linkToRepo(username, filepath.Join(username, "/", reponame))
210224
r, _ := http.NewRequest("POST", filepath.Join("/validate/bids/", username, "/", reponame), bytes.NewReader(body))
211225
w := httptest.NewRecorder()

0 commit comments

Comments
 (0)