Skip to content

Commit 4537239

Browse files
committed
More sacrifices made
1 parent 2b82a74 commit 4537239

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,19 @@ public static async Task TransformOpenApiDocument(
5555
CancellationToken cancellationToken
5656
)
5757
{
58+
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
59+
{
60+
throw new ArgumentException("Please input a file path or URL");
61+
}
5862

5963
try
6064
{
61-
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
62-
{
63-
throw new ArgumentException("Please input a file path or URL");
64-
}
6565
if (output == null)
6666
{
6767
var inputExtension = GetInputPathExtension(openapi, csdl);
6868
output = new FileInfo($"./output{inputExtension}");
6969
};
70+
7071
if (cleanoutput && output.Exists)
7172
{
7273
output.Delete();
@@ -87,7 +88,11 @@ CancellationToken cancellationToken
8788
catch (TaskCanceledException)
8889
{
8990
Console.Error.WriteLine("CTRL+C pressed, aborting the operation.");
90-
}
91+
}
92+
catch (IOException)
93+
{
94+
throw;
95+
}
9196
catch (Exception ex)
9297
{
9398
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
@@ -239,12 +244,13 @@ public static async Task ValidateOpenApiDocument(
239244
ILogger logger,
240245
CancellationToken cancellationToken)
241246
{
247+
if (string.IsNullOrEmpty(openapi))
248+
{
249+
throw new ArgumentNullException(nameof(openapi));
250+
}
251+
242252
try
243253
{
244-
if (string.IsNullOrEmpty(openapi))
245-
{
246-
throw new ArgumentNullException(nameof(openapi));
247-
}
248254
using var stream = await GetStream(openapi, logger, cancellationToken);
249255

250256
var result = await ParseOpenApi(openapi, false, logger, stream);

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,38 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiag
142142
Assert.Contains("graph LR", output);
143143
}
144144

145+
[Fact]
146+
public async Task ThrowIfOpenApiUrlIsNotProvidedWhenValidating()
147+
{
148+
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
149+
await OpenApiService.ValidateOpenApiDocument("", new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
150+
}
151+
152+
145153
[Fact]
146154
public async Task ThrowIfURLIsNotResolvableWhenValidating()
147155
{
148-
var message = Assert.ThrowsAsync<InvalidOperationException>(async () =>
156+
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
149157
await OpenApiService.ValidateOpenApiDocument("https://example.org/itdoesnmatter", new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
150158
}
151159

152160
[Fact]
153161
public async Task ThrowIfFileDoesNotExistWhenValidating()
154162
{
155-
var message = Assert.ThrowsAsync<InvalidOperationException>(async () =>
163+
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
156164
await OpenApiService.ValidateOpenApiDocument("aFileThatBetterNotExist.fake", new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
157165
}
158166

167+
[Fact]
168+
public async Task ValidateCommandProcessesOpenApi()
169+
{
170+
// create a dummy ILogger instance for testing
171+
await OpenApiService.ValidateOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
172+
173+
Assert.True(true);
174+
}
175+
176+
159177
[Fact]
160178
public async Task TransformCommandConvertsOpenApi()
161179
{
@@ -167,6 +185,14 @@ public async Task TransformCommandConvertsOpenApi()
167185
Assert.NotEmpty(output);
168186
}
169187

188+
[Fact]
189+
public async Task ThrowTransformCommandIfOpenApiAndCsdlAreEmpty()
190+
{
191+
await Assert.ThrowsAsync<ArgumentException>(async () =>
192+
await OpenApiService.TransformOpenApiDocument(null, null, null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
193+
194+
}
195+
170196
[Fact]
171197
public void InvokeTransformCommand()
172198
{

0 commit comments

Comments
 (0)