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

Fix index out of range for bicep format with --insert-final-newline #14193

Merged
merged 1 commit into from
May 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Bicep.Cli.IntegrationTests/FormatCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,46 @@ public async Task Format_WithIndentSizeOverride_SetsIndentSizeAccordingly(int in
formatted.Should().BeEquivalentToIgnoringNewlines(expected);
}

[TestMethod]
public async Task Format_WithInsertFinalNewline_AddsFinalNewline()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
["main.bicep"] = "output myOutput string = 'hello!'",
});

var result = await Bicep(services => services.WithFileSystem(fileSystem), "format", "main.bicep", "--insert-final-newline");

AssertSuccess(result);

var formatted = fileSystem.File.ReadAllText("main.bicep");
formatted.Should().BeEquivalentTo("output myOutput string = 'hello!'\n");

result = await Bicep(services => services.WithFileSystem(fileSystem), "format", "main.bicep", "--insert-final-newline", "true");

AssertSuccess(result);

formatted = fileSystem.File.ReadAllText("main.bicep");
formatted.Should().BeEquivalentTo("output myOutput string = 'hello!'\n");
}

[TestMethod]
public async Task Format_WithInsertFinalNewlineFalse_RemovesFinalNewline()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
["main.bicep"] = "output myOutput string = 'hello!'\n",
});

var result = await Bicep(services => services.WithFileSystem(fileSystem), "format", "main.bicep", "--insert-final-newline", "false");

AssertSuccess(result);

var formatted = fileSystem.File.ReadAllText("main.bicep");
formatted.Should().BeEquivalentTo("output myOutput string = 'hello!'");
}


private static IEnumerable<object[]> GetDataSets() => DataSets.AllDataSets
.Where(x => !x.Name.Equals(DataSets.PrettyPrint_LF.Name, StringComparison.Ordinal))
.ToDynamicTestData();
Expand Down
12 changes: 12 additions & 0 deletions src/Bicep.Cli/Arguments/FormatArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public FormatArguments(string[] args, IOContext io, IFileSystem fileSystem) : ba
throw new CommandLineException($"The --insertFinalNewline parameter cannot be specified twice");
}

if (args.Length == i + 1)
{
InsertFinalNewline = true;
break;
}

if (bool.TryParse(args[i + 1], out var insertFinalNewline))
{
InsertFinalNewline = insertFinalNewline;
Expand All @@ -183,6 +189,12 @@ public FormatArguments(string[] args, IOContext io, IFileSystem fileSystem) : ba
throw new CommandLineException($"The --insert-final-newline parameter cannot be specified twice");
}

if (args.Length == i + 1)
{
InsertFinalNewline = true;
break;
}

if (bool.TryParse(args[i + 1], out insertFinalNewline))
{
InsertFinalNewline = insertFinalNewline;
Expand Down
Loading