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

Commit f469c14

Browse files
committed
Restore tests for IsWorkingDirectoryClean
1 parent 22fd349 commit f469c14

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

src/GitHub.Exports.Reactive/Services/IPullRequestService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ IObservable<IPullRequestModel> CreatePullRequest(IModelService modelService,
1717
IBranch sourceBranch, IBranch targetBranch,
1818
string title, string body);
1919

20+
/// <summary>
21+
/// Checks whether the working directory for the specified repository is in a clean state.
22+
/// </summary>
23+
/// <param name="repository">The repository.</param>
24+
/// <returns></returns>
25+
IObservable<bool> IsWorkingDirectoryClean(ILocalRepositoryModel repository);
26+
2027
/// <summary>
2128
/// Checks out a pull request to a local branch.
2229
/// </summary>

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

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,243 @@ static ILocalRepositoryModel CreateLocalRepositoryModel(Repository repo)
397397
static Signature Author => new Signature("foo", "[email protected]", DateTimeOffset.Now);
398398
}
399399

400+
public class TheIsWorkingDirectoryCleanMethod
401+
{
402+
[Fact]
403+
public async Task NewRepo_True()
404+
{
405+
using (var tempDir = new TempDirectory())
406+
using (var repo = CreateRepository(tempDir))
407+
{
408+
var service = CreatePullRequestService(repo);
409+
var repositoryModel = CreateLocalRepositoryModel(repo);
410+
411+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
412+
413+
Assert.True(isClean);
414+
}
415+
}
416+
417+
[Fact]
418+
public async Task UntrackedFile_True()
419+
{
420+
using (var tempDir = new TempDirectory())
421+
using (var repo = CreateRepository(tempDir))
422+
{
423+
var service = CreatePullRequestService(repo);
424+
var repositoryModel = CreateLocalRepositoryModel(repo);
425+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
426+
File.WriteAllText(file, "contents");
427+
428+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
429+
430+
Assert.True(isClean);
431+
}
432+
}
433+
434+
435+
[Fact]
436+
public async Task CommitFile_True()
437+
{
438+
using (var tempDir = new TempDirectory())
439+
using (var repo = CreateRepository(tempDir))
440+
{
441+
var service = CreatePullRequestService(repo);
442+
var repositoryModel = CreateLocalRepositoryModel(repo);
443+
var file = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
444+
File.WriteAllText(file, "contents");
445+
Commands.Stage(repo, file);
446+
repo.Commit("foo", Author, Author);
447+
448+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
449+
450+
Assert.True(isClean);
451+
}
452+
}
453+
454+
[Fact]
455+
public async Task AddedFile_False()
456+
{
457+
using (var tempDir = new TempDirectory())
458+
using (var repo = CreateRepository(tempDir))
459+
{
460+
var service = CreatePullRequestService(repo);
461+
var repositoryModel = CreateLocalRepositoryModel(repo);
462+
var path = "file.txt";
463+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
464+
File.WriteAllText(file, "contents");
465+
Commands.Stage(repo, path);
466+
467+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
468+
469+
Assert.False(isClean);
470+
}
471+
}
472+
473+
[Fact]
474+
public async Task ModifiedFile_False()
475+
{
476+
using (var tempDir = new TempDirectory())
477+
using (var repo = CreateRepository(tempDir))
478+
{
479+
var service = CreatePullRequestService(repo);
480+
var repositoryModel = CreateLocalRepositoryModel(repo);
481+
var path = "file.txt";
482+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
483+
File.WriteAllText(file, "contents");
484+
Commands.Stage(repo, path);
485+
repo.Commit("foo", Author, Author);
486+
File.WriteAllText(file, "contents2");
487+
488+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
489+
490+
Assert.False(isClean);
491+
}
492+
}
493+
494+
[Fact]
495+
public async Task StagedFile_False()
496+
{
497+
using (var tempDir = new TempDirectory())
498+
using (var repo = CreateRepository(tempDir))
499+
{
500+
var service = CreatePullRequestService(repo);
501+
var repositoryModel = CreateLocalRepositoryModel(repo);
502+
var path = "file.txt";
503+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
504+
File.WriteAllText(file, "contents");
505+
Commands.Stage(repo, path);
506+
repo.Commit("foo", Author, Author);
507+
File.WriteAllText(file, "contents2");
508+
Commands.Stage(repo, path);
509+
510+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
511+
512+
Assert.False(isClean);
513+
}
514+
}
515+
516+
[Fact]
517+
public async Task MissingFile_False()
518+
{
519+
using (var tempDir = new TempDirectory())
520+
using (var repo = CreateRepository(tempDir))
521+
{
522+
var service = CreatePullRequestService(repo);
523+
var repositoryModel = CreateLocalRepositoryModel(repo);
524+
var path = "file.txt";
525+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
526+
File.WriteAllText(file, "contents");
527+
Commands.Stage(repo, path);
528+
repo.Commit("foo", Author, Author);
529+
File.Delete(file);
530+
531+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
532+
533+
Assert.False(isClean);
534+
}
535+
}
536+
537+
[Fact]
538+
public async Task RemovedFile_False()
539+
{
540+
using (var tempDir = new TempDirectory())
541+
using (var repo = CreateRepository(tempDir))
542+
{
543+
var service = CreatePullRequestService(repo);
544+
var repositoryModel = CreateLocalRepositoryModel(repo);
545+
var path = "file.txt";
546+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
547+
File.WriteAllText(file, "contents");
548+
Commands.Stage(repo, path);
549+
repo.Commit("foo", Author, Author);
550+
File.Delete(file);
551+
Commands.Stage(repo, path);
552+
553+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
554+
555+
Assert.False(isClean);
556+
}
557+
}
558+
559+
[Fact]
560+
public async Task RenamedInIndexFile_False()
561+
{
562+
using (var tempDir = new TempDirectory())
563+
using (var repo = CreateRepository(tempDir))
564+
{
565+
var service = CreatePullRequestService(repo);
566+
var repositoryModel = CreateLocalRepositoryModel(repo);
567+
var path = "file.txt";
568+
var renamedPath = "renamed.txt";
569+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
570+
var renamedFile = Path.Combine(repo.Info.WorkingDirectory, renamedPath);
571+
File.WriteAllText(file, "contents");
572+
Commands.Stage(repo, path);
573+
repo.Commit("foo", Author, Author);
574+
File.Move(file, renamedFile);
575+
Commands.Stage(repo, path);
576+
Commands.Stage(repo, renamedPath);
577+
578+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
579+
580+
Assert.False(isClean);
581+
}
582+
}
583+
584+
[Fact]
585+
public async Task RenamedInWorkingDirFile_False()
586+
{
587+
using (var tempDir = new TempDirectory())
588+
using (var repo = CreateRepository(tempDir))
589+
{
590+
var service = CreatePullRequestService(repo);
591+
var repositoryModel = CreateLocalRepositoryModel(repo);
592+
var path = "file.txt";
593+
var renamedPath = "renamed.txt";
594+
var file = Path.Combine(repo.Info.WorkingDirectory, path);
595+
var renamedFile = Path.Combine(repo.Info.WorkingDirectory, renamedPath);
596+
File.WriteAllText(file, "contents");
597+
Commands.Stage(repo, path);
598+
repo.Commit("foo", Author, Author);
599+
File.Move(file, renamedFile);
600+
601+
// NOTE: `RetrieveStatus(new StatusOptions { DetectRenamesInWorkDir = true })` would need to be used
602+
// for renamed files to appear as `RenamedInWorkingDir` rather than `Missing` and `Untracked`.
603+
// This isn't required in the current implementation.
604+
var isClean = await service.IsWorkingDirectoryClean(repositoryModel).FirstAsync();
605+
606+
Assert.False(isClean);
607+
}
608+
}
609+
610+
static Repository CreateRepository(TempDirectory tempDirectory)
611+
{
612+
var repoDir = tempDirectory.Directory.FullName;
613+
return new Repository(Repository.Init(repoDir));
614+
}
615+
616+
static IPullRequestService CreatePullRequestService(Repository repo)
617+
{
618+
var repoDir = repo.Info.WorkingDirectory;
619+
var serviceProvider = Substitutes.ServiceProvider;
620+
var gitService = serviceProvider.GetGitService();
621+
gitService.GetRepository(repoDir).Returns(repo);
622+
var service = new PullRequestService(Substitute.For<IGitClient>(), gitService, serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());
623+
return service;
624+
}
625+
626+
static ILocalRepositoryModel CreateLocalRepositoryModel(Repository repo)
627+
{
628+
var repoDir = repo.Info.WorkingDirectory;
629+
var repositoryModel = Substitute.For<ILocalRepositoryModel>();
630+
repositoryModel.LocalPath.Returns(repoDir);
631+
return repositoryModel;
632+
}
633+
634+
static Signature Author => new Signature("foo", "[email protected]", DateTimeOffset.Now);
635+
}
636+
400637
[Fact]
401638
public async Task ExtractHead()
402639
{

0 commit comments

Comments
 (0)