From 365e5921b22348b3dccb959b78e9e8ee5ff49a6d Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 1 Jul 2020 18:30:53 +0200 Subject: [PATCH] chore(scaffolder): added some more tests for the github publisher --- .../stages/publish/__mocks__/nodegit/index.ts | 6 +- .../scaffolder/stages/publish/github.test.ts | 80 ++++++++++++++++++- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/__mocks__/nodegit/index.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/__mocks__/nodegit/index.ts index aa0cb6a23b..e3fb5000d0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/__mocks__/nodegit/index.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/__mocks__/nodegit/index.ts @@ -14,18 +14,18 @@ * limitations under the License. */ -const mockIndex = { +export const mockIndex = { addAll: jest.fn(), write: jest.fn(), writeTree: jest.fn().mockResolvedValue('mockoid'), }; -const mockRepo = { +export const mockRepo = { refreshIndex: jest.fn().mockResolvedValue(mockIndex), createCommit: jest.fn(), }; -const mockRemote = { +export const mockRemote = { push: jest.fn(), }; diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts index aa439a4982..5e886b13aa 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts @@ -18,6 +18,7 @@ jest.mock('@octokit/rest'); jest.mock('nodegit'); import { Octokit } from '@octokit/rest'; +import * as NodeGit from 'nodegit'; import { OctokitResponse, ReposCreateInOrgResponseData } from '@octokit/types'; import { GithubPublisher } from './github'; @@ -25,6 +26,16 @@ const { mockGithubClient } = require('@octokit/rest') as { mockGithubClient: { repos: jest.Mocked }; }; +const { Repository, mockRepo, mockIndex, Signature } = require('nodegit') as { + Repository: jest.Mocked<{ init: any }>; + Signature: jest.Mocked<{ now: any }>; + Cred: jest.Mocked<{ init: any }>; + Remote: jest.Mocked<{ push: any }>; + + mockIndex: jest.Mocked; + mockRepo: jest.Mocked; +}; + describe('Github Publisher', () => { const publisher = new GithubPublisher({ client: new Octokit() }); @@ -78,5 +89,72 @@ describe('Github Publisher', () => { }); }); - describe('publish: createGitDirectory', () => {}); + describe('publish: createGitDirectory', () => { + const values = { + isOrg: true, + storePath: 'blam/test', + owner: 'lols', + }; + + const mockDir = '/tmp/test/dir'; + + mockGithubClient.repos.createInOrg.mockResolvedValue({ + data: { + clone_url: 'mockclone', + }, + } as OctokitResponse); + it('should call init on the repo with the directory', async () => { + await publisher.publish({ + values, + directory: mockDir, + }); + + expect(Repository.init).toHaveBeenCalledWith(mockDir, 0); + }); + + it('should call refresh index on the index and write the new files', async () => { + await publisher.publish({ + values, + directory: mockDir, + }); + + expect(mockRepo.refreshIndex).toHaveBeenCalled(); + }); + + it('should call add all files and write', async () => { + await publisher.publish({ + values, + directory: mockDir, + }); + + expect(mockIndex.addAll).toHaveBeenCalled(); + expect(mockIndex.write).toHaveBeenCalled(); + expect(mockIndex.writeTree).toHaveBeenCalled(); + }); + + it('should create a commit with on head with the right name and commiter', async () => { + const mockSignature = { mockSignature: 'bloblly' }; + Signature.now.mockReturnValue(mockSignature); + + await publisher.publish({ + values, + directory: mockDir, + }); + + expect(Signature.now).toHaveBeenCalledTimes(2); + expect(Signature.now).toHaveBeenCalledWith( + 'Scaffolder', + 'scaffolder@backstage.io', + ); + + expect(mockRepo.createCommit).toHaveBeenCalledWith( + 'HEAD', + mockSignature, + mockSignature, + 'initial commit', + 'mockoid', + [], + ); + }); + }); });