Skip to content

Commit

Permalink
Adding check for existing directory and creating if doesn't exist (do…
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-lethargic authored Jun 2, 2024
1 parent c1ee0e4 commit e4932fb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public static int Main(string[] args)
? Path.Combine(Directory.GetCurrentDirectory(), arg1)
: null;

if (!string.IsNullOrEmpty(outputPath))
{
string directoryPath = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}

using (Stream stream = outputPath != null ? File.Create(outputPath) : Console.OpenStandardOutput())
using (var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture))
{
Expand Down
41 changes: 39 additions & 2 deletions test/Swashbuckle.AspNetCore.Cli.Test/ToolTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using Swashbuckle.AspNetCore.TestSupport.Utilities;
using Xunit;
Expand Down Expand Up @@ -131,10 +132,32 @@ public static void Does_Not_Run_Crashing_HostedService()
Assert.True(path.TryGetProperty("get", out _));
}

private static JsonDocument RunApplication(Func<string, string[]> setup)
[Fact]
public static void Creates_New_Folder_Path()
{
using var document = RunApplication(outputPath =>
[
"tofile",
"--output",
outputPath,
"--serializeasv2",
Path.Combine(Directory.GetCurrentDirectory(), "Basic.dll"),
"v1"
], GenerateRandomString(5));

// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var productsPath = paths.GetProperty("/products");
Assert.True(productsPath.TryGetProperty("post", out _));
}

private static JsonDocument RunApplication(Func<string, string[]> setup, string subOutputPath = default)
{
using var temporaryDirectory = new TemporaryDirectory();
string outputPath = Path.Combine(temporaryDirectory.Path, "swagger.json");

var outputPath = !string.IsNullOrEmpty(subOutputPath)
? Path.Combine(temporaryDirectory.Path, subOutputPath, "swagger.json")
: Path.Combine(temporaryDirectory.Path, "swagger.json");

string[] args = setup(outputPath);

Expand All @@ -143,5 +166,19 @@ private static JsonDocument RunApplication(Func<string, string[]> setup)
string json = File.ReadAllText(outputPath);
return JsonDocument.Parse(json);
}

private static string GenerateRandomString(int length)
{
const string Choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var result = new StringBuilder(length);

for (int i = 0; i < length; i++)
{
result.Append(Choices[Random.Shared.Next(Choices.Length)]);
}

return result.ToString();
}

}
}

0 comments on commit e4932fb

Please sign in to comment.