Skip to content

Commit

Permalink
Restore improvements (#541)
Browse files Browse the repository at this point in the history
* Added condition to check if there are any packages to restore

* Added automatic restore when packages.config changes

* Fixed installing older versions of package when reloading from older package.config

- also fixed updating packages
- fixed some typos

* Fixed restore to properly uninstall packages not listed in packages.config

- added brief summary for OnPostprocessAllAssets

* Added unit tests

* Fixed test readability and added summary for ReloadPackagesConfig

* Fixed tests to not use hardcoded packages.config contents

* Fixed tests to not refresh the database

* CHanged OnPostprocessAllAssets to be internal
  • Loading branch information
popara96 committed Jul 28, 2023
1 parent 653b9d5 commit ce4df2e
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 89 deletions.
66 changes: 66 additions & 0 deletions src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,70 @@ public void TestSerializeNugetPackageIdentifier(string version)
Assert.That(deserialized.InRange(identifier), Is.True);
}
}

[Test]
[TestCase("jQuery", "3.7.0")]
public void TestPostprocessInstall(string packageId, string packageVersion)
{
var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true };
var filepath = NugetHelper.PackagesConfigFilePath;

var packagesConfigFile = new PackagesConfigFile();
packagesConfigFile.AddPackage(package);
packagesConfigFile.Save(filepath);

Assert.IsFalse(NugetHelper.IsInstalled(package), "The package IS installed: {0} {1}", package.Id, package.Version);

var assetsIndex = filepath.LastIndexOf("Assets", StringComparison.Ordinal);
filepath = filepath.Substring(assetsIndex);
NugetPackageAssetPostprocessor.OnPostprocessAllAssets(new[] { filepath }, null, null, null);

Assert.IsTrue(NugetHelper.IsInstalled(package), "The package was NOT installed: {0} {1}", package.Id, package.Version);
}

[Test]
[TestCase("jQuery", "3.7.0")]
public void TestPostprocessUninstall(string packageId, string packageVersion)
{
var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true };
var filepath = NugetHelper.PackagesConfigFilePath;

NugetHelper.InstallIdentifier(package);
Assert.IsTrue(NugetHelper.IsInstalled(package), "The package was NOT installed: {0} {1}", package.Id, package.Version);

var packagesConfigFile = new PackagesConfigFile();
packagesConfigFile.Save(filepath);

var assetsIndex = filepath.LastIndexOf("Assets", StringComparison.Ordinal);
filepath = filepath.Substring(assetsIndex);
NugetPackageAssetPostprocessor.OnPostprocessAllAssets(new[]{filepath},
null, null, null);

Assert.IsFalse(NugetHelper.IsInstalled(package), "The package is STILL installed: {0} {1}", package.Id, package.Version);
}

[Test]
[TestCase("jQuery", "3.6.4", "3.7.0")]
[TestCase("jQuery", "3.7.0", "3.6.4")]
public void TestPostprocessDifferentVersion(string packageId, string packageVersionOld, string packageVersionNew)
{
var packageOld = new NugetPackageIdentifier(packageId, packageVersionOld) { IsManuallyInstalled = true };
var packageNew = new NugetPackageIdentifier(packageId, packageVersionNew) { IsManuallyInstalled = true };
var filepath = NugetHelper.PackagesConfigFilePath;

NugetHelper.InstallIdentifier(packageOld);
Assert.IsTrue(NugetHelper.IsInstalled(packageOld), "The package was NOT installed: {0} {1}", packageOld.Id, packageOld.Version);

var packagesConfigFile = new PackagesConfigFile();
packagesConfigFile.AddPackage(packageNew);
packagesConfigFile.Save(filepath);

var assetsIndex = filepath.LastIndexOf("Assets", StringComparison.Ordinal);
filepath = filepath.Substring(assetsIndex);
NugetPackageAssetPostprocessor.OnPostprocessAllAssets(new[] { filepath }, null, null, null);

Assert.IsFalse(NugetHelper.IsInstalled(packageOld), "The old package version IS STILL installed: {0} {1}", packageOld.Id, packageOld.Version);

Assert.IsTrue(NugetHelper.IsInstalled(packageNew), "The new package version was NOT installed: {0} {1}", packageNew.Id, packageNew.Version);
}
}

0 comments on commit ce4df2e

Please sign in to comment.