From 77ab78634060cf6fd3654692bafa5180ba2bb1b0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 30 Mar 2023 00:21:36 +0800 Subject: [PATCH] add tests --- modules/repository/init.go | 25 ++++++++++++------------- modules/repository/init_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 modules/repository/init_test.go diff --git a/modules/repository/init.go b/modules/repository/init.go index 1e2ad246b76df..2b96b18a8f68c 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -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 } diff --git a/modules/repository/init_test.go b/modules/repository/init_test.go new file mode 100644 index 0000000000000..d592e9a9fc084 --- /dev/null +++ b/modules/repository/init_test.go @@ -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") +}