8000 Merge branch 'master' into ncover/reference-serilog · github/VisualStudio@12a3b95 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 12a3b95

Browse files
authored
Merge branch 'master' into ncover/reference-serilog
2 parents f3d489d + a5f9e8f commit 12a3b95

File tree

3 files changed

+269
-2
lines changed

3 files changed

+269
-2
lines changed

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,22 @@ public IObservable<IReadOnlyList<CommitMessage>> GetMessagesForUniqueCommits(
103103
public IObservable<bool> IsWorkingDirectoryClean(ILocalRepositoryModel repository)
104104
{
105105
var repo = gitService.GetRepository(repository.LocalPath);
106-
return Observable.Return(!repo.RetrieveStatus().IsDirty);
106+
var isClean = !IsFilthy(repo.RetrieveStatus());
107+
return Observable.Return(isClean);
108+
}
109+
110+
static bool IsFilthy(RepositoryStatus status)
111+
{
112+
if (status.IsDirty)
113+
{
114+
// This is similar to IsDirty, but also allows NewInWorkdir files
115+
return status.Any(entry =>
116+
entry.State != FileStatus.Ignored &&
117+
entry.State != FileStatus.Unaltered &&
118+
entry.State != FileStatus.NewInWorkdir);
119+
}
120+
121+
return false;
107122
}
108123

109124
public IObservable<Unit> Pull(ILocalRepositoryModel repository)

test/UnitTests/GitHub.App/Services/PullRequestServiceTests.cs

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,243 @@
1717

1818
public class PullRequestServiceTests : TestBaseClass
1919
{
20+
public class TheIsWorkingDirectoryCleanMethod
21+
{
22+
[Fact]
23+
public async Task NewRepo_True()
24+
{
25+
using (var tempDir = new TempDirectory())
26+
using (var repo = CreateRepository(tempDir))
27+
{
28+
var service = CreatePullRequestService(repo);
29+
var repositoryModel = CreateLocalRepositoryModel(repo);
30+
31+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
32+
33+
Assert.True(isClean< 9E81 span class=pl-kos>);
34+
}
35+
}
36+
37+
[Fact]
38+
public async Task UntrackedFile_True()
39+
{
40+
using (var tempDir = new TempDirectory())
41+
using (var repo = CreateRepository(tempDir))
42+
{
43+
var service = CreatePullRequestService(repo);
44+
var repositoryModel = CreateLocalRepositoryModel(repo);
45+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
46+
File.WriteAllText(file, "contents");
47+
48+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
49+
50+
Assert.True(isClean);
51+
}
52+
}
53+
54+
55+
[Fact]
56+
public async Task CommitFile_True()
57+
{
58+
using (var tempDir = new TempDirectory())
59+
using (var repo = CreateRepository(tempDir))
60+
{
61+
var service = CreatePullRequestService(repo);
62+
var repositoryModel = CreateLocalRepositoryModel(repo);
63+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
64+
File.WriteAllText(file, "contents");
65+
Commands.Stage(repo, file);
66+
repo.Commit("foo", Author, Author);
67+
68+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
69+
70+
Assert.True(isClean);
71+
}
72+
}
73+
74+
[Fact]
75+
public async Task AddedFile_False()
76+
{
77+
using (var tempDir = new TempDirectory())
78+
using (var repo = CreateRepository(tempDir))
79+
{
80+
var service = CreatePullRequestService(repo);
81+
var repositoryModel = CreateLocalRepositoryModel(repo);
82+
var path = "file.txt";
83+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
84+
File.WriteAllText(file, "contents");
85+
Commands.Stage(repo, path);
86+
87+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
88+
89+
Assert.False(isClean);
90+
}
91+
}
92+
93+
[Fact]
94+
public async Task ModifiedFile_False()
95+
{
96+
using (var tempDir = new TempDirectory())
97+
using (var repo = CreateRepository(tempDir))
98+
{
99+
var service = CreatePullRequestService(repo);
100+
var repositoryModel = CreateLocalRepositoryModel(repo);
101+
var path = "file.txt";
102+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
103+
File.WriteAllText(file, "contents");
104+
Commands.Stage(repo, path);
105+
repo.Commit("foo", Author, Author);
106+
File.WriteAllText(file, "contents2");
107+
108+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
109+
110+
Assert.False(isClean);
111+
}
112+
}
113+
114+
[Fact]
115+
public async Task StagedFile_False()
116+
{
117+
using (var tempDir = new TempDirectory())
118+
using (var repo = CreateRepository(tempDir))
119+
{
120+
var service = CreatePullRequestService(repo);
121+
var repositoryModel = CreateLocalRepositoryModel(repo);
122+
var path = "file.txt";
123+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
124+
File.WriteAllText(file, "contents");
125+
Commands.Stage(repo, path);
126+
repo.Commit("foo", Author, Author);
127+
File.WriteAllText(file, "contents2");
128+
Commands.Stage(repo, path);
129+
130+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
131+
132+
Assert.False(isClean);
133+
}
134+
}
135+
136+
[Fact]
137+
public async Task MissingFile_False()
138+
{
139+
using (var tempDir = new TempDirectory())
140+
using (var repo = CreateRepository(tempDir))
141+
{
142+
var service = CreatePullRequestService(repo);
143+
var repositoryModel = CreateLocalRepositoryModel(repo);
144+
var path = "file.txt";
145+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
146+
File.WriteAllText(file, "contents");
147+
Commands.Stage(repo, path);
148+
repo.Commit("foo", Author, Author);
149+
File.Delete(file);
150+
151+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
152+
153+
Assert.False(isClean);
154+
}
155+
}
156+
157+
[Fact]
158+
public async Task RemovedFile_False()
159+
{
160+
using (var tempDir = new TempDirectory())
161+
using (var repo = CreateRepository(tempDir))
162+
{
163+
var service = CreatePullRequestService(repo);
164+
var repositoryModel = CreateLocalRepositoryModel(repo);
165+
var path = "file.txt";
166+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
167+
File.WriteAllText(file, "contents");
168+
Commands.Stage(repo, path);
169+
repo.Commit("foo", Author, Author);
170+
File.Delete(file);
171+
Commands.Stage(repo, path);
172+
173+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
174+
175+
Assert.False(isClean);
176+
}
177+
}
178+
179+
[Fact]
180+
public async Task RenamedInIndexFile_False()
181+
{
182+
using (var tempDir = new TempDirectory())
183+
using (var repo = CreateRepository(tempDir))
184+
{
185+
var service = CreatePullRequestService(repo);
186+
var repositoryModel = CreateLocalRepositoryModel(repo);
187+
var path = "file.txt";
188+
var renamedPath = "renamed.txt";
189+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
190+
var renamedFile = Path.Combine(repo.Info.WorkingDirectory, renamedPath);
191+
File.WriteAllText(file, "contents");
192+
Commands.Stage(repo, path);
193+
repo.Commit("foo", Author, Author);
194+
File.Move(file, renamedFile);
195+
Commands.Stage(repo, path);
196+
Commands.Stage(repo, renamedPath);
197+
198+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
199+
200+
Assert.False(isClean);
201+
}
202+
}
203+
204+
[Fact]
205+
public async Task RenamedInWorkingDirFile_False()
206+
{
207+
using (var tempDir = new TempDirectory())
208+
using (var repo = CreateRepository(tempDir))
209+
{
210+
var service = CreatePullRequestService(repo);
211+
var repositoryModel = CreateLocalRepositoryModel(repo);
212+
var path = "file.txt";
213+
var renamedPath = "renamed.txt";
214+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
215+
var renamedFile = Path.Combine(repo.Info.WorkingDirectory, renamedPath);
216+
File.WriteAllText(file, "contents");
217+
Commands.Stage(repo, path);
218+
repo.Commit("foo", Author, Author);
219+
File.Move(file, renamedFile);
220+
221+
// NOTE: `RetrieveStatus(new StatusOptions { DetectRenamesInWorkDir = true })` would need to be used
222+
// for renamed files to appear as `RenamedInWorkingDir` rather than `Missing` and `Untracked`.
223+
// This isn't required in the current implementation.
224+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
225+
226+
Assert.False(isClean);
227+
}
228+
}
229+
230+
static Repository CreateRepository(TempDirectory tempDirectory)
231+
{
232+
var repoDir = tempDirectory.Directory.FullName;
233+
return new Repository(Repository.Init(repoDir));
234+
}
235+
236+
static IPullRequestService CreatePullRequestService(Repository repo)
237+
{
238+
var repoDir = repo.Info.WorkingDirectory;
239+
var serviceProvider = Substitutes.ServiceProvider;
240+
var gitService = serviceProvider.GetGitService();
241+
gitService.GetRepository(repoDir).Returns(repo);
242+
var service = new PullRequestService(Substitute.For<IGitClient>(), gitService, serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());
243+
return service;
244+
}
245+
246+
static ILocalRepositoryModel CreateLocalRepositoryModel(Repository repo)
247+
{
248+
var repoDir = repo.Info.WorkingDirectory;
249+
var repositoryModel = Substitute.For<ILocalRepositoryModel>();
250+
repositoryModel.LocalPath.Returns(repoDir);
251+
return repositoryModel;
252+
}
253+
254+
static Signature Author => new Signature("foo", "foo@bar.com", DateTimeOffset.Now);
255+
}
256+
20257
public class TheExtractFileMethod
21258
{
22259
[Fact]

test/UnitTests/Helpers/TestBaseClass.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected static PullRequest CreatePullRequest(User user, int id, ItemState stat
6464
null, null, null);
6565
return new PullRequest(0, uri, uri, uri, uri, uri, uri,
6666
id, state, title, "", createdAt, updatedAt,
67-
null, null,
67+
null, null,
6868
new GitReference(uri.ToString(), "foo:bar", "bar", "123", user, repo),
6969
new GitReference(uri.ToString(), "foo:baz", "baz", "123", user, repo),
7070
user, null, null, false, null,
@@ -87,8 +87,23 @@ public TempDirectory()
8787

8888
public void Dispose()
8989
{
90+
// Remove any read-only attributes
91+
SetFileAttributes(Directory, FileAttributes.Normal);
9092
Directory.Delete(true);
9193
}
94+
95+
static void SetFileAttributes(DirectoryInfo dir, FileAttributes attributes)
96+
{
97+
foreach (DirectoryInfo subdir in dir.GetDirectories())
98+
{
99+
SetFileAttributes(subdir, attributes);
100+
}
101+
102+
foreach (var file in dir.GetFiles())
103+
{
104+
File.SetAttributes(file.FullName, attributes);
105+
}
106+
}
92107
}
93108

94109
protected class TempRepository : TempDirectory

0 commit comments

Comments
 (0)
0