repoutil_test.go 3.2 KB

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