Skip to content

Commit 31b9ebd

Browse files
authored
Merge pull request #85 from pavelsne/iss35
Skip .git directory when searching for files (#35) resolved
2 parents 238385d + 2b48726 commit 31b9ebd

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

internal/web/validate.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ func validateNIX(valroot, resdir string) error {
182182
log.ShowWrite("[Error] NIXFinder directory walk caused error at %q: %s", path, err.Error())
183183
return nil
184184
}
185+
186+
// Skip git directory
187+
if info.Name() == ".git" {
188+
return filepath.SkipDir
189+
}
190+
185191
if info.IsDir() {
186192
// nothing to do; continue
187193
return nil
@@ -267,6 +273,11 @@ func validateODML(valroot, resdir string) error {
267273
return nil
268274
}
269275

276+
// Skip git directory
277+
if info.Name() == ".git" {
278+
return filepath.SkipDir
279+
}
280+
270281
extension := strings.ToLower(filepath.Ext(path))
271282
if extension == ".odml" || extension == ".xml" {
272283
odmlfiles = append(odmlfiles, path)
@@ -411,10 +422,11 @@ func runValidatorBoth(validator, repopath, commit, commitname string, gcl *gincl
411422

412423
glog.Init()
413424
clonechan := make(chan git.RepoFileStatus)
425+
pth, _ := os.Getwd()
414426
os.Chdir(tmpdir)
415427
go gcl.CloneRepo(repopath, clonechan)
416428
for stat := range clonechan {
417-
if stat.Err != nil {
429+
if stat.Err != nil && stat.Err.Error() != "Error initialising local directory" {
418430
log.ShowWrite("[Error] Failed to fetch repository data for %q: %s", repopath, stat.Err.Error())
419431
writeValFailure(resdir)
420432
return
@@ -446,6 +458,7 @@ func runValidatorBoth(validator, repopath, commit, commitname string, gcl *gincl
446458
log.ShowWrite("[Info] %s %s %s", stat.State, stat.FileName, stat.Progress)
447459
}
448460
log.ShowWrite("[Info] get-content complete")
461+
os.Chdir(pth)
449462

450463
switch validator {
451464
case "bids":

internal/web/validate_test.go

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,120 @@ func TestValidateODMLNoData(t *testing.T) {
7575
}
7676
}
7777

78-
/*func TestValidateBadgeFail(t *testing.T) { //TODO
78+
func TestValidateBIDSOK(t *testing.T) {
79+
resultfldr, _ := ioutil.TempDir("", "results")
80+
tempdataset, _ := ioutil.TempDir("", "tempdataset")
81+
f, err := os.Create(filepath.Join(tempdataset, "ginvalidation.yaml"))
82+
defer f.Close()
83+
if err != nil {
84+
t.Fatalf(`validateBIDS(valroot, resdir string) = %v`, err)
85+
}
86+
_, err = f.WriteString("bidsconfig:\n bidsroot: \"bids_example\"")
87+
if err != nil {
88+
t.Fatalf(`validateBIDS(valroot, resdir string) = %v`, err)
89+
}
90+
os.Mkdir(filepath.Join(tempdataset, "bids_example"), 0664)
91+
srvcfg := config.Read()
92+
srvcfg.Dir.Result = resultfldr
93+
config.Set(srvcfg)
94+
validateBIDS(tempdataset, resultfldr)
95+
}
96+
97+
func TestValidateNIXOK(t *testing.T) {
98+
resultfldr, _ := ioutil.TempDir("", "results")
99+
tempdataset, _ := ioutil.TempDir("", "tempdataset")
100+
nix, err := ioutil.ReadFile("../../resources/nixdata.nix")
101+
if err != nil {
102+
t.Fatalf(`validateNIX(valroot, resdir string) = %v`, err)
103+
}
104+
err = ioutil.WriteFile(filepath.Join(tempdataset, "nixdata.nix"), nix, 0664)
105+
if err != nil {
106+
t.Fatalf(`validateNIX(valroot, resdir string) = %v`, err)
107+
}
108+
os.Mkdir(filepath.Join(tempdataset, ".git"), 0755)
109+
nix = append([]byte("WTF_this_will_not_work"), nix...)
110+
err = ioutil.WriteFile(filepath.Join(tempdataset, ".git", "nixdata_donottest.nix"), nix, 0664)
111+
if err != nil {
112+
t.Fatalf(`validateNIX(valroot, resdir string) = %v`, err)
113+
}
114+
srvcfg := config.Read()
115+
srvcfg.Dir.Result = resultfldr
116+
config.Set(srvcfg)
117+
validateNIX(tempdataset, resultfldr)
118+
}
119+
120+
func TestValidateODMLOK(t *testing.T) {
121+
resultfldr, _ := ioutil.TempDir("", "results")
122+
tempdataset, _ := ioutil.TempDir("", "tempdataset")
123+
odml, err := ioutil.ReadFile("../../resources/odmldata.odml")
124+
if err != nil {
125+
t.Fatalf(`validateODML(valroot, resdir string) = %v`, err)
126+
}
127+
err = ioutil.WriteFile(filepath.Join(tempdataset, "odmldata.odml"), odml, 0664)
128+
if err != nil {
129+
t.Fatalf(`validateODML(valroot, resdir string) = %v`, err)
130+
}
131+
os.Mkdir(filepath.Join(tempdataset, ".git"), 0755)
132+
odml = append([]byte("WTF_this_will_not_work"), odml...)
133+
err = ioutil.WriteFile(filepath.Join(tempdataset, ".git", "odmldata_donottest.odml"), odml, 0664)
134+
if err != nil {
135+
t.Fatalf(`validateODML(valroot, resdir string) = %v`, err)
136+
}
137+
srvcfg := config.Read()
138+
srvcfg.Dir.Result = resultfldr
139+
config.Set(srvcfg)
140+
validateODML(tempdataset, resultfldr)
141+
}
142+
143+
/*func TestValidateBIDSOK(t *testing.T) {
144+
testValidateOK(t,"bids")
145+
}
146+
147+
func TestValidateNIXOK(t *testing.T) {
148+
testValidateOK(t,"nix")
149+
}
150+
151+
func TestValidateODMLOK(t *testing.T) {
152+
testValidateOK(t,"odml")
153+
}
154+
155+
func testValidateOK(t *testing.T, validator string) {
156+
body := []byte("{\"after\": \"8cea328d5ee9d6d8944bd06802f761f140a31653\"}")
157+
router := mux.NewRouter()
158+
router.HandleFunc("/validate/{validator}/{user}/{repo}", Validate).Methods("POST")
159+
srvcfg := config.Read()
160+
srvcfg.Dir.Tokens = "."
161+
os.Mkdir("tmp", 0755)
162+
srvcfg.Dir.Temp = "./tmp"
163+
srvcfg.GINAddresses.WebURL = weburl
164+
srvcfg.GINAddresses.GitURL = giturl
165+
config.Set(srvcfg)
166+
var tok gweb.UserToken
167+
tok.Username = username
168+
tok.Token = token
169+
saveToken(tok)
170+
os.Mkdir(filepath.Join(srvcfg.Dir.Tokens, "by-repo"), 0755)
171+
linkToRepo(username, filepath.Join(username, "/", reponame))
172+
r, err := http.NewRequest("POST", filepath.Join("/validate", validator, username, reponame), bytes.NewReader(body))
173+
if err != nil {
174+
t.Fatalf(`Validate(w http.ResponseWriter, r *http.Request) = %v`, err)
175+
}
176+
w := httptest.NewRecorder()
177+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
178+
sig.Write(body)
179+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
180+
router.ServeHTTP(w, r)
181+
time.Sleep(5 * time.Second) //TODO HACK
182+
os.RemoveAll(filepath.Join(srvcfg.Dir.Tokens, "by-repo"))
183+
os.RemoveAll("tmp")
184+
status := w.Code
185+
if status != http.StatusOK {
186+
t.Fatalf(`Validate(w http.ResponseWriter, r *http.Request) status code = %v`, status)
187+
}
188+
189+
}
190+
191+
func TestValidateBadgeFail(t *testing.T) {
79192
body := []byte("{}")
80193
router := mux.NewRouter()
81194
router.HandleFunc("/validate/{validator}/{user}/{repo}", Validate).Methods("POST")
@@ -194,6 +307,7 @@ func TestValidatePub(t *testing.T) {
194307
sig.Write(body)
195308
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
196309
router.ServeHTTP(w, r)
310+
os.RemoveAll(filepath.Join(srvcfg.Dir.Tokens, "by-repo"))
197311
time.Sleep(5 * time.Second) //TODO HACK
198312
status := w.Code
199313
if status != http.StatusOK {
@@ -227,6 +341,7 @@ func TestValidateRepoDoesNotExists(t *testing.T) {
227341
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
228342
router.ServeHTTP(w, r)
229343
time.Sleep(5 * time.Second) //TODO HACK
344+
os.RemoveAll(filepath.Join(srvcfg.Dir.Tokens, "by-repo"))
230345
status := w.Code
231346
if status != http.StatusNotFound {
232347
t.Fatalf(`Validate(w http.ResponseWriter, r *http.Request) status code = %v`, status)

0 commit comments

Comments
 (0)