Skip to content

Commit

Permalink
adding packagebuilder tests
Browse files Browse the repository at this point in the history
- adding tests for BuildRetainDirectory
  • Loading branch information
dnenov committed Nov 1, 2023
1 parent 2597eee commit c3bc444
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/DynamoPackages/PackageDirectoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public IDirectoryInfo BuildDirectory(Package package, string packagesDirectory,
WritePackageHeader(package, rootDir);
RemoveUnselectedFiles(contentFiles, rootDir);
CopyFilesIntoPackageDirectory(contentFiles, markdownFiles, dyfDir, binDir, extraDir, docDir);
/*
// Commentint this out until understanding the function of it
RemoveDyfFiles(contentFiles, dyfDir); // Why do we need to remove the dyf files from the publishing folder?
*/

RemapCustomNodeFilePaths(contentFiles, dyfDir.FullName);

return rootDir;
Expand All @@ -79,10 +77,8 @@ public IDirectoryInfo BuildRetainDirectory(Package package, string packagesDirec

RemoveUnselectedFiles(contentFiles.SelectMany(files => files).ToList(), rootDir);
CopyFilesIntoRetainedPackageDirectory(contentFiles, markdownFiles, rootDir, out dyfFiles);
/*
// Commentint this out until understanding the function of it
RemoveRetainDyfFiles(contentFiles, dyfFiles); // Why do we need to remove the dyf files from the publishing folder?
*/
RemoveRetainDyfFiles(contentFiles.SelectMany(files => files).ToList(), dyfFiles); // Why do we need to remove the dyf files from the publishing folder?

RemapRetainCustomNodeFilePaths(contentFiles.SelectMany(files => files).ToList(), dyfFiles);


Expand Down Expand Up @@ -111,7 +107,19 @@ private void RemapRetainCustomNodeFilePaths(IEnumerable<string> filePaths, List<
{
foreach (var func in filePaths.Where(x => x.EndsWith(".dyf")))
{
var remapLocation = dyfFiles.First(x => Path.GetDirectoryName(x).Equals(Path.GetDirectoryName(func)));
//var remapLocation = dyfFiles.First(x => Path.GetDirectoryName(x).Equals(Path.GetDirectoryName(func)));
var remapLocation = dyfFiles.First(x =>
{
var p1 = Path.GetFileName(Path.GetDirectoryName(x));
var f1 = Path.GetFileName(x);
var r1 = Path.Combine(p1, f1);

var p2 = Path.GetFileName(Path.GetDirectoryName(func));
var f2 = Path.GetFileName(func);
var r2 = Path.Combine(p2, f2);

return r1.Equals(r2);
});
pathRemapper.SetPath(func, remapLocation);
}
}
Expand Down Expand Up @@ -212,7 +220,9 @@ internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<stri

foreach (var files in contentFiles)
{
var commonPath = GetLongestCommonPrefix(files.ToArray());
// We expect that files are bundled in root folders
// For single files, just get its folder
var commonPath = files.Count() > 1 ? GetLongestCommonPrefix(files.ToArray()) : Path.GetDirectoryName(files.First());
commonPath = commonPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var commonRootPath = Path.GetDirectoryName(commonPath);
if (commonRootPath == null) commonRootPath = commonPath; // already at the root
Expand Down
11 changes: 11 additions & 0 deletions src/DynamoPackages/PackageUploadBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ private IFileInfo BuildAndZip(Package package, string packagesDirectory, IEnumer
return Zip(dir);
}

private IFileInfo BuildAndZip(Package package, string packagesDirectory, IEnumerable<IEnumerable<string>> files, IEnumerable<string> markdownFiles, PackageUploadHandle handle)
{
handle.UploadState = PackageUploadHandle.State.Copying;

var dir = builder.BuildRetainDirectory(package, packagesDirectory, files, markdownFiles);

handle.UploadState = PackageUploadHandle.State.Compressing;

return Zip(dir);
}

private IFileInfo Zip(IDirectoryInfo directory)
{
IFileInfo info;
Expand Down
217 changes: 217 additions & 0 deletions test/Libraries/PackageManagerTests/PackageDirectoryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public void BuildPackageDirectory_DoesExpectedNumberOfOperations()
Assert.AreEqual(1, fs.NewFilesWritten.Count());
}

[Test]
public void BuildRetainPackageDirectory_DoesExpectedNumberOfOperations()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));
var db = new PackageDirectoryBuilder(fs, MockMaker.Empty<IPathRemapper>());

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

Assert.AreEqual(1, fs.DirectoriesCreated.Count());
Assert.AreEqual(2, fs.CopiedFiles.Count());
Assert.AreEqual(2, fs.DeletedFiles.Count());
Assert.AreEqual(1, fs.NewFilesWritten.Count());
}

[Test]
public void BuildPackageDirectory_BuildsExpectedDirectories()
{
Expand Down Expand Up @@ -65,6 +83,23 @@ public void BuildPackageDirectory_BuildsExpectedDirectories()
Assert.IsTrue(fs.DirectoriesCreated.Any(x => x.FullName == docDir));
}

[Test]
public void BuildRetainPackageDirectory_BuildsExpectedDirectories()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));
var db = new PackageDirectoryBuilder(fs, MockMaker.Empty<IPathRemapper>());

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

var rootDir = Path.Combine(pkgsDir, pkg.Name);

Assert.IsTrue(fs.DirectoriesCreated.Any(x => x.FullName == rootDir));
}

[Test]
public void BuildPackageDirectory_FormsPackageHeader()
{
Expand All @@ -87,6 +122,27 @@ public void BuildPackageDirectory_FormsPackageHeader()
Assert.IsTrue(fs.NewFilesWritten.Any(x => x.Item1 == Path.Combine(rootDir, PackageDirectoryBuilder.PackageJsonName)));
}

[Test]
public void BuildRetainPackageDirectory_FormsPackageHeader()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

var pr = new Mock<IPathRemapper>();
var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

// where the magic happens...
db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

var rootDir = Path.Combine(pkgsDir, pkg.Name);

Assert.AreEqual(1, fs.NewFilesWritten.Count());
Assert.IsTrue(fs.NewFilesWritten.Any(x => x.Item1 == Path.Combine(rootDir, PackageDirectoryBuilder.PackageJsonName)));
}

[Test]
public void BuildPackageDirectory_RemapsCustomNodePaths()
{
Expand Down Expand Up @@ -117,6 +173,36 @@ public void BuildPackageDirectory_RemapsCustomNodePaths()
Assert.IsTrue(remappedPaths.Any(x => x.Item1 == files[1] && x.Item2 == dyfDir));
}


[Test]
public void BuildRetainPackageDirectory_RemapsCustomNodePaths()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

var pr = new Mock<IPathRemapper>();
var remappedPaths = new List<Tuple<string, string>>();

pr.Setup(x => x.SetPath(files[0].First(), It.IsAny<string>()))
.Callback((string f, string s) => remappedPaths.Add(new Tuple<string, string>(f, s)));

pr.Setup(x => x.SetPath(files[1].First(), It.IsAny<string>()))
.Callback((string f, string s) => remappedPaths.Add(new Tuple<string, string>(f, s)));

var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

var dyfDir1 = Path.Combine(pkgsDir, pkg.Name, Path.GetFileName( Path.GetDirectoryName(files[0].First()) ), Path.GetFileName(files[0].First()));
var dyfDir2 = Path.Combine(pkgsDir, pkg.Name, Path.GetFileName( Path.GetDirectoryName(files[1].First()) ), Path.GetFileName(files[1].First()));

Assert.IsTrue(remappedPaths.Any(x => x.Item1 == files[0].First() && x.Item2 == dyfDir1));
Assert.IsTrue(remappedPaths.Any(x => x.Item1 == files[1].First() && x.Item2 == dyfDir2));
}

[Test]
public void BuildPackageDirectory_UpdatesTheArgumentPackageWithNewDirectories()
{
Expand Down Expand Up @@ -147,6 +233,30 @@ public void BuildPackageDirectory_UpdatesTheArgumentPackageWithNewDirectories()
Assert.AreEqual(docDir, pkg.NodeDocumentaionDirectory);
}



[Test]
public void BuildRetainPackageDirectory_UpdatesTheArgumentPackageWithNewDirectories()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));


var pr = new Mock<IPathRemapper>();
var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

var rootDir = Path.Combine(pkgsDir, pkg.Name);

// The package itself is updated

Assert.AreEqual(rootDir, pkg.RootDirectory);
}

[Test]
public void BuildPackageDirectory_CopiesTheOriginalFiles()
{
Expand All @@ -172,6 +282,32 @@ public void BuildPackageDirectory_CopiesTheOriginalFiles()
Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(dyfDir, "file2.dyf"))));
}


[Test]
public void BuildPackageRetainDirectory_CopiesTheOriginalFiles()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

var pr = new Mock<IPathRemapper>();
var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

var dyfDir1 = Path.Combine(pkgsDir, pkg.Name, Path.GetFileName(Path.GetDirectoryName(files[0].First())));
var dyfDir2 = Path.Combine(pkgsDir, pkg.Name, Path.GetFileName(Path.GetDirectoryName(files[1].First())));

Assert.AreEqual(2, fs.CopiedFiles.Count());
Assert.AreEqual(2, fs.DeletedFiles.Count());
Assert.AreEqual(0, fs.DeletedDirectories.Count());

Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(dyfDir1, "file1.dyf"))));
Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(dyfDir2, "file2.dyf"))));
}

[Test]
public void BuildPackageDirectory_CopiesMarkDownFiles()
{
Expand Down Expand Up @@ -199,6 +335,34 @@ public void BuildPackageDirectory_CopiesMarkDownFiles()
Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(mdDir, "file3.jpg"))));
}


[Test]
public void BuildRetainPackageDirectory_CopiesMarkDownFiles()
{
var files = new List<IEnumerable<string>>() { new[] { @"C:\folder1\file1.dyn" }, new[] { @"C:\folder2\file2.dyn" } };
var markdownFiles = new[] { @"C:\file1.md", @"C:\file2.md", @"C:\image\file3.jpg" };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

var pr = new Mock<IPathRemapper>();
var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, markdownFiles);

var mdDir = Path.Combine(pkgsDir, pkg.Name, PackageDirectoryBuilder.DocumentationDirectoryName);

Assert.AreEqual(5, fs.CopiedFiles.Count());
Assert.AreEqual(0, fs.DeletedFiles.Count());
Assert.AreEqual(0, fs.DeletedDirectories.Count());

Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(mdDir, "file1.md"))));
Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(mdDir, "file2.md"))));
Assert.IsTrue(fs.CopiedFiles.Any(x => ComparePaths(x.Item2, Path.Combine(mdDir, "file3.jpg"))));
}

// Why?
[Test]
public void BuildPackageDirectory_DeletesTheOriginalFiles()
{
Expand All @@ -224,6 +388,30 @@ public void BuildPackageDirectory_DeletesTheOriginalFiles()
Assert.Contains(files[1], fs.DeletedFiles.ToList());
}

[Test]
public void BuildRetainPackageDirectory_DeletesTheOriginalFiles()
{
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 fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

var pr = new Mock<IPathRemapper>();

var db = new PackageDirectoryBuilder(fs, pr.Object);

var pkgsDir = @"C:\dynamopackages";

db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

// The original files are moved

Assert.AreEqual(2, fs.DeletedFiles.Count());
Assert.AreEqual(0, fs.DeletedDirectories.Count());

Assert.Contains(files[0].First(), fs.DeletedFiles.ToList());
Assert.Contains(files[1].First(), fs.DeletedFiles.ToList());
}

[Test]
public void BuildPackageDirectory_DoesNotIncludeUnselectedFiles()
{
Expand All @@ -248,6 +436,35 @@ public void BuildPackageDirectory_DoesNotIncludeUnselectedFiles()

Assert.AreEqual(5, fs.DirectoriesCreated.Count());
Assert.AreEqual(4, fs.CopiedFiles.Count());
Assert.AreEqual(3, fs.DeletedFiles.Count());
Assert.AreEqual(2, fs.DeletedDirectories.Count());
Assert.AreEqual(1, fs.NewFilesWritten.Count());
}

[Test]
public void BuildRetainPackageDirectory_DoesNotIncludeUnselectedFiles()
{
// For http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-7676

var files = new List<IEnumerable<string>>() { new[] { "C:/pkg/bin/file1.dll", "C:/pkg/dyf/file2.dyf",
"C:/pkg/extra/file3.txt", "C:/pkg/extra/subfolder/file4.dwg" } };
var pkg = new Package(@"C:\pkg", "Foo", "0.1.0", "MIT");
var fs = new RecordedFileSystem((fn) => files.SelectMany(files => files).ToList().Any((x) => ComparePaths(x, fn)));

// Specifying directory contents in the disk
fs.SetFiles(new List<string>() {
"C:/pkg/bin/file1.dll", "C:/pkg/dyf/file2.dyf", "C:/pkg/dyf/backup/file2.dyf.0.backup",
"C:/pkg/extra/file3.txt", "C:/pkg/extra/Backup/file3.txt.backup", "C:/pkg/extra/subfolder/file4.dwg" });
fs.SetDirectories(new List<string>() {
"C:/pkg/bin", "C:/pkg/dyf", "C:/pkg/dyf/backup", "C:/pkg/extra",
"C:/pkg/extra/Backup", "C:/pkg/extra/subfolder" });

var db = new PackageDirectoryBuilder(fs, MockMaker.Empty<IPathRemapper>());
var pkgsDir = @"C:\dynamopackages";
db.BuildRetainDirectory(pkg, pkgsDir, files, Enumerable.Empty<string>());

Assert.AreEqual(1, fs.DirectoriesCreated.Count());
Assert.AreEqual(4, fs.CopiedFiles.Count());
Assert.AreEqual(3, fs.DeletedFiles.Count());
Assert.AreEqual(2, fs.DeletedDirectories.Count());
Assert.AreEqual(1, fs.NewFilesWritten.Count());
Expand Down

0 comments on commit c3bc444

Please sign in to comment.