chore(scaffolder): making some tests nicer and a little more readable and typescript compliant
This commit is contained in:
@@ -43,6 +43,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "^0.1.1-alpha.12",
|
||||
"@octokit/types": "^5.0.1",
|
||||
"@types/fs-extra": "^9.0.1",
|
||||
"@types/git-url-parse": "^9.0.0",
|
||||
"@types/nodegit": "0.26.5",
|
||||
|
||||
@@ -19,7 +19,6 @@ const mocks = {
|
||||
CheckoutOptions: jest.fn(() => {}),
|
||||
};
|
||||
jest.doMock('nodegit', () => mocks);
|
||||
// require('nodegit');
|
||||
|
||||
import { GithubPreparer } from './github';
|
||||
import {
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export const mockGithubClient = {
|
||||
repos: {
|
||||
createInOrg: jest.fn(),
|
||||
createForAuthenticatedUser: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
export class Octokit {
|
||||
constructor() {
|
||||
return mockGithubClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const mockIndex = {
|
||||
addAll: jest.fn(),
|
||||
write: jest.fn(),
|
||||
writeTree: jest.fn().mockResolvedValue('mockoid'),
|
||||
};
|
||||
|
||||
const mockRepo = {
|
||||
refreshIndex: jest.fn().mockResolvedValue(mockIndex),
|
||||
createCommit: jest.fn(),
|
||||
};
|
||||
|
||||
const mockRemote = {
|
||||
push: jest.fn(),
|
||||
};
|
||||
|
||||
const Repository = { init: jest.fn().mockResolvedValue(mockRepo) };
|
||||
const Remote = { create: jest.fn().mockResolvedValue(mockRemote) };
|
||||
const Signature = { now: jest.fn() };
|
||||
const Cred = {
|
||||
userpassPlaintextNew: jest.fn(),
|
||||
};
|
||||
|
||||
export { Repository, Remote, Signature, Cred };
|
||||
@@ -13,4 +13,70 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
describe('Github Store', () => {});
|
||||
|
||||
jest.mock('@octokit/rest');
|
||||
jest.mock('nodegit');
|
||||
|
||||
import { Octokit } from '@octokit/rest';
|
||||
import { OctokitResponse, ReposCreateInOrgResponseData } from '@octokit/types';
|
||||
import { GithubPublisher } from './github';
|
||||
|
||||
const { mockGithubClient } = require('@octokit/rest') as {
|
||||
mockGithubClient: { repos: jest.Mocked<Octokit['repos']> };
|
||||
};
|
||||
|
||||
describe('Github Publisher', () => {
|
||||
const publisher = new GithubPublisher({ client: new Octokit() });
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('publish: createRemoteInGithub', () => {
|
||||
it('should use octokit to create a repo in an organisation if the organisation property is set', async () => {
|
||||
mockGithubClient.repos.createInOrg.mockResolvedValue({
|
||||
data: {
|
||||
clone_url: 'mockclone',
|
||||
},
|
||||
} as OctokitResponse<ReposCreateInOrgResponseData>);
|
||||
|
||||
await publisher.publish({
|
||||
values: {
|
||||
isOrg: true,
|
||||
storePath: 'blam/test',
|
||||
owner: 'bob',
|
||||
},
|
||||
directory: '/tmp/test',
|
||||
});
|
||||
|
||||
expect(mockGithubClient.repos.createInOrg).toHaveBeenCalledWith({
|
||||
org: 'blam',
|
||||
name: 'test',
|
||||
});
|
||||
});
|
||||
|
||||
it('should use octokit to create a repo in the authed user if the organisation property is not set', async () => {
|
||||
mockGithubClient.repos.createForAuthenticatedUser.mockResolvedValue({
|
||||
data: {
|
||||
clone_url: 'mockclone',
|
||||
},
|
||||
} as OctokitResponse<ReposCreateInOrgResponseData>);
|
||||
|
||||
await publisher.publish({
|
||||
values: {
|
||||
storePath: 'blam/test',
|
||||
owner: 'bob',
|
||||
},
|
||||
directory: '/tmp/test',
|
||||
});
|
||||
|
||||
expect(
|
||||
mockGithubClient.repos.createForAuthenticatedUser,
|
||||
).toHaveBeenCalledWith({
|
||||
name: 'test',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('publish: createGitDirectory', () => {});
|
||||
});
|
||||
|
||||
@@ -40,17 +40,18 @@ export class GithubPublisher implements Publisher {
|
||||
return { remoteUrl };
|
||||
}
|
||||
|
||||
private async createRemote(values: RequiredTemplateValues) {
|
||||
private async createRemote(
|
||||
values: RequiredTemplateValues & Record<string, JsonValue>,
|
||||
) {
|
||||
const [owner, name] = values.storePath.split('/');
|
||||
|
||||
const {
|
||||
data: { clone_url: cloneUrl },
|
||||
} = await this.client.repos.createInOrg({
|
||||
name,
|
||||
org: owner,
|
||||
});
|
||||
const repoCreationPromise = values.isOrg
|
||||
? this.client.repos.createInOrg({ name, org: owner })
|
||||
: this.client.repos.createForAuthenticatedUser({ name });
|
||||
|
||||
return cloneUrl;
|
||||
const { data } = await repoCreationPromise;
|
||||
|
||||
return data?.clone_url;
|
||||
}
|
||||
|
||||
private async pushToRemote(directory: string, remote: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user