forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockGitProcess.cs
More file actions
147 lines (121 loc) · 5.33 KB
/
MockGitProcess.cs
File metadata and controls
147 lines (121 loc) · 5.33 KB
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
using GVFS.Common.Git;
using GVFS.Common.Tracing;
using GVFS.Tests.Should;
using GVFS.UnitTests.Mock.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace GVFS.UnitTests.Mock.Git
{
public class MockGitProcess : GitProcess
{
private List<CommandInfo> expectedCommandInfos = new List<CommandInfo>();
public MockGitProcess()
: base(new MockGVFSEnlistment())
{
this.CommandsRun = new List<string>();
this.StoredCredentials = new Dictionary<string, Credential>(StringComparer.OrdinalIgnoreCase);
this.CredentialApprovals = new Dictionary<string, List<Credential>>();
this.CredentialRejections = new Dictionary<string, List<Credential>>();
}
public List<string> CommandsRun { get; }
public bool ShouldFail { get; set; }
public Dictionary<string, Credential> StoredCredentials { get; }
public Dictionary<string, List<Credential>> CredentialApprovals { get; }
public Dictionary<string, List<Credential>> CredentialRejections { get; }
public void SetExpectedCommandResult(string command, Func<Result> result, bool matchPrefix = false)
{
CommandInfo commandInfo = new CommandInfo(command, result, matchPrefix);
this.expectedCommandInfos.Add(commandInfo);
}
public override bool TryStoreCredential(ITracer tracer, string repoUrl, string username, string password, out string error)
{
Credential credential = new Credential(username, password);
// Record the approval request for this credential
List<Credential> acceptedCredentials;
if (!this.CredentialApprovals.TryGetValue(repoUrl, out acceptedCredentials))
{
acceptedCredentials = new List<Credential>();
this.CredentialApprovals[repoUrl] = acceptedCredentials;
}
acceptedCredentials.Add(credential);
// Store the credential
this.StoredCredentials[repoUrl] = credential;
return base.TryStoreCredential(tracer, repoUrl, username, password, out error);
}
public override bool TryDeleteCredential(ITracer tracer, string repoUrl, string username, string password, out string error)
{
Credential credential = new Credential(username, password);
// Record the rejection request for this credential
List<Credential> rejectedCredentials;
if (!this.CredentialRejections.TryGetValue(repoUrl, out rejectedCredentials))
{
rejectedCredentials = new List<Credential>();
this.CredentialRejections[repoUrl] = rejectedCredentials;
}
rejectedCredentials.Add(credential);
// Erase the credential
this.StoredCredentials.Remove(repoUrl);
return base.TryDeleteCredential(tracer, repoUrl, username, password, out error);
}
protected override Result InvokeGitImpl(
string command,
string workingDirectory,
string dotGitDirectory,
bool useReadObjectHook,
Action<StreamWriter> writeStdIn,
Action<string> parseStdOutLine,
int timeoutMs,
string gitObjectsDirectory = null)
{
this.CommandsRun.Add(command);
if (this.ShouldFail)
{
return new Result(string.Empty, string.Empty, Result.GenericFailureCode);
}
Func<CommandInfo, bool> commandMatchFunction =
(CommandInfo commandInfo) =>
{
if (commandInfo.MatchPrefix)
{
return command.StartsWith(commandInfo.Command);
}
else
{
return string.Equals(command, commandInfo.Command, StringComparison.Ordinal);
}
};
CommandInfo matchedCommand = this.expectedCommandInfos.Last(commandMatchFunction);
matchedCommand.ShouldNotBeNull("Unexpected command: " + command);
return matchedCommand.Result();
}
public class Credential
{
public Credential(string username, string password)
{
this.Username = username;
this.Password = password;
}
public string Username { get; }
public string Password { get; }
public string BasicAuthString
{
get => Convert.ToBase64String(Encoding.ASCII.GetBytes(this.Username + ":" + this.Password));
}
}
private class CommandInfo
{
public CommandInfo(string command, Func<Result> result, bool matchPrefix)
{
this.Command = command;
this.Result = result;
this.MatchPrefix = matchPrefix;
}
public string Command { get; private set; }
public Func<Result> Result { get; private set; }
public bool MatchPrefix { get; private set; }
}
}
}