Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RepositoryOptionsFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b62de3c7659cd6d173c59ad4171f00e0e282aa55 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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 path1 = CloneStandardTestRepo();
            using (var repo = new Repository(path1))
            {
                Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus("1/branch_file.txt"));
            }

            var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir };

            var path2 = CloneStandardTestRepo();
            using (var repo = new Repository(path2, options))
            {
                Assert.Equal(FileStatus.Missing, repo.Index.RetrieveStatus("1/branch_file.txt"));
            }
        }

        [Fact]
        public void CanProvideADifferentIndexToAStandardRepo()
        {
            var path1 = CloneStandardTestRepo();
            using (var repo = new Repository(path1))
            {
                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 };

            var path2 = CloneStandardTestRepo();
            using (var repo = new Repository(path2, options))
            {
                Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus("new_untracked_file.txt"));
            }
        }

        [Fact]
        public void OpeningABareRepoWithoutProvidingBothWorkDirAndIndexThrows()
        {
            Assert.Throws<ArgumentException>(() => new Repository(BareTestRepoPath, new RepositoryOptions {IndexPath = newIndex}));
            Assert.Throws<ArgumentException>(() => new Repository(BareTestRepoPath, new RepositoryOptions {WorkingDirectoryPath = newWorkdir}));
        }

        [Fact]
        public void CanSneakAdditionalCommitsIntoAStandardRepoWithoutAlteringTheWorkdirOrTheIndex()
        {
            string path = CloneStandardTestRepo();
            using (var repo = new Repository(path))
            {
                Branch head = repo.Head;

                Assert.Equal(FileStatus.Nonexistent, repo.Index.RetrieveStatus("zomg.txt"));

                string commitSha = MeanwhileInAnotherDimensionAnEvilMastermindIsAtWork(path);

                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 xdgLocation = Path.Combine(newWorkdir, "my-xdg-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());
            File.WriteAllText(systemLocation, string.Empty);
            File.WriteAllText(xdgLocation, string.Empty);

            var options = new RepositoryOptions {
                GlobalConfigurationLocation = globalLocation,
                XdgConfigurationLocation = xdgLocation,
                SystemConfigurationLocation = systemLocation,
            };

            using (var repo = new Repository(BareTestRepoPath, options))
            {
                Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
                Assert.Equal(name, repo.Config.Get<string>("user.name").Value);
                Assert.Equal(email, repo.Config.Get<string>("user.email").Value);

                repo.Config.Set("xdg.setting", "https://twitter.com/libgit2sharp", ConfigurationLevel.Xdg);
                repo.Config.Set("help.link", "https://twitter.com/xpaulbettsx/status/205761932626636800", ConfigurationLevel.System);
            }

            AssertValueInConfigFile(systemLocation, "xpaulbettsx");
        }

        [Fact]
        public void CanProvideDifferentWorkingDirOnInit()
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
            var options = new RepositoryOptions {WorkingDirectoryPath = newWorkdir};

            using (var repo = Repository.Init(scd.DirectoryPath, false, options))
            {
                Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, repo.Info.WorkingDirectory);
            }
        }

        [Fact]
        public void CanProvideDifferentConfigurationFilesOnInit()
        {
            SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
            var options = BuildFakeConfigs(scd);

            using (var repo = Repository.Init(scd.DirectoryPath, false, options))
            {
                Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
                Assert.Equal("global", repo.Config.Get<string>("woot.this-rocks").Value);
                Assert.Equal(42, repo.Config.Get<int>("wow.man-I-am-totally-global").Value);

                Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg));
                Assert.Equal("xdg", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.Xdg).Value);

                Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
                Assert.Equal("system", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.System).Value);
            }
        }

        [Fact]
        public void CanProvideDifferentWorkingDirOnClone()
        {
            string url = "https://github.com/libgit2/TestGitRepository";
            var scd = BuildSelfCleaningDirectory();
            var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir };

            using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
            {
                Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, repo.Info.WorkingDirectory);
            }
        }

        [Fact]
        public void CanProvideDifferentConfigurationFilesOnClone()
        {
            string url = "https://github.com/libgit2/TestGitRepository";
            var scd = BuildSelfCleaningDirectory();
            var configScd = BuildSelfCleaningDirectory();
            var options = BuildFakeConfigs(configScd);

            using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
            {
                Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
                Assert.Equal("global", repo.Config.Get<string>("woot.this-rocks").Value);
                Assert.Equal(42, repo.Config.Get<int>("wow.man-I-am-totally-global").Value);

                Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg));
                Assert.Equal("xdg", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.Xdg).Value);

                Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
                Assert.Equal("system", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.System).Value);
            }
        }
    }
}