Read-only mirror — the public repositories of the Forgejo instance, refreshed every 10 minutes. The live instance (fg.dieschoenewolke.de) is reachable only over the VPN; clone here with https://git.dieschoenewolke.de/git/<owner>/<repo>.git.
summaryrefslogtreecommitdiff
path: root/__test__/ref-helper.test.ts
blob: 4943abd6dd4de5bcc02dfddab8aeeecf68b39a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import * as assert from 'assert'
import * as refHelper from '../lib/ref-helper'
import {IGitCommandManager} from '../lib/git-command-manager'

const commit = '1234567890123456789012345678901234567890'
let git: IGitCommandManager

describe('ref-helper tests', () => {
  beforeEach(() => {
    git = {} as unknown as IGitCommandManager
  })

  it('getCheckoutInfo requires git', async () => {
    const git = null as unknown as IGitCommandManager
    try {
      await refHelper.getCheckoutInfo(git, 'refs/heads/my/branch', commit)
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe('Arg git cannot be empty')
    }
  })

  it('getCheckoutInfo requires ref or commit', async () => {
    try {
      await refHelper.getCheckoutInfo(git, '', '')
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe(
        'Args ref and commit cannot both be empty'
      )
    }
  })

  it('getCheckoutInfo sha only', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit)
    expect(checkoutInfo.ref).toBe(commit)
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/heads/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/heads/my/branch',
      commit
    )
    expect(checkoutInfo.ref).toBe('my/branch')
    expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  })

  it('getCheckoutInfo refs/pull/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/pull/123/merge',
      commit
    )
    expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/tags/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/tags/my-tag',
      commit
    )
    expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/gh/queue/main/pr-123',
      commit
    )
    expect(checkoutInfo.ref).toBe(commit)
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/ without commit', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/non-standard-ref',
      ''
    )
    expect(checkoutInfo.ref).toBe('refs/non-standard-ref')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo unqualified branch only', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return true
    })

    const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '')

    expect(checkoutInfo.ref).toBe('my/branch')
    expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  })

  it('getCheckoutInfo unqualified tag only', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return false
    })
    git.tagExists = jest.fn(async (pattern: string) => {
      return true
    })

    const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '')

    expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return false
    })
    git.tagExists = jest.fn(async (pattern: string) => {
      return false
    })

    try {
      await refHelper.getCheckoutInfo(git, 'my-ref', '')
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe(
        "A branch or tag with the name 'my-ref' could not be found"
      )
    }
  })

  it('getRefSpec requires ref or commit', async () => {
    assert.throws(
      () => refHelper.getRefSpec('', ''),
      /Args ref and commit cannot both be empty/
    )
  })

  it('getRefSpec sha + refs/heads/', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  })

  it('getRefSpec sha + refs/pull/', async () => {
    const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`)
  })

  it('getRefSpec sha + refs/tags/', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+refs/tags/my-tag:refs/tags/my-tag`)
  })

  it('getRefSpec sha + refs/tags/ with fetchTags', async () => {
    // When fetchTags is true, only include tags wildcard (specific tag is redundant)
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit, true)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
  })

  it('getRefSpec sha + refs/heads/ with fetchTags', async () => {
    // When fetchTags is true, include both the branch refspec and tags wildcard
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit, true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  })

  it('getRefSpec sha only', async () => {
    const refSpec = refHelper.getRefSpec('', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(commit)
  })

  it('getRefSpec unqualified ref only', async () => {
    const refSpec = refHelper.getRefSpec('my-ref', '')
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
    expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*')
  })

  it('getRefSpec unqualified ref only with fetchTags', async () => {
    // When fetchTags is true, skip specific tag pattern since wildcard covers all
    const refSpec = refHelper.getRefSpec('my-ref', '', true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
  })

  it('getRefSpec refs/heads/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(
      '+refs/heads/my/branch:refs/remotes/origin/my/branch'
    )
  })

  it('getRefSpec refs/pull/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge')
  })

  it('getRefSpec refs/tags/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag')
  })

  it('getRefSpec refs/tags/ only with fetchTags', async () => {
    // When fetchTags is true, only include tags wildcard (specific tag is redundant)
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '', true)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
  })

  it('getRefSpec refs/heads/ only with fetchTags', async () => {
    // When fetchTags is true, include both the branch refspec and tags wildcard
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '', true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe(
      '+refs/heads/my/branch:refs/remotes/origin/my/branch'
    )
  })
})