numstat.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /** Numstat parsing. */
  2. import { describe, expect, it } from 'vitest'
  3. import { parseNumstat } from '../src/numstat.ts'
  4. describe('parseNumstat', () => {
  5. it('reads plain, binary, and rename records', () => {
  6. const output = ['3\t1\tsrc/a.ts', '-\t-\timg.png', '2\t0\t', 'old.txt', 'new.txt', ''].join('\0')
  7. expect(parseNumstat(output)).toEqual([
  8. { path: 'src/a.ts', added: 3, deleted: 1, binary: false },
  9. { path: 'img.png', added: 0, deleted: 0, binary: true },
  10. { path: 'new.txt', oldPath: 'old.txt', added: 2, deleted: 0, binary: false },
  11. ])
  12. })
  13. it('returns nothing for empty output', () => {
  14. expect(parseNumstat('')).toEqual([])
  15. })
  16. it('rejects output without a final terminator or with malformed records', () => {
  17. expect(() => parseNumstat('1\t1\ta.txt')).toThrow('NUL-terminated')
  18. expect(() => parseNumstat('garbage\0')).toThrow('malformed numstat record')
  19. expect(() => parseNumstat('1\t0\t\0old.txt\0')).toThrow('rename')
  20. })
  21. })
  22. describe('parseNumstat with unusual names', () => {
  23. it('keeps tabs inside a file name', () => {
  24. expect(parseNumstat(['1\t0\ta\tb.txt', '2\t0\t', 'old\tx', 'new\ty', ''].join('\0'))).toEqual([
  25. { path: 'a\tb.txt', added: 1, deleted: 0, binary: false },
  26. { path: 'new\ty', oldPath: 'old\tx', added: 2, deleted: 0, binary: false },
  27. ])
  28. expect(() => parseNumstat('1\ta.txt\0')).toThrow('malformed numstat record')
  29. expect(() => parseNumstat('garbage\0')).toThrow('malformed numstat record')
  30. })
  31. })