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.

path_test.go 993B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "net/url"
  6. "runtime"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestFileURLToPath(t *testing.T) {
  11. cases := []struct {
  12. url string
  13. expected string
  14. haserror bool
  15. windows bool
  16. }{
  17. // case 0
  18. {
  19. url: "",
  20. haserror: true,
  21. },
  22. // case 1
  23. {
  24. url: "http://test.io",
  25. haserror: true,
  26. },
  27. // case 2
  28. {
  29. url: "file:///path",
  30. expected: "/path",
  31. },
  32. // case 3
  33. {
  34. url: "file:///C:/path",
  35. expected: "C:/path",
  36. windows: true,
  37. },
  38. }
  39. for n, c := range cases {
  40. if c.windows && runtime.GOOS != "windows" {
  41. continue
  42. }
  43. u, _ := url.Parse(c.url)
  44. p, err := FileURLToPath(u)
  45. if c.haserror {
  46. assert.Error(t, err, "case %d: should return error", n)
  47. } else {
  48. assert.NoError(t, err, "case %d: should not return error", n)
  49. assert.Equal(t, c.expected, p, "case %d: should be equal", n)
  50. }
  51. }
  52. }