refactor(dev-infra): use a singleton for GitClient (#41515)

Creates a singleton class for GitClient rather than relying on creating an instance to
require being passed around throughout its usages.

PR Close #41515
This commit is contained in:
Joey Perrott
2021-04-08 12:34:55 -07:00
committed by Zach Arend
parent c20db69f9f
commit 9bf8e5164d
32 changed files with 439 additions and 323 deletions
+21 -7
View File
@@ -58,7 +58,21 @@ export interface Commit {
* Virtual git client that mocks Git commands and keeps track of the repository state
* in memory. This allows for convenient test assertions with Git interactions.
*/
export class VirtualGitClient extends GitClient {
export class VirtualGitClient<Authenticated extends boolean> extends GitClient<Authenticated> {
static getInstance(config = mockNgDevConfig, tmpDir = testTmpDir): VirtualGitClient<false> {
return new VirtualGitClient(undefined, config, tmpDir);
}
static getAuthenticatedInstance(config = mockNgDevConfig, tmpDir = testTmpDir):
VirtualGitClient<true> {
return new VirtualGitClient('abc123', config, tmpDir);
}
private constructor(token: Authenticated extends true? string: undefined, config: NgDevConfig,
tmpDir: string) {
super(token, config, tmpDir);
}
/** Current Git HEAD that has been previously fetched. */
fetchHeadRef: RemoteRef|null = null;
/** List of known branches in the repository. */
@@ -190,10 +204,10 @@ export class VirtualGitClient extends GitClient {
}
/**
* Builds a Virtual Git Client instance with the provided config and set the temporary test
* directory.
*/
export function buildVirtualGitClient(config = mockNgDevConfig, tmpDir = testTmpDir) {
return (new VirtualGitClient(undefined, config, tmpDir));
export function installVirtualGitClientSpies() {
const authenticatedVirtualGitClient = VirtualGitClient.getAuthenticatedInstance();
spyOn(GitClient, 'getAuthenticatedInstance').and.returnValue(authenticatedVirtualGitClient);
const unauthenticatedVirtualGitClient = VirtualGitClient.getInstance();
spyOn(GitClient, 'getInstance').and.returnValue(unauthenticatedVirtualGitClient);
}
@@ -27,13 +27,13 @@ export function getBranchPushMatcher(options: BranchPushMatchParameters) {
const {targetRepo, targetBranch, baseBranch, baseRepo, expectedCommits} = options;
return jasmine.objectContaining({
remote: {
repoUrl: `https://github.com/${targetRepo.owner}/${targetRepo.name}.git`,
repoUrl: `https://abc123@github.com/${targetRepo.owner}/${targetRepo.name}.git`,
name: `refs/heads/${targetBranch}`
},
head: jasmine.objectContaining({
newCommits: expectedCommits,
ref: {
repoUrl: `https://github.com/${baseRepo.owner}/${baseRepo.name}.git`,
repoUrl: `https://abc123@github.com/${baseRepo.owner}/${baseRepo.name}.git`,
name: baseBranch,
},
})