|
1 |
| -/* eslint-disable camelcase */ |
2 |
| -import { GithubRepo } from './github-repo'; |
3 | 1 | import { expect } from 'chai';
|
4 |
| -import sinon from 'ts-sinon'; |
5 |
| -import path from 'path'; |
6 | 2 | import fs from 'fs';
|
7 | 3 | import os from 'os';
|
| 4 | +import path from 'path'; |
| 5 | +import sinon from 'ts-sinon'; |
| 6 | +import { GithubRepo } from './github-repo'; |
8 | 7 | import {
|
9 | 8 | createTarball,
|
10 |
| - tarballPath, |
| 9 | + tarballPath |
11 | 10 | } from './tarball';
|
12 | 11 |
|
13 | 12 | function getTestGithubRepo(octokitStub: any = {}): GithubRepo {
|
@@ -183,4 +182,246 @@ describe('GithubRepo', () => {
|
183 | 182 | });
|
184 | 183 | });
|
185 | 184 | });
|
| 185 | + |
| 186 | + describe('createBranch', () => { |
| 187 | + let octokit: any; |
| 188 | + let getRef: sinon.SinonStub; |
| 189 | + let createRef: sinon.SinonStub; |
| 190 | + |
| 191 | + beforeEach(() => { |
| 192 | + getRef = sinon.stub().rejects(); |
| 193 | + createRef = sinon.stub().rejects(); |
| 194 | + octokit = { |
| 195 | + git: { |
| 196 | + getRef, createRef |
| 197 | + } |
| 198 | + }; |
| 199 | + githubRepo = getTestGithubRepo(octokit); |
| 200 | + }); |
| 201 | + |
| 202 | + it('creates the branch based on the given base', async() => { |
| 203 | + getRef.withArgs({ |
| 204 | + ...githubRepo.repo, |
| 205 | + ref: 'heads/base' |
| 206 | + }).resolves({ |
| 207 | + data: { |
| 208 | + object: { |
| 209 | + sha: 'sha' |
| 210 | + } |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + createRef.withArgs({ |
| 215 | + ...githubRepo.repo, |
| 216 | + ref: 'refs/heads/newBranch', |
| 217 | + sha: 'sha' |
| 218 | + }).resolves(); |
| 219 | + |
| 220 | + await githubRepo.createBranch('newBranch', 'base'); |
| 221 | + expect(getRef).to.have.been.called; |
| 222 | + expect(createRef).to.have.been.called; |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + describe('deleteBranch', () => { |
| 227 | + let octokit: any; |
| 228 | + let deleteRef: sinon.SinonStub; |
| 229 | + |
| 230 | + beforeEach(() => { |
| 231 | + deleteRef = sinon.stub().rejects(); |
| 232 | + octokit = { |
| 233 | + git: { |
| 234 | + deleteRef |
| 235 | + } |
| 236 | + }; |
| 237 | + githubRepo = getTestGithubRepo(octokit); |
| 238 | + }); |
| 239 | + |
| 240 | + it('deletes the branch', async() => { |
| 241 | + deleteRef.withArgs({ |
| 242 | + ...githubRepo.repo, |
| 243 | + ref: 'heads/branch' |
| 244 | + }).resolves(); |
| 245 | + |
| 246 | + await githubRepo.deleteBranch('branch'); |
| 247 | + expect(deleteRef).to.have.been.called; |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + describe('getFileContent', () => { |
| 252 | + let octokit: any; |
| 253 | + let getContents: sinon.SinonStub; |
| 254 | + |
| 255 | + beforeEach(() => { |
| 256 | + getContents = sinon.stub(); |
| 257 | + getContents.rejects(); |
| 258 | + |
| 259 | + octokit = { |
| 260 | + repos: { |
| 261 | + getContents |
| 262 | + } |
| 263 | + }; |
| 264 | + githubRepo = getTestGithubRepo(octokit); |
| 265 | + }); |
| 266 | + |
| 267 | + it('loads the file content and decodes it', async() => { |
| 268 | + getContents.withArgs({ |
| 269 | + ...githubRepo.repo, |
| 270 | + path: 'file/path', |
| 271 | + ref: 'branch' |
| 272 | + }).resolves({ |
| 273 | + data: { |
| 274 | + type: 'file', |
| 275 | + encoding: 'base64', |
| 276 | + content: Buffer.from('🎉', 'utf-8').toString('base64'), |
| 277 | + sha: 'sha' |
| 278 | + } |
| 279 | + }); |
| 280 | + |
| 281 | + const result = await githubRepo.getFileContent('file/path', 'branch'); |
| 282 | + expect(result.content).to.equal('🎉'); |
| 283 | + expect(result.blobSha).to.equal('sha'); |
| 284 | + }); |
| 285 | + |
| 286 | + it('fails when data type is not file', async() => { |
| 287 | + getContents.withArgs({ |
| 288 | + ...githubRepo.repo, |
| 289 | + path: 'file/path', |
| 290 | + ref: 'branch' |
| 291 | + }).resolves({ |
| 292 | + data: { |
| 293 | + type: 'directory' |
| 294 | + } |
| 295 | + }); |
| 296 | + |
| 297 | + try { |
| 298 | + await githubRepo.getFileContent('file/path', 'branch'); |
| 299 | + } catch (e) { |
| 300 | + return expect(e.message).to.equal('file/path does not reference a file'); |
| 301 | + } |
| 302 | + expect.fail('expected error'); |
| 303 | + }); |
| 304 | + |
| 305 | + it('fails when data encoding is not base64', async() => { |
| 306 | + getContents.withArgs({ |
| 307 | + ...githubRepo.repo, |
| 308 | + path: 'file/path', |
| 309 | + ref: 'branch' |
| 310 | + }).resolves({ |
| 311 | + data: { |
| 312 | + type: 'file', |
| 313 | + encoding: 'whatever' |
| 314 | + } |
| 315 | + }); |
| 316 | + |
| 317 | + try { |
| 318 | + await githubRepo.getFileContent('file/path', 'branch'); |
| 319 | + } catch (e) { |
| 320 | + return expect(e.message).to.equal('Octokit returned unexpected encoding: whatever'); |
| 321 | + } |
| 322 | + expect.fail('expected error'); |
| 323 | + }); |
| 324 | + }); |
| 325 | + |
| 326 | + describe('commitFileUpdate', () => { |
| 327 | + let octokit: any; |
| 328 | + let createOrUpdateFile: sinon.SinonStub; |
| 329 | + |
| 330 | + beforeEach(() => { |
| 331 | + createOrUpdateFile = sinon.stub(); |
| 332 | + createOrUpdateFile.rejects(); |
| 333 | + |
| 334 | + octokit = { |
| 335 | + repos: { |
| 336 | + createOrUpdateFile |
| 337 | + } |
| 338 | + }; |
| 339 | + githubRepo = getTestGithubRepo(octokit); |
| 340 | + }); |
| 341 | + |
| 342 | + it('commits the file with new content', async() => { |
| 343 | + createOrUpdateFile.withArgs({ |
| 344 | + ...githubRepo.repo, |
| 345 | + message: 'Commit Message', |
| 346 | + content: Buffer.from('🎉', 'utf-8').toString('base64'), |
| 347 | + path: 'file/path', |
| 348 | + sha: 'base', |
| 349 | + branch: 'branch' |
| 350 | + }).resolves({ |
| 351 | + data: { |
| 352 | + content: { |
| 353 | + sha: 'contentSha' |
| 354 | + }, |
| 355 | + commit: { |
| 356 | + sha: 'commitSha' |
| 357 | + } |
| 358 | + } |
| 359 | + }); |
| 360 | + |
| 361 | + const result = await githubRepo.commitFileUpdate('Commit Message', 'base', 'file/path', '🎉', 'branch'); |
| 362 | + expect(result.blobSha).to.equal('contentSha'); |
| 363 | + expect(result.commitSha).to.equal('commitSha'); |
| 364 | + }); |
| 365 | + }); |
| 366 | + |
| 367 | + describe('createPullRequest', () => { |
| 368 | + let octokit: any; |
| 369 | + let createPullRequest: sinon.SinonStub; |
| 370 | + |
| 371 | + beforeEach(() => { |
| 372 | + createPullRequest = sinon.stub(); |
| 373 | + createPullRequest.rejects(); |
| 374 | + |
| 375 | + octokit = { |
| 376 | + pulls: { |
| 377 | + create: createPullRequest |
| 378 | + } |
| 379 | + }; |
| 380 | + githubRepo = getTestGithubRepo(octokit); |
| 381 | + }); |
| 382 | + |
| 383 | + it('creates a proper PR', async() => { |
| 384 | + createPullRequest.withArgs({ |
| 385 | + ...githubRepo.repo, |
| 386 | + base: 'toBase', |
| 387 | + head: 'fromBranch', |
| 388 | + title: 'PR' |
| 389 | + }).resolves({ |
| 390 | + data: { |
| 391 | + number: 42, |
| 392 | + html_url: 'url' |
| 393 | + } |
| 394 | + }); |
| 395 | + |
| 396 | + const result = await githubRepo.createPullRequest('PR', 'fromBranch', 'toBase'); |
| 397 | + expect(result.prNumber).to.equal(42); |
| 398 | + expect(result.url).to.equal('url'); |
| 399 | + }); |
| 400 | + }); |
| 401 | + |
| 402 | + describe('mergePullRequest', () => { |
| 403 | + let octokit: any; |
| 404 | + let mergePullRequest: sinon.SinonStub; |
| 405 | + |
| 406 | + beforeEach(() => { |
| 407 | + mergePullRequest = sinon.stub(); |
| 408 | + mergePullRequest.rejects(); |
| 409 | + |
| 410 | + octokit = { |
| 411 | + pulls: { |
| 412 | + merge: mergePullRequest |
| 413 | + } |
| 414 | + }; |
| 415 | + githubRepo = getTestGithubRepo(octokit); |
| 416 | + }); |
| 417 | + |
| 418 | + it('creates a proper PR', async() => { |
| 419 | + mergePullRequest.withArgs({ |
| 420 | + ...githubRepo.repo, |
| 421 | + pull_number: 42 |
| 422 | + }).resolves(); |
| 423 | + |
| 424 | + await githubRepo.mergePullRequest(42); |
| 425 | + }); |
| 426 | + }); |
186 | 427 | });
|
0 commit comments