Skip to content

Commit c052391

Browse files
authored
Merge pull request #259 from AArnott/fix250
Improve handling of version.json parsing failures
2 parents 337def8 + d712dff commit c052391

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,25 @@ public void CanUseGitProjectRelativePathWithGitRepoRoot()
279279
oracle = VersionOracle.Create(Path.Combine(this.RepoPath, "otherChildProject"), this.RepoPath, null, null, "otherChildProject");
280280
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
281281
}
282+
283+
[Fact]
284+
public void VersionJsonWithoutVersion()
285+
{
286+
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), "{}");
287+
this.InitializeSourceControl();
288+
var oracle = VersionOracle.Create(this.RepoPath);
289+
Assert.Equal(0, oracle.Version.Major);
290+
Assert.Equal(0, oracle.Version.Minor);
291+
}
292+
293+
[Fact]
294+
public void VersionJsonWithSingleIntegerForVersion()
295+
{
296+
File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), @"{""version"":""3""}");
297+
this.InitializeSourceControl();
298+
var ex = Assert.Throws<FormatException>(() => VersionOracle.Create(this.RepoPath));
299+
Assert.Contains(this.Repo.Head.Commits.First().Sha, ex.Message);
300+
Assert.Contains("\"3\"", ex.InnerException.Message);
301+
this.Logger.WriteLine(ex.ToString());
302+
}
282303
}

src/NerdBank.GitVersioning/SemanticVersionJsonConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2424
{
2525
return value;
2626
}
27+
else
28+
{
29+
throw new FormatException($"The value \"{reader.Value}\" is not a valid semantic version.");
30+
}
2731
}
2832

2933
throw new NotSupportedException();

src/NerdBank.GitVersioning/VersionFile.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,19 @@ public static VersionOptions GetVersion(LibGit2Sharp.Commit commit, string repoR
6969
versionJsonContent = sr.ReadToEnd();
7070
}
7171

72-
VersionOptions result = TryReadVersionJsonContent(versionJsonContent);
72+
VersionOptions result;
73+
try
74+
{
75+
result = TryReadVersionJsonContent(versionJsonContent);
76+
}
77+
catch (FormatException ex)
78+
{
79+
throw new FormatException(
80+
$"Failure while reading {JsonFileName} from commit {commit.Sha}. " +
81+
"Fix this commit with rebase if this is an error, or review this doc on how to migrate to Nerdbank.GitVersioning: " +
82+
"https://github.com/AArnott/Nerdbank.GitVersioning/blob/master/doc/migrating.md", ex);
83+
}
84+
7385
if (result?.Inherit ?? false)
7486
{
7587
if (parentDirectory != null)

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, LibG
9898

9999
this.VersionHeightOffset = this.VersionOptions?.BuildNumberOffsetOrDefault ?? 0;
100100

101-
this.PrereleaseVersion = this.ReplaceMacros(this.VersionOptions?.Version.Prerelease ?? string.Empty);
101+
this.PrereleaseVersion = this.ReplaceMacros(this.VersionOptions?.Version?.Prerelease ?? string.Empty);
102102

103103
this.CloudBuildNumberOptions = this.VersionOptions?.CloudBuild?.BuildNumberOrDefault ?? VersionOptions.CloudBuildNumberOptions.DefaultInstance;
104104

0 commit comments

Comments
 (0)