Skip to content

Commit

Permalink
Cover the case when a package has been loaded already with test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
sm6srw committed Mar 23, 2021
1 parent 630e817 commit b340de3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/DynamoCore/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/DynamoCore/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,7 @@ Parameter name: {0}</value>
<data name="InvalidPackageVersion" xml:space="preserve">
<value>The version of the package called {0} found at {1} is invalid (version: "{2}"). Ignoring it.</value>
</data>
<data name="DuplicatedPackageAlreadyLoaded" xml:space="preserve">
<value>A newer version of the package called {0} version {2} was found at {1} with version {3}. The old version of the package is already loaded. Please restart Dynamo for loading the new version of the package.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/DynamoCore/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,7 @@ Parameter name: {0}</value>
<data name="InvalidPackageVersion" xml:space="preserve">
<value>The version of the package called {0} found at {1} is invalid (version: "{2}"). Ignoring it.</value>
</data>
<data name="DuplicatedPackageAlreadyLoaded" xml:space="preserve">
<value>A newer version of the package called {0} version {2} was found at {1} with version {3}. The old version of the package is already loaded. Please restart Dynamo for loading the new version of the package.</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/DynamoPackages/PackageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ public Package ScanPackageDirectory(string directory, bool checkCertificates)
return null;
}

// If the existing package already loaded
if (existingPackage.Loaded)
{
throw new LibraryLoadFailedException(directory, String.Format(Properties.Resources.DuplicatedPackageAlreadyLoaded, existingPackage.Name, discoveredPackage.RootDirectory, existingVersion.ToString(), discoveredVersion.ToString()));
}

// Older version exist, replace with newer version
Remove(existingPackage);
Add(discoveredPackage);
Expand Down
44 changes: 44 additions & 0 deletions test/Libraries/PackageManagerTests/PackageLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,50 @@ public void PackageLoaderLoadOldPackage()
Assert.IsNull(loader.LocalPackages.FirstOrDefault(package => package.Description == @"Old package"));
}

[Test]
public void PackageLoaderLoadNewWhenOldPackageHasBeenLoadedAlready()
{
// Arrange
var loader = GetPackageLoader();
var oldPackageLocation = Path.Combine(PackagesDirectory, @"Version\PackageWithOldVersion");
var newPackageLocation = Path.Combine(PackagesDirectory, @"Version\PackageWithNewVersion");

// Act
var oldPackage = loader.ScanPackageDirectory(oldPackageLocation);
oldPackage.Loaded = true;
var newPackage = loader.ScanPackageDirectory(newPackageLocation);

// Assert
Assert.IsNotNull(oldPackage);
Assert.IsNull(newPackage);
Assert.AreEqual("Package", oldPackage.Name);
Assert.AreEqual("1.0.0", oldPackage.VersionName);
Assert.IsNull(loader.LocalPackages.FirstOrDefault(package => package.Description == @"New package"));
Assert.IsNotNull(loader.LocalPackages.FirstOrDefault(package => package.Description == @"Old package"));
}

[Test]
public void PackageLoaderLoadOldWhenNewPackageHasBeenLoadedAlready()
{
// Arrange
var loader = GetPackageLoader();
var oldPackageLocation = Path.Combine(PackagesDirectory, @"Version\PackageWithOldVersion");
var newPackageLocation = Path.Combine(PackagesDirectory, @"Version\PackageWithNewVersion");

// Act
var newPackage = loader.ScanPackageDirectory(newPackageLocation);
newPackage.Loaded = true;
var oldPackage = loader.ScanPackageDirectory(oldPackageLocation);

// Assert
Assert.IsNull(oldPackage);
Assert.IsNotNull(newPackage);
Assert.AreEqual("Package", newPackage.Name);
Assert.AreEqual("2.0.0", newPackage.VersionName);
Assert.IsNotNull(loader.LocalPackages.FirstOrDefault(package => package.Description == @"New package"));
Assert.IsNull(loader.LocalPackages.FirstOrDefault(package => package.Description == @"Old package"));
}

[Test]
public void IsUnderPackageControlIsCorrectForValidFunctionDefinition()
{
Expand Down

0 comments on commit b340de3

Please sign in to comment.