Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding check for existing directory and creating if doesn't exist #2927

Merged
merged 4 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 40 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,20 @@ 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 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder result = new StringBuilder(length);
Random random = new Random();

for (int i = 0; i < length; i++)
{
result.Append(chars[random.Next(chars.Length)]);
}
martincostello marked this conversation as resolved.
Show resolved Hide resolved

return result.ToString();
}

}
}