flabbet 4 年 前
コミット
97bd9d1310

+ 7 - 2
PixiEditor.UpdateModule/UpdateChecker.cs

@@ -54,13 +54,18 @@ namespace PixiEditor.UpdateModule
             return latestRelease.WasDataFetchSuccessful && VersionBigger(CurrentVersionTag, latestRelease.TagName);
         }
 
+        public bool IsUpdateCompatible(string[] incompatibleVersions)
+        {
+            return !incompatibleVersions.Select(x => x.Trim()).Contains(CurrentVersionTag.Trim());
+        }
+
         public async Task<bool> IsUpdateCompatible()
         {
             string[] incompatibleVersions = await GetUpdateIncompatibleVersionsAsync(LatestReleaseInfo.TargetCommitish);
-            return !incompatibleVersions.Contains(CurrentVersionTag);
+            return IsUpdateCompatible(incompatibleVersions);
         }
 
-        private async Task<string[]> GetUpdateIncompatibleVersionsAsync(string targetCommitish)
+        public async Task<string[]> GetUpdateIncompatibleVersionsAsync(string targetCommitish)
         {
             using (HttpClient client = new HttpClient())
             {

+ 37 - 30
PixiEditor/Views/MainWindow.xaml.cs

@@ -13,11 +13,12 @@ using PixiEditor.ViewModels;
 namespace PixiEditor
 {
     /// <summary>
-    ///     Interaction logic for MainWindow.xaml
+    ///     Interaction logic for MainWindow.xaml.
     /// </summary>
     public partial class MainWindow : Window
     {
-        ViewModelMain viewModel;
+        private readonly ViewModelMain viewModel;
+
         public MainWindow()
         {
             InitializeComponent();
@@ -32,19 +33,16 @@ namespace PixiEditor
             e.CanExecute = true;
         }
 
-
         private void CommandBinding_Executed_Minimize(object sender, ExecutedRoutedEventArgs e)
         {
             SystemCommands.MinimizeWindow(this);
         }
 
-
         private void CommandBinding_Executed_Maximize(object sender, ExecutedRoutedEventArgs e)
         {
             SystemCommands.MaximizeWindow(this);
         }
 
-
         private void CommandBinding_Executed_Restore(object sender, ExecutedRoutedEventArgs e)
         {
             SystemCommands.RestoreWindow(this);
@@ -55,7 +53,6 @@ namespace PixiEditor
             SystemCommands.CloseWindow(this);
         }
 
-
         private void MainWindowStateChangeRaised(object sender, EventArgs e)
         {
             if (WindowState == WindowState.Maximized)
@@ -81,33 +78,43 @@ namespace PixiEditor
             string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
             if (updateZipExists && File.Exists(updaterPath))
             {
-                try
-                {
-                    ProcessHelper.RunAsAdmin(updaterPath);
-                    Close();
-                }
-                catch (Win32Exception)
-                {
-                    MessageBox.Show(
-                        "Couldn't update without administrator rights.",
-                        "Insufficient permissions",
-                        MessageBoxButton.OK,
-                        MessageBoxImage.Error);
-                }
+                InstallHeadless(updaterPath);
             }
             else if (updateExeExists)
             {
-                bool alreadyUpdated = AssemblyHelper.GetCurrentAssemblyVersion() ==
-                    updateExeFiles[0].Split('-')[1].Split(".exe")[0];
-                if (!alreadyUpdated)
-                {
-                    Process.Start(updateExeFiles[0]);
-                    Close();
-                }
-                else
-                {
-                    File.Delete(updateExeFiles[0]);
-                }
+                OpenExeInstaller(updateExeFiles[0]);
+            }
+        }
+
+        private void InstallHeadless(string updaterPath)
+        {
+            try
+            {
+                ProcessHelper.RunAsAdmin(updaterPath);
+                Close();
+            }
+            catch (Win32Exception)
+            {
+                MessageBox.Show(
+                    "Couldn't update without administrator rights.",
+                    "Insufficient permissions",
+                    MessageBoxButton.OK,
+                    MessageBoxImage.Error);
+            }
+        }
+
+        private void OpenExeInstaller(string updateExeFile)
+        {
+            bool alreadyUpdated = AssemblyHelper.GetCurrentAssemblyVersion() ==
+                    updateExeFile.Split('-')[1].Split(".exe")[0];
+            if (!alreadyUpdated)
+            {
+                Process.Start(updateExeFile);
+                Close();
+            }
+            else
+            {
+                File.Delete(updateExeFile);
             }
         }
     }

+ 1 - 0
PixiEditorTests/PixiEditorTests.csproj

@@ -18,6 +18,7 @@
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
+    <PackageReference Include="Moq" Version="4.15.1" />
     <PackageReference Include="OpenCover" Version="4.7.922" />
     <PackageReference Include="xunit" Version="2.4.1" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

+ 14 - 1
PixiEditorTests/UpdateModuleTests/UpdateCheckerTests.cs

@@ -1,4 +1,6 @@
-using PixiEditor.UpdateModule;
+using Moq;
+using PixiEditor.UpdateModule;
+using System.Threading.Tasks;
 using Xunit;
 
 namespace PixiEditorTests.UpdateModuleTests
@@ -28,5 +30,16 @@ namespace PixiEditorTests.UpdateModuleTests
         {
             Assert.True(UpdateChecker.VersionBigger(currentVersion, newVersion) == expectedValue);
         }
+
+        [Theory]
+        [InlineData("0.1.3.5", new string[] { "" }, true)]
+        [InlineData("0.1.3.5", new string[] { "0.1.3.5" }, false)]
+        [InlineData("0.1.0.0", new string[] { "0.1.3.5", "0.4.2.1" }, true)]
+        [InlineData("0.1.2.2", new string[] { " 0.1.2.2 " }, false)]
+        public void TestThatIsUpdateCompatibleChecksVersionCorrectly(string version, string[] incompatibleVersions, bool expectedResult)
+        {
+            UpdateChecker checker = new UpdateChecker(version);
+            Assert.Equal(expectedResult, checker.IsUpdateCompatible(incompatibleVersions));
+        }
     }
 }