Skip to content

Commit 64a7883

Browse files
committed
Minor bug fixes in NuGet.Server (#23)
* Catch all JsonException when loading cache file (JsonSerializationException is definitely possible) * Ignore extract packages under the NuGet.Server drop directory * Use the Created date on the server package as the published date * Lock the file system watcher suppression (previously suppressed was unsynchronized) * Add unit tests for fix in ServerPackageStore This cherry-picks commit f658bf9 from master.
1 parent 135ee96 commit 64a7883

11 files changed

Lines changed: 277 additions & 161 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ FakesAssemblies/
196196

197197
*.orig
198198
*.vs10x
199-
src/NuGet.Server/Packages/*.nupkg
199+
src/NuGet.Server/Packages/**/*.nupkg
200+
src/NuGet.Server/Packages/**/*.sha512
201+
src/NuGet.Server/Packages/**/*.nuspec
200202
src/NuGet.Server/Server.Publish.xml
201203
DebugConstants.cs
202204
*.log

src/NuGet.Server.Core/DataServices/PackageExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static ODataPackage AsODataPackage(this IPackage package)
1818
return AsODataPackage(serverPackage);
1919
}
2020

21+
var utcNow = DateTime.UtcNow;
22+
2123
return new ODataPackage
2224
{
2325
Id = package.Id,
@@ -36,8 +38,8 @@ public static ODataPackage AsODataPackage(this IPackage package)
3638
Description = package.Description,
3739
Summary = package.Summary,
3840
ReleaseNotes = package.ReleaseNotes,
39-
Published = package.Published.HasValue ? package.Published.Value.UtcDateTime : DateTime.UtcNow,
40-
LastUpdated = package.Published.HasValue ? package.Published.Value.UtcDateTime : DateTime.UtcNow,
41+
Published = package.Published.HasValue ? package.Published.Value.UtcDateTime : utcNow,
42+
LastUpdated = package.Published.HasValue ? package.Published.Value.UtcDateTime : utcNow,
4143
Dependencies = string.Join("|", package.DependencySets.SelectMany(ConvertDependencySetToStrings)),
4244
PackageHash = package.GetHash(Constants.HashAlgorithm),
4345
PackageHashAlgorithm = Constants.HashAlgorithm,
@@ -73,7 +75,7 @@ public static ODataPackage AsODataPackage(this ServerPackage package)
7375
Description = package.Description,
7476
Summary = package.Summary,
7577
ReleaseNotes = package.ReleaseNotes,
76-
Published = package.Published.HasValue ? package.Published.Value.UtcDateTime : DateTime.UtcNow,
78+
Published = package.Created.UtcDateTime,
7779
LastUpdated = package.LastUpdated.UtcDateTime,
7880
Dependencies = string.Join("|", package.DependencySets.SelectMany(ConvertDependencySetToStrings)),
7981
PackageHash = package.PackageHash,

src/NuGet.Server.Core/Infrastructure/ServerPackage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ public ServerPackage(IPackage package, PackageDerivedData packageDerivedData)
9898
FullPath = packageDerivedData.FullPath;
9999
}
100100

101+
[JsonRequired]
101102
public string Id { get; set; }
102103

103-
[JsonConverter(typeof(SemanticVersionJsonConverter))]
104+
[JsonRequired, JsonConverter(typeof(SemanticVersionJsonConverter))]
104105
public SemanticVersion Version { get; set; }
105106

106107
public string Title { get; set; }

src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs

Lines changed: 181 additions & 154 deletions
Large diffs are not rendered by default.

src/NuGet.Server.Core/Infrastructure/ServerPackageStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void Load()
5555
}
5656
catch (Exception ex)
5757
{
58-
if (ex is JsonReaderException || ex is SerializationException)
58+
if (ex is JsonException || ex is SerializationException)
5959
{
6060
// In case this happens, remove the file
6161
_fileSystem.DeleteFile(_fileName);

test/NuGet.Server.Core.Tests/NuGet.Server.Core.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
34
<Import Project="..\..\NuGet.Settings.targets" />
45
<PropertyGroup>
56
<ProjectGuid>{FDE8DDFB-E01B-47FD-9945-0A151F3AE589}</ProjectGuid>
@@ -88,6 +89,7 @@
8889
<Compile Include="Properties\AssemblyInfo.cs" />
8990
<Compile Include="SemanticVersionJsonConverterTests.cs" />
9091
<Compile Include="ServerPackageRepositoryTest.cs" />
92+
<Compile Include="ServerPackageStoreTest.cs" />
9193
</ItemGroup>
9294
<ItemGroup>
9395
<ProjectReference Include="..\..\src\NuGet.Server.Core\NuGet.Server.Core.csproj">
@@ -102,4 +104,10 @@
102104
<None Include="packages.config" />
103105
</ItemGroup>
104106
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
107+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
108+
<PropertyGroup>
109+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
110+
</PropertyGroup>
111+
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
112+
</Target>
105113
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using Moq;
8+
using NuGet.Server.Core.Infrastructure;
9+
using Xunit;
10+
11+
namespace NuGet.Server.Core.Tests
12+
{
13+
public class ServerPackageStoreTest
14+
{
15+
[Theory]
16+
[InlineData("[")]
17+
[InlineData("]")]
18+
[InlineData("{")]
19+
[InlineData("}")]
20+
[InlineData("[{")]
21+
[InlineData("[{}")]
22+
[InlineData("{}")]
23+
[InlineData("[{\"foo\": \"bar\"}]")]
24+
public void Constructor_IgnoresAndDeletesInvalidCacheFile(string content)
25+
{
26+
// Arrange
27+
var fileName = "store.json";
28+
var fileSystem = new Mock<IFileSystem>();
29+
fileSystem
30+
.Setup(x => x.FileExists(fileName))
31+
.Returns(true);
32+
fileSystem
33+
.Setup(x => x.OpenFile(fileName))
34+
.Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(content)));
35+
36+
// Act
37+
var actual = new ServerPackageStore(fileSystem.Object, fileName);
38+
39+
// Assert
40+
Assert.Empty(actual.GetAll());
41+
fileSystem.Verify(x => x.DeleteFile(fileName), Times.Once);
42+
}
43+
44+
[Theory]
45+
[InlineData("[]", 0)]
46+
[InlineData("[{\"Id\":\"NuGet.Versioning\",\"Version\":\"3.5.0\"}]", 1)]
47+
public void Constructor_LeavesValidCacheFile(string content, int count)
48+
{
49+
// Arrange
50+
var fileName = "store.json";
51+
var fileSystem = new Mock<IFileSystem>();
52+
fileSystem
53+
.Setup(x => x.FileExists(fileName))
54+
.Returns(true);
55+
fileSystem
56+
.Setup(x => x.OpenFile(fileName))
57+
.Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(content)));
58+
59+
// Act
60+
var actual = new ServerPackageStore(fileSystem.Object, fileName);
61+
62+
// Assert
63+
Assert.Equal(count, actual.GetAll().Count());
64+
fileSystem.Verify(x => x.DeleteFile(It.IsAny<string>()), Times.Never);
65+
}
66+
}
67+
}

test/NuGet.Server.Core.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<package id="xunit.core" version="2.1.0" targetFramework="net46" />
1212
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net46" />
1313
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net46" />
14+
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net46" />
1415
</packages>

test/NuGet.Server.Tests/NuGet.Server.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
34
<Import Project="..\..\NuGet.Settings.targets" />
45
<PropertyGroup>
56
<ProjectGuid>{92D18050-3867-4E39-B305-9F9870F66F5E}</ProjectGuid>
@@ -99,4 +100,10 @@
99100
<None Include="packages.config" />
100101
</ItemGroup>
101102
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
103+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
104+
<PropertyGroup>
105+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
106+
</PropertyGroup>
107+
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
108+
</Target>
102109
</Project>

test/NuGet.Server.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<package id="xunit.core" version="2.1.0" targetFramework="net46" />
1212
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net46" />
1313
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net46" />
14+
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net46" />
1415
</packages>

0 commit comments

Comments
 (0)