Skip to content

Commit a4f9a83

Browse files
louis-zToa741
andauthored
Fix some analyzer issues (#753)
Co-authored-by: Thomas Cortes <78750681+Toa741@users.noreply.github.com>
1 parent 6890d21 commit a4f9a83

19 files changed

+44
-51
lines changed

NGitLab.Mock/Clients/PipelineClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public IEnumerable<Bridge> GetBridges(PipelineBridgeQuery query)
135135
{
136136
var project = Context.Server.AllProjects.FindById(_projectId);
137137
var bridges = project.Jobs.Where(j => j.Pipeline.Id == query.PipelineId && j.DownstreamPipeline != null).Select(j => j.ToBridgeClient());
138-
return (query.Scope == null || !query.Scope.Any()) ? bridges : bridges.Where(j => query.Scope.Contains(j.Status.ToString(), StringComparer.Ordinal));
138+
return (query.Scope == null || query.Scope.Length == 0) ? bridges : bridges.Where(j => query.Scope.Contains(j.Status.ToString(), StringComparer.Ordinal));
139139
}
140140
}
141141

@@ -152,7 +152,7 @@ public Models.Job[] GetJobs(int pipelineId)
152152
using (Context.BeginOperationScope())
153153
{
154154
var jobs = _jobClient.GetJobs(JobScopeMask.All).Where(j => j.Pipeline.Id == query.PipelineId);
155-
return (query.Scope == null || !query.Scope.Any()) ? jobs : jobs.Where(j => query.Scope.Contains(j.Status.ToString(), StringComparer.Ordinal));
155+
return (query.Scope == null || query.Scope.Length == 0) ? jobs : jobs.Where(j => query.Scope.Contains(j.Status.ToString(), StringComparer.Ordinal));
156156
}
157157
}
158158

NGitLab.Mock/Internals/TemporaryDirectory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void MakeReadOnly()
6060
MakeReadOnly(new DirectoryInfo(FullPath));
6161
}
6262

63-
private void MakeReadOnly(FileSystemInfo fileSystemInfo)
63+
private static void MakeReadOnly(FileSystemInfo fileSystemInfo)
6464
{
6565
if (fileSystemInfo is DirectoryInfo directoryInfo)
6666
{

NGitLab.Mock/UserCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal IEnumerable<User> Get(UserQuery query)
108108

109109
if (query.ExcludeExternal == true)
110110
{
111-
users = users.Where(u => !u.Identities.Any() || u.Identities.All(i => string.IsNullOrEmpty(i.ExternUid)));
111+
users = users.Where(u => u.Identities.Length == 0 || u.Identities.All(i => string.IsNullOrEmpty(i.ExternUid)));
112112
}
113113

114114
if (!string.IsNullOrEmpty(query.Search))

NGitLab.Tests/Docker/GitLabTestContext.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class GitLabTestContext : IDisposable
2525
private static readonly Policy s_gitlabRetryPolicy = Policy.Handle<GitLabException>()
2626
.WaitAndRetry(MaxRetryCount, _ => TimeSpan.FromSeconds(1), (exception, timespan, retryCount, context) =>
2727
{
28-
TestContext.WriteLine($"[{TestContext.CurrentContext.Test.FullName}] {exception.Message} -> Polly Retry {retryCount} of {MaxRetryCount}...");
28+
TestContext.Out.WriteLine($"[{TestContext.CurrentContext.Test.FullName}] {exception.Message} -> Polly Retry {retryCount} of {MaxRetryCount}...");
2929
});
3030

3131
private static readonly HashSet<string> s_generatedValues = new(StringComparer.Ordinal);
@@ -381,15 +381,15 @@ public async Task<IDisposable> StartRunnerForOneJobAsync(int projectId)
381381
}
382382
}
383383

384-
TestContext.WriteLine("Test runner downloaded");
384+
TestContext.Out.WriteLine("Test runner downloaded");
385385
if (OperatingSystem.IsLinux())
386386
{
387387
using var chmodProcess = Process.Start("chmod", "+x \"" + path + "\"");
388388
chmodProcess.WaitForExit();
389389
if (chmodProcess.ExitCode != 0)
390390
throw new InvalidOperationException("chmod failed");
391391

392-
TestContext.WriteLine("chmod run");
392+
TestContext.Out.WriteLine("chmod run");
393393
}
394394

395395
if (!IsContinuousIntegration())
@@ -401,7 +401,7 @@ public async Task<IDisposable> StartRunnerForOneJobAsync(int projectId)
401401
if (gitConfigProcess.ExitCode != 0)
402402
throw new InvalidOperationException("git config failed");
403403

404-
TestContext.WriteLine("git config changed");
404+
TestContext.Out.WriteLine("git config changed");
405405
}
406406

407407
var project = AdminClient.Projects[projectId];
@@ -412,7 +412,7 @@ public async Task<IDisposable> StartRunnerForOneJobAsync(int projectId)
412412
if (runner.Token == null)
413413
throw new InvalidOperationException("Runner token is null");
414414

415-
TestContext.WriteLine($"Runner registered '{runner.Token}'");
415+
TestContext.Out.WriteLine($"Runner registered '{runner.Token}'");
416416

417417
// Use run-single, so we don't need to manage the configuration file.
418418
// Also, I don't think there is a need to run multiple jobs in a test
@@ -437,9 +437,7 @@ public async Task<IDisposable> StartRunnerForOneJobAsync(int projectId)
437437
RedirectStandardError = true,
438438
RedirectStandardOutput = true,
439439
};
440-
var process = Process.Start(psi);
441-
if (process == null)
442-
throw new InvalidOperationException("Cannot start the runner");
440+
var process = Process.Start(psi) ?? throw new InvalidOperationException("Cannot start the runner");
443441

444442
// Give some time to ensure the runner doesn't stop immediately
445443
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
@@ -452,7 +450,7 @@ public async Task<IDisposable> StartRunnerForOneJobAsync(int projectId)
452450
process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
453451
process.OutputDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
454452

455-
TestContext.WriteLine($"Runner started for project '{project.Id}' on '{DockerContainer.GitLabUrl}'");
453+
TestContext.Out.WriteLine($"Runner started for project '{project.Id}' on '{DockerContainer.GitLabUrl}'");
456454
return new ProcessKill(process);
457455
}
458456
finally
@@ -474,7 +472,7 @@ public static async Task<T> RetryUntilAsync<T>(Func<T> action, Func<T, bool> pre
474472
while (!predicate(result))
475473
{
476474
cancellationToken.ThrowIfCancellationRequested();
477-
TestContext.WriteLine($"[{TestContext.CurrentContext.Test.FullName}] RetryUntilAsync {retryCount++}...");
475+
TestContext.Out.WriteLine($"[{TestContext.CurrentContext.Test.FullName}] RetryUntilAsync {retryCount++}...");
478476
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
479477

480478
result = action();

NGitLab.Tests/Docker/GitLabTestContextRequestOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private WebResponse LogRequest(HttpWebRequest request, WebResponse response)
174174
}
175175

176176
var logs = sb.ToString();
177-
TestContext.WriteLine(new string('-', 100) + "\nGitLab request: " + logs);
177+
TestContext.Out.WriteLine(new string('-', 100) + "\nGitLab request: " + logs);
178178
return response;
179179
}
180180

NGitLab.Tests/JobTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public async Task Test_get_job_trace()
170170
var job = jobs.FirstOrDefault();
171171
if (jobs.Any())
172172
{
173-
TestContext.WriteLine("Job status: " + job.Status);
173+
TestContext.Out.WriteLine("Job status: " + job.Status);
174174
return job.Status == JobStatus.Success || job.Status == JobStatus.Failed;
175175
}
176176

NGitLab.Tests/LintClientTests.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Linq;
2-
using System.Threading;
1+
using System.Threading;
32
using System.Threading.Tasks;
43
using NGitLab.Models;
54
using NGitLab.Tests.Docker;
@@ -38,8 +37,8 @@ public async Task LintValidCIYaml()
3837
var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), ValidCIYaml, new(), CancellationToken.None);
3938

4039
Assert.That(result.Valid, Is.True);
41-
Assert.That(result.Errors.Any(), Is.False);
42-
Assert.That(result.Warnings.Any(), Is.False);
40+
Assert.That(result.Errors.Length != 0, Is.False);
41+
Assert.That(result.Warnings.Length != 0, Is.False);
4342
}
4443

4544
[Test]
@@ -53,8 +52,8 @@ public async Task LintInvalidCIYaml()
5352
var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), InvalidCIYaml, new(), CancellationToken.None);
5453

5554
Assert.That(result.Valid, Is.False);
56-
Assert.That(result.Errors.Any(), Is.True);
57-
Assert.That(result.Warnings.Any(), Is.False);
55+
Assert.That(result.Errors.Length != 0, Is.True);
56+
Assert.That(result.Warnings.Length != 0, Is.False);
5857
}
5958

6059
[Test]
@@ -76,8 +75,8 @@ public async Task LintValidCIProjectYaml()
7675
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);
7776

7877
Assert.That(result.Valid, Is.True);
79-
Assert.That(result.Errors.Any(), Is.False);
80-
Assert.That(result.Warnings.Any(), Is.False);
78+
Assert.That(result.Errors.Length != 0, Is.False);
79+
Assert.That(result.Warnings.Length != 0, Is.False);
8180
}
8281

8382
[Test]
@@ -99,7 +98,7 @@ public async Task LintInvalidProjectCIYaml()
9998
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);
10099

101100
Assert.That(result.Valid, Is.False);
102-
Assert.That(result.Errors.Any(), Is.True);
103-
Assert.That(result.Warnings.Any(), Is.False);
101+
Assert.That(result.Errors.Length != 0, Is.True);
102+
Assert.That(result.Warnings.Length != 0, Is.False);
104103
}
105104
}

NGitLab.Tests/MergeRequest/MergeRequestChangesClientTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using System.Threading.Tasks;
43
using NGitLab.Tests.Docker;
54
using NUnit.Framework;
@@ -19,7 +18,7 @@ public async Task GetChangesOnMergeRequest()
1918

2019
var changes = await GitLabTestContext.RetryUntilAsync(
2120
() => mergeRequestChanges.MergeRequestChange.Changes,
22-
changes => changes.Any(),
21+
changes => changes.Length != 0,
2322
TimeSpan.FromSeconds(10));
2423

2524
Assert.That(changes, Has.Length.EqualTo(1));

NGitLab.Tests/MergeRequest/MergeRequestClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ await GitLabTestContext.RetryUntilAsync(
3636

3737
Assert.That(context.Client.GetRepository(project.Id).Branches[mergeRequest.SourceBranch].Protected, Is.False, "The source branch is protected but should not be");
3838

39-
TestContext.WriteLine("MR is ready to be merged");
39+
TestContext.Out.WriteLine("MR is ready to be merged");
4040
AcceptMergeRequest(mergeRequestClient, mergeRequest);
41-
TestContext.WriteLine("MR is merged");
41+
TestContext.Out.WriteLine("MR is merged");
4242

4343
// Since GitLab 13.10, this part is flaky
4444
// await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);

NGitLab.Tests/PipelineTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public async Task Test_can_list_all_jobs_from_project()
7171
var pipelineClient = context.Client.GetPipelines(project.Id);
7272
JobTests.AddGitLabCiFile(context.Client, project);
7373

74-
var allJobs = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.AllJobs.ToList(), p => p.Any(), TimeSpan.FromSeconds(120));
75-
Assert.That(allJobs.Any());
74+
var allJobs = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.AllJobs.ToList(), p => p.Count != 0, TimeSpan.FromSeconds(120));
75+
Assert.That(allJobs.Count != 0);
7676
}
7777

7878
[Test]
@@ -91,8 +91,8 @@ public async Task Test_can_list_jobs_from_pipeline()
9191
{
9292
PipelineId = pipeline.Id, Scope = new[] { "success", "pending" },
9393
};
94-
var allJobs = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.GetJobsAsync(pipelineJobQuery).ToList(), p => p.Any(), TimeSpan.FromSeconds(120));
95-
Assert.That(allJobs.Any());
94+
var allJobs = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.GetJobsAsync(pipelineJobQuery).ToList(), p => p.Count != 0, TimeSpan.FromSeconds(120));
95+
Assert.That(allJobs.Count != 0);
9696
}
9797

9898
[Test]
@@ -108,9 +108,9 @@ public async Task Test_search_for_pipeline()
108108
{
109109
Ref = project.DefaultBranch,
110110
};
111-
var pipelinesFromQuery = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.Search(query).ToList(), p => p.Any(), TimeSpan.FromSeconds(120));
111+
var pipelinesFromQuery = await GitLabTestContext.RetryUntilAsync(() => pipelineClient.Search(query).ToList(), p => p.Count != 0, TimeSpan.FromSeconds(120));
112112

113-
Assert.That(pipelinesFromQuery.Any(), Is.True);
113+
Assert.That(pipelinesFromQuery.Count != 0, Is.True);
114114
}
115115

116116
[Test]
@@ -245,7 +245,7 @@ public async Task Test_retry()
245245
{
246246
if (pipeline != null)
247247
{
248-
TestContext.WriteLine("Pipeline status: " + pipeline.Status);
248+
TestContext.Out.WriteLine("Pipeline status: " + pipeline.Status);
249249
return pipeline.Status is JobStatus.Failed;
250250
}
251251

0 commit comments

Comments
 (0)