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.

compare_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/tests"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCompareTag(t *testing.T) {
  13. defer tests.PrepareTestEnv(t)()
  14. session := loginUser(t, "user2")
  15. req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1...master")
  16. resp := session.MakeRequest(t, req, http.StatusOK)
  17. htmlDoc := NewHTMLParser(t, resp.Body)
  18. selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
  19. // A dropdown for both base and head.
  20. assert.Lenf(t, selection.Nodes, 2, "The template has changed")
  21. req = NewRequest(t, "GET", "/user2/repo1/compare/invalid")
  22. resp = session.MakeRequest(t, req, http.StatusNotFound)
  23. assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
  24. }
  25. // Compare with inferred default branch (master)
  26. func TestCompareDefault(t *testing.T) {
  27. defer tests.PrepareTestEnv(t)()
  28. session := loginUser(t, "user2")
  29. req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1")
  30. resp := session.MakeRequest(t, req, http.StatusOK)
  31. htmlDoc := NewHTMLParser(t, resp.Body)
  32. selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
  33. assert.Lenf(t, selection.Nodes, 2, "The template has changed")
  34. }
  35. // Ensure the comparison matches what we expect
  36. func inspectCompare(t *testing.T, htmlDoc *HTMLDoc, diffCount int, diffChanges []string) {
  37. selection := htmlDoc.doc.Find("#diff-file-boxes").Children()
  38. assert.Lenf(t, selection.Nodes, diffCount, "Expected %v diffed files, found: %v", diffCount, len(selection.Nodes))
  39. for _, diffChange := range diffChanges {
  40. selection = htmlDoc.doc.Find(fmt.Sprintf("[data-new-filename=\"%s\"]", diffChange))
  41. assert.Lenf(t, selection.Nodes, 1, "Expected 1 match for [data-new-filename=\"%s\"], found: %v", diffChange, len(selection.Nodes))
  42. }
  43. }
  44. // Git commit graph for repo20
  45. // * 8babce9 (origin/remove-files-b) Add a dummy file
  46. // * b67e43a Delete test.csv and link_hi
  47. // | * cfe3b3c (origin/remove-files-a) Delete test.csv and link_hi
  48. // |/
  49. // * c8e31bc (origin/add-csv) Add test csv file
  50. // * 808038d (HEAD -> master, origin/master, origin/HEAD) Added test links
  51. func TestCompareBranches(t *testing.T) {
  52. defer tests.PrepareTestEnv(t)()
  53. session := loginUser(t, "user2")
  54. // Inderect compare remove-files-b (head) with add-csv (base) branch
  55. //
  56. // 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
  57. req := NewRequest(t, "GET", "/user2/repo20/compare/add-csv...remove-files-b")
  58. resp := session.MakeRequest(t, req, http.StatusOK)
  59. htmlDoc := NewHTMLParser(t, resp.Body)
  60. diffCount := 3
  61. diffChanges := []string{"link_hi", "test.csv", "test.txt"}
  62. inspectCompare(t, htmlDoc, diffCount, diffChanges)
  63. // Inderect compare remove-files-b (head) with remove-files-a (base) branch
  64. //
  65. // 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
  66. req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-a...remove-files-b")
  67. resp = session.MakeRequest(t, req, http.StatusOK)
  68. htmlDoc = NewHTMLParser(t, resp.Body)
  69. diffCount = 3
  70. diffChanges = []string{"link_hi", "test.csv", "test.txt"}
  71. inspectCompare(t, htmlDoc, diffCount, diffChanges)
  72. // Inderect compare remove-files-a (head) with remove-files-b (base) branch
  73. //
  74. // 'link_hi' and 'test.csv' are deleted
  75. req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b...remove-files-a")
  76. resp = session.MakeRequest(t, req, http.StatusOK)
  77. htmlDoc = NewHTMLParser(t, resp.Body)
  78. diffCount = 2
  79. diffChanges = []string{"link_hi", "test.csv"}
  80. inspectCompare(t, htmlDoc, diffCount, diffChanges)
  81. // Direct compare remove-files-b (head) with remove-files-a (base) branch
  82. //
  83. // 'test.txt' is deleted
  84. req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b..remove-files-a")
  85. resp = session.MakeRequest(t, req, http.StatusOK)
  86. htmlDoc = NewHTMLParser(t, resp.Body)
  87. diffCount = 1
  88. diffChanges = []string{"test.txt"}
  89. inspectCompare(t, htmlDoc, diffCount, diffChanges)
  90. }