Родитель
a75a7adda3
Коммит
0c0b181016
|
@ -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
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче