Skip to content

Commit ff04c40

Browse files
remove the setting for sample output folder (#5351)
* remove the setting for sample output folder and make it to always be the tests project's directory * regen * update the sample directory logic * remove the unexpected generated files
1 parent 4941be2 commit ff04c40

File tree

5 files changed

+19
-46
lines changed

5 files changed

+19
-46
lines changed

src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpProj.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,7 @@ private void WriteCSProjFiles(Action<string, string> writeFile)
6969
if (Configuration.MgmtTestConfiguration is not null)
7070
{
7171
var testCSProjContent = GetTestCSProj();
72-
string testGenProjectFolder;
73-
if (Configuration.MgmtTestConfiguration.OutputFolder is { } testGenProjectOutputFolder)
74-
{
75-
testGenProjectFolder = Path.Combine(testGenProjectOutputFolder, "../");
76-
}
77-
else
78-
{
79-
testGenProjectFolder = "../";
80-
}
72+
string testGenProjectFolder = "../";
8173
Console.WriteLine(Path.Combine(testGenProjectFolder, $"{Configuration.Namespace}.Tests.csproj"));
8274
writeFile(FormatPath(Path.Combine(testGenProjectFolder, $"{Configuration.Namespace}.Tests.csproj")), testCSProjContent);
8375
}

src/AutoRest.CSharp/Mgmt/AutoRest/MgmtTarget.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,15 @@ public static async Task ExecuteAsync(GeneratedCodeWorkspace project)
251251
// write samples if enabled
252252
if (Configuration.MgmtTestConfiguration?.Sample ?? Configuration.GenerateSampleProject)
253253
{
254-
string sampleOutputFolder = GetSampleOutputFolder(SAMPLE_DEFAULT_OUTPUT_PATH);
255-
foreach (var sampleProvider in MgmtContext.Library.SampleProviders.Value)
254+
var sampleOutputFolder = GetSampleOutputFolder();
255+
if (sampleOutputFolder is not null)
256256
{
257-
var sampleWriter = new CodeWriter();
258-
new ExpressionTypeProviderWriter(sampleWriter, sampleProvider).Write();
259-
project.AddGeneratedTestFile(Path.Combine(sampleOutputFolder, $"Samples/{sampleProvider.Type.Name}.cs"), sampleWriter.ToString());
257+
foreach (var sampleProvider in MgmtContext.Library.SampleProviders.Value)
258+
{
259+
var sampleWriter = new CodeWriter();
260+
new ExpressionTypeProviderWriter(sampleWriter, sampleProvider).Write();
261+
project.AddGeneratedTestFile(Path.Combine(sampleOutputFolder, $"Samples/{sampleProvider.Type.Name}.cs"), sampleWriter.ToString());
262+
}
260263
}
261264
}
262265
}
@@ -336,25 +339,17 @@ private static void WriteSerialization(GeneratedCodeWorkspace project, TypeProvi
336339
AddGeneratedFile(project, serializationFileName, serializerCodeWriter.ToString());
337340
}
338341

339-
private const string SOURCE_DEFAULT_OUTPUT_PATH = $"/src/Generated";
340-
private const string MOCK_TEST_DEFAULT_OUTPUT_PATH = "/tests/Generated";
341-
private const string SAMPLE_DEFAULT_OUTPUT_PATH = "/samples/Generated";
342-
343-
private static string GetSampleOutputFolder(string defaultOutputPath)
342+
private static string? GetSampleOutputFolder()
344343
{
345-
if (!string.IsNullOrEmpty(Configuration.MgmtTestConfiguration?.OutputFolder))
346-
return Configuration.MgmtTestConfiguration.OutputFolder;
347-
348344
string folder = FormatPath(Configuration.OutputFolder);
349-
// if the output folder is not given explicitly, try to figure it out from general output folder if possible according to default folder structure:
350-
// Azure.ResourceManager.XXX \ src \ Generated <- default sdk source output folder
351-
// \ samples(or tests) \ Generated <- default sample output folder defined in msbuild
352-
if (folder.EndsWith(SOURCE_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase))
353-
return Path.Combine(folder, $"../../{defaultOutputPath}");
354-
else if (folder.EndsWith(SAMPLE_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase) || folder.EndsWith(MOCK_TEST_DEFAULT_OUTPUT_PATH, StringComparison.InvariantCultureIgnoreCase))
355-
return folder;
356-
else
357-
throw new InvalidOperationException("'sample-gen.output-folder' is not configured and can't figure it out from give general output-folder");
345+
// we find the `src` part and change it to tests
346+
var index = folder.IndexOf("/src/", StringComparison.InvariantCultureIgnoreCase);
347+
if (index >= 0)
348+
{
349+
return folder.Replace("/src/", "/tests/", StringComparison.InvariantCultureIgnoreCase);
350+
}
351+
// indicates we cannot figure out an output folder for samples, we will not generate samples
352+
return null;
358353
}
359354

360355
private static string FormatPath(string? path)

src/AutoRest.CSharp/Mgmt/AutoRest/MgmtTestConfiguration.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.IO;
76
using System.Linq;
87
using System.Text.Json;
98
using AutoRest.CSharp.AutoRest.Communication;
@@ -17,7 +16,6 @@ internal class MgmtTestConfiguration
1716
private const string TestGenOptionsFormat = $"{TestGenOptionsRoot}.{{0}}";
1817

1918
public string? SourceCodePath { get; }
20-
public string? OutputFolder { get; }
2119
public bool Sample { get; }
2220
public IReadOnlyList<string> SkippedOperations { get; }
2321
public bool ClearOutputFolder { get; }
@@ -26,13 +24,11 @@ public MgmtTestConfiguration(
2624
IReadOnlyList<string> skippedOperations,
2725
JsonElement? sourceCodePath = default,
2826
JsonElement? sample = default,
29-
JsonElement? outputFolder = default,
3027
JsonElement? clearOutputFolder = default)
3128
{
3229
SkippedOperations = skippedOperations;
3330
SourceCodePath = !Configuration.IsValidJsonElement(sourceCodePath) ? null : sourceCodePath.ToString();
3431
Sample = Configuration.DeserializeBoolean(sample, true);
35-
OutputFolder = !Configuration.IsValidJsonElement(outputFolder) ? null : Configuration.TrimFileSuffix(outputFolder.ToString() ?? "");
3632
ClearOutputFolder = Configuration.DeserializeBoolean(clearOutputFolder, false);
3733
}
3834

@@ -46,7 +42,6 @@ public MgmtTestConfiguration(
4642
testGenRoot.TryGetProperty(Options.SkippedOperations, out var skippedOperationsElement);
4743
testGenRoot.TryGetProperty(Options.SourcePath, out var sourceCodePath);
4844
testGenRoot.TryGetProperty(Options.Sample, out var sample);
49-
testGenRoot.TryGetProperty(Options.OutputFolder, out var testGenOutputFolder);
5045
testGenRoot.TryGetProperty(Options.ClearOutputFolder, out var testGenClearOutputFolder);
5146

5247
var skippedOperations = Configuration.DeserializeArray(skippedOperationsElement);
@@ -55,7 +50,6 @@ public MgmtTestConfiguration(
5550
skippedOperations,
5651
sourceCodePath: sourceCodePath,
5752
sample: sample,
58-
outputFolder: testGenOutputFolder,
5953
clearOutputFolder: testGenClearOutputFolder);
6054
}
6155

@@ -68,7 +62,6 @@ public MgmtTestConfiguration(
6862
skippedOperations: autoRest.GetValue<string[]?>(string.Format(TestGenOptionsFormat, "skipped-operations")).GetAwaiter().GetResult() ?? Array.Empty<string>(),
6963
sourceCodePath: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "source-path")).GetAwaiter().GetResult(),
7064
sample: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "sample")).GetAwaiter().GetResult(),
71-
outputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "output-folder")).GetAwaiter().GetResult(),
7265
clearOutputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "clear-output-folder")).GetAwaiter().GetResult());
7366
}
7467

@@ -80,8 +73,6 @@ private static class Options
8073

8174
internal const string Sample = "sample";
8275

83-
internal const string OutputFolder = "output-folder";
84-
8576
internal const string ClearOutputFolder = "clear-output-folder";
8677
}
8778

@@ -97,9 +88,6 @@ internal void SaveConfiguration(Utf8JsonWriter writer)
9788
if (Sample)
9889
writer.WriteBoolean(Options.Sample, Sample);
9990

100-
if (OutputFolder is not null)
101-
writer.WriteString(Options.OutputFolder, Path.GetRelativePath(Configuration.OutputFolder, OutputFolder));
102-
10391
if (!ClearOutputFolder)
10492
writer.WriteBoolean(Options.ClearOutputFolder, ClearOutputFolder);
10593

test/TestProjects/MgmtMockAndSample/src/Generated/Configuration.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/TestProjects/MgmtMockAndSample/src/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ modelerfour:
2626
include-x-ms-examples-original-file: false
2727
sample-gen:
2828
sample: true
29-
output-folder: $(this-folder)../tests/Generated
3029
clear-output-folder: true
3130
skipped-operations: # only to test if the configuration works
3231
- Vaults_GetDeleted

0 commit comments

Comments
 (0)