Skip to content

Commit

Permalink
Preserve existing ProductCode when updating (#215)
Browse files Browse the repository at this point in the history
* preserve product code and other fields when updating

* Add unit tests

Co-authored-by: Ryan Fu <Fu.Ryan@microsoft.com>
  • Loading branch information
ryfu-msft and Ryan Fu authored Jan 21, 2022
1 parent 74de458 commit 9f1ecdc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/WingetCreateCore/Common/PackageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,13 @@ private static void UpdateInstallerMetadata(Installer existingInstaller, Install
existingInstaller.Architecture = newInstaller.Architecture;
existingInstaller.InstallerUrl = newInstaller.InstallerUrl;
existingInstaller.InstallerSha256 = newInstaller.InstallerSha256;
existingInstaller.SignatureSha256 = newInstaller.SignatureSha256;
existingInstaller.ProductCode = newInstaller.ProductCode;
existingInstaller.MinimumOSVersion = newInstaller.MinimumOSVersion;
existingInstaller.PackageFamilyName = newInstaller.PackageFamilyName;
existingInstaller.Platform = newInstaller.Platform;
existingInstaller.SignatureSha256 = newInstaller.SignatureSha256;

// If the newInstaller field value is null, we default to using the existingInstaller field value.
existingInstaller.ProductCode = newInstaller.ProductCode ?? existingInstaller.ProductCode;
existingInstaller.MinimumOSVersion = newInstaller.MinimumOSVersion ?? existingInstaller.MinimumOSVersion;
existingInstaller.PackageFamilyName = newInstaller.PackageFamilyName ?? existingInstaller.PackageFamilyName;
existingInstaller.Platform = newInstaller.Platform ?? existingInstaller.Platform;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PackageIdentifier: TestPublisher.SingleExe
PackageVersion: 0.1.2
PackageName: Single exe test
Publisher: Test publisher
License: MIT
ShortDescription: A manifest used to test the update command with a single exe installer.
InstallerLocale: en-US
Installers:
- Architecture: x64
InstallerUrl: https://fakedomain.com/WingetCreateTestExeInstaller.exe
InstallerType: exe
InstallerSha256: A7803233EEDB6A4B59B3024CCF9292A6FFFB94507DC998AA67C5B745D197A5DC
ProductCode: FakeProductCode
PackageFamilyName: FakePackageFamilyName
Platform:
- Windows.Desktop
PackageLocale: en-US
ManifestType: singleton
ManifestVersion: 1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,41 @@ public async Task UpdateWithMultipleArchitectureOverrides()
}
}

/// <summary>
/// Tests that the provided installer url with leading and trailing spaces is trimmed prior to being updated.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
[Test]
public async Task UpdateWithUntrimmedInstallerUrl()
{
string untrimmedInstallerUrl = $" https://fakedomain.com/{TestConstants.TestExeInstaller} ";
TestUtils.InitializeMockDownloads(TestConstants.TestExeInstaller);
(UpdateCommand command, var initialManifestContent) =
GetUpdateCommandAndManifestData("TestPublisher.SingleExe", "1.2.3.4", this.tempPath, new[] { untrimmedInstallerUrl });
var updatedManifests = await RunUpdateCommand(command, initialManifestContent);
Assert.IsNotNull(updatedManifests, "Command should have succeeded");
Assert.AreNotEqual(untrimmedInstallerUrl, updatedManifests.InstallerManifest.Installers.First().InstallerUrl, "InstallerUrl was not trimmed prior to update.");
}

/// <summary>
/// Tests if a new installer has null values, the update does not overwrite existing values for ProductCode, PackageFamilyName, and Platform from the existing manifest.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
[Test]
public async Task UpdatePreservesExistingValues()
{
string installerUrl = $"https://fakedomain.com/{TestConstants.TestExeInstaller}";
TestUtils.InitializeMockDownloads(TestConstants.TestExeInstaller);
(UpdateCommand command, var initialManifestContent) =
GetUpdateCommandAndManifestData("TestPublisher.SingleExe", "1.2.3.4", this.tempPath, new[] { $"{installerUrl}" });
var updatedManifests = await RunUpdateCommand(command, initialManifestContent);
Assert.IsNotNull(updatedManifests, "Command should have succeeded");
var updatedInstaller = updatedManifests.InstallerManifest.Installers.First();
Assert.AreEqual("FakeProductCode", updatedInstaller.ProductCode, "Existing value for ProductCode was overwritten.");
Assert.AreEqual("FakePackageFamilyName", updatedInstaller.PackageFamilyName, "Existing value for PackageFamilyName was overwritten.");
Assert.IsNotNull(updatedInstaller.Platform, "Existing value for Platform was overwritten.;");
}

private static (UpdateCommand UpdateCommand, List<string> InitialManifestContent) GetUpdateCommandAndManifestData(string id, string version, string outputDir, IEnumerable<string> installerUrls)
{
var updateCommand = new UpdateCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<None Update="Resources\Multifile.MsixTest\Multifile.MsixTest.locale.en-US.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\TestPublisher.SingleExe.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\TestPublisher.EmptyFields.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 9f1ecdc

Please sign in to comment.