Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c870c41
Initial commit,...
jgonz120 Nov 3, 2023
9083583
Finshed up moving all the objects to use STJ
jgonz120 Nov 6, 2023
49541e0
Added a really long unit test but then just validated using other uni…
jgonz120 Nov 8, 2023
4d1fe5b
Went through the lock file converters, using Utf8JsonReaderExtensions
jgonz120 Nov 8, 2023
a1af274
Seperated the STJ logic from the JsonPackageSpec reader
jgonz120 Nov 9, 2023
39fffa5
Small optimizations
jgonz120 Nov 10, 2023
da7d17c
Split the JsonPackageReader, added obsolete and surpressed in tests
jgonz120 Nov 10, 2023
9f9738f
Changes to get unit tests working for exceptions
jgonz120 Nov 13, 2023
d3d9f32
merge
jgonz120 Nov 13, 2023
0e02fe1
Updated Package reader with new field
jgonz120 Nov 13, 2023
8422e96
Updated exception unit test
jgonz120 Nov 14, 2023
a5fdf9a
Fix unit tests wrt to exceptions
jgonz120 Nov 14, 2023
57813dd
Implemented deserialization with memory stream
jgonz120 Nov 16, 2023
1cb4fd3
Updated tests to work with new streaming logic
jgonz120 Nov 16, 2023
08b36d1
Update tests to use double quotes instead of single quotes for json
jgonz120 Nov 20, 2023
6e3a887
Additonal single quote removals
jgonz120 Nov 20, 2023
1dc7682
Fix another test that uses single quotes
jgonz120 Nov 21, 2023
e62dcfe
Added unit tests and renamed some functions
jgonz120 Nov 21, 2023
5cd4486
Arrange files
jgonz120 Nov 21, 2023
e1d989d
Rename StreamingUtf8JsonReader to Utf8JsonStreamReader
jgonz120 Nov 21, 2023
68c2021
change classes to no longer implement converter
jgonz120 Nov 22, 2023
3ab5dd4
Added environment variable check
jgonz120 Nov 22, 2023
d0fa9ca
updated comments
jgonz120 Nov 22, 2023
20658a6
merge with dev
jgonz120 Dec 4, 2023
9df9654
Remove the floating package version
jgonz120 Dec 4, 2023
3895c46
removed unused references
jgonz120 Dec 4, 2023
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
Prev Previous commit
Next Next commit
Changes to get unit tests working for exceptions
  • Loading branch information
jgonz120 committed Nov 13, 2023
commit 9f9738f4da6c8d5bb20245f8b0f22798fea55159
57 changes: 47 additions & 10 deletions src/NuGet.Core/NuGet.ProjectModel/FileFormatException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace NuGet.ProjectModel
{
public class FileFormatException : Exception
{
static internal string SurfaceMessage = "SurfaceMessage";

public FileFormatException(string message)
: base(message)
{
Expand All @@ -21,6 +23,7 @@ public FileFormatException(string message, Exception innerException)
}

public string Path { get; private set; }
public string ElementPath { get; private set; }
public int Line { get; private set; }
public int Column { get; private set; }

Expand All @@ -31,6 +34,12 @@ private FileFormatException WithFilePath(string path)
return this;
}

private FileFormatException WithElementPath(string path)
{
ElementPath = path;
return this;
}

private FileFormatException WithLineInfo(JsonReaderException exception)
{
Line = exception.LinePosition;
Expand All @@ -47,6 +56,14 @@ private FileFormatException WithLineInfo(int line, int column)
return this;
}

private FileFormatException WithLineInfo(long? line, long? column)
{
Line = unchecked((int)line.Value);
Column = unchecked((int)column);

return this;
}

private FileFormatException WithLineInfo(IJsonLineInfo lineInfo)
{
Line = lineInfo.LineNumber;
Expand Down Expand Up @@ -103,29 +120,49 @@ internal static FileFormatException Create(string message, int line, int column,

internal static FileFormatException Create(Exception exception, string path)
{
var jex = exception as JsonReaderException;

string message;
if (jex == null)
var jrex = exception as JsonReaderException;
if (jrex is not null)
{
message = string.Format(CultureInfo.CurrentCulture,
Strings.Log_ErrorReadingProjectJson,
path,
Strings.Log_ErrorReadingProjectJsonWithLocation,
path, jrex.LineNumber,
jrex.LinePosition,
exception.Message);

return new FileFormatException(message, exception).WithFilePath(path);
return new FileFormatException(message, exception)
.WithFilePath(path)
.WithLineInfo(jrex);
}
else

var jex = exception as System.Text.Json.JsonException;
if (jex is not null)
{
message = string.Format(CultureInfo.CurrentCulture,
if (jex.Data.Contains(SurfaceMessage))
{
message = jex.Message;
}
else
{
message = string.Format(CultureInfo.CurrentCulture,
Strings.Log_ErrorReadingProjectJsonWithLocation,
path, jex.LineNumber,
jex.LinePosition,
jex.BytePositionInLine,
exception.Message);
}

return new FileFormatException(message, exception)
.WithFilePath(path)
.WithLineInfo(jex);
.WithElementPath(jex.Path)
.WithLineInfo(jex.LineNumber, jex.BytePositionInLine);
}
else
{
message = string.Format(CultureInfo.CurrentCulture,
Strings.Log_ErrorReadingProjectJson,
path,
exception.Message);
return new FileFormatException(message, exception).WithFilePath(path);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ namespace NuGet.ProjectModel
{
public static class JsonPackageSpecReader
{
public static readonly string RestoreOptions = "restore";
public static readonly string RestoreSettings = "restoreSettings";
public static readonly string HideWarningsAndErrors = "hideWarningsAndErrors";
public static readonly string PackOptions = "packOptions";
public static readonly string PackageType = "packageType";
public static readonly string Files = "files";

/// <summary>
/// Load and parse a project.json file
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private static System.Text.Json.JsonSerializerOptions CreateJsonSerializerOption
{
PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
ReadCommentHandling = System.Text.Json.JsonCommentHandling.Skip,
};

options.Converters.Add(new LockFileConverter());
Expand All @@ -42,6 +43,8 @@ private static System.Text.Json.JsonSerializerOptions CreateJsonSerializerOption
options.Converters.Add(new LockFileItemConverter<LockFileRuntimeTarget>());
options.Converters.Add(new LockFileTargetLibraryConverter());
options.Converters.Add(new ProjectFileDependencyGroupConverter());
options.Converters.Add(new PackageSpecConverter());


options.Converters.Add(new ListObjectConvertor<LockFileContentFile>());
options.Converters.Add(new ListObjectConvertor<LockFileRuntimeTarget>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override AssetsLogMessage Read(ref Utf8JsonReader reader, Type typeToConv
}
else if (reader.ValueTextEquals(Utf8TargetGraphs))
{
targetGraphs = reader.ReadStringArrayAsList();
targetGraphs = reader.ReadNextStringArrayAsList();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public override LockFile Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
}
else if (reader.ValueTextEquals(Utf8Project))
{
reader.ReadNextToken();
lockFile.PackageSpec = StjPackageSpecReader.GetPackageSpec(
ref reader,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ private static List<CentralTransitiveDependencyGroup> ReadProjectFileTransitiveD
NuGetFramework framework = NuGetFramework.Parse(frameworkPropertyName);
var dependencies = new List<LibraryDependency>();

JsonPackageSpecReader.ReadCentralTransitiveDependencyGroup(
NjPackageSpecReader.ReadCentralTransitiveDependencyGroup(
jsonReader: jsonReader,
results: dependencies,
packageSpecPath: path);
Expand Down
7 changes: 0 additions & 7 deletions src/NuGet.Core/NuGet.ProjectModel/NjPackageSpecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ internal static class NjPackageSpecReader
private static readonly char[] DelimitedStringSeparators = { ' ', ',' };
private static readonly char[] VersionSeparators = new[] { ';' };

public static readonly string RestoreOptions = "restore";
public static readonly string RestoreSettings = "restoreSettings";
public static readonly string HideWarningsAndErrors = "hideWarningsAndErrors";
public static readonly string PackOptions = "packOptions";
public static readonly string PackageType = "packageType";
public static readonly string Files = "files";

internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string packageSpecPath)
{
return GetPackageSpec(jsonReader, name: null, packageSpecPath, snapshotValue: null);
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.ProjectModel/PackageSpecConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class PackageSpecConverter : JsonConverter<PackageSpec>
{
public override PackageSpec Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return StjPackageSpecReader.GetPackageSpec(ref reader, options, name: string.Empty, packageSpecPath: string.Empty, snapshotValue: string.Empty);
return StjPackageSpecReader.GetPackageSpec(ref reader, options, name: null, packageSpecPath: null, snapshotValue: null);
}

public override void Write(Utf8JsonWriter writer, PackageSpec value, JsonSerializerOptions options)
Expand Down
5 changes: 1 addition & 4 deletions src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
#nullable enable
NuGet.ProjectModel.StjPackageSpecReader
~static NuGet.ProjectModel.StjPackageSpecReader.GetPackageSpec(string json, string name, string packageSpecPath) -> NuGet.ProjectModel.PackageSpec
~static NuGet.ProjectModel.StjPackageSpecReader.GetPackageSpec(string name, string packageSpecPath) -> NuGet.ProjectModel.PackageSpec
~static NuGet.ProjectModel.StjPackageSpecReader.GetPackageSpec(System.IO.Stream stream, string name, string packageSpecPath, string snapshotValue) -> NuGet.ProjectModel.PackageSpec
~NuGet.ProjectModel.FileFormatException.ElementPath.get -> string
Loading