[Settings] Update version in General Settings (#2601)

* Update version in General Settings

* Set version in settings.json file

* Only overwrite the settings.json file if it is newer than the old version.

* Fix tests

* Fix test names and added comment
This commit is contained in:
Tomas Agustin Raies
2020-05-04 14:40:32 -07:00
committed by GitHub
parent d401474981
commit 7fc140af01
13 changed files with 174 additions and 46 deletions

View File

@@ -59,7 +59,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
this.AutoDownloadUpdates = false;
this.Theme = "system";
this.SystemTheme = "light";
this.PowertoysVersion = "v0.15.3";
this.PowertoysVersion = interop.CommonManaged.GetProductVersion();
this.Enabled = new EnabledModules();
this.CustomActionName = string.Empty;
}

View File

@@ -5,9 +5,11 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.PowerToys.Settings.UI.Lib.CustomAction;
namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities
@@ -78,5 +80,42 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities
{
return layoutMap.GetKeyName(key);
}
public static string GetProductVersion()
{
return interop.CommonManaged.GetProductVersion();
}
public static int CompareVersions(string version1, string version2)
{
try
{
// Split up the version strings into int[]
// Example: v10.0.2 -> {10, 0, 2};
var v1 = version1.Substring(1).Split('.').Select(int.Parse).ToArray();
var v2 = version2.Substring(1).Split('.').Select(int.Parse).ToArray();
if (v1.Count() != 3 || v2.Count() != 3)
{
throw new FormatException();
}
if (v1[0] - v2[0] != 0)
{
return v1[0] - v2[0];
}
if (v1[1] - v2[1] != 0)
{
return v1[1] - v2[1];
}
return v1[2] - v2[2];
}
catch (Exception)
{
throw new FormatException("Bad product version format");
}
}
}
}

View File

@@ -10,6 +10,8 @@ using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using Windows.Data.Html;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
@@ -32,6 +34,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
try
{
GeneralSettingsConfigs = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
if (Helper.CompareVersions(GeneralSettingsConfigs.PowertoysVersion, Helper.GetProductVersion()) < 0)
{
// Update settings
GeneralSettingsConfigs.PowertoysVersion = Helper.GetProductVersion();
SettingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty);
}
}
catch (FormatException e)
{
// If there is an issue with the version number format, don't migrate settings.
Debug.WriteLine(e.Message);
}
catch
{
@@ -216,6 +230,14 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public string PowerToysVersion
{
get
{
return Helper.GetProductVersion();
}
}
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
// Notify UI of property change

View File

@@ -89,7 +89,7 @@
Style="{StaticResource SettingsGroupTitleStyle}"
Margin="{StaticResource XSmallBottomMargin}"/>
<TextBlock Text="Version 0.15.2"
<TextBlock Text="{x:Bind ViewModel.PowerToysVersion }"
FontWeight="Bold"
Margin="{StaticResource SmallTopMargin}" />

View File

@@ -119,6 +119,7 @@
<SDKReference Include="TestPlatform.Universal, Version=$(UnitTestPlatformVersion)" />
</ItemGroup>
<ItemGroup>
<Compile Include="ModelsTests\HelperTest.cs" />
<Compile Include="ViewModelTests\ImageResizer.cs" />
<Compile Include="ModelsTests\BasePTModuleSettingsTest.cs" />
<Compile Include="ModelsTests\BasePTSettingsTest.cs" />

View File

@@ -0,0 +1,73 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using System;
namespace CommonLibTest
{
[TestClass]
public class HelperTest
{
public static void TestStringIsSmaller(string v1, string v2)
{
var res = Helper.CompareVersions(v1, v2);
Assert.IsTrue(res < 0);
}
public static void TestStringsAreEqual(string v1, string v2)
{
var res = Helper.CompareVersions(v1, v2);
Assert.IsTrue(res == 0);
}
[TestMethod]
public void Helper_CompareVersions_ShouldBeEqual_WhenSuccessful()
{
TestStringsAreEqual("v0.0.0", "v0.0.0");
TestStringsAreEqual("v0.1.1", "v0.1.1");
TestStringsAreEqual("v1.1.1", "v1.1.1");
TestStringsAreEqual("v1.999.99", "v1.999.99");
}
[TestMethod]
public void Helper_CompareVersions_ShouldBeSmaller_WhenSuccessful()
{
TestStringIsSmaller("v0.0.0", "v0.0.1");
TestStringIsSmaller("v0.0.0", "v0.1.0");
TestStringIsSmaller("v0.0.0", "v1.0.0");
TestStringIsSmaller("v1.0.1", "v1.0.2");
TestStringIsSmaller("v1.1.1", "v1.1.2");
TestStringIsSmaller("v1.1.1", "v1.2.0");
TestStringIsSmaller("v1.999.99", "v2.0.0");
TestStringIsSmaller("v1.0.99", "v1.2.0");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenNoVersionString()
{
Helper.CompareVersions("v0.0.1", "");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenShortVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenLongVersionString()
{
Helper.CompareVersions("v0.0.1", "v0.0.0.1");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Helper_CompareVersions_ShouldThrowBadFormat_WhenItIsNotAVersionString()
{
Helper.CompareVersions("v0.0.1", "HelloWorld");
}
}
}