Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Correct parsing of 'Format Version' header in sln files
Browse files Browse the repository at this point in the history
Prior to this change the exception on line 138 could not
be thrown, as far as I can tell. The reason for this
is that`HeaderPrefix` contained a trailing space, and we
had verified that `line` (trimmed) started with
`HeaderPrefix`. Hence `line` must contain something more
than `HeaderPrefix` otherwise the tailing space would
have been removed, so the length of `line` could not be
less than or equal to the length of `HeaderPrefix`.

Added and changed tests to ensure that both exceptions
regarding `FormatVersion` are thrown.

Fixes #5978
  • Loading branch information
mikkelbu committed May 14, 2017
1 parent 58f709e commit 80b293d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Microsoft.DotNet.Cli.Sln.Internal/LocalizableStrings.cs
Expand Up @@ -17,7 +17,9 @@ internal class LocalizableStrings

public const string GlobalSectionNotClosedError = "Global section not closed";

public const string FileHeaderMissingError = "File header is missing";
public const string FileHeaderMissingVersionError = "File header is missing version";

public const string FileHeaderMissingError = "Expected file header not found";

public const string ProjectSectionNotClosedError = "Project section not closed";

Expand Down
15 changes: 9 additions & 6 deletions src/Microsoft.DotNet.Cli.Sln.Internal/SlnFile.cs
Expand Up @@ -120,7 +120,7 @@ public static SlnFile Read(string file)

private void Read(TextReader reader)
{
const string HeaderPrefix = "Microsoft Visual Studio Solution File, Format Version ";
const string HeaderPrefix = "Microsoft Visual Studio Solution File, Format Version";

string line;
int curLineNum = 0;
Expand All @@ -137,10 +137,10 @@ private void Read(TextReader reader)
{
throw new InvalidSolutionFormatException(
curLineNum,
LocalizableStrings.FileHeaderMissingError);
LocalizableStrings.FileHeaderMissingVersionError);
}

FormatVersion = line.Substring(HeaderPrefix.Length);
FormatVersion = line.Substring(HeaderPrefix.Length).Trim();
_prefixBlankLines = curLineNum - 1;
}
if (line.StartsWith("# ", StringComparison.Ordinal))
Expand Down Expand Up @@ -199,9 +199,7 @@ private void Read(TextReader reader)
}
if (FormatVersion == null)
{
throw new InvalidSolutionFormatException(
curLineNum,
LocalizableStrings.FileHeaderMissingError);
throw new InvalidSolutionFormatException(LocalizableStrings.FileHeaderMissingError);
}
}

Expand Down Expand Up @@ -1167,6 +1165,11 @@ protected override void ClearItems()

public class InvalidSolutionFormatException : Exception
{
public InvalidSolutionFormatException(string details)
: base(details)
{
}

public InvalidSolutionFormatException(int line, string details)
: base(string.Format(LocalizableStrings.ErrorMessageFormatString, line, details))
{
Expand Down
Expand Up @@ -287,9 +287,29 @@ public void WhenGivenAValidSlnFileItModifiesSavesAndVerifiesContents()
.Should().Be(SolutionModified);
}

[Theory]
[InlineData("Microsoft Visual Studio Solution File, Format Version ", 1)]
[InlineData("First Line\nMicrosoft Visual Studio Solution File, Format Version ", 2)]
[InlineData("First Line\nMicrosoft Visual Studio Solution File, Format Version \nThird Line", 2)]
[InlineData("First Line\nSecondLine\nMicrosoft Visual Studio Solution File, Format Version \nFourth Line", 3)]
public void WhenGivenASolutionWithMissingHeaderVersionItThrows(string fileContents, int lineNum)
{
var tmpFile = Temp.CreateFile();
tmpFile.WriteAllText(fileContents);

Action action = () =>
{
SlnFile.Read(tmpFile.Path);
};

action.ShouldThrow<InvalidSolutionFormatException>()
.WithMessage($"Invalid format in line {lineNum}: File header is missing version");
}

[Theory]
[InlineData("Invalid Solution")]
[InlineData("Microsoft Visual Studio Solution File, Format Version ")]
[InlineData("Invalid Solution\nSpanning Multiple Lines")]
[InlineData("Microsoft Visual\nStudio Solution File,\nFormat Version ")]
public void WhenGivenASolutionWithMissingHeaderItThrows(string fileContents)
{
var tmpFile = Temp.CreateFile();
Expand All @@ -301,7 +321,7 @@ public void WhenGivenASolutionWithMissingHeaderItThrows(string fileContents)
};

action.ShouldThrow<InvalidSolutionFormatException>()
.WithMessage("Invalid format in line 1: File header is missing");
.WithMessage("Expected file header not found");
}

[Fact]
Expand Down

0 comments on commit 80b293d

Please sign in to comment.