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

[release/3.1] Handle shortened JSON file in dotnet openapi #36362

Merged
merged 3 commits into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -569,7 +569,7 @@ private async Task WriteToFileAsync(Stream content, string destinationPath, bool

// Create or overwrite the destination file.
reachedCopy = true;
using var fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.Write);
using var fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
fileStream.Seek(0, SeekOrigin.Begin);
if (content.CanSeek)
{
Expand Down
74 changes: 67 additions & 7 deletions src/Tools/Microsoft.dotnet-openapi/test/OpenApiRefreshTests.cs
Expand Up @@ -3,7 +3,6 @@

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.OpenApi.Tests;
using Xunit;
Expand All @@ -20,29 +19,90 @@ public async Task OpenApi_Refresh_Basic()
{
CreateBasicProject(withOpenApi: false);

// Add <OpenApiReference/> to the project. Ignore initial filename.json content.
var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error.ToString()}");
Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

// File will grow after the refresh.
var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
var json = await File.ReadAllTextAsync(expectedJsonPath);
json += "trash";
await File.WriteAllTextAsync(expectedJsonPath, json);
await File.WriteAllTextAsync(expectedJsonPath, "trash");

var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);

Thread.Sleep(TimeSpan.FromSeconds(1));
await Task.Delay(TimeSpan.FromSeconds(1));

app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error.ToString()}");
Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.True(firstWriteTime < secondWriteTime, $"File wasn't updated! {firstWriteTime} {secondWriteTime}");
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath), ignoreLineEndingDifferences: true);
}

// Regression test for #35767 scenario.
[Fact]
public async Task OpenApi_Refresh_MuchShorterFile()
{
CreateBasicProject(withOpenApi: false);

// Add <OpenApiReference/> to the project. Ignore initial filename.json content.
var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

// File will shrink after the refresh.
var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
await File.WriteAllTextAsync(expectedJsonPath, PackageUrlContent);

var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);

await Task.Delay(TimeSpan.FromSeconds(1));

app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.True(firstWriteTime < secondWriteTime, $"File wasn't updated! {firstWriteTime} {secondWriteTime}");
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath), ignoreLineEndingDifferences: true);
}

[Fact]
public async Task OpenApi_Refresh_UnchangedFile()
{
CreateBasicProject(withOpenApi: false);

// Add <OpenApiReference/> to the project and write the filename.json file.
var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);

await Task.Delay(TimeSpan.FromSeconds(1));

app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl });

Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, run);

var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.Equal(firstWriteTime, secondWriteTime);
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath));
}
}
}