Skip to content

Commit

Permalink
Allow empty folders for known v3 folders
Browse files Browse the repository at this point in the history
  • Loading branch information
emgarten committed Nov 23, 2015
1 parent 24760a9 commit 23a4429
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
51 changes: 48 additions & 3 deletions src/Core/Authoring/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,13 @@ private void AddFiles(string basePath, string source, string destination, string
if (_includeEmptyDirectories)
{
// we only allow empty directories which are legit framework folders.
searchFiles.RemoveAll(file => file.TargetFramework == null &&
Path.GetFileName(file.TargetPath) == Constants.PackageEmptyFileName);
// Folders for nuget v3 should be included here also since this part of nuget.core is still used
// by nuget.exe 3.3.0.
searchFiles.RemoveAll(file => file.TargetFramework == null
&& Path.GetFileName(file.TargetPath) == Constants.PackageEmptyFileName
&& !IsKnownV3Folder(file.TargetPath));
}

ExcludeFiles(searchFiles, basePath, exclude);

if (!PathResolver.IsWildcardSearch(source) && !PathResolver.IsDirectoryPath(source) && !searchFiles.Any())
Expand All @@ -531,6 +534,48 @@ private void AddFiles(string basePath, string source, string destination, string
Files.AddRange(searchFiles);
}

/// <summary>
/// Returns true if the path uses a known v3 folder root.
/// </summary>
private static bool IsKnownV3Folder(string targetPath)
{
if (targetPath != null)
{
var parts = targetPath.Split(
new char[] { '\\', '/' },
StringSplitOptions.RemoveEmptyEntries);

// exclude things in the root of the directory, this is not allowed
// for any of the v3 folders.
// example: an empty 'native' folder does not have a TxM and cannot be used.
if (parts.Length > 1)
{
var topLevelDirectory = parts.FirstOrDefault();

return KnownFoldersForV3.Any(folder =>
folder.Equals(topLevelDirectory, StringComparison.OrdinalIgnoreCase));
}
}

return false;
}

/// <summary>
/// Folders used in NuGet v3 that are not used in NuGet.Core
/// </summary>
private static IEnumerable<string> KnownFoldersForV3
{
get
{
yield return "contentFiles";
yield return "ref";
yield return "runtimes";
yield return "native";
yield return "analyzers";
yield break;
}
}

private static void ExcludeFiles(List<PhysicalPackageFile> searchFiles, string basePath, string exclude)
{
if (String.IsNullOrEmpty(exclude))
Expand Down
60 changes: 60 additions & 0 deletions test/Core.Test/PackageBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,66 @@ namespace NuGet.Test
{
public class PackageBuilderTest
{
[Fact]
public void CreatePackageWithEmptyFoldersForV3Folders()
{
// Arrange
PackageBuilder builder = new PackageBuilder()
{
Id = "A",
Version = new SemanticVersion("1.0"),
Description = "Descriptions",
};

builder.Authors.Add("testAuthor");

var dependencies = new List<PackageDependency>();
dependencies.Add(new PackageDependency("packageB", VersionUtility.ParseVersionSpec("1.0.0"), null, "z"));
dependencies.Add(new PackageDependency(
"packageC",
VersionUtility.ParseVersionSpec("1.0.0"),
"a,b,c",
"b,c"));

var set = new PackageDependencySet(null, dependencies);
builder.DependencySets.Add(set);

builder.Files.Add(CreatePackageFile(@"build\_._"));
builder.Files.Add(CreatePackageFile(@"content\_._"));
builder.Files.Add(CreatePackageFile(@"contentFiles\any\any\_._"));
builder.Files.Add(CreatePackageFile(@"lib\net45\_._"));
builder.Files.Add(CreatePackageFile(@"native\net45\_._"));
builder.Files.Add(CreatePackageFile(@"ref\net45\_._"));
builder.Files.Add(CreatePackageFile(@"runtimes\net45\_._"));
builder.Files.Add(CreatePackageFile(@"tools\_._"));

using (var ms = new MemoryStream())
{
// Act
builder.Save(ms);

ms.Seek(0, SeekOrigin.Begin);

var zip = new ZipPackage(ms);
var files = zip.GetFiles()
.Select(file => file.Path)
.Where(file => Path.GetFileName(file) == "_._")
.OrderBy(s => s)
.ToArray();

// Assert
Assert.Equal(8, files.Length);
Assert.Equal(@"build\_._", files[0]);
Assert.Equal(@"content\_._", files[1]);
Assert.Equal(@"contentFiles\any\any\_._", files[2]);
Assert.Equal(@"lib\net45\_._", files[3]);
Assert.Equal(@"native\net45\_._", files[4]);
Assert.Equal(@"ref\net45\_._", files[5]);
Assert.Equal(@"runtimes\net45\_._", files[6]);
Assert.Equal(@"tools\_._", files[7]);
}
}

[Fact]
public void CreatePackageWithNuspecIncludeExcludeAnyGroup()
{
Expand Down

0 comments on commit 23a4429

Please sign in to comment.