/
RepositoryOptionsFixture.cs
178 lines (135 loc) · 6.8 KB
/
RepositoryOptionsFixture.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.IO;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class RepositoryOptionsFixture : BaseFixture
{
private readonly string newWorkdir;
private readonly string newIndex;
//TODO: Add facts ensuring the correct opening of a workdir/index through relative and absolute paths
public RepositoryOptionsFixture()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
newWorkdir = Path.Combine(scd.DirectoryPath, "wd");
Directory.CreateDirectory(newWorkdir);
newIndex = Path.Combine(scd.DirectoryPath, "my-index");
}
[Fact]
public void CanOpenABareRepoAsIfItWasAStandardOne()
{
File.Copy(Path.Combine(StandardTestRepoPath, "index"), newIndex);
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir, IndexPath = newIndex };
using (var repo = new Repository(BareTestRepoPath, options))
{
var st = repo.Index.RetrieveStatus("1/branch_file.txt");
Assert.Equal(FileStatus.Missing, st);
}
}
[Fact]
public void CanOpenABareRepoAsIfItWasAStandardOneWithANonExisitingIndexFile()
{
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir, IndexPath = newIndex };
using (var repo = new Repository(BareTestRepoPath, options))
{
var st = repo.Index.RetrieveStatus("1/branch_file.txt");
Assert.Equal(FileStatus.Removed, st);
}
}
[Fact]
public void CanProvideADifferentWorkDirToAStandardRepo()
{
var scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(scd.DirectoryPath))
{
Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus("1/branch_file.txt"));
}
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir };
scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(scd.DirectoryPath, options))
{
Assert.Equal(FileStatus.Missing, repo.Index.RetrieveStatus("1/branch_file.txt"));
}
}
[Fact]
public void CanProvideADifferentIndexToAStandardRepo()
{
var scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(scd.DirectoryPath))
{
Assert.Equal(FileStatus.Untracked, repo.Index.RetrieveStatus("new_untracked_file.txt"));
repo.Index.Stage("new_untracked_file.txt");
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus("new_untracked_file.txt"));
File.Copy(Path.Combine(repo.Info.Path, "index"), newIndex);
}
var options = new RepositoryOptions { IndexPath = newIndex };
scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(scd.DirectoryPath, options))
{
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus("new_untracked_file.txt"));
}
}
[Fact]
public void OpeningABareRepoWithoutProvidingBothWorkDirAndIndexThrows()
{
Repository repo;
Assert.Throws<ArgumentException>(() => repo = new Repository(BareTestRepoPath, new RepositoryOptions { IndexPath = newIndex }));
Assert.Throws<ArgumentException>(() => repo = new Repository(BareTestRepoPath, new RepositoryOptions { WorkingDirectoryPath = newWorkdir }));
}
[Fact]
public void CanSneakAdditionalCommitsIntoAStandardRepoWithoutAlteringTheWorkdirOrTheIndex()
{
var scd = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(scd.DirectoryPath))
{
Branch head = repo.Head;
Assert.Equal(FileStatus.Nonexistent, repo.Index.RetrieveStatus("zomg.txt"));
string commitSha = MeanwhileInAnotherDimensionAnEvilMastermindIsAtWork(scd.DirectoryPath);
Branch newHead = repo.Head;
Assert.NotEqual(head.Tip.Sha, newHead.Tip.Sha);
Assert.Equal(commitSha, newHead.Tip.Sha);
Assert.Equal(FileStatus.Removed, repo.Index.RetrieveStatus("zomg.txt"));
}
}
private string MeanwhileInAnotherDimensionAnEvilMastermindIsAtWork(string workingDirectoryPath)
{
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir, IndexPath = newIndex };
using (var sneakyRepo = new Repository(workingDirectoryPath, options))
{
Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, Path.GetFullPath(sneakyRepo.Info.WorkingDirectory));
sneakyRepo.Reset(ResetOptions.Mixed, sneakyRepo.Head.Tip.Sha);
var filepath = Path.Combine(sneakyRepo.Info.WorkingDirectory, "zomg.txt");
File.WriteAllText(filepath, "I'm being sneaked in!\n");
sneakyRepo.Index.Stage(filepath);
return sneakyRepo.Commit("Tadaaaa!", DummySignature, DummySignature).Sha;
}
}
[Fact]
public void CanProvideDifferentConfigurationFilesToARepository()
{
string globalLocation = Path.Combine(newWorkdir, "my-global-config");
string systemLocation = Path.Combine(newWorkdir, "my-system-config");
const string name = "Adam 'aroben' Roben";
const string email = "adam@github.com";
StringBuilder sb = new StringBuilder()
.AppendLine("[user]")
.AppendFormat("name = {0}{1}", name, Environment.NewLine)
.AppendFormat("email = {0}{1}", email, Environment.NewLine);
File.WriteAllText(globalLocation, sb.ToString());
var options = new RepositoryOptions {
GlobalConfigurationLocation = globalLocation,
SystemConfigurationLocation = systemLocation,
};
using (var repo = new Repository(BareTestRepoPath, options))
{
Assert.True(repo.Config.HasGlobalConfig);
Assert.Equal(name, repo.Config.Get<string>("user", "name", null));
Assert.Equal(email, repo.Config.Get<string>("user", "email", null));
repo.Config.Set("help.link", "https://twitter.com/xpaulbettsx/status/205761932626636800", ConfigurationLevel.System);
}
AssertValueInConfigFile(systemLocation, "xpaulbettsx");
}
}
}