You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_test.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. )
  8. var giteaTemplate = []byte(`
  9. # Header
  10. # All .go files
  11. **.go
  12. # All text files in /text/
  13. text/*.txt
  14. # All files in modules folders
  15. **/modules/*
  16. `)
  17. func TestGiteaTemplate(t *testing.T) {
  18. gt := GiteaTemplate{Content: giteaTemplate}
  19. assert.Len(t, gt.Globs(), 3)
  20. tt := []struct {
  21. Path string
  22. Match bool
  23. }{
  24. {Path: "main.go", Match: true},
  25. {Path: "a/b/c/d/e.go", Match: true},
  26. {Path: "main.txt", Match: false},
  27. {Path: "a/b.txt", Match: false},
  28. {Path: "text/a.txt", Match: true},
  29. {Path: "text/b.txt", Match: true},
  30. {Path: "text/c.json", Match: false},
  31. {Path: "a/b/c/modules/README.md", Match: true},
  32. {Path: "a/b/c/modules/d/README.md", Match: false},
  33. }
  34. for _, tc := range tt {
  35. t.Run(tc.Path, func(t *testing.T) {
  36. match := false
  37. for _, g := range gt.Globs() {
  38. if g.Match(tc.Path) {
  39. match = true
  40. break
  41. }
  42. }
  43. assert.Equal(t, tc.Match, match)
  44. })
  45. }
  46. }
  47. func TestFileNameSanitize(t *testing.T) {
  48. assert.Equal(t, "test_CON", fileNameSanitize("test_CON"))
  49. assert.Equal(t, "test CON", fileNameSanitize("test CON "))
  50. assert.Equal(t, "__traverse__", fileNameSanitize("../traverse/.."))
  51. assert.Equal(t, "http___localhost_3003_user_test.git", fileNameSanitize("http://localhost:3003/user/test.git"))
  52. assert.Equal(t, "_", fileNameSanitize("CON"))
  53. assert.Equal(t, "_", fileNameSanitize("con"))
  54. assert.Equal(t, "_", fileNameSanitize("\u0000"))
  55. assert.Equal(t, "目标", fileNameSanitize("目标"))
  56. }