Skip to content

Commit

Permalink
Ensure language property is read from the nupkg
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Aug 17, 2012
1 parent 5d648f4 commit 9199584
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ _AzurePackage
*.bak
NuGetGallery.sln.docstates
_ReSharper.NuGetGallery
_PackagedWebsite
_PackagedWebsite
TestResults
37 changes: 37 additions & 0 deletions Facts/Services/PackageServiceFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,34 @@ public void WillReadThePropertiesFromTheNuGetPackageWhenCreatingANewPackage()
Assert.Equal("theTags", package.Tags);
Assert.Equal("theTitle", package.Title);
Assert.Equal("theCopyright", package.Copyright);
Assert.Null(package.Language);
Assert.False(package.IsPrerelease);

Assert.Equal("theFirstAuthor, theSecondAuthor", package.FlattenedAuthors);
Assert.Equal("theFirstDependency:[1.0, 2.0):net4000|theSecondDependency:[1.0]:net4000|theThirdDependency::net4000|theFourthDependency:[1.0]:net35", package.FlattenedDependencies);
}

[Fact]
public void WillReadTheLanguagePropertyFromThePackage()
{
var packageRegistrationRepo = new Mock<IEntityRepository<PackageRegistration>>();
var service = CreateService(
packageRegistrationRepo: packageRegistrationRepo,
setup: mockPackageSvc =>
{
mockPackageSvc.Setup(x => x.FindPackageRegistrationById(It.IsAny<string>())).Returns((PackageRegistration)null);
});
var nugetPackage = CreateNuGetPackage(p => p.Setup(s => s.Language).Returns("fr"));
var currentUser = new User();

var package = service.CreatePackage(
nugetPackage.Object,
currentUser);

// Assert
Assert.Equal("fr", package.Language);
}

[Fact]
public void WillReadPrereleaseFlagFromNuGetPackage()
{
Expand Down Expand Up @@ -414,6 +436,21 @@ void WillThrowIfTheNuGetPackageTitleIsLongerThan4000()
Assert.Equal(String.Format(Strings.NuGetPackagePropertyTooLong, "Title", "4000"), ex.Message);
}

[Fact]
void WillThrowIfTheNuGetPackageLanguageIsLongerThan20()
{
// Arrange
var service = CreateService();
var nugetPackage = CreateNuGetPackage();
nugetPackage.Setup(x => x.Language).Returns(new string('a', 21));

// Act
var ex = Assert.Throws<EntityException>(() => service.CreatePackage(nugetPackage.Object, null));

// Assert
Assert.Equal(String.Format(Strings.NuGetPackagePropertyTooLong, "Language", "20"), ex.Message);
}

[Fact]
void WillSaveSupportedFrameworks()
{
Expand Down
3 changes: 2 additions & 1 deletion Website/DataServices/PackageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static IQueryable<V1FeedPackage> ToV1FeedPackageQuery(this IQueryable<Pac
GalleryDetailsUrl = siteRoot + "packages/" + p.PackageRegistration.Id + "/" + p.Version,
IconUrl = p.IconUrl,
IsLatestVersion = p.IsLatestStable,
Language = p.Language,
LastUpdated = p.LastUpdated,
LicenseUrl = p.LicenseUrl,
PackageHash = p.Hash,
Expand Down Expand Up @@ -75,7 +76,7 @@ public static IQueryable<V2FeedPackage> ToV2FeedPackageQuery(this IQueryable<Pac
IsPrerelease = p.IsPrerelease,
LastUpdated = p.LastUpdated,
LicenseUrl = p.LicenseUrl,
Language = null,
Language = p.Language,
PackageHash = p.Hash,
PackageHashAlgorithm = p.HashAlgorithm,
PackageSize = p.PackageFileSize,
Expand Down
2 changes: 1 addition & 1 deletion Website/DataServices/V1FeedPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class V1FeedPackage
public string GalleryDetailsUrl { get; set; }
public string IconUrl { get; set; }
public bool IsLatestVersion { get; set; }
public string Language { get; set; }
public DateTime LastUpdated { get; set; }
public string LicenseUrl { get; set; }
public string PackageHash { get; set; }
Expand Down Expand Up @@ -62,7 +63,6 @@ public double VersionRating
}

public string Categories { get { return String.Empty; } }
public string Language { get { return ""; } }
public string PackageType { get { return "Package"; } }
public decimal Price { get { return 0; } }
public bool Prerelease { get { return false; } }
Expand Down
2 changes: 1 addition & 1 deletion Website/DataServices/V2FeedPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class V2FeedPackage
public bool IsLatestVersion { get; set; }
public bool IsAbsoluteLatestVersion { get; set; }
public bool IsPrerelease { get; set; }
public string Language { get; set; }
public DateTime LastUpdated { get; set; }
public DateTime Published { get; set; }
public string Language { get; set; }
public string LicenseUrl { get; set; }
public string PackageHash { get; set; }
public string PackageHashAlgorithm { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions Website/Entities/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public Package()
public bool IsLatestStable { get; set; }
public DateTime LastUpdated { get; set; }
public string LicenseUrl { get; set; }

[StringLength(20)]
public string Language { get; set; }

public DateTime Published { get; set; }
public long PackageFileSize { get; set; }
public string ProjectUrl { get; set; }
Expand Down
24 changes: 24 additions & 0 deletions Website/Migrations/201208171904586_Language.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Website/Migrations/201208171904586_Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NuGetGallery.Migrations
{
using System.Data.Entity.Migrations;

public partial class Language : DbMigration
{
public override void Up()
{
AddColumn("Packages", "Language", c => c.String(maxLength: 20));
}

public override void Down()
{
DropColumn("Packages", "Language");
}
}
}
13 changes: 10 additions & 3 deletions Website/Services/PackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, I
Hash = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes()),
PackageFileSize = packageFileStream.Length,
Created = now,
Language = nugetPackage.Language,
LastUpdated = now,
Published = now,
Copyright = nugetPackage.Copyright,
IsPrerelease = !nugetPackage.IsReleaseVersion(),
Listed = true
Listed = true,
};

if (nugetPackage.IconUrl != null)
Expand Down Expand Up @@ -326,6 +327,7 @@ public virtual IEnumerable<FrameworkName> GetSupportedFrameworks(IPackage packag

static void ValidateNuGetPackage(IPackage nugetPackage)
{
// TODO: Change this to use DataAnnotations
if (nugetPackage.Id.Length > 128)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Id", "128");
if (nugetPackage.Authors != null && String.Join(",", nugetPackage.Authors.ToArray()).Length > 4000)
Expand All @@ -342,12 +344,17 @@ static void ValidateNuGetPackage(IPackage nugetPackage)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "LicenseUrl", "4000");
if (nugetPackage.ProjectUrl != null && nugetPackage.ProjectUrl.ToString().Length > 4000)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "ProjectUrl", "4000");
if (nugetPackage.Summary != null && nugetPackage.Summary.ToString().Length > 4000)
if (nugetPackage.Summary != null && nugetPackage.Summary.Length > 4000)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Summary", "4000");
if (nugetPackage.Tags != null && nugetPackage.Tags.ToString().Length > 4000)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Tags", "4000");
if (nugetPackage.Title != null && nugetPackage.Title.ToString().Length > 4000)
if (nugetPackage.Title != null && nugetPackage.Title.Length > 4000)
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Title", "4000");

if (nugetPackage.Language != null && nugetPackage.Language.Length > 20)
{
throw new EntityException(Strings.NuGetPackagePropertyTooLong, "Language", "20");
}
}

private static void UpdateIsLatest(PackageRegistration packageRegistration)
Expand Down
4 changes: 4 additions & 0 deletions Website/Website.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@
<Compile Include="Migrations\201206250141447_ExecuteELMAHSql.Designer.cs">
<DependentUpon>201206250141447_ExecuteELMAHSql.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\201208171904586_Language.cs" />
<Compile Include="Migrations\201208171904586_Language.Designer.cs">
<DependentUpon>201208171904586_Language.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\MigrationsConfiguration.cs" />
<Compile Include="PackageCurators\IAutomaticPackageCurator.cs" />
<Compile Include="PackageCurators\AutomaticPackageCurator.cs" />
Expand Down

0 comments on commit 9199584

Please sign in to comment.