Skip to content

Commit

Permalink
submit package retaining folder structure
Browse files Browse the repository at this point in the history
- created the second path to submitting packages online retaining folder structure
- tests added
  • Loading branch information
dnenov committed Nov 5, 2023
1 parent c3bc444 commit 9d6b738
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,12 @@ private void Submit()
return;
}
var contentFiles = BuildPackage();

//do not create the updatedFiles used for retain folder route unless needed
IEnumerable<IEnumerable<string>> updatedFiles = null;
if(RetainFolderStructureOverride)
updatedFiles = UpdateFilesForRetainFolderStructure(contentFiles);

try
{
//if buildPackage() returns no files then the package
Expand All @@ -1794,7 +1800,9 @@ private void Submit()
}
// begin submission
var pmExtension = dynamoViewModel.Model.GetPackageManagerExtension();
var handle = pmExtension.PackageManagerClient.PublishAsync(Package, contentFiles, MarkdownFiles, IsNewVersion);
var handle = RetainFolderStructureOverride ?
pmExtension.PackageManagerClient.PublishRetainAsync(Package, updatedFiles, MarkdownFiles, IsNewVersion) :
pmExtension.PackageManagerClient.PublishAsync(Package, contentFiles, MarkdownFiles, IsNewVersion);

// start upload
Uploading = true;
Expand Down
52 changes: 52 additions & 0 deletions src/DynamoPackages/PackageManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ internal PackageUploadHandle PublishAsync(Package package, IEnumerable<string> f
return packageUploadHandle;
}


internal PackageUploadHandle PublishRetainAsync(Package package, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, bool isNewVersion)
{
var packageUploadHandle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(package));

Task.Factory.StartNew(() =>
{
PublishRetainFolderStructure(package, files, markdownFiles, isNewVersion, packageUploadHandle);
});

return packageUploadHandle;
}

internal void Publish(Package package, IEnumerable<string> files, IEnumerable<string> markdownFiles, bool isNewVersion, PackageUploadHandle packageUploadHandle)
{
try
Expand Down Expand Up @@ -276,6 +289,45 @@ internal void Publish(Package package, IEnumerable<string> files, IEnumerable<st
}
}

internal void PublishRetainFolderStructure(Package package, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, bool isNewVersion, PackageUploadHandle packageUploadHandle)
{
try
{
ResponseBody ret = null;
if (isNewVersion)
{
var pkg = uploadBuilder.NewPackageVersionRetainUpload(package, packageUploadDirectory, files, markdownFiles,
packageUploadHandle);
packageUploadHandle.UploadState = PackageUploadHandle.State.Uploading;
ret = this.client.ExecuteAndDeserialize(pkg);
}
else
{
var pkg = uploadBuilder.NewPackageRetainUpload(package, packageUploadDirectory, files, markdownFiles,
packageUploadHandle);
packageUploadHandle.UploadState = PackageUploadHandle.State.Uploading;
ret = this.client.ExecuteAndDeserialize(pkg);
}
if (ret == null)
{
packageUploadHandle.Error("Failed to submit. Try again later.");
return;
}

if (ret != null && !ret.success)
{
packageUploadHandle.Error(ret.message);
return;
}

packageUploadHandle.Done(null);
}
catch (Exception e)
{
packageUploadHandle.Error(e.GetType() + ": " + e.Message);
}
}

[Obsolete("No longer used. Delete in 3.0")]
internal PackageManagerResult DownloadPackageHeader(string id, out PackageHeader header)
{
Expand Down
49 changes: 49 additions & 0 deletions src/DynamoPackages/PackageUploadBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ public interface IPackageUploadBuilder
PackageUpload NewPackageUpload(Package package, string packagesDirectory, IEnumerable<string> files, IEnumerable<string> markdownFiles,
PackageUploadHandle handle);

PackageUpload NewPackageRetainUpload(Package package, string packagesDirectory, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles,
PackageUploadHandle handle);

PackageVersionUpload NewPackageVersionUpload(Package package, string packagesDirectory,
IEnumerable<string> files, IEnumerable<string> markdownFiles, PackageUploadHandle handle);

PackageVersionUpload NewPackageVersionRetainUpload(Package package, string packagesDirectory,
IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, PackageUploadHandle handle);
}

internal class PackageUploadBuilder : IPackageUploadBuilder
Expand Down Expand Up @@ -77,6 +83,28 @@ public PackageUpload NewPackageUpload(Package package, string packagesDirectory,
BuildAndZip(package, packagesDirectory, files, markdownFiles, handle).Name);
}

/// <summary>
/// Build a new package and upload retaining folder structure
/// TODO: Should that be a separate method or an override? Break API ok?
/// </summary>
/// <param name="package"></param>
/// <param name="packagesDirectory"></param>
/// <param name="files"></param>
/// <param name="markdownFiles"></param>
/// <param name="handle"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public PackageUpload NewPackageRetainUpload(Package package, string packagesDirectory, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, PackageUploadHandle handle)
{
if (package == null) throw new ArgumentNullException("package");
if (packagesDirectory == null) throw new ArgumentNullException("packagesDirectory");
if (files == null) throw new ArgumentNullException("files");
if (handle == null) throw new ArgumentNullException("handle");

return new PackageUpload(NewRequestBody(package),
BuildAndZip(package, packagesDirectory, files, markdownFiles, handle).Name);
}

/// <summary>
/// Build a new version of the package and upload
/// </summary>
Expand All @@ -97,6 +125,27 @@ public PackageVersionUpload NewPackageVersionUpload(Package package, string pack
return new PackageVersionUpload(NewRequestBody(package), BuildAndZip(package, packagesDirectory, files, markdownFiles, handle).Name);
}

/// <summary>
/// Build a new version of the package and upload retaining folder structure
/// TODO: Should that be a separate method or an override? Break API ok?
/// </summary>
/// <param name="package"></param>
/// <param name="packagesDirectory"></param>
/// <param name="files"></param>
/// <param name="markdownFiles"></param>
/// <param name="handle"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public PackageVersionUpload NewPackageVersionRetainUpload(Package package, string packagesDirectory, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, PackageUploadHandle handle)
{
if (package == null) throw new ArgumentNullException("package");
if (packagesDirectory == null) throw new ArgumentNullException("packagesDirectory");
if (files == null) throw new ArgumentNullException("files");
if (handle == null) throw new ArgumentNullException("handle");

return new PackageVersionUpload(NewRequestBody(package), BuildAndZip(package, packagesDirectory, files, markdownFiles, handle).Name);
}

#endregion

#region Private Class Methods
Expand Down
88 changes: 88 additions & 0 deletions test/Libraries/PackageManagerTests/PackageManagerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,94 @@ public void Publish_SetsErrorStatusWhenResponseIsNull()

#endregion

#region PublishRetainingFolderStructure

[Test]
public void PublishRetain_SetsHandleToDoneWhenNewPackagePublishSucceeds()
{
var gc = new Mock<IGregClient>();
gc.Setup(x => x.ExecuteAndDeserialize(It.IsAny<PackageUpload>()))
.Returns(new ResponseBody()
{
success = true
});

var pc = new PackageManagerClient(gc.Object, MockMaker.Empty<IPackageUploadBuilder>(), "");

var pkg = new Package("", "Package", "0.1.0", "MIT");

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));
var listOfEmptyEnumerables = Enumerable.Range(1, 5)
.Select(_ => Enumerable.Empty<string>());

pc.PublishRetainFolderStructure(pkg, listOfEmptyEnumerables, Enumerable.Empty<string>(), false, handle);

Assert.AreEqual(PackageUploadHandle.State.Uploaded, handle.UploadState);
}

[Test]
public void PublishRetain_SetsHandleToDoneWhenNewPackageVersionPublishSucceeds()
{
var gc = new Mock<IGregClient>();
gc.Setup(x => x.ExecuteAndDeserialize(It.IsAny<PackageVersionUpload>()))
.Returns(new ResponseBody()
{
success = true
});

var pc = new PackageManagerClient(gc.Object, MockMaker.Empty<IPackageUploadBuilder>(), "");

var pkg = new Package("", "Package", "0.1.0", "MIT");

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));
var listOfEmptyEnumerables = Enumerable.Range(1, 5)
.Select(_ => Enumerable.Empty<string>());
pc.PublishRetainFolderStructure(pkg, listOfEmptyEnumerables, Enumerable.Empty<string>(), false, handle);

Assert.AreEqual(PackageUploadHandle.State.Uploaded, handle.UploadState);
}

[Test]
public void PublishRetain_SetsErrorStatusWhenRequestThrowsAnException()
{
var gc = new Mock<IGregClient>();
gc.Setup(x => x.ExecuteAndDeserialize(It.IsAny<PackageUpload>())).Throws<Exception>();

var pc = new PackageManagerClient(gc.Object, MockMaker.Empty<IPackageUploadBuilder>(), "");

var pkg = new Package("", "Package", "0.1.0", "MIT");

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));
var listOfEmptyEnumerables = Enumerable.Range(1, 5)
.Select(_ => Enumerable.Empty<string>());
pc.PublishRetainFolderStructure(pkg, listOfEmptyEnumerables, Enumerable.Empty<string>(), false, handle);

Assert.AreEqual(PackageUploadHandle.State.Error, handle.UploadState);
}

[Test]
public void PublishRetain_SetsErrorStatusWhenResponseIsNull()
{
var gc = new Mock<IGregClient>();
var rb = new ResponseBody();
rb.success = false;

gc.Setup(x => x.ExecuteAndDeserialize(It.IsAny<PackageUpload>())).Returns(rb);

var pc = new PackageManagerClient(gc.Object, MockMaker.Empty<IPackageUploadBuilder>(), "");

var pkg = new Package("", "Package", "0.1.0", "MIT");

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));
var listOfEmptyEnumerables = Enumerable.Range(1, 5)
.Select(_ => Enumerable.Empty<string>());
pc.PublishRetainFolderStructure(pkg, listOfEmptyEnumerables, Enumerable.Empty<string>(), true, handle);

Assert.AreEqual(PackageUploadHandle.State.Error, handle.UploadState);
}

#endregion

#region Deprecate

[Test]
Expand Down
87 changes: 87 additions & 0 deletions test/Libraries/PackageManagerTests/PackageUploadBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ private IPackageUploadBuilder BigPackageUploadBuilderMock()
return new PackageUploadBuilder(pdb.Object, zipper.Object);
}

private IPackageUploadBuilder BigPackageUploadRetainBuilderMock()
{
// a IFileInfo object that is, by mocking, too large
var bigzip = new Mock<IFileInfo>();
bigzip.Setup(x => x.Length).Returns(PackageUploadBuilder.MaximumPackageSize + 1);

// the zipper returns a big zip
var zipper = new Mock<IFileCompressor>();
zipper.Setup((x) => x.Zip(It.IsAny<IDirectoryInfo>())).Returns(bigzip.Object);

var pdb = new Mock<IPackageDirectoryBuilder>();
pdb.Setup(x => x.BuildRetainDirectory(It.IsAny<Package>(), It.IsAny<string>(), It.IsAny<IEnumerable<IEnumerable<string>>>(), It.IsAny<IEnumerable<string>>()))
.Returns((new Mock<IDirectoryInfo>()).Object);

// this package upload builder will try to return a zip that is too big
return new PackageUploadBuilder(pdb.Object, zipper.Object);
}

private class directoryTestClass : IDirectoryInfo
{
public string FullName { get; set; }
Expand Down Expand Up @@ -160,6 +178,40 @@ public void NewPackageVersionUpload_ThrowsForNullArguments()

#endregion

#region NewPackageUploadRetainFolderStructure

[Test]
public void NewPackageUploadRetain_ThrowsExceptionWhenPackageIsTooBig()
{
var pub = BigPackageUploadRetainBuilderMock();

var files = new List<IEnumerable<string>>() { new[] { @"C:\folder1\file1.dyf" }, new[] { @"C:\folder2\file2.dyf" } };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var pkgsDir = @"C:\dynamopackages";

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));

Assert.Throws<Exception>(() => pub.NewPackageRetainUpload(pkg, pkgsDir, files, Enumerable.Empty<string>(), handle));
}

[Test]
public void NewPackageVersionUploadRetain_ThrowsForNullArguments()
{
var files = new List<IEnumerable<string>>() { new[] { @"C:\folder1\file1.dyf" }, new[] { @"C:\folder2\file2.dyf" } };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var pkgsDir = @"C:\dynamopackages";
var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));

var m = new PackageUploadBuilder(MockMaker.Empty<IPackageDirectoryBuilder>(), MockMaker.Empty<IFileCompressor>());

Assert.Throws<ArgumentNullException>(() => m.NewPackageRetainUpload(null, pkgsDir, files, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageRetainUpload(pkg, null, files, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageRetainUpload(pkg, pkgsDir, null, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageRetainUpload(pkg, pkgsDir, files, Enumerable.Empty<string>(), null));
}

#endregion

#region NewPackageVersionUpload

[Test]
Expand Down Expand Up @@ -194,6 +246,41 @@ public void NewPackageUpload_ThrowsForNullArguments()

#endregion

#region NewPackageVersionUploadRetainFolderStructure

[Test]
public void NewPackageVersionUploadRetain_ThrowsExceptionWhenPackageIsTooBig()
{
var pub = BigPackageUploadBuilderMock();

var files = new List<IEnumerable<string>>() { new[] { @"C:\folder1\file1.dyf" }, new[] { @"C:\folder2\file2.dyf" } };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var pkgsDir = @"C:\dynamopackages";

var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));

Assert.Throws<Exception>(() => pub.NewPackageVersionRetainUpload(pkg, pkgsDir, files, Enumerable.Empty<string>(), handle));
}

[Test]
public void NewPackageUploadRetain_ThrowsForNullArguments()
{
var files = new List<IEnumerable<string>>() { new[] { @"C:\folder1\file1.dyf" }, new[] { @"C:\folder2\file2.dyf" } };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var pkgsDir = @"C:\dynamopackages";
var handle = new PackageUploadHandle(PackageUploadBuilder.NewRequestBody(pkg));

var m = new PackageUploadBuilder(MockMaker.Empty<IPackageDirectoryBuilder>(), MockMaker.Empty<IFileCompressor>());

Assert.Throws<ArgumentNullException>(() => m.NewPackageVersionRetainUpload(null, pkgsDir, files, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageVersionRetainUpload(pkg, null, files, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageVersionRetainUpload(pkg, pkgsDir, null, Enumerable.Empty<string>(), handle));
Assert.Throws<ArgumentNullException>(() => m.NewPackageVersionRetainUpload(pkg, pkgsDir, files, Enumerable.Empty<string>(), null));
}

#endregion


#region NewRequestBody

[Test]
Expand Down

0 comments on commit 9d6b738

Please sign in to comment.