-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
GenerateSampleSpecificationTests.cs
101 lines (90 loc) · 4.46 KB
/
GenerateSampleSpecificationTests.cs
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
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
namespace NSwag.ConsoleCore.Tests
{
[UsesVerify]
public class GenerateSampleSpecificationTests
{
[Theory]
[InlineData("NSwag.Sample.NET60", "net6.0", false)]
[InlineData("NSwag.Sample.NET60Minimal", "net6.0", false)]
[InlineData("NSwag.Sample.NET70", "net7.0", false)]
[InlineData("NSwag.Sample.NET70Minimal", "net7.0", true)]
[InlineData("NSwag.Sample.NET80", "net8.0", false)]
[InlineData("NSwag.Sample.NET80Minimal", "net8.0", true)]
public async Task Should_generate_openapi_for_project(string projectName, string targetFramework, bool generatesCode)
{
// Arrange
#if DEBUG
const string configuration = "debug";
#else
const string configuration = "release";
#endif
var executablePath = Path.GetFullPath($"../../../../artifacts/bin/NSwag.ConsoleCore/{configuration}_{targetFramework}/dotnet-nswag.dll");
var nswagJsonPath = Path.GetFullPath($"../../../../src/{projectName}/nswag.json");
var openApiJsonPath = Path.GetFullPath($"../../../../src/{projectName}/openapi.json");
var generatedClientsCsPath = Path.GetFullPath($"../../../../src/{projectName}/GeneratedClientsCs.gen");
var generatedClientsTsPath = Path.GetFullPath($"../../../../src/{projectName}/GeneratedClientsTs.gen");
var generatedControllersCsPath = Path.GetFullPath($"../../../../src/{projectName}/GeneratedControllersCs.gen");
File.Delete(openApiJsonPath);
File.Delete(generatedClientsTsPath);
File.Delete(generatedClientsCsPath);
File.Delete(generatedControllersCsPath);
Assert.False(File.Exists(openApiJsonPath));
// Act
var process = Process.Start(new ProcessStartInfo
{
FileName = "dotnet",
Arguments = executablePath + " run " + nswagJsonPath,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
Environment =
{
{ "NSWAG_NOVERSION", "true" }
}
});
try
{
process.WaitForExit(20000);
}
finally
{
process.Kill();
}
// Assert
if (process.ExitCode != 0)
{
var output = await process.StandardOutput.ReadToEndAsync();
var error = await process.StandardError.ReadToEndAsync();
Assert.Fail(output + error);
}
var json = await File.ReadAllTextAsync(openApiJsonPath);
await Verifier.Verify(json).UseParameters(projectName, targetFramework, generatesCode);
if (generatesCode)
{
await CheckTypeScriptAsync(projectName, targetFramework, generatesCode, generatedClientsTsPath);
await CheckCSharpClientsAsync(projectName, targetFramework, generatesCode, generatedClientsCsPath);
await CheckCSharpControllersAsync(projectName, targetFramework, generatesCode, generatedControllersCsPath);
}
}
private static async Task CheckCSharpControllersAsync(string projectName, string targetFramework, bool generatesCode, string generatedControllersCsPath)
{
var code = await File.ReadAllTextAsync(generatedControllersCsPath);
await Verifier.Verify(code).UseMethodName(nameof(CheckCSharpControllersAsync)).UseParameters(projectName, targetFramework, generatesCode);
}
private static async Task CheckCSharpClientsAsync(string projectName, string targetFramework, bool generatesCode, string generatedClientsCsPath)
{
var code = await File.ReadAllTextAsync(generatedClientsCsPath);
await Verifier.Verify(code).UseMethodName(nameof(CheckCSharpClientsAsync)).UseParameters(projectName, targetFramework, generatesCode);
}
private static async Task CheckTypeScriptAsync(string projectName, string targetFramework, bool generatesCode, string generatedClientsTsPath)
{
var code = await File.ReadAllTextAsync(generatedClientsTsPath);
await Verifier.Verify(code).UseMethodName(nameof(CheckTypeScriptAsync)).UseParameters(projectName, targetFramework, generatesCode);
}
}
}