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

Commit 3ef8a8e

Browse files
committed
Check for repo status when we don't allow PR checkout
We don't allow checkout when: A file has been added to index A file has been modified in working dir A file has been staged A file is missing from working dir A file has been removed from index A file has been renamed in index A file has been renamed in working dir We do allow checkout when: The index and working dir is unaltered since last commit There are untracked files
1 parent ff2cb73 commit 3ef8a8e

File tree

1 file changed

+148
-11
lines changed

1 file changed

+148
-11
lines changed

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

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class PullRequestServiceTests : TestBaseClass
2020
public class TheIsWorkingDirectoryCleanMethod
2121
{
2222
[Fact]
23-
public async Task NewRepo_IsWorkingDirectoryClean_True()
23+
public async Task NewRepo_True()
2424
{
2525
using (var tempDir = new TempDirectory())
2626
using (var repo = CreateRepository(tempDir))
@@ -35,14 +35,14 @@ public async Task NewRepo_IsWorkingDirectoryClean_True()
3535
}
3636

3737
[Fact]
38-
public async Task UntrackedFile_IsWorkingDirectoryClean_True()
38+
public async Task UntrackedFile_True()
3939
{
4040
using (var tempDir = new TempDirectory())
4141
using (var repo = CreateRepository(tempDir))
4242
{
4343
var service = CreatePullRequestService(repo);
4444
var repositoryModel = CreateLocalRepositoryModel(repo);
45-
var file = Path.Combine(repo.Info.WorkingDirectory, "untracked.txt");
45+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
4646
File.WriteAllText(file, "contents");
4747

4848
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
@@ -51,17 +51,38 @@ public async Task UntrackedFile_IsWorkingDirectoryClean_True()
5151
}
5252
}
5353

54+
5455
[Fact]
55-
public async Task StagedFile_IsWorkingDirectoryClean_False()
56+
public async Task CommitFile_True()
5657
{
5758
using (var tempDir = new TempDirectory())
5859
using (var repo = CreateRepository(tempDir))
5960
{
6061
var service = CreatePullRequestService(repo);
6162
var repositoryModel = CreateLocalRepositoryModel(repo);
62-
var file = Path.Combine(repo.Info.WorkingDirectory, "modified.txt");
63+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
6364
File.WriteAllText(file, "contents");
6465
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);
6586

6687
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
6788

@@ -70,22 +91,137 @@ public async Task StagedFile_IsWorkingDirectoryClean_False()
7091
}
7192

7293
[Fact]
73-
public async Task CommittedFile_IsWorkingDirectoryClean_True()
94+
public async Task ModifiedFile_False()
7495
{
7596
using (var tempDir = new TempDirectory())
7697
using (var repo = CreateRepository(tempDir))
7798
{
7899
var service = CreatePullRequestService(repo);
79100
var repositoryModel = CreateLocalRepositoryModel(repo);
80-
var file = Path.Combine(repo.Info.WorkingDirectory, "modified.txt");
101+
var path = "file.txt";
102+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
81103
File.WriteAllText(file, "contents");
82-
Commands.Stage(repo, file);
83-
var author = new Signature("foo", "[email protected]", DateTimeOffset.Now);
84-
repo.Commit("foo", author, author);
104+
Commands.Stage(repo, path);
105+
repo.Commit("foo", Author, Author);
106+
File.WriteAllText(file, "contents2");
85107

86108
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
87109

88-
Assert.True(isClean);
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+
// NOTE: Renamed files appear as Missing and Untracked rather than RenamedInWorkingDir. Is this a bug?
221+
222+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
223+
224+
Assert.False(isClean);
89225
}
90226
}
91227

@@ -113,6 +249,7 @@ static ILocalRepositoryModel CreateLocalRepositoryModel(Repository repo)
113249
return repositoryModel;
114250
}
115251

252+
static Signature Author => new Signature("foo", "[email protected]", DateTimeOffset.Now);
116253
}
117254

118255
public class TheExtractFileMethod

0 commit comments

Comments
 (0)