From d5fc9782aa4bcdec3cde4c2f0d5683d4e2b5fb6b Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:04:36 -0800 Subject: [PATCH] Support non-kebab-case directories for cadl-ranch scenarios (#5286) Fixes https://github.com/microsoft/typespec/issues/5287 Follow up to https://github.com/microsoft/typespec/pull/5273/. The directories defined in cadl-ranch spec for the versioning scenarios do not use kebab-case which led to our test attribute skipping the tests. Also adds one missing scenario for versioning. --- .../Removed/V1/VersioningRemovedV1Tests.cs | 12 ++++++++++++ .../Infrastructure/CadlRanchTestAttribute.cs | 10 ++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs index 3fdf09cc9e..d96e2a4292 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs @@ -3,8 +3,10 @@ using System; using System.Linq; +using System.Threading.Tasks; using NUnit.Framework; using Versioning.Removed.V1; +using Versioning.Removed.V1.Models; namespace TestProjects.CadlRanch.Tests.Http.Versioning.Removed.V1 { @@ -39,5 +41,15 @@ public void TestRemovedMembers() var enumType = typeof(RemovedClientOptions.ServiceVersion); Assert.AreEqual(new string[] { "V1" }, enumType.GetEnumNames()); } + + [CadlRanchTest] + public Task Versioning_Removed_V3Model() => Test(async (host) => + { + var model = new ModelV3("123", EnumV3.EnumMemberV1); + var response = await new RemovedClient(host).ModelV3Async(model); + Assert.AreEqual(200, response.GetRawResponse().Status); + Assert.AreEqual("123", response.Value.Id); + Assert.AreEqual(EnumV3.EnumMemberV1, response.Value.EnumProp); + }); } } diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Infrastructure/CadlRanchTestAttribute.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Infrastructure/CadlRanchTestAttribute.cs index 32688b3275..8e4e8f2729 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Infrastructure/CadlRanchTestAttribute.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Infrastructure/CadlRanchTestAttribute.cs @@ -24,6 +24,12 @@ internal partial class CadlRanchTestAttribute : TestAttribute, IApplyToTest { string clientCodeDirectory = GetGeneratedDirectory(test); + if (!Directory.Exists(clientCodeDirectory)) + { + // Not all cadl-ranch scenarios use kebab-case directories, so try again without kebab-case. + clientCodeDirectory = GetGeneratedDirectory(test, false); + } + var clientCsFile = GetClientCsFile(clientCodeDirectory); TestContext.Progress.WriteLine($"Checking if '{clientCsFile}' is a stubbed implementation."); @@ -69,14 +75,14 @@ private static void SkipTest(Test test) .FirstOrDefault(); } - private static string GetGeneratedDirectory(Test test) + private static string GetGeneratedDirectory(Test test, bool kebabCaseDirectories = true) { var namespaceParts = test.FullName.Split('.').Skip(3); namespaceParts = namespaceParts.Take(namespaceParts.Count() - 2); var clientCodeDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "..", "..", "..", "..", "..", "TestProjects", "CadlRanch"); foreach (var part in namespaceParts) { - clientCodeDirectory = Path.Combine(clientCodeDirectory, FixName(part)); + clientCodeDirectory = Path.Combine(clientCodeDirectory, kebabCaseDirectories ? FixName(part) : part); } return Path.Combine(clientCodeDirectory, "src", "Generated"); }