Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 29, 2023
1 parent 1ba0a41 commit 77ab786
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
25 changes: 12 additions & 13 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ func mergeCustomLabels(fl optionFileList) []string {
return exts[filepath.Ext(fl.custom[i])] < exts[filepath.Ext(fl.custom[j])]
})

files := fl.all
if len(fl.custom) > 0 {
m := map[string]string{}
for _, f := range fl.all {
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
}
for _, f := range fl.custom {
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
}
files = make([]string, 0, len(m))
for _, f := range m {
files = append(files, f)
}
m := map[string]string{}
for _, f := range fl.all {
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
}
for _, f := range fl.custom {
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
}

files := make([]string, 0, len(m))
for _, f := range m {
files = append(files, f)
}

sort.Strings(files)
return files
}
Expand Down
30 changes: 30 additions & 0 deletions modules/repository/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repository

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMergeCustomLabels(t *testing.T) {
files := mergeCustomLabels(optionFileList{
all: []string{"a", "a.yaml", "a.yml"},
custom: nil,
})
assert.EqualValues(t, []string{"a.yaml"}, files, "yaml file should win")

files = mergeCustomLabels(optionFileList{
all: []string{"a", "a.yaml"},
custom: []string{"a"},
})
assert.EqualValues(t, []string{"a"}, files, "custom file should win")

files = mergeCustomLabels(optionFileList{
all: []string{"a", "a.yml", "a.yaml"},
custom: []string{"a", "a.yml"},
})
assert.EqualValues(t, []string{"a.yml"}, files, "custom yml file should win if no yaml")
}

0 comments on commit 77ab786

Please sign in to comment.