Skip to content

Commit 712b75b

Browse files
committed
Embeded the test files into project and make test self-contain
1 parent 6af46af commit 712b75b

16 files changed

+283
-188
lines changed

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
<AssemblyOriginatorKeyFile>..\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
2626
</PropertyGroup>
2727

28-
<ItemGroup>
29-
<Compile Remove="ReferenceServices\OpenApiReferenceServiceBase.cs" />
30-
</ItemGroup>
31-
3228
<ItemGroup>
3329
<PackageReference Include="SharpYaml" Version="1.6.1" />
3430
</ItemGroup>

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 169 additions & 129 deletions
Large diffs are not rendered by default.

test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.OpenApi.Readers.Tests.ReferenceService
1515
[Collection("DefaultSettings")]
1616
public class TryLoadReferenceV2Tests
1717
{
18-
private const string SampleFolderPath = "ReferenceService/Samples";
18+
private const string SampleFolderPath = "ReferenceService/Samples/";
1919

2020
[Fact]
2121
public void LoadSchemaReference()
@@ -25,7 +25,7 @@ public void LoadSchemaReference()
2525
var diagnostic = new OpenApiDiagnostic();
2626
RootNode rootNode;
2727

28-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
28+
using (var stream = Resources.GetStream(SampleFolderPath + "multipleReferences.v2.yaml"))
2929
{
3030
var yamlStream = new YamlStream();
3131
yamlStream.Load(new StreamReader(stream));
@@ -82,7 +82,7 @@ public void LoadParameterReference()
8282
var diagnostic = new OpenApiDiagnostic();
8383
RootNode rootNode;
8484

85-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
85+
using (var stream = Resources.GetStream(SampleFolderPath + "multipleReferences.v2.yaml"))
8686
{
8787
var yamlStream = new YamlStream();
8888
yamlStream.Load(new StreamReader(stream));
@@ -127,7 +127,7 @@ public void LoadSecuritySchemeReference()
127127
var diagnostic = new OpenApiDiagnostic();
128128
RootNode rootNode;
129129

130-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
130+
using (var stream = Resources.GetStream(SampleFolderPath + "multipleReferences.v2.yaml"))
131131
{
132132
var yamlStream = new YamlStream();
133133
yamlStream.Load(new StreamReader(stream));
@@ -165,7 +165,7 @@ public void LoadResponseReference()
165165
var diagnostic = new OpenApiDiagnostic();
166166
RootNode rootNode;
167167

168-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
168+
using (var stream = Resources.GetStream(SampleFolderPath + "multipleReferences.v2.yaml"))
169169
{
170170
var yamlStream = new YamlStream();
171171
yamlStream.Load(new StreamReader(stream));
@@ -202,7 +202,7 @@ public void LoadResponseAndSchemaReference()
202202
var diagnostic = new OpenApiDiagnostic();
203203
RootNode rootNode;
204204

205-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
205+
using (var stream = Resources.GetStream(SampleFolderPath + "multipleReferences.v2.yaml"))
206206
{
207207
var yamlStream = new YamlStream();
208208
yamlStream.Load(new StreamReader(stream));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.IO;
5+
6+
namespace Microsoft.OpenApi.Readers.Tests
7+
{
8+
internal static class Resources
9+
{
10+
/// <summary>
11+
/// Get the file contents.
12+
/// </summary>
13+
/// <param name="fileName">The file name with relative path. "/V3Tests/Samples/OpenApiCallback/..</param>
14+
/// <returns>The file contents.</returns>
15+
public static string GetString(string fileName)
16+
{
17+
using (Stream stream = GetStream(fileName))
18+
using (TextReader reader = new StreamReader(stream))
19+
{
20+
return reader.ReadToEnd();
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Get the file stream.
26+
/// </summary>
27+
/// <param name="fileName">The file name with relative path. "/V3Tests/Samples/OpenApiCallback/..</param>
28+
/// <returns>The file stream.</returns>
29+
public static Stream GetStream(string fileName)
30+
{
31+
string path = GetPath(fileName);
32+
Stream stream = typeof(Resources).Assembly.GetManifestResourceStream(path);
33+
34+
if (stream == null)
35+
{
36+
string message = Error.Format("The embedded resource '{0}' was not found.", path);
37+
throw new FileNotFoundException(message, path);
38+
}
39+
40+
return stream;
41+
}
42+
43+
private static string GetPath(string fileName)
44+
{
45+
const string projectDefaultNamespace = "Microsoft.OpenApi.Readers.Tests";
46+
const string resourcesFolderName = "Resources";
47+
const string pathSeparator = ".";
48+
49+
if (fileName.Contains("/"))
50+
{
51+
string temp = fileName.Replace('/', '.');
52+
return projectDefaultNamespace + pathSeparator + temp;
53+
}
54+
55+
return projectDefaultNamespace + pathSeparator + resourcesFolderName + pathSeparator + fileName;
56+
}
57+
}
58+
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class ComparisonTests
1818
//[InlineData("definitions")] //Currently broken due to V3 references not behaving the same as V2
1919
public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string fileName)
2020
{
21-
using (var streamV2 = File.OpenRead(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
22-
using (var streamV3 = File.OpenRead(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")))
21+
using (var streamV2 = Resources.GetStream(SampleFolderPath + $"{fileName}.v2.yaml"))
22+
using (var streamV3 = Resources.GetStream(SampleFolderPath + $"{fileName}.v3.yaml"))
2323
{
2424
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var contextV2);
2525
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var contextV3);

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ namespace Microsoft.OpenApi.Readers.Tests.V2Tests
1313
[Collection("DefaultSettings")]
1414
public class OpenApiParameterTests
1515
{
16-
private const string SampleFolderPath = "V2Tests/Samples/OpenApiParameter";
16+
private const string SampleFolderPath = "V2Tests/Samples/OpenApiParameter/";
1717

1818
[Fact]
1919
public void ParseBodyParameterShouldSucceed()
2020
{
2121
// Arrange
2222
MapNode node;
23-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "bodyParameter.yaml")))
23+
using (var stream = Resources.GetStream(SampleFolderPath + "bodyParameter.yaml"))
2424
{
2525
node = TestHelper.CreateYamlMapNode(stream);
2626
}
@@ -39,7 +39,7 @@ public void ParsePathParameterShouldSucceed()
3939
{
4040
// Arrange
4141
MapNode node;
42-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "pathParameter.yaml")))
42+
using (var stream = Resources.GetStream(SampleFolderPath + "pathParameter.yaml"))
4343
{
4444
node = TestHelper.CreateYamlMapNode(stream);
4545
}
@@ -67,7 +67,7 @@ public void ParseQueryParameterShouldSucceed()
6767
{
6868
// Arrange
6969
MapNode node;
70-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "queryParameter.yaml")))
70+
using (var stream = Resources.GetStream(SampleFolderPath + "queryParameter.yaml"))
7171
{
7272
node = TestHelper.CreateYamlMapNode(stream);
7373
}
@@ -101,7 +101,7 @@ public void ParseFormDataParameterShouldSucceed()
101101
{
102102
// Arrange
103103
MapNode node;
104-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "formDataParameter.yaml")))
104+
using (var stream = Resources.GetStream(SampleFolderPath + "formDataParameter.yaml"))
105105
{
106106
node = TestHelper.CreateYamlMapNode(stream);
107107
}
@@ -120,7 +120,7 @@ public void ParseHeaderParameterShouldSucceed()
120120
{
121121
// Arrange
122122
MapNode node;
123-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "headerParameter.yaml")))
123+
using (var stream = Resources.GetStream(SampleFolderPath + "headerParameter.yaml"))
124124
{
125125
node = TestHelper.CreateYamlMapNode(stream);
126126
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSecuritySchemeTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V2Tests
1616
[Collection("DefaultSettings")]
1717
public class OpenApiSecuritySchemeTests
1818
{
19-
private const string SampleFolderPath = "V2Tests/Samples/OpenApiSecurityScheme";
19+
private const string SampleFolderPath = "V2Tests/Samples/OpenApiSecurityScheme/";
2020

2121
[Fact]
2222
public void ParseHttpSecuritySchemeShouldSucceed()
2323
{
24-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicSecurityScheme.yaml")))
24+
using (var stream = Resources.GetStream(SampleFolderPath + "basicSecurityScheme.yaml"))
2525
{
2626
var yamlStream = new YamlStream();
2727
yamlStream.Load(new StreamReader(stream));
@@ -48,7 +48,7 @@ public void ParseHttpSecuritySchemeShouldSucceed()
4848
[Fact]
4949
public void ParseApiKeySecuritySchemeShouldSucceed()
5050
{
51-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml")))
51+
using (var stream = Resources.GetStream(SampleFolderPath + "apiKeySecurityScheme.yaml"))
5252
{
5353
var yamlStream = new YamlStream();
5454
yamlStream.Load(new StreamReader(stream));
@@ -76,7 +76,7 @@ public void ParseApiKeySecuritySchemeShouldSucceed()
7676
[Fact]
7777
public void ParseOAuth2ImplicitSecuritySchemeShouldSucceed()
7878
{
79-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2ImplicitSecurityScheme.yaml")))
79+
using (var stream = Resources.GetStream(SampleFolderPath + "oauth2ImplicitSecurityScheme.yaml"))
8080
{
8181
var yamlStream = new YamlStream();
8282
yamlStream.Load(new StreamReader(stream));
@@ -114,7 +114,7 @@ public void ParseOAuth2ImplicitSecuritySchemeShouldSucceed()
114114
[Fact]
115115
public void ParseOAuth2PasswordSecuritySchemeShouldSucceed()
116116
{
117-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2PasswordSecurityScheme.yaml")))
117+
using (var stream = Resources.GetStream(SampleFolderPath + "oauth2PasswordSecurityScheme.yaml"))
118118
{
119119
var yamlStream = new YamlStream();
120120
yamlStream.Load(new StreamReader(stream));
@@ -152,7 +152,7 @@ public void ParseOAuth2PasswordSecuritySchemeShouldSucceed()
152152
[Fact]
153153
public void ParseOAuth2ApplicationSecuritySchemeShouldSucceed()
154154
{
155-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2ApplicationSecurityScheme.yaml")))
155+
using (var stream = Resources.GetStream(SampleFolderPath + "oauth2ApplicationSecurityScheme.yaml"))
156156
{
157157
var yamlStream = new YamlStream();
158158
yamlStream.Load(new StreamReader(stream));
@@ -190,7 +190,7 @@ public void ParseOAuth2ApplicationSecuritySchemeShouldSucceed()
190190
[Fact]
191191
public void ParseOAuth2AccessCodeSecuritySchemeShouldSucceed()
192192
{
193-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2AccessCodeSecurityScheme.yaml")))
193+
using (var stream = Resources.GetStream(SampleFolderPath + "oauth2AccessCodeSecurityScheme.yaml"))
194194
{
195195
var yamlStream = new YamlStream();
196196
yamlStream.Load(new StreamReader(stream));

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1616
[Collection("DefaultSettings")]
1717
public class OpenApiCallbackTests
1818
{
19-
private const string SampleFolderPath = "V3Tests/Samples/OpenApiCallback";
19+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiCallback/";
2020

2121
[Fact]
2222
public void ParseBasicCallbackShouldSucceed()
2323
{
24-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicCallback.yaml")))
24+
using (var stream = Resources.GetStream(SampleFolderPath + "basicCallback.yaml"))
2525
{
26+
// Arrange
2627
var yamlStream = new YamlStream();
2728
yamlStream.Load(new StreamReader(stream));
2829
var yamlNode = yamlStream.Documents.First().RootNode;
@@ -76,7 +77,7 @@ public void ParseBasicCallbackShouldSucceed()
7677
[Fact]
7778
public void ParseAdvancedCallbackWithReferenceShouldSucceed()
7879
{
79-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
80+
using (var stream = Resources.GetStream(SampleFolderPath + "advancedCallbackWithReference.yaml"))
8081
{
8182
// Act
8283
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1515
[Collection("DefaultSettings")]
1616
public class OpenApiDiscriminatorTests
1717
{
18-
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator";
18+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator/";
1919

2020
[Fact]
2121
public void ParseBasicDiscriminatorShouldSucceed()
2222
{
23-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")))
23+
using (var stream = Resources.GetStream(SampleFolderPath + "basicDiscriminator.yaml"))
2424
{
2525
var yamlStream = new YamlStream();
2626
yamlStream.Load(new StreamReader(stream));

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void ParseDocumentFromInlineStringShouldSucceed()
5353
[Fact]
5454
public void ParseBasicDocumentWithMultipleServersShouldSucceed()
5555
{
56-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml")))
56+
using (var stream = Resources.GetStream(SampleFolderPath + "basicDocumentWithMultipleServers.yaml"))
5757
{
5858
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5959

@@ -87,7 +87,7 @@ public void ParseBasicDocumentWithMultipleServersShouldSucceed()
8787
[Fact]
8888
public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
8989
{
90-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")))
90+
using (var stream = Resources.GetStream(SampleFolderPath + "brokenMinimalDocument.yaml"))
9191
{
9292
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
9393

@@ -114,7 +114,7 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
114114
[Fact]
115115
public void ParseMinimalDocumentShouldSucceed()
116116
{
117-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
117+
using (var stream = Resources.GetStream(SampleFolderPath + "minimalDocument.yaml"))
118118
{
119119
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
120120

@@ -137,7 +137,7 @@ public void ParseMinimalDocumentShouldSucceed()
137137
public void ParseStandardPetStoreDocumentShouldSucceed()
138138
{
139139
OpenApiDiagnostic context;
140-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStore.yaml")))
140+
using (var stream = Resources.GetStream(SampleFolderPath + "petStore.yaml"))
141141
{
142142
var actual = new OpenApiStreamReader().Read(stream, out context);
143143

@@ -548,7 +548,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
548548
public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
549549
{
550550
OpenApiDiagnostic context;
551-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity.yaml")))
551+
using (var stream = Resources.GetStream(SampleFolderPath + "petStoreWithTagAndSecurity.yaml"))
552552
{
553553
var actual = new OpenApiStreamReader().Read(stream, out context);
554554

@@ -1050,7 +1050,7 @@ public void ParsePetStoreExpandedShouldSucceed()
10501050
{
10511051
OpenApiDiagnostic context;
10521052

1053-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")))
1053+
using (var stream = Resources.GetStream(SampleFolderPath + "petStoreExpanded.yaml"))
10541054
{
10551055
var actual = new OpenApiStreamReader().Read(stream, out context);
10561056

0 commit comments

Comments
 (0)