|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package gitdiff |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/db" |
| 11 | + "code.gitea.io/gitea/modules/setting" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestParseSubmoduleInfo(t *testing.T) { |
| 17 | + type testcase struct { |
| 18 | + name string |
| 19 | + gitdiff string |
| 20 | + infos map[int]SubmoduleInfo |
| 21 | + } |
| 22 | + |
| 23 | + tests := []testcase{ |
| 24 | + { |
| 25 | + name: "added", |
| 26 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 27 | +new file mode 100644 |
| 28 | +index 0000000..4ac13c1 |
| 29 | +--- /dev/null |
| 30 | ++++ b/.gitmodules |
| 31 | +@@ -0,0 +1,3 @@ |
| 32 | ++[submodule "gitea-mirror"] |
| 33 | ++ path = gitea-mirror |
| 34 | ++ url = https://gitea.com/gitea/gitea-mirror |
| 35 | +diff --git a/gitea-mirror b/gitea-mirror |
| 36 | +new file mode 160000 |
| 37 | +index 0000000..68972a9 |
| 38 | +--- /dev/null |
| 39 | ++++ b/gitea-mirror |
| 40 | +@@ -0,0 +1 @@ |
| 41 | ++Subproject commit 68972a994719ae5c74e28d8fa82fa27c23399bc8 |
| 42 | +`, |
| 43 | + infos: map[int]SubmoduleInfo{ |
| 44 | + 1: {NewRefID: "68972a994719ae5c74e28d8fa82fa27c23399bc8"}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "updated", |
| 49 | + gitdiff: `diff --git a/gitea-mirror b/gitea-mirror |
| 50 | +index 68972a9..c8ffe77 160000 |
| 51 | +--- a/gitea-mirror |
| 52 | ++++ b/gitea-mirror |
| 53 | +@@ -1 +1 @@ |
| 54 | +-Subproject commit 68972a994719ae5c74e28d8fa82fa27c23399bc8 |
| 55 | ++Subproject commit c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d |
| 56 | +`, |
| 57 | + infos: map[int]SubmoduleInfo{ |
| 58 | + 0: { |
| 59 | + PreviousRefID: "68972a994719ae5c74e28d8fa82fa27c23399bc8", |
| 60 | + NewRefID: "c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d", |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "rename", |
| 66 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 67 | +index 4ac13c1..0510edd 100644 |
| 68 | +--- a/.gitmodules |
| 69 | ++++ b/.gitmodules |
| 70 | +@@ -1,3 +1,3 @@ |
| 71 | + [submodule "gitea-mirror"] |
| 72 | +- path = gitea-mirror |
| 73 | ++ path = gitea |
| 74 | + url = https://gitea.com/gitea/gitea-mirror |
| 75 | +diff --git a/gitea-mirror b/gitea |
| 76 | +similarity index 100% |
| 77 | +rename from gitea-mirror |
| 78 | +rename to gitea |
| 79 | +`, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "deleted", |
| 83 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 84 | +index 0510edd..e69de29 100644 |
| 85 | +--- a/.gitmodules |
| 86 | ++++ b/.gitmodules |
| 87 | +@@ -1,3 +0,0 @@ |
| 88 | +-[submodule "gitea-mirror"] |
| 89 | +- path = gitea |
| 90 | +- url = https://gitea.com/gitea/gitea-mirror |
| 91 | +diff --git a/gitea b/gitea |
| 92 | +deleted file mode 160000 |
| 93 | +index c8ffe77..0000000 |
| 94 | +--- a/gitea |
| 95 | ++++ /dev/null |
| 96 | +@@ -1 +0,0 @@ |
| 97 | +-Subproject commit c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d |
| 98 | +`, |
| 99 | + infos: map[int]SubmoduleInfo{ |
| 100 | + 1: { |
| 101 | + PreviousRefID: "c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "moved and updated", |
| 107 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 108 | +index 0510edd..bced3d8 100644 |
| 109 | +--- a/.gitmodules |
| 110 | ++++ b/.gitmodules |
| 111 | +@@ -1,3 +1,3 @@ |
| 112 | + [submodule "gitea-mirror"] |
| 113 | +- path = gitea |
| 114 | ++ path = gitea-1.22 |
| 115 | + url = https://gitea.com/gitea/gitea-mirror |
| 116 | +diff --git a/gitea b/gitea |
| 117 | +deleted file mode 160000 |
| 118 | +index c8ffe77..0000000 |
| 119 | +--- a/gitea |
| 120 | ++++ /dev/null |
| 121 | +@@ -1 +0,0 @@ |
| 122 | +-Subproject commit c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d |
| 123 | +diff --git a/gitea-1.22 b/gitea-1.22 |
| 124 | +new file mode 160000 |
| 125 | +index 0000000..8eefa1f |
| 126 | +--- /dev/null |
| 127 | ++++ b/gitea-1.22 |
| 128 | +@@ -0,0 +1 @@ |
| 129 | ++Subproject commit 8eefa1f6dedf2488db2c9e12c916e8e51f673160 |
| 130 | +`, |
| 131 | + infos: map[int]SubmoduleInfo{ |
| 132 | + 1: { |
| 133 | + PreviousRefID: "c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d", |
| 134 | + }, |
| 135 | + 2: { |
| 136 | + NewRefID: "8eefa1f6dedf2488db2c9e12c916e8e51f673160", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "converted to file", |
| 142 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 143 | +index 0510edd..e69de29 100644 |
| 144 | +--- a/.gitmodules |
| 145 | ++++ b/.gitmodules |
| 146 | +@@ -1,3 +0,0 @@ |
| 147 | +-[submodule "gitea-mirror"] |
| 148 | +- path = gitea |
| 149 | +- url = https://gitea.com/gitea/gitea-mirror |
| 150 | +diff --git a/gitea b/gitea |
| 151 | +deleted file mode 160000 |
| 152 | +index c8ffe77..0000000 |
| 153 | +--- a/gitea |
| 154 | ++++ /dev/null |
| 155 | +@@ -1 +0,0 @@ |
| 156 | +-Subproject commit c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d |
| 157 | +diff --git a/gitea b/gitea |
| 158 | +new file mode 100644 |
| 159 | +index 0000000..33a9488 |
| 160 | +--- /dev/null |
| 161 | ++++ b/gitea |
| 162 | +@@ -0,0 +1 @@ |
| 163 | ++example |
| 164 | +`, |
| 165 | + infos: map[int]SubmoduleInfo{ |
| 166 | + 1: { |
| 167 | + PreviousRefID: "c8ffe777cf9c5bb47a38e3e0b3a3b5de6cd8813d", |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "converted to submodule", |
| 173 | + gitdiff: `diff --git a/.gitmodules b/.gitmodules |
| 174 | +index e69de29..14ee267 100644 |
| 175 | +--- a/.gitmodules |
| 176 | ++++ b/.gitmodules |
| 177 | +@@ -0,0 +1,3 @@ |
| 178 | ++[submodule "gitea"] |
| 179 | ++ path = gitea |
| 180 | ++ url = https://gitea.com/gitea/gitea-mirror |
| 181 | +diff --git a/gitea b/gitea |
| 182 | +deleted file mode 100644 |
| 183 | +index 33a9488..0000000 |
| 184 | +--- a/gitea |
| 185 | ++++ /dev/null |
| 186 | +@@ -1 +0,0 @@ |
| 187 | +-example |
| 188 | +diff --git a/gitea b/gitea |
| 189 | +new file mode 160000 |
| 190 | +index 0000000..68972a9 |
| 191 | +--- /dev/null |
| 192 | ++++ b/gitea |
| 193 | +@@ -0,0 +1 @@ |
| 194 | ++Subproject commit 68972a994719ae5c74e28d8fa82fa27c23399bc8 |
| 195 | +`, |
| 196 | + infos: map[int]SubmoduleInfo{ |
| 197 | + 2: { |
| 198 | + NewRefID: "68972a994719ae5c74e28d8fa82fa27c23399bc8", |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | + |
| 204 | + for _, testcase := range tests { |
| 205 | + testcase := testcase |
| 206 | + t.Run(testcase.name, func(t *testing.T) { |
| 207 | + diff, err := ParsePatch(db.DefaultContext, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "") |
| 208 | + assert.NoError(t, err) |
| 209 | + |
| 210 | + for i, expected := range testcase.infos { |
| 211 | + actual := diff.Files[i] |
| 212 | + assert.NotNil(t, actual) |
| 213 | + assert.Equal(t, expected, *actual.SubmoduleInfo) |
| 214 | + } |
| 215 | + }) |
| 216 | + } |
| 217 | +} |
0 commit comments