repoutil_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package repoutil
  2. import (
  3. "runtime"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gogs.io/gogs/internal/conf"
  7. )
  8. func TestNewCloneLink(t *testing.T) {
  9. conf.SetMockApp(t,
  10. conf.AppOpts{
  11. RunUser: "git",
  12. },
  13. )
  14. conf.SetMockServer(t,
  15. conf.ServerOpts{
  16. ExternalURL: "https://example.com/",
  17. },
  18. )
  19. t.Run("regular SSH port", func(t *testing.T) {
  20. conf.SetMockSSH(t,
  21. conf.SSHOpts{
  22. Domain: "example.com",
  23. Port: 22,
  24. },
  25. )
  26. got := NewCloneLink("alice", "example", false)
  27. want := &CloneLink{
  28. SSH: "git@example.com:alice/example.git",
  29. HTTPS: "https://example.com/alice/example.git",
  30. }
  31. assert.Equal(t, want, got)
  32. })
  33. t.Run("irregular SSH port", func(t *testing.T) {
  34. conf.SetMockSSH(t,
  35. conf.SSHOpts{
  36. Domain: "example.com",
  37. Port: 2222,
  38. },
  39. )
  40. got := NewCloneLink("alice", "example", false)
  41. want := &CloneLink{
  42. SSH: "ssh://git@example.com:2222/alice/example.git",
  43. HTTPS: "https://example.com/alice/example.git",
  44. }
  45. assert.Equal(t, want, got)
  46. })
  47. t.Run("wiki", func(t *testing.T) {
  48. conf.SetMockSSH(t,
  49. conf.SSHOpts{
  50. Domain: "example.com",
  51. Port: 22,
  52. },
  53. )
  54. got := NewCloneLink("alice", "example", true)
  55. want := &CloneLink{
  56. SSH: "git@example.com:alice/example.wiki.git",
  57. HTTPS: "https://example.com/alice/example.wiki.git",
  58. }
  59. assert.Equal(t, want, got)
  60. })
  61. }
  62. func TestHTMLURL(t *testing.T) {
  63. conf.SetMockServer(t,
  64. conf.ServerOpts{
  65. ExternalURL: "https://example.com/",
  66. },
  67. )
  68. got := HTMLURL("alice", "example")
  69. want := "https://example.com/alice/example"
  70. assert.Equal(t, want, got)
  71. }
  72. func TestCompareCommitsPath(t *testing.T) {
  73. got := CompareCommitsPath("alice", "example", "old", "new")
  74. want := "alice/example/compare/old...new"
  75. assert.Equal(t, want, got)
  76. }
  77. func TestUserPath(t *testing.T) {
  78. if runtime.GOOS == "windows" {
  79. t.Skip("Skipping testing on Windows")
  80. return
  81. }
  82. conf.SetMockRepository(t,
  83. conf.RepositoryOpts{
  84. Root: "/home/git/gogs-repositories",
  85. },
  86. )
  87. got := UserPath("alice")
  88. want := "/home/git/gogs-repositories/alice"
  89. assert.Equal(t, want, got)
  90. }
  91. func TestRepositoryPath(t *testing.T) {
  92. if runtime.GOOS == "windows" {
  93. t.Skip("Skipping testing on Windows")
  94. return
  95. }
  96. conf.SetMockRepository(t,
  97. conf.RepositoryOpts{
  98. Root: "/home/git/gogs-repositories",
  99. },
  100. )
  101. got := RepositoryPath("alice", "example")
  102. want := "/home/git/gogs-repositories/alice/example.git"
  103. assert.Equal(t, want, got)
  104. }
  105. func TestRepositoryLocalPath(t *testing.T) {
  106. if runtime.GOOS == "windows" {
  107. t.Skip("Skipping testing on Windows")
  108. return
  109. }
  110. conf.SetMockServer(
  111. t,
  112. conf.ServerOpts{
  113. AppDataPath: "data",
  114. },
  115. )
  116. got := RepositoryLocalPath(1)
  117. want := "data/tmp/local-repo/1"
  118. assert.Equal(t, want, got)
  119. }
  120. func TestRepositoryLocalWikiPath(t *testing.T) {
  121. if runtime.GOOS == "windows" {
  122. t.Skip("Skipping testing on Windows")
  123. return
  124. }
  125. conf.SetMockServer(
  126. t,
  127. conf.ServerOpts{
  128. AppDataPath: "data",
  129. },
  130. )
  131. got := RepositoryLocalWikiPath(1)
  132. want := "data/tmp/local-wiki/1"
  133. assert.Equal(t, want, got)
  134. }