forgejo/tests/integration/repo_commits_template_test.go
pat-s b6046c17a1 feat: add tag label to commit list view (#8759)
Mainly a port of https://github.com/go-gitea/gitea/pull/31082.

closes #3573

## Screenshots

![image](/attachments/a1ba8729-e7f3-4d3d-ab20-04593b119c3f)

![image](/attachments/13790703-8a71-41a0-a875-9c087da989f8)

---

## Checklist

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- User Interface features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/8759): <!--number 8759 --><!--line 0 --><!--description YWRkIHRhZyBsYWJlbCB0byBjb21taXQgbGlzdCB2aWV3-->add tag label to commit list view<!--description-->
<!--end release-notes-assistant-->

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8759
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: pat-s <patrick.schratz@gmail.com>
Co-committed-by: pat-s <patrick.schratz@gmail.com>
2025-08-06 14:47:51 +02:00

61 lines
2.6 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"strings"
"testing"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
// TestRepoCommitsTemplateVariables ensures that template variables in commits_list.tmpl are correctly referenced
func TestRepoCommitsTemplateVariables(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
// Test the main commits page
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
resp := session.MakeRequest(t, req, http.StatusOK)
assert.Equal(t, http.StatusOK, resp.Code, "Template should render without errors")
doc := NewHTMLParser(t, resp.Body)
// 1. Repository.Link is used in tag template
tagLinks := doc.doc.Find("a.ui.label.basic[href*='/src/tag/']")
if tagLinks.Length() > 0 {
href, _ := tagLinks.First().Attr("href")
assert.Contains(t, href, "/user2/repo1/src/tag/", "Repository link should be correctly rendered in tag URLs")
}
// 2. Repository.ObjectFormatName is used in the SHA column header
shaHeader := doc.doc.Find("#commits-table thead tr th.sha")
assert.Equal(t, 1, shaHeader.Length(), "SHA column header should exist")
headerText := strings.TrimSpace(shaHeader.Text())
assert.NotEmpty(t, headerText, "SHA column header should have text (ObjectFormatName)")
// Should be uppercase SHA1 or SHA256 depending on the repository format
assert.True(t, headerText == "SHA1" || headerText == "SHA256", "ObjectFormatName should be rendered correctly, got: %s", headerText)
// 3. Repository.ComposeMetas is used for rendering commit messages
commitMessages := doc.doc.Find("#commits-table tbody tr td.message .commit-summary")
assert.Positive(t, commitMessages.Length(), "Should have commit messages rendered")
// 4. RepoLink variable is used throughout
commitLinks := doc.doc.Find("#commits-table tbody tr td.sha a[href*='/commit/']")
assert.Positive(t, commitLinks.Length(), "Should have commit links")
firstCommitLink, _ := commitLinks.First().Attr("href")
assert.Contains(t, firstCommitLink, "/user2/repo1/commit/", "RepoLink should be correctly used in commit URLs")
// 5. CommitTagsMap is used for tag rendering
// If $.CommitTagsMap is mistyped, the template would fail with a 500 error
// (for detailed tag rendering tests see repo_commits_tags_test.go)
tagLabels := doc.doc.Find("#commits-table tbody tr td.message a.ui.label.basic")
if tagLabels.Length() > 0 {
assert.NotContains(t, tagLabels.First().Text(), "{{", "Tags should be properly rendered without template syntax")
}
}