repo_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package database
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/markup"
  10. "gogs.io/gogs/internal/osutil"
  11. )
  12. func TestRepository_ComposeMetas(t *testing.T) {
  13. repo := &Repository{
  14. Name: "testrepo",
  15. Owner: &User{
  16. Name: "testuser",
  17. },
  18. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  19. }
  20. t.Run("no external tracker is configured", func(t *testing.T) {
  21. repo.EnableExternalTracker = false
  22. metas := repo.ComposeMetas()
  23. assert.Equal(t, metas["repoLink"], repo.Link())
  24. // Should not have format and style if no external tracker is configured
  25. _, ok := metas["format"]
  26. assert.False(t, ok)
  27. _, ok = metas["style"]
  28. assert.False(t, ok)
  29. })
  30. t.Run("an external issue tracker is configured", func(t *testing.T) {
  31. repo.ExternalMetas = nil
  32. repo.EnableExternalTracker = true
  33. // Default to numeric issue style
  34. assert.Equal(t, markup.IssueNameStyleNumeric, repo.ComposeMetas()["style"])
  35. repo.ExternalMetas = nil
  36. repo.ExternalTrackerStyle = markup.IssueNameStyleNumeric
  37. assert.Equal(t, markup.IssueNameStyleNumeric, repo.ComposeMetas()["style"])
  38. repo.ExternalMetas = nil
  39. repo.ExternalTrackerStyle = markup.IssueNameStyleAlphanumeric
  40. assert.Equal(t, markup.IssueNameStyleAlphanumeric, repo.ComposeMetas()["style"])
  41. repo.ExternalMetas = nil
  42. metas := repo.ComposeMetas()
  43. assert.Equal(t, "testuser", metas["user"])
  44. assert.Equal(t, "testrepo", metas["repo"])
  45. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  46. })
  47. }
  48. func Test_CreateRepository_PreventDeletion(t *testing.T) {
  49. tempRepositoryRoot := filepath.Join(os.TempDir(), "createRepository-tempRepositoryRoot")
  50. conf.SetMockRepository(
  51. t,
  52. conf.RepositoryOpts{
  53. Root: tempRepositoryRoot,
  54. },
  55. )
  56. err := os.RemoveAll(tempRepositoryRoot)
  57. require.NoError(t, err)
  58. defer func() { _ = os.RemoveAll(tempRepositoryRoot) }()
  59. owner := &User{Name: "testuser"}
  60. opts := CreateRepoOptionsLegacy{Name: "safety-test"}
  61. repoPath := RepoPath(owner.Name, opts.Name)
  62. require.NoError(t, os.MkdirAll(repoPath, os.ModePerm))
  63. canary := filepath.Join(repoPath, "canary.txt")
  64. require.NoError(t, os.WriteFile(canary, []byte("should survive"), 0o644))
  65. _, err = CreateRepository(owner, owner, opts)
  66. require.Error(t, err)
  67. assert.Contains(t, err.Error(), "repository directory already exists")
  68. assert.True(t, osutil.Exist(canary))
  69. }