code.gitea.io/gitea@v1.22.3/services/repository/files/diff_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package files
     5  
     6  import (
     7  	"testing"
     8  
     9  	repo_model "code.gitea.io/gitea/models/repo"
    10  	"code.gitea.io/gitea/models/unittest"
    11  	"code.gitea.io/gitea/modules/json"
    12  	"code.gitea.io/gitea/services/contexttest"
    13  	"code.gitea.io/gitea/services/gitdiff"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestGetDiffPreview(t *testing.T) {
    19  	unittest.PrepareTestEnv(t)
    20  	ctx, _ := contexttest.MockContext(t, "user2/repo1")
    21  	ctx.SetParams(":id", "1")
    22  	contexttest.LoadRepo(t, ctx, 1)
    23  	contexttest.LoadRepoCommit(t, ctx)
    24  	contexttest.LoadUser(t, ctx, 2)
    25  	contexttest.LoadGitRepo(t, ctx)
    26  	defer ctx.Repo.GitRepo.Close()
    27  
    28  	branch := ctx.Repo.Repository.DefaultBranch
    29  	treePath := "README.md"
    30  	content := "# repo1\n\nDescription for repo1\nthis is a new line"
    31  
    32  	expectedDiff := &gitdiff.Diff{
    33  		TotalAddition: 2,
    34  		TotalDeletion: 1,
    35  		Files: []*gitdiff.DiffFile{
    36  			{
    37  				Name:        "README.md",
    38  				OldName:     "README.md",
    39  				NameHash:    "8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d",
    40  				Index:       1,
    41  				Addition:    2,
    42  				Deletion:    1,
    43  				Type:        2,
    44  				IsCreated:   false,
    45  				IsDeleted:   false,
    46  				IsBin:       false,
    47  				IsLFSFile:   false,
    48  				IsRenamed:   false,
    49  				IsSubmodule: false,
    50  				Sections: []*gitdiff.DiffSection{
    51  					{
    52  						FileName: "README.md",
    53  						Name:     "",
    54  						Lines: []*gitdiff.DiffLine{
    55  							{
    56  								LeftIdx:  0,
    57  								RightIdx: 0,
    58  								Type:     4,
    59  								Content:  "@@ -1,3 +1,4 @@",
    60  								Comments: nil,
    61  								SectionInfo: &gitdiff.DiffLineSectionInfo{
    62  									Path:          "README.md",
    63  									LastLeftIdx:   0,
    64  									LastRightIdx:  0,
    65  									LeftIdx:       1,
    66  									RightIdx:      1,
    67  									LeftHunkSize:  3,
    68  									RightHunkSize: 4,
    69  								},
    70  							},
    71  							{
    72  								LeftIdx:  1,
    73  								RightIdx: 1,
    74  								Type:     1,
    75  								Content:  " # repo1",
    76  								Comments: nil,
    77  							},
    78  							{
    79  								LeftIdx:  2,
    80  								RightIdx: 2,
    81  								Type:     1,
    82  								Content:  " ",
    83  								Comments: nil,
    84  							},
    85  							{
    86  								LeftIdx:  3,
    87  								RightIdx: 0,
    88  								Match:    4,
    89  								Type:     3,
    90  								Content:  "-Description for repo1",
    91  								Comments: nil,
    92  							},
    93  							{
    94  								LeftIdx:  0,
    95  								RightIdx: 3,
    96  								Match:    3,
    97  								Type:     2,
    98  								Content:  "+Description for repo1",
    99  								Comments: nil,
   100  							},
   101  							{
   102  								LeftIdx:  0,
   103  								RightIdx: 4,
   104  								Match:    -1,
   105  								Type:     2,
   106  								Content:  "+this is a new line",
   107  								Comments: nil,
   108  							},
   109  						},
   110  					},
   111  				},
   112  				IsIncomplete: false,
   113  			},
   114  		},
   115  		IsIncomplete: false,
   116  	}
   117  	expectedDiff.NumFiles = len(expectedDiff.Files)
   118  
   119  	t.Run("with given branch", func(t *testing.T) {
   120  		diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, treePath, content)
   121  		assert.NoError(t, err)
   122  		expectedBs, err := json.Marshal(expectedDiff)
   123  		assert.NoError(t, err)
   124  		bs, err := json.Marshal(diff)
   125  		assert.NoError(t, err)
   126  		assert.EqualValues(t, string(expectedBs), string(bs))
   127  	})
   128  
   129  	t.Run("empty branch, same results", func(t *testing.T) {
   130  		diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, "", treePath, content)
   131  		assert.NoError(t, err)
   132  		expectedBs, err := json.Marshal(expectedDiff)
   133  		assert.NoError(t, err)
   134  		bs, err := json.Marshal(diff)
   135  		assert.NoError(t, err)
   136  		assert.EqualValues(t, expectedBs, bs)
   137  	})
   138  }
   139  
   140  func TestGetDiffPreviewErrors(t *testing.T) {
   141  	unittest.PrepareTestEnv(t)
   142  	ctx, _ := contexttest.MockContext(t, "user2/repo1")
   143  	ctx.SetParams(":id", "1")
   144  	contexttest.LoadRepo(t, ctx, 1)
   145  	contexttest.LoadRepoCommit(t, ctx)
   146  	contexttest.LoadUser(t, ctx, 2)
   147  	contexttest.LoadGitRepo(t, ctx)
   148  	defer ctx.Repo.GitRepo.Close()
   149  
   150  	branch := ctx.Repo.Repository.DefaultBranch
   151  	treePath := "README.md"
   152  	content := "# repo1\n\nDescription for repo1\nthis is a new line"
   153  
   154  	t.Run("empty repo", func(t *testing.T) {
   155  		diff, err := GetDiffPreview(ctx, &repo_model.Repository{}, branch, treePath, content)
   156  		assert.Nil(t, diff)
   157  		assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
   158  	})
   159  
   160  	t.Run("bad branch", func(t *testing.T) {
   161  		badBranch := "bad_branch"
   162  		diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, badBranch, treePath, content)
   163  		assert.Nil(t, diff)
   164  		assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
   165  	})
   166  
   167  	t.Run("empty treePath", func(t *testing.T) {
   168  		diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, "", content)
   169  		assert.Nil(t, diff)
   170  		assert.EqualError(t, err, "path is invalid [path: ]")
   171  	})
   172  }