-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Formatting on the command line works again (#4950)
This PR makes sure StreamReaders are closed after use, which prevented formatting from overwriting the files randomly. The reason why to do this despite an obvious memory increase is that in [all places where we use this StreamReader](#4918 (comment)), we actually already read to the end and don't make use of the streaming. Before, the "dafny format" command was throwing exceptions on large files, and now it no longer throws an exception (test added for that) Even better, this solved the issue that when I create a blank Dafny file and try to save it, it used to complain with "Failed to save 'todelete.dfy': Unable to write file". (Fixes dafny-lang/ide-vscode#452) <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
- Loading branch information
1 parent
2998296
commit d29e234
Showing
5 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Diagnostics; | ||
using System.IO.Pipelines; | ||
using System.Reactive.Concurrency; | ||
using System.Reflection; | ||
using System.Text; | ||
using Microsoft.Dafny; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.JsonRpc.Client; | ||
using OmniSharp.Extensions.JsonRpc.Serialization; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
|
||
namespace DafnyDriver.Test; | ||
|
||
public class FormatterCommandTest { | ||
[Fact] | ||
public async Task GitIssueVSCode452FormattingWorksOnBigFiles() { | ||
// We write to a temporary file | ||
var oneSource = (int i) => $@"function Test{i}(x: int): int | ||
// This returns {i} if x is greater than 20 | ||
{{ | ||
if x < 10 then | ||
0 | ||
else if x < 20 then | ||
1 | ||
else {i} | ||
}} | ||
"; | ||
|
||
var oneSourceFormatted = (int i) => $@"function Test{i}(x: int): int | ||
// This returns {i} if x is greater than 20 | ||
{{ | ||
if x < 10 then | ||
0 | ||
else if x < 20 then | ||
1 | ||
else {i} | ||
}} | ||
"; | ||
var source = ""; | ||
var sourceFormatted = ""; | ||
for (var i = 0; i < 1000; i++) { | ||
source += oneSource(i); | ||
sourceFormatted += oneSourceFormatted(i); | ||
} | ||
|
||
var file = Path.GetTempFileName() + ".dfy"; | ||
|
||
await File.WriteAllTextAsync(file, source); | ||
StringWriter strWriter = new StringWriter(); | ||
var options = DafnyOptions.Create(strWriter, null, | ||
file | ||
); | ||
|
||
var exitValue = await FormatCommand.DoFormatting(options); | ||
Assert.Equal(0, (int)exitValue); | ||
|
||
var result = await File.ReadAllTextAsync(file); | ||
Assert.Equal(sourceFormatted, result); | ||
File.Delete(file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters