code.gitea.io/gitea@v1.21.7/tests/integration/compare_test.go (about)

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