handling files when stages and dirty at once
This commit is contained in:
@@ -9,17 +9,33 @@ import { classify, discard, load, stage } from '../src/main/git-service'
|
||||
|
||||
describe('classify', () => {
|
||||
it('reads the index code as staged, working code as unstaged', () => {
|
||||
expect(classify('M', ' ')).toEqual({ letter: 'M', staged: true })
|
||||
expect(classify(' ', 'M')).toEqual({ letter: 'M', staged: false })
|
||||
expect(classify('A', ' ')).toEqual({ letter: 'A', staged: true })
|
||||
expect(classify('D', ' ')).toEqual({ letter: 'D', staged: true })
|
||||
expect(classify('R', ' ')).toEqual({ letter: 'R', staged: true })
|
||||
expect(classify('M', ' ')).toEqual([{ letter: 'M', staged: true }])
|
||||
expect(classify(' ', 'M')).toEqual([{ letter: 'M', staged: false }])
|
||||
expect(classify('A', ' ')).toEqual([{ letter: 'A', staged: true }])
|
||||
expect(classify('D', ' ')).toEqual([{ letter: 'D', staged: true }])
|
||||
expect(classify('R', ' ')).toEqual([{ letter: 'R', staged: true }])
|
||||
})
|
||||
it('treats untracked as a new (A) unstaged file', () => {
|
||||
expect(classify('?', '?')).toEqual({ letter: 'A', staged: false })
|
||||
expect(classify('?', '?')).toEqual([{ letter: 'A', staged: false }])
|
||||
})
|
||||
it('maps unmerged (U) to modified', () => {
|
||||
expect(classify('U', 'U').letter).toBe('M')
|
||||
it('splits a staged-then-edited file into two rows', () => {
|
||||
expect(classify('M', 'M')).toEqual([
|
||||
{ letter: 'M', staged: true },
|
||||
{ letter: 'M', staged: false },
|
||||
])
|
||||
expect(classify('A', 'M')).toEqual([
|
||||
{ letter: 'A', staged: true },
|
||||
{ letter: 'M', staged: false },
|
||||
])
|
||||
expect(classify('M', 'D')).toEqual([
|
||||
{ letter: 'M', staged: true },
|
||||
{ letter: 'D', staged: false },
|
||||
])
|
||||
})
|
||||
it('keeps a merge conflict as one row', () => {
|
||||
expect(classify('U', 'U')).toEqual([{ letter: 'M', staged: true }])
|
||||
expect(classify('A', 'A')).toEqual([{ letter: 'M', staged: true }])
|
||||
expect(classify('D', 'D')).toEqual([{ letter: 'M', staged: true }])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -77,6 +93,51 @@ describe('load (integration against a temp repo)', () => {
|
||||
expect(res!.changes.find((c) => c.path === 'a.txt')?.staged).toBe(true)
|
||||
})
|
||||
|
||||
it('shows a staged-then-edited file in both groups, each with its own pair', async () => {
|
||||
dir = await repo()
|
||||
await writeFile(join(dir, 'a.txt'), '1\nSTAGED\n3\n')
|
||||
await stage(dir, ['a.txt'])
|
||||
await writeFile(join(dir, 'a.txt'), '1\nSTAGED\n3\n4\n')
|
||||
const res = await load(dir)
|
||||
const rows = res!.changes.filter((c) => c.path === 'a.txt')
|
||||
expect(rows).toHaveLength(2)
|
||||
|
||||
// Staged row: HEAD -> index. It must NOT include the newer edit.
|
||||
const s = rows.find((c) => c.staged)!
|
||||
expect(s.original).toBe('1\n2\n3\n')
|
||||
expect(s.updated).toBe('1\nSTAGED\n3\n')
|
||||
|
||||
// Unstaged row: index -> disk. Only the newer edit.
|
||||
const w = rows.find((c) => !c.staged)!
|
||||
expect(w.original).toBe('1\nSTAGED\n3\n')
|
||||
expect(w.updated).toBe('1\nSTAGED\n3\n4\n')
|
||||
})
|
||||
|
||||
it('gives a staged-new file that was edited again both rows', async () => {
|
||||
dir = await repo()
|
||||
await writeFile(join(dir, 'fresh.txt'), 'one\n')
|
||||
await stage(dir, ['fresh.txt'])
|
||||
await writeFile(join(dir, 'fresh.txt'), 'one\ntwo\n')
|
||||
const res = await load(dir)
|
||||
const rows = res!.changes.filter((c) => c.path === 'fresh.txt')
|
||||
expect(rows.map((r) => [r.status, r.staged])).toEqual([['A', true], ['M', false]])
|
||||
expect(rows[0].original).toBe('')
|
||||
expect(rows[0].updated).toBe('one\n')
|
||||
expect(rows[1].original).toBe('one\n')
|
||||
expect(rows[1].updated).toBe('one\ntwo\n')
|
||||
})
|
||||
|
||||
it('keeps one row when a file is only staged', async () => {
|
||||
dir = await repo()
|
||||
await writeFile(join(dir, 'a.txt'), '1\n2\n3\n4\n')
|
||||
await stage(dir, ['a.txt'])
|
||||
const rows = (await load(dir))!.changes.filter((c) => c.path === 'a.txt')
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].staged).toBe(true)
|
||||
expect(rows[0].original).toBe('1\n2\n3\n')
|
||||
expect(rows[0].updated).toBe('1\n2\n3\n4\n')
|
||||
})
|
||||
|
||||
it('discard reverts a modified tracked file to HEAD', async () => {
|
||||
dir = await repo()
|
||||
await writeFile(join(dir, 'a.txt'), '1\nCHANGED\n3\n')
|
||||
|
||||
Reference in New Issue
Block a user