Resolves #5206
This commit is contained in:
Heath Stewart 2023-01-24 16:31:37 -08:00 коммит произвёл GitHub
Родитель a75a7adda3
Коммит 0c0b181016
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 70 добавлений и 8 удалений

Просмотреть файл

@ -6,12 +6,25 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="NUnit" Version="3.13.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Azure.Sdk.Tools.SnippetGenerator\Azure.Sdk.Tools.SnippetGenerator.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="TestData\Snippets.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="TestData\EmptySnippet.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,20 @@
using System;
using System.IO;
using NUnit.Framework;
namespace Azure.Sdk.Tools.SnippetGenerator.Tests
{
public class DirectoryProcessorTests
{
[Test]
public void DirectoryProcessorValidatesEmptySnippets()
{
var path = Path.Join(TestContext.CurrentContext.TestDirectory, "TestData");
var files = new string[] { Path.Join(path, "EmptySnippet.md") };
var sut = new DirectoryProcessor(path);
InvalidOperationException ex = Assert.ThrowsAsync<InvalidOperationException>(async () => await sut.ProcessAsync(files));
StringAssert.Contains("Snippet 'Snippet:EmptySnippet' is empty", ex.Message);
}
}
}

Просмотреть файл

@ -0,0 +1,2 @@
```C# Snippet:EmptySnippet
```

Просмотреть файл

@ -0,0 +1,17 @@
using NUnit.Framework;
namespace Azure.Sdk.Tools.SnippetGenerator.TestData
{
public class Snippets
{
[Test]
public void EmptySnippet()
{
#if SHOULD_NEVER_BE_DEFINED
#region Snippet:EmptySnippet
throw new NotImplementedException();
#endregion
#endif
}
}
}

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
@ -38,11 +38,16 @@ namespace Azure.Sdk.Tools.SnippetGenerator
_snippets = new Lazy<Task<List<Snippet>>>(DiscoverSnippetsAsync);
}
public async Task<IEnumerable<string>> ProcessAsync()
public async Task<IEnumerable<string>> ProcessAsync(IEnumerable<string> files = null)
{
List<string> files = new List<string>();
files.AddRange(Directory.EnumerateFiles(_directory, "*.md", SearchOption.AllDirectories));
files.AddRange(Directory.EnumerateFiles(_directory, "*.cs", SearchOption.AllDirectories));
if (files is null)
{
List<string> discoveredFiles = new();
discoveredFiles.AddRange(Directory.EnumerateFiles(_directory, "*.md", SearchOption.AllDirectories));
discoveredFiles.AddRange(Directory.EnumerateFiles(_directory, "*.cs", SearchOption.AllDirectories));
files = discoveredFiles;
}
foreach (var file in files)
{
@ -64,6 +69,11 @@ namespace Azure.Sdk.Tools.SnippetGenerator
selectedSnippet.IsUsed = true;
Console.WriteLine($"Replaced {selectedSnippet.Name} in {file}");
var result = FormatSnippet(selectedSnippet.Text);
if (string.IsNullOrWhiteSpace(result))
{
throw new InvalidOperationException($"Snippet '{s}' is empty. Did you remember to implement the snippet code or are preprocessor conditions excluding it?");
}
return result;
}
@ -339,4 +349,4 @@ namespace Azure.Sdk.Tools.SnippetGenerator
}
}
}
}
}