Skip to content

Commit

Permalink
Fix array code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Nov 28, 2024
1 parent 385d223 commit f109707
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public async Task Run(Settings settings)
.Select(f => new Regex(f, RegexOptions.None, TimeSpan.FromSeconds(1)))
.ToArray();

var filteredRepos = regexFilters.Any()
var filteredRepos = regexFilters.Length != 0
? allRepos.Where(r => regexFilters.Any(f => f.IsMatch(r.Name)))
: allRepos;

var repos = filteredRepos.ToArray();
if (!repos.Any())
if (repos.Length == 0)
{
_log("(no repositories to sync)");
}
Expand Down
6 changes: 3 additions & 3 deletions src/Synchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void ShowSynchronizedLabels(Repository repo, IEnumerable<Label> accountL
_log($"{matchingLabels.Length,3} {"sync'd",-9} : {LabelNames(matchingLabels)}");
}

private IEnumerable<Label> GetLabelsToAdd(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
private Label[] GetLabelsToAdd(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
{
_setStatus($"Finding labels to add to {repo.Name}...");
var newLabels = accountLabels.Where(al => repoLabels
Expand All @@ -133,15 +133,15 @@ private IEnumerable<Label> GetLabelsToAdd(Repository repo, IEnumerable<Label> ac
return newLabels;
}

private IEnumerable<Label> GetLabelsToEdit(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
private Label[] GetLabelsToEdit(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
{
_setStatus($"Finding labels to edit in {repo.Name}...");
var editLabels = accountLabels.Where(al => repoLabels.Any(rl => NeedsUpdating(al, rl))).ToArray();
_log($"{editLabels.Length,3} {"to edit",-9} : {LabelNames(editLabels)}");
return editLabels;
}

private IEnumerable<Label> GetLabelsToDelete(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
private Label[] GetLabelsToDelete(Repository repo, IEnumerable<Label> accountLabels, IEnumerable<Label> repoLabels)
{
_setStatus($"Finding labels to delete from {repo.Name}...");
var oldLabels = repoLabels.Where(rl => accountLabels
Expand Down
33 changes: 18 additions & 15 deletions test/SynchronizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GitHubLabelSync.Tests;

public class SynchronizerTests
{
private readonly Stubs.Label[] _accountLabels =
private static readonly Stubs.Label[] AccountLabels =
{
new("t1", "test1", "aaaaaa"),
new("t2", "test2", "bbbbbb"),
Expand All @@ -17,7 +17,7 @@ public class SynchronizerTests
new("t6", "test6", "ffffff")
};

private readonly Stubs.Label[] _repoLabels =
private static readonly Stubs.Label[] RepoLabels =
{
new("T3", "", "cccccc"),
new("t4", "Test4", "eeeeee"),
Expand All @@ -30,14 +30,17 @@ public class SynchronizerTests
};

private readonly Action<string> _noOp = _ => { };
private static readonly Stubs.Label EmptyLabel = new Stubs.Label(string.Empty, string.Empty, string.Empty);
private static readonly Stubs.Label EmptyLabel = new(string.Empty, string.Empty, string.Empty);

private static readonly string[] ReadWriteAccess = ["repo", "delete_repo"];
private static readonly string[] ReadOnlyAccess = ["repo"];

[Fact]
public async Task ValidAccessReturnsSuccess()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetAccess().Returns(new[] { "repo", "delete_repo" });
gitHub.GetAccess().Returns(ReadWriteAccess);

var sync = new Synchronizer(gitHub, _noOp, _noOp);

Expand Down Expand Up @@ -69,7 +72,7 @@ public async Task ValidatingNoDeleteAccessFails()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetAccess().Returns(new[] { "repo" });
gitHub.GetAccess().Returns(ReadOnlyAccess);

var sync = new Synchronizer(gitHub, _noOp, _noOp);

Expand Down Expand Up @@ -328,11 +331,11 @@ public async Task PerformsCorrectActions()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
gitHub.GetLabels(Arg.Any<Repository>()).Returns(RepoLabels);
var sync = new Synchronizer(gitHub, _noOp, _noOp);

//act
await sync.SyncRepo(new Repository(), new Settings(), _accountLabels);
await sync.SyncRepo(new Repository(), new Settings(), AccountLabels);

//assert
await gitHub.Received(2).AddLabel(Arg.Any<Repository>(), Arg.Any<Label>());
Expand All @@ -345,13 +348,13 @@ public async Task NoAddDoesNotAdd()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
gitHub.GetLabels(Arg.Any<Repository>()).Returns(RepoLabels);
var sync = new Synchronizer(gitHub, _noOp, _noOp);

var settings = new Settings { NoAdd = true };

//act
await sync.SyncRepo(new Repository(), settings, _accountLabels);
await sync.SyncRepo(new Repository(), settings, AccountLabels);

//assert
await gitHub.DidNotReceive().AddLabel(Arg.Any<Repository>(), Arg.Any<Label>());
Expand All @@ -364,13 +367,13 @@ public async Task NoEditDoesNotEdit()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
gitHub.GetLabels(Arg.Any<Repository>()).Returns(RepoLabels);
var sync = new Synchronizer(gitHub, _noOp, _noOp);

var settings = new Settings { NoEdit = true };

//act
await sync.SyncRepo(new Repository(), settings, _accountLabels);
await sync.SyncRepo(new Repository(), settings, AccountLabels);

//assert
await gitHub.Received(2).AddLabel(Arg.Any<Repository>(), Arg.Any<Label>());
Expand All @@ -383,13 +386,13 @@ public async Task NoDeleteDoesNotDelete()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
gitHub.GetLabels(Arg.Any<Repository>()).Returns(RepoLabels);
var sync = new Synchronizer(gitHub, _noOp, _noOp);

var settings = new Settings { NoDelete = true };

//act
await sync.SyncRepo(new Repository(), settings, _accountLabels);
await sync.SyncRepo(new Repository(), settings, AccountLabels);

//assert
await gitHub.Received(2).AddLabel(Arg.Any<Repository>(), Arg.Any<Label>());
Expand All @@ -402,13 +405,13 @@ public async Task DryRunPerformsNoActions()
{
//arrange
var gitHub = Substitute.For<IGitHub>();
gitHub.GetLabels(Arg.Any<Repository>()).Returns(_repoLabels);
gitHub.GetLabels(Arg.Any<Repository>()).Returns(RepoLabels);
var sync = new Synchronizer(gitHub, _noOp, _noOp);

var settings = new Settings { DryRun = true };

//act
await sync.SyncRepo(new Repository(), settings, _accountLabels);
await sync.SyncRepo(new Repository(), settings, AccountLabels);

//assert
await gitHub.DidNotReceive().AddLabel(Arg.Any<Repository>(), Arg.Any<Label>());
Expand Down

0 comments on commit f109707

Please sign in to comment.