Repository navigation
Expand file tree
/
Copy pathLintClientTests.cs
More file actions
155 lines (135 loc) · 4.95 KB
/
Copy pathLintClientTests.cs
File metadata and controls
155 lines (135 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Threading;
using System.Threading.Tasks;
using NGitLab.Models;
using NGitLab.Tests.Docker;
using NUnit.Framework;
namespace NGitLab.Tests;
public class LintClientTests
{
private const string ValidCIYaml = """
variables:
CI_DEBUG_TRACE: "true"
build-job:
stage: build
tags:
- Runner-Build
before_script:
- echo before start
- echo before end
script:
- echo test start
- echo test end
after_script:
- echo after start
- echo after end
when: always
allow_failure: true
""";
private const string InvalidCIYaml = """
variables:
CI_DEBUG_TRACE: "true"
build-job:
script:
- echo test
this_key_should_not_exist:
- this should fail the linting
""";
[Test]
[NGitLabRetry]
public async Task LintValidCIYaml()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;
var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), ValidCIYaml, new(), CancellationToken.None);
Assert.Multiple((Action)(() =>
{
Assert.That(result.Valid, Is.True);
Assert.That(result.Errors, Is.Empty);
Assert.That(result.Warnings, Is.Empty);
}));
}
[Test]
[NGitLabRetry]
public async Task LintValidCIYamlWithJobs()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;
var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), ValidCIYaml, new() { IncludeJobs = true }, CancellationToken.None);
Assert.That(result.Jobs, Has.Length.EqualTo(1));
var job = result.Jobs[0];
Assert.Multiple((Action)(() =>
{
Assert.That(job.Name, Is.EqualTo("build-job"));
Assert.That(job.Stage, Is.EqualTo("build"));
Assert.That(job.BeforeScript, Is.EqualTo(["echo before start", "echo before end"]));
Assert.That(job.Script, Is.EqualTo(["echo test start", "echo test end"]));
Assert.That(job.AfterScript, Is.EqualTo(["echo after start", "echo after end"]));
Assert.That(job.TagList, Is.EqualTo(["Runner-Build"]));
Assert.That(job.Environment, Is.Null);
Assert.That(job.When, Is.EqualTo("always"));
Assert.That(job.AllowFailure, Is.True);
}));
}
[Test]
[NGitLabRetry]
public async Task LintInvalidCIYaml()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;
var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), InvalidCIYaml, new(), CancellationToken.None);
Assert.Multiple((Action)(() =>
{
Assert.That(result.Valid, Is.False);
Assert.That(result.Errors, Is.Not.Empty);
Assert.That(result.Warnings, Is.Empty);
}));
}
[Test]
[NGitLabRetry]
public async Task LintValidCIProjectYaml()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;
context.Client.GetRepository(project.Id).Files.Create(new FileUpsert
{
Branch = project.DefaultBranch,
CommitMessage = "test",
Path = ".gitlab-ci.yml",
Content = ValidCIYaml,
});
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);
Assert.Multiple((Action)(() =>
{
Assert.That(result.Valid, Is.True);
Assert.That(result.Errors, Is.Empty);
Assert.That(result.Warnings, Is.Empty);
}));
}
[Test]
[NGitLabRetry]
public async Task LintInvalidProjectCIYaml()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;
context.Client.GetRepository(project.Id).Files.Create(new FileUpsert
{
Branch = project.DefaultBranch,
CommitMessage = "test",
Path = ".gitlab-ci.yml",
Content = InvalidCIYaml,
});
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);
Assert.Multiple((Action)(() =>
{
Assert.That(result.Valid, Is.False);
Assert.That(result.Errors, Is.Not.Empty);
Assert.That(result.Warnings, Is.Empty);
}));
}
}