Skip to content

Commit beb8ef7

Browse files
change the test project to reference cadl ranch source code projects instead of including their source code (microsoft#5570)
Fixes microsoft#5564 Comparing with our previous "adding new cadl ranch case" process, now we have an additional step of "adding the new cadl ranch source code project into the `generator\TestProjects\CadlRanch.Tests\TestProjects.CadlRanch.Tests.csproj` file, with an alias when needed.
1 parent b53c324 commit beb8ef7

File tree

14 files changed

+338
-90
lines changed

14 files changed

+338
-90
lines changed

packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ $azureSpecsDirectory = Join-Path $packageRoot 'node_modules' '@azure-tools' 'azu
1414
$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch'
1515
$cadlRanchRootHttp = Join-Path $cadlRanchRoot 'http'
1616
$directories = Get-ChildItem -Path "$cadlRanchRootHttp" -Directory -Recurse
17-
$cadlRanchCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch.Tests' 'TestProjects.CadlRanch.Tests.csproj'
1817

1918
$coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage'
2019

@@ -46,7 +45,7 @@ foreach ($directory in $directories) {
4645
$testFilter += "._$segment"
4746
$testPath = Join-Path $testPath "_$segment"
4847
}
49-
else{
48+
else {
5049
$testFilter += ".$segment"
5150
$testPath = Join-Path $testPath $segment
5251
}

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/ClientOperationGroup/ClientOperationGroupTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Linq;
5+
using System.Threading.Tasks;
46
using Client.Structure.Service;
57
using Client.Structure.Service.Models;
68
using NUnit.Framework;
7-
using System.Linq;
8-
using System.Threading.Tasks;
99

1010
namespace TestProjects.CadlRanch.Tests.Http.Client.Structure.ClientOperationGroup
1111
{

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/Default/DefaultTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using Client.Structure.Service.Default;
88
using Client.Structure.Service.Default.Models;
9-
using Client.Structure.Service.Renamed.Operation;
109
using NUnit.Framework;
1110

1211
namespace TestProjects.CadlRanch.Tests.Http.Client.Structure.Default

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.ClientModel;
5+
using System.Globalization;
6+
using System;
47
using System.IO;
8+
using System.Net.Http.Headers;
9+
using System.Net.Http;
10+
using System.Threading;
511
using System.Threading.Tasks;
612
using NUnit.Framework;
713
using Payload.MultiPart;
@@ -219,5 +225,185 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) =>
219225
var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(content, content.ContentType, null);
220226
Assert.AreEqual(204, response.GetRawResponse().Status);
221227
});
228+
229+
internal partial class MultiPartFormDataBinaryContent : BinaryContent
230+
{
231+
private readonly MultipartFormDataContent _multipartContent;
232+
private static readonly Random _random = new Random();
233+
private static readonly char[] _boundaryValues = "0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray();
234+
235+
public MultiPartFormDataBinaryContent()
236+
{
237+
_multipartContent = new MultipartFormDataContent(CreateBoundary());
238+
}
239+
240+
public string? ContentType
241+
{
242+
get
243+
{
244+
return _multipartContent.Headers.ContentType?.ToString();
245+
}
246+
}
247+
248+
internal HttpContent HttpContent => _multipartContent;
249+
250+
private static string CreateBoundary()
251+
{
252+
Span<char> chars = new char[70];
253+
byte[] random = new byte[70];
254+
_random.NextBytes(random);
255+
int mask = 255 >> 2;
256+
int i = 0;
257+
for (; i < 70; i++)
258+
{
259+
chars[i] = _boundaryValues[random[i] & mask];
260+
}
261+
return chars.ToString();
262+
}
263+
264+
public void Add(string content, string name, string? filename = default, string? contentType = default)
265+
{
266+
ArgumentNullException.ThrowIfNull(content, nameof(content));
267+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
268+
269+
Add(new StringContent(content), name, filename, contentType);
270+
}
271+
272+
public void Add(int content, string name, string? filename = default, string? contentType = default)
273+
{
274+
ArgumentNullException.ThrowIfNull(content, nameof(content));
275+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
276+
277+
string value = content.ToString("G", CultureInfo.InvariantCulture);
278+
Add(new StringContent(value), name, filename, contentType);
279+
}
280+
281+
public void Add(long content, string name, string? filename = default, string? contentType = default)
282+
{
283+
ArgumentNullException.ThrowIfNull(content, nameof(content));
284+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
285+
286+
string value = content.ToString("G", CultureInfo.InvariantCulture);
287+
Add(new StringContent(value), name, filename, contentType);
288+
}
289+
290+
public void Add(float content, string name, string? filename = default, string? contentType = default)
291+
{
292+
ArgumentNullException.ThrowIfNull(content, nameof(content));
293+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
294+
295+
string value = content.ToString("G", CultureInfo.InvariantCulture);
296+
Add(new StringContent(value), name, filename, contentType);
297+
}
298+
299+
public void Add(double content, string name, string? filename = default, string? contentType = default)
300+
{
301+
ArgumentNullException.ThrowIfNull(content, nameof(content));
302+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
303+
304+
string value = content.ToString("G", CultureInfo.InvariantCulture);
305+
Add(new StringContent(value), name, filename, contentType);
306+
}
307+
308+
public void Add(decimal content, string name, string? filename = default, string? contentType = default)
309+
{
310+
ArgumentNullException.ThrowIfNull(content, nameof(content));
311+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
312+
313+
string value = content.ToString("G", CultureInfo.InvariantCulture);
314+
Add(new StringContent(value), name, filename, contentType);
315+
}
316+
317+
public void Add(bool content, string name, string? filename = default, string? contentType = default)
318+
{
319+
ArgumentNullException.ThrowIfNull(content, nameof(content));
320+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
321+
322+
string value = content ? "true" : "false";
323+
Add(new StringContent(value), name, filename, contentType);
324+
}
325+
326+
public void Add(Stream content, string name, string? filename = default, string? contentType = default)
327+
{
328+
ArgumentNullException.ThrowIfNull(content, nameof(content));
329+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
330+
331+
Add(new StreamContent(content), name, filename, contentType);
332+
}
333+
334+
public void Add(byte[] content, string name, string? filename = default, string? contentType = default)
335+
{
336+
ArgumentNullException.ThrowIfNull(content, nameof(content));
337+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
338+
339+
Add(new ByteArrayContent(content), name, filename, contentType);
340+
}
341+
342+
public void Add(BinaryData content, string name, string? filename = default, string? contentType = default)
343+
{
344+
ArgumentNullException.ThrowIfNull(content, nameof(content));
345+
ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name));
346+
347+
Add(new ByteArrayContent(content.ToArray()), name, filename, contentType);
348+
}
349+
350+
private void Add(HttpContent content, string name, string? filename, string? contentType)
351+
{
352+
if (contentType != null)
353+
{
354+
ArgumentNullException.ThrowIfNullOrEmpty(contentType, nameof(contentType));
355+
AddContentTypeHeader(content, contentType);
356+
}
357+
if (filename != null)
358+
{
359+
ArgumentNullException.ThrowIfNullOrEmpty(filename, nameof(filename));
360+
_multipartContent.Add(content, name, filename);
361+
}
362+
else
363+
{
364+
_multipartContent.Add(content, name);
365+
}
366+
}
367+
368+
public static void AddContentTypeHeader(HttpContent content, string contentType)
369+
{
370+
MediaTypeHeaderValue header = new MediaTypeHeaderValue(contentType);
371+
content.Headers.ContentType = header;
372+
}
373+
374+
public override bool TryComputeLength(out long length)
375+
{
376+
if (_multipartContent.Headers.ContentLength is long contentLength)
377+
{
378+
length = contentLength;
379+
return true;
380+
}
381+
length = 0;
382+
return false;
383+
}
384+
385+
public override void WriteTo(Stream stream, CancellationToken cancellationToken = default)
386+
{
387+
#if NET6_0_OR_GREATER
388+
_multipartContent.CopyTo(stream, default, cancellationToken);
389+
#else
390+
_multipartContent.CopyToAsync(stream).GetAwaiter().GetResult();
391+
#endif
392+
}
393+
394+
public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default)
395+
{
396+
#if NET6_0_OR_GREATER
397+
await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);
398+
#else
399+
await _multipartContent.CopyToAsync(stream).ConfigureAwait(false);
400+
#endif
401+
}
402+
403+
public override void Dispose()
404+
{
405+
_multipartContent.Dispose();
406+
}
407+
}
222408
}
223409
}

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V1/VersioningAddedV1Tests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using NUnit.Framework;
4+
extern alias VersioningAddedV1;
5+
56
using System;
67
using System.Linq;
7-
using Versioning.Added.V1;
8-
using Versioning.Added.V1.Models;
8+
using NUnit.Framework;
9+
using VersioningAddedV1::Versioning.Added.V1;
10+
using VersioningAddedV1::Versioning.Added.V1.Models;
911

1012
namespace TestProjects.CadlRanch.Tests.Http.Versioning.Added.V1
1113
{

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V2/VersioningAddedV2Tests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using System.Threading.Tasks;
5-
using NUnit.Framework;
4+
extern alias VersioningAddedV2;
5+
66
using System;
77
using System.Linq;
8-
using Versioning.Added.V2;
9-
using Versioning.Added.V2.Models;
8+
using System.Threading.Tasks;
9+
using NUnit.Framework;
10+
using VersioningAddedV2::Versioning.Added.V2;
11+
using VersioningAddedV2::Versioning.Added.V2.Models;
1012

1113
namespace TestProjects.CadlRanch.Tests.Http.Versioning.Added.V2
1214
{
@@ -31,10 +33,12 @@ public void TestAddedMembersV2Client()
3133
Assert.IsTrue(enumValues.Contains("EnumMemberV2"));
3234

3335
/* check existence of the added model ModelV2. */
34-
Assert.IsNotNull(Type.GetType("Versioning.Added.V2.Models.ModelV2"));
36+
var modelV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.Models.ModelV2");
37+
Assert.IsNotNull(modelV2Type);
3538

3639
/* check existence of the added enum EnumV2. */
37-
Assert.IsNotNull(Type.GetType("Versioning.Added.V2.Models.EnumV2"));
40+
var enumV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.Models.EnumV2");
41+
Assert.IsNotNull(enumV2Type);
3842

3943
/* check the added parameter. */
4044
var methods = typeof(AddedClient).GetMethods().Where(m => m.Name == "V1" || m.Name == "V1Async");
@@ -52,7 +56,8 @@ public void TestAddedMembersV2Client()
5256
Assert.AreEqual(4, addedMethods.Count());
5357

5458
/* check the existence of added interface in V2. */
55-
Assert.IsNotNull(Type.GetType("Versioning.Added.V2.InterfaceV2"));
59+
var interfaceV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.InterfaceV2");
60+
Assert.IsNotNull(interfaceV2Type);
5661
}
5762

5863
[CadlRanchTest]

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ public class VersioningRemovedV1Tests : CadlRanchTestBase
1515
[CadlRanchTest]
1616
public void TestRemovedMembers()
1717
{
18+
var assembly = typeof(RemovedClient).Assembly;
1819
/* check existence of the removed model ModelV1. */
19-
Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.Models.ModelV1"));
20+
var modelV1Type = assembly.GetType("Versioning.Removed.V1.Models.ModelV1");
21+
Assert.IsNotNull(modelV1Type);
2022

2123
/* check existence of the removed enum EnumV1. */
22-
Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.Models.EnumV1"));
24+
var enumV1Type = assembly.GetType("Versioning.Removed.V1.Models.EnumV1");
25+
Assert.IsNotNull(enumV1Type);
2326

2427
/* check existence of removed method V1 */
2528
var removedMethods = typeof(RemovedClient).GetMethods().Where(m => m.Name == "V1" || m.Name == "V1Async");
@@ -35,7 +38,8 @@ public void TestRemovedMembers()
3538
}
3639

3740
/* check existence of removed interface. */
38-
Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.InterfaceV1"));
41+
var interfaceV1Type = assembly.GetType("Versioning.Removed.V1.InterfaceV1");
42+
Assert.IsNotNull(interfaceV1Type);
3943

4044
// Only initial versions is defined
4145
var enumType = typeof(RemovedClientOptions.ServiceVersion);

packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V1/VersioningRenamedFromV1Tests.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)