| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {filterFilesFromPatch, parsePatch} from '../patch/parse'; |
| b69ab31 | | | 9 | import {stringifyPatch} from '../patch/stringify'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | describe('patch/stringify', () => { |
| b69ab31 | | | 12 | describe('round-trip conversion', () => { |
| b69ab31 | | | 13 | it('should round-trip basic modified patch', () => { |
| b69ab31 | | | 14 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 15 | --- sapling/eden/scm/a |
| b69ab31 | | | 16 | +++ sapling/eden/scm/a |
| b69ab31 | | | 17 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 18 | 1 |
| b69ab31 | | | 19 | +2 |
| b69ab31 | | | 20 | `; |
| b69ab31 | | | 21 | const parsed = parsePatch(patch); |
| b69ab31 | | | 22 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 23 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 24 | }); |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | it('should round-trip rename', () => { |
| b69ab31 | | | 27 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 28 | rename from sapling/eden/scm/a |
| b69ab31 | | | 29 | rename to sapling/eden/scm/b |
| b69ab31 | | | 30 | `; |
| b69ab31 | | | 31 | const parsed = parsePatch(patch); |
| b69ab31 | | | 32 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 33 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 34 | }); |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | it('should round-trip rename and modify', () => { |
| b69ab31 | | | 37 | const patch = `diff --git sapling/eden/addons/LICENSE sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 38 | rename from sapling/eden/addons/LICENSE |
| b69ab31 | | | 39 | rename to sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 40 | --- sapling/eden/addons/LICENSE |
| b69ab31 | | | 41 | +++ sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 42 | @@ -2,6 +2,7 @@ |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | Copyright (c) Meta Platforms, Inc. and its affiliates. |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | + |
| b69ab31 | | | 47 | `; |
| b69ab31 | | | 48 | const parsed = parsePatch(patch); |
| b69ab31 | | | 49 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 50 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | it('should round-trip new file', () => { |
| b69ab31 | | | 54 | const patch = `diff --git sapling/eden/scm/c sapling/eden/scm/c |
| b69ab31 | | | 55 | new file mode 100644 |
| b69ab31 | | | 56 | --- /dev/null |
| b69ab31 | | | 57 | +++ sapling/eden/scm/c |
| b69ab31 | | | 58 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 59 | +1 |
| b69ab31 | | | 60 | `; |
| b69ab31 | | | 61 | const parsed = parsePatch(patch); |
| b69ab31 | | | 62 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 63 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 64 | }); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | it('should round-trip new empty file', () => { |
| b69ab31 | | | 67 | const patch = `diff --git sapling/eden/addons/d sapling/eden/addons/d |
| b69ab31 | | | 68 | new file mode 100644 |
| b69ab31 | | | 69 | `; |
| b69ab31 | | | 70 | const parsed = parsePatch(patch); |
| b69ab31 | | | 71 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 72 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 73 | }); |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | it('should round-trip deleted file', () => { |
| b69ab31 | | | 76 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 77 | deleted file mode 100644 |
| b69ab31 | | | 78 | --- sapling/eden/scm/a |
| b69ab31 | | | 79 | +++ /dev/null |
| b69ab31 | | | 80 | @@ -1,1 +0,0 @@ |
| b69ab31 | | | 81 | -1 |
| b69ab31 | | | 82 | `; |
| b69ab31 | | | 83 | const parsed = parsePatch(patch); |
| b69ab31 | | | 84 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 85 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | it('should round-trip copied file', () => { |
| b69ab31 | | | 89 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 90 | copy from sapling/eden/scm/a |
| b69ab31 | | | 91 | copy to sapling/eden/scm/b |
| b69ab31 | | | 92 | `; |
| b69ab31 | | | 93 | const parsed = parsePatch(patch); |
| b69ab31 | | | 94 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 95 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 96 | }); |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | it('should round-trip multiple files', () => { |
| b69ab31 | | | 99 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 100 | --- sapling/eden/scm/a |
| b69ab31 | | | 101 | +++ sapling/eden/scm/a |
| b69ab31 | | | 102 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 103 | 1 |
| b69ab31 | | | 104 | +2 |
| b69ab31 | | | 105 | diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 106 | copy from sapling/eden/scm/a |
| b69ab31 | | | 107 | copy to sapling/eden/scm/b |
| b69ab31 | | | 108 | diff --git sapling/eden/scm/c sapling/eden/scm/d |
| b69ab31 | | | 109 | copy from sapling/eden/scm/c |
| b69ab31 | | | 110 | copy to sapling/eden/scm/d |
| b69ab31 | | | 111 | `; |
| b69ab31 | | | 112 | const parsed = parsePatch(patch); |
| b69ab31 | | | 113 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 114 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | it('should round-trip file mode change', () => { |
| b69ab31 | | | 118 | const patch = `diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 119 | old mode 100644 |
| b69ab31 | | | 120 | new mode 100755 |
| b69ab31 | | | 121 | `; |
| b69ab31 | | | 122 | const parsed = parsePatch(patch); |
| b69ab31 | | | 123 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 124 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 125 | }); |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | it('should round-trip submodule modification', () => { |
| b69ab31 | | | 128 | const patch = `diff --git a/external/brotli b/external/brotli |
| b69ab31 | | | 129 | --- a/external/brotli |
| b69ab31 | | | 130 | +++ b/external/brotli |
| b69ab31 | | | 131 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 132 | -Subproject commit 892110204ccf44fcd493ae415c9a69c470c2a9cf |
| b69ab31 | | | 133 | +Subproject commit 57de5cc4288565a9c3a7af978ef15f0abf0ada1b |
| b69ab31 | | | 134 | `; |
| b69ab31 | | | 135 | const parsed = parsePatch(patch); |
| b69ab31 | | | 136 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 137 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 138 | }); |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | it('should round-trip added submodule', () => { |
| b69ab31 | | | 141 | const patch = `diff --git a/path/to/submodule b/path/to/submodule |
| b69ab31 | | | 142 | new file mode 160000 |
| b69ab31 | | | 143 | --- /dev/null |
| b69ab31 | | | 144 | +++ b/path/to/submodule |
| b69ab31 | | | 145 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 146 | +Subproject commit 7ef4220022059b9b1e1d8ec4eea6f7abd011894f |
| b69ab31 | | | 147 | `; |
| b69ab31 | | | 148 | const parsed = parsePatch(patch); |
| b69ab31 | | | 149 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 150 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 151 | }); |
| b69ab31 | | | 152 | }); |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | describe('hunk range formatting', () => { |
| b69ab31 | | | 155 | it('should format single line hunk with count', () => { |
| b69ab31 | | | 156 | const patch = `diff --git a/file.txt b/file.txt |
| b69ab31 | | | 157 | --- a/file.txt |
| b69ab31 | | | 158 | +++ b/file.txt |
| b69ab31 | | | 159 | @@ -5,1 +5,2 @@ |
| b69ab31 | | | 160 | line 5 |
| b69ab31 | | | 161 | +new line |
| b69ab31 | | | 162 | `; |
| b69ab31 | | | 163 | const parsed = parsePatch(patch); |
| b69ab31 | | | 164 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 165 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | it('should format empty old range', () => { |
| b69ab31 | | | 169 | const patch = `diff --git sapling/eden/scm/c sapling/eden/scm/c |
| b69ab31 | | | 170 | new file mode 100644 |
| b69ab31 | | | 171 | --- /dev/null |
| b69ab31 | | | 172 | +++ sapling/eden/scm/c |
| b69ab31 | | | 173 | @@ -0,0 +1,3 @@ |
| b69ab31 | | | 174 | +line 1 |
| b69ab31 | | | 175 | +line 2 |
| b69ab31 | | | 176 | +line 3 |
| b69ab31 | | | 177 | `; |
| b69ab31 | | | 178 | const parsed = parsePatch(patch); |
| b69ab31 | | | 179 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 180 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 181 | }); |
| b69ab31 | | | 182 | |
| b69ab31 | | | 183 | it('should format empty new range', () => { |
| b69ab31 | | | 184 | const patch = `diff --git a/file.txt b/file.txt |
| b69ab31 | | | 185 | --- a/file.txt |
| b69ab31 | | | 186 | +++ b/file.txt |
| b69ab31 | | | 187 | @@ -1,3 +0,0 @@ |
| b69ab31 | | | 188 | -line 1 |
| b69ab31 | | | 189 | -line 2 |
| b69ab31 | | | 190 | -line 3 |
| b69ab31 | | | 191 | `; |
| b69ab31 | | | 192 | const parsed = parsePatch(patch); |
| b69ab31 | | | 193 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 194 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 195 | }); |
| b69ab31 | | | 196 | }); |
| b69ab31 | | | 197 | |
| b69ab31 | | | 198 | describe('line delimiter handling', () => { |
| b69ab31 | | | 199 | it('should preserve hunk line delimiters', () => { |
| b69ab31 | | | 200 | const patch = `diff --git a/file.txt b/file.txt |
| b69ab31 | | | 201 | --- a/file.txt |
| b69ab31 | | | 202 | +++ b/file.txt |
| b69ab31 | | | 203 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 204 | line 1\r |
| b69ab31 | | | 205 | +line 2\r |
| b69ab31 | | | 206 | `; |
| b69ab31 | | | 207 | const parsed = parsePatch(patch); |
| b69ab31 | | | 208 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 209 | // Header lines use standard \n, but hunk content preserves \r |
| b69ab31 | | | 210 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 211 | }); |
| b69ab31 | | | 212 | }); |
| b69ab31 | | | 213 | |
| b69ab31 | | | 214 | describe('multiple hunks', () => { |
| b69ab31 | | | 215 | it('should handle multiple hunks in a single file', () => { |
| b69ab31 | | | 216 | const patch = `diff --git a/file.txt b/file.txt |
| b69ab31 | | | 217 | --- a/file.txt |
| b69ab31 | | | 218 | +++ b/file.txt |
| b69ab31 | | | 219 | @@ -1,3 +1,4 @@ |
| b69ab31 | | | 220 | line 1 |
| b69ab31 | | | 221 | +new line 1.5 |
| b69ab31 | | | 222 | line 2 |
| b69ab31 | | | 223 | line 3 |
| b69ab31 | | | 224 | @@ -10,2 +11,3 @@ |
| b69ab31 | | | 225 | line 10 |
| b69ab31 | | | 226 | +new line 10.5 |
| b69ab31 | | | 227 | line 11 |
| b69ab31 | | | 228 | `; |
| b69ab31 | | | 229 | const parsed = parsePatch(patch); |
| b69ab31 | | | 230 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 231 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 232 | }); |
| b69ab31 | | | 233 | }); |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | describe('no newline at end of file', () => { |
| b69ab31 | | | 236 | it('should handle backslash-no-newline marker', () => { |
| b69ab31 | | | 237 | const patch = `diff --git a/file.txt b/file.txt |
| b69ab31 | | | 238 | --- a/file.txt |
| b69ab31 | | | 239 | +++ b/file.txt |
| b69ab31 | | | 240 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 241 | -old content |
| b69ab31 | | | 242 | \\ No newline at end of file |
| b69ab31 | | | 243 | +new content |
| b69ab31 | | | 244 | \\ No newline at end of file |
| b69ab31 | | | 245 | `; |
| b69ab31 | | | 246 | const parsed = parsePatch(patch); |
| b69ab31 | | | 247 | const stringified = stringifyPatch(parsed); |
| b69ab31 | | | 248 | expect(stringified).toEqual(patch); |
| b69ab31 | | | 249 | }); |
| b69ab31 | | | 250 | }); |
| b69ab31 | | | 251 | }); |
| b69ab31 | | | 252 | |
| b69ab31 | | | 253 | describe('patch/filterFilesFromPatch', () => { |
| b69ab31 | | | 254 | describe('basic filtering', () => { |
| b69ab31 | | | 255 | it('should filter out a single modified file', () => { |
| b69ab31 | | | 256 | const patch = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 257 | --- a/keep.ts |
| b69ab31 | | | 258 | +++ b/keep.ts |
| b69ab31 | | | 259 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 260 | line 1 |
| b69ab31 | | | 261 | +line 2 |
| b69ab31 | | | 262 | diff --git a/remove.ts b/remove.ts |
| b69ab31 | | | 263 | --- a/remove.ts |
| b69ab31 | | | 264 | +++ b/remove.ts |
| b69ab31 | | | 265 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 266 | original |
| b69ab31 | | | 267 | +changed |
| b69ab31 | | | 268 | `; |
| b69ab31 | | | 269 | const filtered = filterFilesFromPatch(patch, ['a/remove.ts']); |
| b69ab31 | | | 270 | const expected = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 271 | --- a/keep.ts |
| b69ab31 | | | 272 | +++ b/keep.ts |
| b69ab31 | | | 273 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 274 | line 1 |
| b69ab31 | | | 275 | +line 2 |
| b69ab31 | | | 276 | `; |
| b69ab31 | | | 277 | expect(filtered).toEqual(expected); |
| b69ab31 | | | 278 | }); |
| b69ab31 | | | 279 | |
| b69ab31 | | | 280 | it('should filter out multiple files', () => { |
| b69ab31 | | | 281 | const patch = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 282 | --- a/keep.ts |
| b69ab31 | | | 283 | +++ b/keep.ts |
| b69ab31 | | | 284 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 285 | line 1 |
| b69ab31 | | | 286 | +line 2 |
| b69ab31 | | | 287 | diff --git a/remove1.ts b/remove1.ts |
| b69ab31 | | | 288 | --- a/remove1.ts |
| b69ab31 | | | 289 | +++ b/remove1.ts |
| b69ab31 | | | 290 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 291 | -old |
| b69ab31 | | | 292 | +new |
| b69ab31 | | | 293 | diff --git a/remove2.ts b/remove2.ts |
| b69ab31 | | | 294 | --- a/remove2.ts |
| b69ab31 | | | 295 | +++ b/remove2.ts |
| b69ab31 | | | 296 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 297 | -old |
| b69ab31 | | | 298 | +new |
| b69ab31 | | | 299 | `; |
| b69ab31 | | | 300 | const filtered = filterFilesFromPatch(patch, ['a/remove1.ts', 'b/remove2.ts']); |
| b69ab31 | | | 301 | const expected = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 302 | --- a/keep.ts |
| b69ab31 | | | 303 | +++ b/keep.ts |
| b69ab31 | | | 304 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 305 | line 1 |
| b69ab31 | | | 306 | +line 2 |
| b69ab31 | | | 307 | `; |
| b69ab31 | | | 308 | expect(filtered).toEqual(expected); |
| b69ab31 | | | 309 | }); |
| b69ab31 | | | 310 | |
| b69ab31 | | | 311 | it('should return empty string when all files are filtered', () => { |
| b69ab31 | | | 312 | const patch = `diff --git a/remove.ts b/remove.ts |
| b69ab31 | | | 313 | --- a/remove.ts |
| b69ab31 | | | 314 | +++ b/remove.ts |
| b69ab31 | | | 315 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 316 | original |
| b69ab31 | | | 317 | +changed |
| b69ab31 | | | 318 | `; |
| b69ab31 | | | 319 | const filtered = filterFilesFromPatch(patch, ['a/remove.ts']); |
| b69ab31 | | | 320 | expect(filtered).toEqual(''); |
| b69ab31 | | | 321 | }); |
| b69ab31 | | | 322 | |
| b69ab31 | | | 323 | it('should return original patch when no files match filter', () => { |
| b69ab31 | | | 324 | const patch = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 325 | --- a/keep.ts |
| b69ab31 | | | 326 | +++ b/keep.ts |
| b69ab31 | | | 327 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 328 | line 1 |
| b69ab31 | | | 329 | +line 2 |
| b69ab31 | | | 330 | `; |
| b69ab31 | | | 331 | const filtered = filterFilesFromPatch(patch, ['a/nonexistent.ts']); |
| b69ab31 | | | 332 | expect(filtered).toEqual(patch); |
| b69ab31 | | | 333 | }); |
| b69ab31 | | | 334 | }); |
| b69ab31 | | | 335 | |
| b69ab31 | | | 336 | describe('path prefix handling', () => { |
| b69ab31 | | | 337 | it('should filter files with a/ prefix', () => { |
| b69ab31 | | | 338 | const patch = `diff --git a/file.ts b/file.ts |
| b69ab31 | | | 339 | --- a/file.ts |
| b69ab31 | | | 340 | +++ b/file.ts |
| b69ab31 | | | 341 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 342 | line 1 |
| b69ab31 | | | 343 | +line 2 |
| b69ab31 | | | 344 | `; |
| b69ab31 | | | 345 | const filtered = filterFilesFromPatch(patch, ['a/file.ts']); |
| b69ab31 | | | 346 | expect(filtered).toEqual(''); |
| b69ab31 | | | 347 | }); |
| b69ab31 | | | 348 | |
| b69ab31 | | | 349 | it('should filter files with b/ prefix', () => { |
| b69ab31 | | | 350 | const patch = `diff --git a/file.ts b/file.ts |
| b69ab31 | | | 351 | --- a/file.ts |
| b69ab31 | | | 352 | +++ b/file.ts |
| b69ab31 | | | 353 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 354 | line 1 |
| b69ab31 | | | 355 | +line 2 |
| b69ab31 | | | 356 | `; |
| b69ab31 | | | 357 | const filtered = filterFilesFromPatch(patch, ['b/file.ts']); |
| b69ab31 | | | 358 | expect(filtered).toEqual(''); |
| b69ab31 | | | 359 | }); |
| b69ab31 | | | 360 | |
| b69ab31 | | | 361 | it('should filter files without prefix', () => { |
| b69ab31 | | | 362 | const patch = `diff --git a/file.ts b/file.ts |
| b69ab31 | | | 363 | --- a/file.ts |
| b69ab31 | | | 364 | +++ b/file.ts |
| b69ab31 | | | 365 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 366 | line 1 |
| b69ab31 | | | 367 | +line 2 |
| b69ab31 | | | 368 | `; |
| b69ab31 | | | 369 | const filtered = filterFilesFromPatch(patch, ['file.ts']); |
| b69ab31 | | | 370 | expect(filtered).toEqual(''); |
| b69ab31 | | | 371 | }); |
| b69ab31 | | | 372 | |
| b69ab31 | | | 373 | it('should handle paths with slashes', () => { |
| b69ab31 | | | 374 | const patch = `diff --git a/src/components/App.tsx b/src/components/App.tsx |
| b69ab31 | | | 375 | --- a/src/components/App.tsx |
| b69ab31 | | | 376 | +++ b/src/components/App.tsx |
| b69ab31 | | | 377 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 378 | import React from 'react'; |
| b69ab31 | | | 379 | +import {useState} from 'react'; |
| b69ab31 | | | 380 | `; |
| b69ab31 | | | 381 | const filtered = filterFilesFromPatch(patch, ['src/components/App.tsx']); |
| b69ab31 | | | 382 | expect(filtered).toEqual(''); |
| b69ab31 | | | 383 | }); |
| b69ab31 | | | 384 | }); |
| b69ab31 | | | 385 | |
| b69ab31 | | | 386 | describe('special file operations', () => { |
| b69ab31 | | | 387 | it('should filter renamed files by old name', () => { |
| b69ab31 | | | 388 | const patch = `diff --git a/old.ts b/new.ts |
| b69ab31 | | | 389 | rename from a/old.ts |
| b69ab31 | | | 390 | rename to b/new.ts |
| b69ab31 | | | 391 | --- a/old.ts |
| b69ab31 | | | 392 | +++ b/new.ts |
| b69ab31 | | | 393 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 394 | line 1 |
| b69ab31 | | | 395 | +line 2 |
| b69ab31 | | | 396 | `; |
| b69ab31 | | | 397 | const filtered = filterFilesFromPatch(patch, ['a/old.ts']); |
| b69ab31 | | | 398 | expect(filtered).toEqual(''); |
| b69ab31 | | | 399 | }); |
| b69ab31 | | | 400 | |
| b69ab31 | | | 401 | it('should filter renamed files by new name', () => { |
| b69ab31 | | | 402 | const patch = `diff --git a/old.ts b/new.ts |
| b69ab31 | | | 403 | rename from a/old.ts |
| b69ab31 | | | 404 | rename to b/new.ts |
| b69ab31 | | | 405 | --- a/old.ts |
| b69ab31 | | | 406 | +++ b/new.ts |
| b69ab31 | | | 407 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 408 | line 1 |
| b69ab31 | | | 409 | +line 2 |
| b69ab31 | | | 410 | `; |
| b69ab31 | | | 411 | const filtered = filterFilesFromPatch(patch, ['b/new.ts']); |
| b69ab31 | | | 412 | expect(filtered).toEqual(''); |
| b69ab31 | | | 413 | }); |
| b69ab31 | | | 414 | |
| b69ab31 | | | 415 | it('should filter new files', () => { |
| b69ab31 | | | 416 | const patch = `diff --git a/existing.ts b/existing.ts |
| b69ab31 | | | 417 | --- a/existing.ts |
| b69ab31 | | | 418 | +++ b/existing.ts |
| b69ab31 | | | 419 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 420 | line 1 |
| b69ab31 | | | 421 | +line 2 |
| b69ab31 | | | 422 | diff --git a/new.ts b/new.ts |
| b69ab31 | | | 423 | new file mode 100644 |
| b69ab31 | | | 424 | --- /dev/null |
| b69ab31 | | | 425 | +++ b/new.ts |
| b69ab31 | | | 426 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 427 | +new content |
| b69ab31 | | | 428 | `; |
| b69ab31 | | | 429 | const filtered = filterFilesFromPatch(patch, ['new.ts']); |
| b69ab31 | | | 430 | const expected = `diff --git a/existing.ts b/existing.ts |
| b69ab31 | | | 431 | --- a/existing.ts |
| b69ab31 | | | 432 | +++ b/existing.ts |
| b69ab31 | | | 433 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 434 | line 1 |
| b69ab31 | | | 435 | +line 2 |
| b69ab31 | | | 436 | `; |
| b69ab31 | | | 437 | expect(filtered).toEqual(expected); |
| b69ab31 | | | 438 | }); |
| b69ab31 | | | 439 | |
| b69ab31 | | | 440 | it('should filter deleted files', () => { |
| b69ab31 | | | 441 | const patch = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 442 | --- a/keep.ts |
| b69ab31 | | | 443 | +++ b/keep.ts |
| b69ab31 | | | 444 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 445 | line 1 |
| b69ab31 | | | 446 | +line 2 |
| b69ab31 | | | 447 | diff --git a/deleted.ts b/deleted.ts |
| b69ab31 | | | 448 | deleted file mode 100644 |
| b69ab31 | | | 449 | --- a/deleted.ts |
| b69ab31 | | | 450 | +++ /dev/null |
| b69ab31 | | | 451 | @@ -1,1 +0,0 @@ |
| b69ab31 | | | 452 | -old content |
| b69ab31 | | | 453 | `; |
| b69ab31 | | | 454 | const filtered = filterFilesFromPatch(patch, ['deleted.ts']); |
| b69ab31 | | | 455 | const expected = `diff --git a/keep.ts b/keep.ts |
| b69ab31 | | | 456 | --- a/keep.ts |
| b69ab31 | | | 457 | +++ b/keep.ts |
| b69ab31 | | | 458 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 459 | line 1 |
| b69ab31 | | | 460 | +line 2 |
| b69ab31 | | | 461 | `; |
| b69ab31 | | | 462 | expect(filtered).toEqual(expected); |
| b69ab31 | | | 463 | }); |
| b69ab31 | | | 464 | |
| b69ab31 | | | 465 | it('should filter copied files', () => { |
| b69ab31 | | | 466 | const patch = `diff --git a/original.ts b/copy.ts |
| b69ab31 | | | 467 | copy from a/original.ts |
| b69ab31 | | | 468 | copy to b/copy.ts |
| b69ab31 | | | 469 | `; |
| b69ab31 | | | 470 | const filtered = filterFilesFromPatch(patch, ['copy.ts']); |
| b69ab31 | | | 471 | expect(filtered).toEqual(''); |
| b69ab31 | | | 472 | }); |
| b69ab31 | | | 473 | |
| b69ab31 | | | 474 | it('should filter mode changes', () => { |
| b69ab31 | | | 475 | const patch = `diff --git a/script.sh b/script.sh |
| b69ab31 | | | 476 | old mode 100644 |
| b69ab31 | | | 477 | new mode 100755 |
| b69ab31 | | | 478 | `; |
| b69ab31 | | | 479 | const filtered = filterFilesFromPatch(patch, ['script.sh']); |
| b69ab31 | | | 480 | expect(filtered).toEqual(''); |
| b69ab31 | | | 481 | }); |
| b69ab31 | | | 482 | }); |
| b69ab31 | | | 483 | |
| b69ab31 | | | 484 | describe('real-world use case: filtering generated files', () => { |
| b69ab31 | | | 485 | it('should remove generated code files from patch', () => { |
| b69ab31 | | | 486 | const patch = `diff --git a/src/manual.ts b/src/manual.ts |
| b69ab31 | | | 487 | --- a/src/manual.ts |
| b69ab31 | | | 488 | +++ b/src/manual.ts |
| b69ab31 | | | 489 | @@ -1,3 +1,4 @@ |
| b69ab31 | | | 490 | // Manually written code |
| b69ab31 | | | 491 | export function doSomething() { |
| b69ab31 | | | 492 | + console.log('new feature'); |
| b69ab31 | | | 493 | } |
| b69ab31 | | | 494 | diff --git a/generated/types.ts b/generated/types.ts |
| b69ab31 | | | 495 | --- a/generated/types.ts |
| b69ab31 | | | 496 | +++ b/generated/types.ts |
| b69ab31 | | | 497 | @@ -1,100 +1,200 @@ |
| b69ab31 | | | 498 | -// Auto-generated - do not edit |
| b69ab31 | | | 499 | +// Auto-generated - do not edit |
| b69ab31 | | | 500 | +// Lots of noisy changes |
| b69ab31 | | | 501 | export type GeneratedType = string; |
| b69ab31 | | | 502 | diff --git a/generated/schema.ts b/generated/schema.ts |
| b69ab31 | | | 503 | --- a/generated/schema.ts |
| b69ab31 | | | 504 | +++ b/generated/schema.ts |
| b69ab31 | | | 505 | @@ -1,50 +1,100 @@ |
| b69ab31 | | | 506 | -// Auto-generated |
| b69ab31 | | | 507 | +// Auto-generated |
| b69ab31 | | | 508 | +// More noise |
| b69ab31 | | | 509 | export const schema = {}; |
| b69ab31 | | | 510 | `; |
| b69ab31 | | | 511 | const filtered = filterFilesFromPatch(patch, ['generated/types.ts', 'generated/schema.ts']); |
| b69ab31 | | | 512 | const expected = `diff --git a/src/manual.ts b/src/manual.ts |
| b69ab31 | | | 513 | --- a/src/manual.ts |
| b69ab31 | | | 514 | +++ b/src/manual.ts |
| b69ab31 | | | 515 | @@ -1,3 +1,4 @@ |
| b69ab31 | | | 516 | // Manually written code |
| b69ab31 | | | 517 | export function doSomething() { |
| b69ab31 | | | 518 | + console.log('new feature'); |
| b69ab31 | | | 519 | } |
| b69ab31 | | | 520 | `; |
| b69ab31 | | | 521 | expect(filtered).toEqual(expected); |
| b69ab31 | | | 522 | }); |
| b69ab31 | | | 523 | }); |
| b69ab31 | | | 524 | |
| b69ab31 | | | 525 | describe('edge cases', () => { |
| b69ab31 | | | 526 | it('should handle empty patch', () => { |
| b69ab31 | | | 527 | const filtered = filterFilesFromPatch('', ['file.ts']); |
| b69ab31 | | | 528 | expect(filtered).toEqual(''); |
| b69ab31 | | | 529 | }); |
| b69ab31 | | | 530 | |
| b69ab31 | | | 531 | it('should handle empty file list', () => { |
| b69ab31 | | | 532 | const patch = `diff --git a/file.ts b/file.ts |
| b69ab31 | | | 533 | --- a/file.ts |
| b69ab31 | | | 534 | +++ b/file.ts |
| b69ab31 | | | 535 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 536 | line 1 |
| b69ab31 | | | 537 | +line 2 |
| b69ab31 | | | 538 | `; |
| b69ab31 | | | 539 | const filtered = filterFilesFromPatch(patch, []); |
| b69ab31 | | | 540 | expect(filtered).toEqual(patch); |
| b69ab31 | | | 541 | }); |
| b69ab31 | | | 542 | |
| b69ab31 | | | 543 | it('should handle files with multiple hunks', () => { |
| b69ab31 | | | 544 | const patch = `diff --git a/file.ts b/file.ts |
| b69ab31 | | | 545 | --- a/file.ts |
| b69ab31 | | | 546 | +++ b/file.ts |
| b69ab31 | | | 547 | @@ -1,3 +1,4 @@ |
| b69ab31 | | | 548 | line 1 |
| b69ab31 | | | 549 | +new line |
| b69ab31 | | | 550 | line 2 |
| b69ab31 | | | 551 | line 3 |
| b69ab31 | | | 552 | @@ -10,2 +11,3 @@ |
| b69ab31 | | | 553 | line 10 |
| b69ab31 | | | 554 | +another new line |
| b69ab31 | | | 555 | line 11 |
| b69ab31 | | | 556 | `; |
| b69ab31 | | | 557 | const filtered = filterFilesFromPatch(patch, ['file.ts']); |
| b69ab31 | | | 558 | expect(filtered).toEqual(''); |
| b69ab31 | | | 559 | }); |
| b69ab31 | | | 560 | }); |
| b69ab31 | | | 561 | }); |