Skip to content

Commit 77ab786

Browse files
committed
add tests
1 parent 1ba0a41 commit 77ab786

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

modules/repository/init.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ func mergeCustomLabels(fl optionFileList) []string {
6161
return exts[filepath.Ext(fl.custom[i])] < exts[filepath.Ext(fl.custom[j])]
6262
})
6363

64-
files := fl.all
65-
if len(fl.custom) > 0 {
66-
m := map[string]string{}
67-
for _, f := range fl.all {
68-
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
69-
}
70-
for _, f := range fl.custom {
71-
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
72-
}
73-
files = make([]string, 0, len(m))
74-
for _, f := range m {
75-
files = append(files, f)
76-
}
64+
m := map[string]string{}
65+
for _, f := range fl.all {
66+
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
67+
}
68+
for _, f := range fl.custom {
69+
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
7770
}
71+
72+
files := make([]string, 0, len(m))
73+
for _, f := range m {
74+
files = append(files, f)
75+
}
76+
7877
sort.Strings(files)
7978
return files
8079
}

modules/repository/init_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repository
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestMergeCustomLabels(t *testing.T) {
13+
files := mergeCustomLabels(optionFileList{
14+
all: []string{"a", "a.yaml", "a.yml"},
15+
custom: nil,
16+
})
17+
assert.EqualValues(t, []string{"a.yaml"}, files, "yaml file should win")
18+
19+
files = mergeCustomLabels(optionFileList{
20+
all: []string{"a", "a.yaml"},
21+
custom: []string{"a"},
22+
})
23+
assert.EqualValues(t, []string{"a"}, files, "custom file should win")
24+
25+
files = mergeCustomLabels(optionFileList{
26+
all: []string{"a", "a.yml", "a.yaml"},
27+
custom: []string{"a", "a.yml"},
28+
})
29+
assert.EqualValues(t, []string{"a.yml"}, files, "custom yml file should win if no yaml")
30+
}

0 commit comments

Comments
 (0)