| 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 {splitLines} from '../diff'; |
| b69ab31 | | | 9 | import {guessIsSubmodule, parseParsedDiff, parsePatch} from '../patch/parse'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | describe('patch/parse', () => { |
| b69ab31 | | | 12 | it('should parse basic modified patch', () => { |
| b69ab31 | | | 13 | const patch = ` |
| b69ab31 | | | 14 | 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 expected = [ |
| b69ab31 | | | 22 | { |
| b69ab31 | | | 23 | hunks: [ |
| b69ab31 | | | 24 | { |
| b69ab31 | | | 25 | linedelimiters: ['\n', '\n'], |
| b69ab31 | | | 26 | lines: [' 1', '+2'], |
| b69ab31 | | | 27 | newLines: 2, |
| b69ab31 | | | 28 | newStart: 1, |
| b69ab31 | | | 29 | oldLines: 1, |
| b69ab31 | | | 30 | oldStart: 1, |
| b69ab31 | | | 31 | }, |
| b69ab31 | | | 32 | ], |
| b69ab31 | | | 33 | newFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 34 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 35 | type: 'Modified', |
| b69ab31 | | | 36 | }, |
| b69ab31 | | | 37 | ]; |
| b69ab31 | | | 38 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 39 | }); |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | it('should parse rename', () => { |
| b69ab31 | | | 42 | const patch = ` |
| b69ab31 | | | 43 | diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 44 | rename from sapling/eden/scm/a |
| b69ab31 | | | 45 | rename to sapling/eden/scm/b |
| b69ab31 | | | 46 | `; |
| b69ab31 | | | 47 | const expected = [ |
| b69ab31 | | | 48 | { |
| b69ab31 | | | 49 | hunks: [], |
| b69ab31 | | | 50 | newFileName: 'sapling/eden/scm/b', |
| b69ab31 | | | 51 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 52 | type: 'Renamed', |
| b69ab31 | | | 53 | }, |
| b69ab31 | | | 54 | ]; |
| b69ab31 | | | 55 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | it('should parse rename and modify', () => { |
| b69ab31 | | | 59 | const patch = ` |
| b69ab31 | | | 60 | diff --git sapling/eden/addons/LICENSE sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 61 | rename from sapling/eden/addons/LICENSE |
| b69ab31 | | | 62 | rename to sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 63 | --- sapling/eden/addons/LICENSE |
| b69ab31 | | | 64 | +++ sapling/eden/addons/LICENSE.bak |
| b69ab31 | | | 65 | @@ -2,6 +2,7 @@ |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | Copyright (c) Meta Platforms, Inc. and its affiliates. |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | + |
| b69ab31 | | | 70 | `; |
| b69ab31 | | | 71 | const expected = [ |
| b69ab31 | | | 72 | { |
| b69ab31 | | | 73 | hunks: [ |
| b69ab31 | | | 74 | { |
| b69ab31 | | | 75 | linedelimiters: ['\n', '\n', '\n', '\n'], |
| b69ab31 | | | 76 | lines: ['', ' Copyright (c) Meta Platforms, Inc. and its affiliates.', '', '+'], |
| b69ab31 | | | 77 | newLines: 7, |
| b69ab31 | | | 78 | newStart: 2, |
| b69ab31 | | | 79 | oldLines: 6, |
| b69ab31 | | | 80 | oldStart: 2, |
| b69ab31 | | | 81 | }, |
| b69ab31 | | | 82 | ], |
| b69ab31 | | | 83 | newFileName: 'sapling/eden/addons/LICENSE.bak', |
| b69ab31 | | | 84 | oldFileName: 'sapling/eden/addons/LICENSE', |
| b69ab31 | | | 85 | type: 'Renamed', |
| b69ab31 | | | 86 | }, |
| b69ab31 | | | 87 | ]; |
| b69ab31 | | | 88 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 89 | }); |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | it('should parse new file', () => { |
| b69ab31 | | | 92 | const patch = ` |
| b69ab31 | | | 93 | diff --git sapling/eden/scm/c sapling/eden/scm/c |
| b69ab31 | | | 94 | new file mode 100644 |
| b69ab31 | | | 95 | --- /dev/null |
| b69ab31 | | | 96 | +++ sapling/eden/scm/c |
| b69ab31 | | | 97 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 98 | +1 |
| b69ab31 | | | 99 | `; |
| b69ab31 | | | 100 | const expected = [ |
| b69ab31 | | | 101 | { |
| b69ab31 | | | 102 | hunks: [ |
| b69ab31 | | | 103 | { |
| b69ab31 | | | 104 | linedelimiters: ['\n'], |
| b69ab31 | | | 105 | lines: ['+1'], |
| b69ab31 | | | 106 | newLines: 1, |
| b69ab31 | | | 107 | newStart: 1, |
| b69ab31 | | | 108 | oldLines: 0, |
| b69ab31 | | | 109 | oldStart: 1, |
| b69ab31 | | | 110 | }, |
| b69ab31 | | | 111 | ], |
| b69ab31 | | | 112 | newFileName: 'sapling/eden/scm/c', |
| b69ab31 | | | 113 | newMode: '100644', |
| b69ab31 | | | 114 | oldFileName: 'sapling/eden/scm/c', |
| b69ab31 | | | 115 | type: 'Added', |
| b69ab31 | | | 116 | }, |
| b69ab31 | | | 117 | ]; |
| b69ab31 | | | 118 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | it('should parse new empty file', () => { |
| b69ab31 | | | 122 | const patch = ` |
| b69ab31 | | | 123 | diff --git sapling/eden/addons/d sapling/eden/addons/d |
| b69ab31 | | | 124 | new file mode 100644 |
| b69ab31 | | | 125 | `; |
| b69ab31 | | | 126 | const expected = [ |
| b69ab31 | | | 127 | { |
| b69ab31 | | | 128 | hunks: [], |
| b69ab31 | | | 129 | newFileName: 'sapling/eden/addons/d', |
| b69ab31 | | | 130 | newMode: '100644', |
| b69ab31 | | | 131 | oldFileName: 'sapling/eden/addons/d', |
| b69ab31 | | | 132 | type: 'Added', |
| b69ab31 | | | 133 | }, |
| b69ab31 | | | 134 | ]; |
| b69ab31 | | | 135 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 136 | }); |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | it('should parse deleted file', () => { |
| b69ab31 | | | 139 | const patch = ` |
| b69ab31 | | | 140 | diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 141 | deleted file mode 100644 |
| b69ab31 | | | 142 | --- sapling/eden/scm/a |
| b69ab31 | | | 143 | +++ /dev/null |
| b69ab31 | | | 144 | @@ -1,1 +0,0 @@ |
| b69ab31 | | | 145 | -1 |
| b69ab31 | | | 146 | `; |
| b69ab31 | | | 147 | const expected = [ |
| b69ab31 | | | 148 | { |
| b69ab31 | | | 149 | hunks: [ |
| b69ab31 | | | 150 | { |
| b69ab31 | | | 151 | linedelimiters: ['\n'], |
| b69ab31 | | | 152 | lines: ['-1'], |
| b69ab31 | | | 153 | newLines: 0, |
| b69ab31 | | | 154 | newStart: 1, |
| b69ab31 | | | 155 | oldLines: 1, |
| b69ab31 | | | 156 | oldStart: 1, |
| b69ab31 | | | 157 | }, |
| b69ab31 | | | 158 | ], |
| b69ab31 | | | 159 | newFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 160 | newMode: '100644', |
| b69ab31 | | | 161 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 162 | type: 'Removed', |
| b69ab31 | | | 163 | }, |
| b69ab31 | | | 164 | ]; |
| b69ab31 | | | 165 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | it('should parse copied file', () => { |
| b69ab31 | | | 169 | const patch = ` |
| b69ab31 | | | 170 | diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 171 | copy from sapling/eden/scm/a |
| b69ab31 | | | 172 | copy to sapling/eden/scm/b |
| b69ab31 | | | 173 | `; |
| b69ab31 | | | 174 | const expected = [ |
| b69ab31 | | | 175 | { |
| b69ab31 | | | 176 | hunks: [], |
| b69ab31 | | | 177 | newFileName: 'sapling/eden/scm/b', |
| b69ab31 | | | 178 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 179 | type: 'Copied', |
| b69ab31 | | | 180 | }, |
| b69ab31 | | | 181 | ]; |
| b69ab31 | | | 182 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 183 | }); |
| b69ab31 | | | 184 | |
| b69ab31 | | | 185 | it('should parse multiple files', () => { |
| b69ab31 | | | 186 | const patch = ` |
| b69ab31 | | | 187 | diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 188 | --- sapling/eden/scm/a |
| b69ab31 | | | 189 | +++ sapling/eden/scm/a |
| b69ab31 | | | 190 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 191 | 1 |
| b69ab31 | | | 192 | +2 |
| b69ab31 | | | 193 | diff --git sapling/eden/scm/a sapling/eden/scm/b |
| b69ab31 | | | 194 | copy from sapling/eden/scm/a |
| b69ab31 | | | 195 | copy to sapling/eden/scm/b |
| b69ab31 | | | 196 | diff --git sapling/eden/scm/c sapling/eden/scm/d |
| b69ab31 | | | 197 | copy from sapling/eden/scm/c |
| b69ab31 | | | 198 | copy to sapling/eden/scm/d |
| b69ab31 | | | 199 | `; |
| b69ab31 | | | 200 | const expected = [ |
| b69ab31 | | | 201 | { |
| b69ab31 | | | 202 | hunks: [ |
| b69ab31 | | | 203 | { |
| b69ab31 | | | 204 | linedelimiters: ['\n', '\n'], |
| b69ab31 | | | 205 | lines: [' 1', '+2'], |
| b69ab31 | | | 206 | newLines: 2, |
| b69ab31 | | | 207 | newStart: 1, |
| b69ab31 | | | 208 | oldLines: 1, |
| b69ab31 | | | 209 | oldStart: 1, |
| b69ab31 | | | 210 | }, |
| b69ab31 | | | 211 | ], |
| b69ab31 | | | 212 | newFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 213 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 214 | type: 'Modified', |
| b69ab31 | | | 215 | }, |
| b69ab31 | | | 216 | { |
| b69ab31 | | | 217 | hunks: [], |
| b69ab31 | | | 218 | newFileName: 'sapling/eden/scm/b', |
| b69ab31 | | | 219 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 220 | type: 'Copied', |
| b69ab31 | | | 221 | }, |
| b69ab31 | | | 222 | { |
| b69ab31 | | | 223 | hunks: [], |
| b69ab31 | | | 224 | newFileName: 'sapling/eden/scm/d', |
| b69ab31 | | | 225 | oldFileName: 'sapling/eden/scm/c', |
| b69ab31 | | | 226 | type: 'Copied', |
| b69ab31 | | | 227 | }, |
| b69ab31 | | | 228 | ]; |
| b69ab31 | | | 229 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 230 | }); |
| b69ab31 | | | 231 | |
| b69ab31 | | | 232 | it('should parse file mode change', () => { |
| b69ab31 | | | 233 | const patch = ` |
| b69ab31 | | | 234 | diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 235 | old mode 100644 |
| b69ab31 | | | 236 | new mode 100755 |
| b69ab31 | | | 237 | `; |
| b69ab31 | | | 238 | const expected = [ |
| b69ab31 | | | 239 | { |
| b69ab31 | | | 240 | hunks: [], |
| b69ab31 | | | 241 | newFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 242 | newMode: '100755', |
| b69ab31 | | | 243 | oldFileName: 'sapling/eden/scm/a', |
| b69ab31 | | | 244 | oldMode: '100644', |
| b69ab31 | | | 245 | type: 'Modified', |
| b69ab31 | | | 246 | }, |
| b69ab31 | | | 247 | ]; |
| b69ab31 | | | 248 | expect(parsePatch(patch)).toEqual(expected); |
| b69ab31 | | | 249 | }); |
| b69ab31 | | | 250 | |
| b69ab31 | | | 251 | it('should fail for invalid file mode format', () => { |
| b69ab31 | | | 252 | const patch = ` |
| b69ab31 | | | 253 | diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 254 | old mode XXX |
| b69ab31 | | | 255 | new mode 100755 |
| b69ab31 | | | 256 | `; |
| b69ab31 | | | 257 | expect(() => parsePatch(patch)).toThrow("invalid format 'old mode XXX'"); |
| b69ab31 | | | 258 | }); |
| b69ab31 | | | 259 | }); |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | describe('guessSubmodule', () => { |
| b69ab31 | | | 262 | it('modified submodules', () => { |
| b69ab31 | | | 263 | const patch = ` |
| b69ab31 | | | 264 | diff --git a/external/brotli b/external/brotli |
| b69ab31 | | | 265 | --- a/external/brotli |
| b69ab31 | | | 266 | +++ b/external/brotli |
| b69ab31 | | | 267 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 268 | -Subproject commit 892110204ccf44fcd493ae415c9a69c470c2a9cf |
| b69ab31 | | | 269 | +Subproject commit 57de5cc4288565a9c3a7af978ef15f0abf0ada1b |
| b69ab31 | | | 270 | diff --git a/external/rust/cxx b/external/rust/cxx |
| b69ab31 | | | 271 | --- a/external/rust/cxx |
| b69ab31 | | | 272 | +++ b/external/rust/cxx |
| b69ab31 | | | 273 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 274 | -Subproject commit 862a23082a087566776280a5b1539d3b62701bcb |
| b69ab31 | | | 275 | +Subproject commit 1869e93e54fa9d9425bd88bdb25073af9ed7e782 |
| b69ab31 | | | 276 | `; |
| b69ab31 | | | 277 | const parsed = parsePatch(patch); |
| b69ab31 | | | 278 | expect(parsed.length).toEqual(2); |
| b69ab31 | | | 279 | expect(guessIsSubmodule(parsed[0])).toEqual(true); |
| b69ab31 | | | 280 | expect(guessIsSubmodule(parsed[1])).toEqual(true); |
| b69ab31 | | | 281 | }); |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | it('added submodule', () => { |
| b69ab31 | | | 284 | const patch = ` |
| b69ab31 | | | 285 | diff --git a/path/to/submodule b/path/to/submodule |
| b69ab31 | | | 286 | new file mode 160000 |
| b69ab31 | | | 287 | --- /dev/null |
| b69ab31 | | | 288 | +++ b/path/to/submodule |
| b69ab31 | | | 289 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 290 | +Subproject commit 7ef4220022059b9b1e1d8ec4eea6f7abd011894f |
| b69ab31 | | | 291 | `; |
| b69ab31 | | | 292 | const parsed = parsePatch(patch); |
| b69ab31 | | | 293 | expect(parsed.length).toEqual(1); |
| b69ab31 | | | 294 | expect(guessIsSubmodule(parsed[0])).toEqual(true); |
| b69ab31 | | | 295 | }); |
| b69ab31 | | | 296 | |
| b69ab31 | | | 297 | it('invalid file modification', () => { |
| b69ab31 | | | 298 | const patch = ` |
| b69ab31 | | | 299 | diff --git sapling/eden/scm/a sapling/eden/scm/a |
| b69ab31 | | | 300 | --- sapling/eden/scm/a |
| b69ab31 | | | 301 | +++ sapling/eden/scm/a |
| b69ab31 | | | 302 | @@ -1,1 +1,2 @@ |
| b69ab31 | | | 303 | Subproject commit abcdef01234556789ABCDEF |
| b69ab31 | | | 304 | +Subproject commit abcdef01234556789ABCDEF |
| b69ab31 | | | 305 | `; |
| b69ab31 | | | 306 | const parsed = parsePatch(patch); |
| b69ab31 | | | 307 | expect(parsed.length).toEqual(1); |
| b69ab31 | | | 308 | expect(guessIsSubmodule(parsed[0])).toEqual(false); |
| b69ab31 | | | 309 | }); |
| b69ab31 | | | 310 | |
| b69ab31 | | | 311 | it('invalid hash value', () => { |
| b69ab31 | | | 312 | const patch = ` |
| b69ab31 | | | 313 | diff --git a/external/rust/cxx b/external/rust/cxx |
| b69ab31 | | | 314 | --- a/external/rust/cxx |
| b69ab31 | | | 315 | +++ b/external/rust/cxx |
| b69ab31 | | | 316 | @@ -1,1 +1,1 @@ |
| b69ab31 | | | 317 | -Subproject commit ghijklmnGHIJKLMN |
| b69ab31 | | | 318 | +Subproject commit ghijklmnGHIJKLMN |
| b69ab31 | | | 319 | `; |
| b69ab31 | | | 320 | const parsed = parsePatch(patch); |
| b69ab31 | | | 321 | expect(parsed.length).toEqual(1); |
| b69ab31 | | | 322 | expect(guessIsSubmodule(parsed[0])).toEqual(false); |
| b69ab31 | | | 323 | }); |
| b69ab31 | | | 324 | }); |
| b69ab31 | | | 325 | |
| b69ab31 | | | 326 | describe('createParsedDiffWithLines', () => { |
| b69ab31 | | | 327 | it('return no hunks for empty lines', () => { |
| b69ab31 | | | 328 | expect(parseParsedDiff([], [], 1)).toMatchObject({hunks: []}); |
| b69ab31 | | | 329 | }); |
| b69ab31 | | | 330 | |
| b69ab31 | | | 331 | it('returns no hunks when comparing same lines', () => { |
| b69ab31 | | | 332 | const lines = splitLines('a\nb\nc\nd\ne\n'); |
| b69ab31 | | | 333 | expect(parseParsedDiff(lines, lines, 1)).toMatchObject({hunks: []}); |
| b69ab31 | | | 334 | }); |
| b69ab31 | | | 335 | |
| b69ab31 | | | 336 | it('return all "-" for old code and "=" for new code for totally different contents', () => { |
| b69ab31 | | | 337 | const aLines = splitLines('x\ny\n'); |
| b69ab31 | | | 338 | const bLines = splitLines('a\nb\nc\n'); |
| b69ab31 | | | 339 | expect(parseParsedDiff(aLines, bLines, 1)).toMatchObject({ |
| b69ab31 | | | 340 | hunks: [ |
| b69ab31 | | | 341 | { |
| b69ab31 | | | 342 | oldStart: 1, |
| b69ab31 | | | 343 | oldLines: 2, |
| b69ab31 | | | 344 | newStart: 1, |
| b69ab31 | | | 345 | newLines: 3, |
| b69ab31 | | | 346 | lines: ['-x\n', '-y\n', '+a\n', '+b\n', '+c\n'], |
| b69ab31 | | | 347 | linedelimiters: ['\n', '\n', '\n', '\n', '\n'], |
| b69ab31 | | | 348 | }, |
| b69ab31 | | | 349 | ], |
| b69ab31 | | | 350 | }); |
| b69ab31 | | | 351 | }); |
| b69ab31 | | | 352 | |
| b69ab31 | | | 353 | it('return for when a line was changed in the middle', () => { |
| b69ab31 | | | 354 | const aLines = splitLines('a\nb\nc\nd\ne\n'); |
| b69ab31 | | | 355 | const bLines = splitLines('a\nb\nc\nd1\nd2\ne\n'); |
| b69ab31 | | | 356 | expect(parseParsedDiff(aLines, bLines, 1)).toMatchObject({ |
| b69ab31 | | | 357 | hunks: [ |
| b69ab31 | | | 358 | { |
| b69ab31 | | | 359 | oldStart: 4, |
| b69ab31 | | | 360 | oldLines: 1, |
| b69ab31 | | | 361 | newStart: 4, |
| b69ab31 | | | 362 | newLines: 2, |
| b69ab31 | | | 363 | lines: ['-d\n', '+d1\n', '+d2\n'], |
| b69ab31 | | | 364 | linedelimiters: ['\n', '\n', '\n'], |
| b69ab31 | | | 365 | }, |
| b69ab31 | | | 366 | ], |
| b69ab31 | | | 367 | }); |
| b69ab31 | | | 368 | }); |
| b69ab31 | | | 369 | }); |