|
| 1 | +import { promiseError } from '@kwsites/promise-result'; |
| 2 | +import { GitResponseError, PullFailedResult } from '../../typings'; |
| 3 | +import { createTestContext, like, newSimpleGit, setUpInit, SimpleGitTestContext } from '../__fixtures__'; |
| 4 | + |
| 5 | +describe('pull --ff-only', () => { |
| 6 | + let context: SimpleGitTestContext; |
| 7 | + |
| 8 | + beforeEach(async () => context = await createTestContext()); |
| 9 | + beforeEach(async () => { |
| 10 | + const upstream = await context.dir('upstream'); |
| 11 | + const local = context.path('local'); |
| 12 | + await context.file(['upstream', 'file']); |
| 13 | + |
| 14 | + await givenRemote(upstream); |
| 15 | + await givenLocal(upstream, local); |
| 16 | + }); |
| 17 | + |
| 18 | + async function givenLocal(upstream: string, local: string) { |
| 19 | + await newSimpleGit(context.root).clone(upstream, local); |
| 20 | + await setUpInit({git: newSimpleGit(local)}); |
| 21 | + } |
| 22 | + |
| 23 | + async function givenRemote(upstream: string) { |
| 24 | + const git = newSimpleGit(upstream); |
| 25 | + await setUpInit({git}); |
| 26 | + await git.add('.'); |
| 27 | + await git.commit('first'); |
| 28 | + } |
| 29 | + |
| 30 | + async function givenRemoteFileChanged() { |
| 31 | + await context.file(['upstream', 'file'], 'new remote file content'); |
| 32 | + await newSimpleGit(context.path('upstream')).add('.').commit('remote updated'); |
| 33 | + } |
| 34 | + |
| 35 | + async function givenLocalFileChanged() { |
| 36 | + await context.file(['local', 'file'], 'new local file content'); |
| 37 | + await newSimpleGit(context.path('local')).add('.').commit('local updated'); |
| 38 | + } |
| 39 | + |
| 40 | + it('allows fast-forward when there are no changes local or remote', async () => { |
| 41 | + const git = newSimpleGit(context.path('local')); |
| 42 | + const result = await git.pull(['--ff-only']); |
| 43 | + |
| 44 | + expect(result.files).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('allows fast-forward when there are some remote but no local changes', async () => { |
| 48 | + await givenRemoteFileChanged(); |
| 49 | + |
| 50 | + const git = newSimpleGit(context.path('local')); |
| 51 | + const result = await git.pull(['--ff-only']); |
| 52 | + |
| 53 | + expect(result.files).toEqual(['file']); |
| 54 | + }); |
| 55 | + |
| 56 | + it('allows fast-forward when there are no remote but some local changes', async () => { |
| 57 | + await givenLocalFileChanged(); |
| 58 | + |
| 59 | + const git = newSimpleGit(context.path('local')); |
| 60 | + const result = await git.pull(['--ff-only']); |
| 61 | + |
| 62 | + expect(result.files).toEqual([]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('fails fast-forward when there are both remote and local changes', async () => { |
| 66 | + await givenLocalFileChanged(); |
| 67 | + await givenRemoteFileChanged(); |
| 68 | + |
| 69 | + const git = newSimpleGit(context.path('local')); |
| 70 | + const err = await promiseError<GitResponseError<PullFailedResult>>(git.pull(['--ff-only'])); |
| 71 | + |
| 72 | + expect(err?.git.message).toMatch('Not possible to fast-forward, aborting'); |
| 73 | + expect(err?.git).toEqual(like({ |
| 74 | + remote: context.path('upstream'), |
| 75 | + hash: { |
| 76 | + local: expect.any(String), |
| 77 | + remote: expect.any(String), |
| 78 | + }, |
| 79 | + branch: { |
| 80 | + local: expect.any(String), |
| 81 | + remote: expect.any(String), |
| 82 | + }, |
| 83 | + message: String(err?.git), |
| 84 | + })) |
| 85 | + }); |
| 86 | + |
| 87 | +}); |
0 commit comments