gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. var giteaTemplate = []byte(`
  10. # Header
  11. # All .go files
  12. **.go
  13. # All text files in /text/
  14. text/*.txt
  15. # All files in modules folders
  16. **/modules/*
  17. `)
  18. func TestGiteaTemplate(t *testing.T) {
  19. gt := GiteaTemplate{Content: giteaTemplate}
  20. assert.Len(t, gt.Globs(), 3)
  21. tt := []struct {
  22. Path string
  23. Match bool
  24. }{
  25. {Path: "main.go", Match: true},
  26. {Path: "a/b/c/d/e.go", Match: true},
  27. {Path: "main.txt", Match: false},
  28. {Path: "a/b.txt", Match: false},
  29. {Path: "text/a.txt", Match: true},
  30. {Path: "text/b.txt", Match: true},
  31. {Path: "text/c.json", Match: false},
  32. {Path: "a/b/c/modules/README.md", Match: true},
  33. {Path: "a/b/c/modules/d/README.md", Match: false},
  34. }
  35. for _, tc := range tt {
  36. t.Run(tc.Path, func(t *testing.T) {
  37. match := false
  38. for _, g := range gt.Globs() {
  39. if g.Match(tc.Path) {
  40. match = true
  41. break
  42. }
  43. }
  44. assert.Equal(t, tc.Match, match)
  45. })
  46. }
  47. }
  48. func TestFileNameSanitize(t *testing.T) {
  49. assert.Equal(t, "test_CON", fileNameSanitize("test_CON"))
  50. assert.Equal(t, "test CON", fileNameSanitize("test CON "))
  51. assert.Equal(t, "__traverse__", fileNameSanitize("../traverse/.."))
  52. assert.Equal(t, "http___localhost_3003_user_test.git", fileNameSanitize("http://localhost:3003/user/test.git"))
  53. assert.Equal(t, "_", fileNameSanitize("CON"))
  54. assert.Equal(t, "_", fileNameSanitize("con"))
  55. assert.Equal(t, "_", fileNameSanitize("\u0000"))
  56. assert.Equal(t, "目标", fileNameSanitize("目标"))
  57. }
  58. func TestTransformers(t *testing.T) {
  59. cases := []struct {
  60. name string
  61. expected string
  62. }{
  63. {"SNAKE", "abc_def_xyz"},
  64. {"KEBAB", "abc-def-xyz"},
  65. {"CAMEL", "abcDefXyz"},
  66. {"PASCAL", "AbcDefXyz"},
  67. {"LOWER", "abc_def-xyz"},
  68. {"UPPER", "ABC_DEF-XYZ"},
  69. {"TITLE", "Abc_def-Xyz"},
  70. }
  71. input := "Abc_Def-XYZ"
  72. assert.Len(t, defaultTransformers, len(cases))
  73. for i, c := range cases {
  74. tf := defaultTransformers[i]
  75. require.Equal(t, c.name, tf.Name)
  76. assert.Equal(t, c.expected, tf.Transform(input), "case %s", c.name)
  77. }
  78. }