作者 yangfu

fix: file name error

1 package domain 1 package domain
2 2
3 import ( 3 import (
  4 + "path"
4 "path/filepath" 5 "path/filepath"
5 - "strings"  
6 "time" 6 "time"
7 ) 7 )
8 8
@@ -58,8 +58,13 @@ func (file *File) Update(data map[string]interface{}) error { @@ -58,8 +58,13 @@ func (file *File) Update(data map[string]interface{}) error {
58 } 58 }
59 59
60 func FileName(fileName string) string { 60 func FileName(fileName string) string {
  61 + if len(fileName) == 0 {
  62 + return ""
  63 + }
61 base := filepath.Base(fileName) 64 base := filepath.Base(fileName)
62 - return strings.Split(base, ".")[0] 65 + suffix := path.Ext(fileName)
  66 + prefix := base[0 : len(base)-len(suffix)]
  67 + return prefix
63 } 68 }
64 69
65 func (file *File) CopyTo(fileType FileType, ctx *Context) *File { 70 func (file *File) CopyTo(fileType FileType, ctx *Context) *File {
  1 +package domain
  2 +
  3 +import (
  4 + "github.com/stretchr/testify/assert"
  5 + "testing"
  6 +)
  7 +
  8 +func TestFileName(t *testing.T) {
  9 + inputs := []struct {
  10 + input string
  11 + want string
  12 + }{
  13 + {
  14 + input: "device/sdk/CMakeLists.txt",
  15 + want: "CMakeLists",
  16 + },
  17 + {
  18 + input: "",
  19 + want: "",
  20 + },
  21 + {
  22 + input: "CMakeLists.txt",
  23 + want: "CMakeLists",
  24 + },
  25 + {
  26 + input: "d:\\CMakeLists.txt",
  27 + want: "CMakeLists",
  28 + },
  29 + }
  30 +
  31 + for _, input := range inputs {
  32 + got := FileName(input.input)
  33 + assert.Equal(t, input.want, got)
  34 + }
  35 +}