Browse Source

Handle exceptions when installing and uninstalling NuGet packages.

pull/21/head
mrward 15 years ago
parent
commit
f3e3671a3c
  1. 2
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. BIN
      src/AddIns/Misc/PackageManagement/Project/Resources/exclamation.png
  3. 29
      src/AddIns/Misc/PackageManagement/Project/Src/AddPackageReferenceView.xaml
  4. 40
      src/AddIns/Misc/PackageManagement/Project/Src/AddPackageReferenceViewModel.cs
  5. 3
      src/AddIns/Misc/PackageManagement/Project/Src/AvailablePackagesViewModel.cs
  6. 2
      src/AddIns/Misc/PackageManagement/Project/Src/Design/DesignTimePackagesViewModel.cs
  7. 4
      src/AddIns/Misc/PackageManagement/Project/Src/Design/FakePackageManagementOutputMessagesView.cs
  8. 19
      src/AddIns/Misc/PackageManagement/Project/Src/Design/FakePackageManagementService.cs
  9. 13
      src/AddIns/Misc/PackageManagement/Project/Src/IMessageReporter.cs
  10. 3
      src/AddIns/Misc/PackageManagement/Project/Src/InstalledPackagesViewModel.cs
  11. 3
      src/AddIns/Misc/PackageManagement/Project/Src/PackageUpdatesViewModel.cs
  12. 47
      src/AddIns/Misc/PackageManagement/Project/Src/PackageViewModel.cs
  13. 13
      src/AddIns/Misc/PackageManagement/Project/Src/PackageViewModelFactory.cs
  14. 9
      src/AddIns/Misc/PackageManagement/Project/Src/PackagesViewModel.cs
  15. 7
      src/AddIns/Misc/PackageManagement/Project/Src/RecentPackagesViewModel.cs
  16. 3
      src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
  17. 103
      src/AddIns/Misc/PackageManagement/Test/Src/AddPackageReferenceViewModelTests.cs
  18. 3
      src/AddIns/Misc/PackageManagement/Test/Src/AvailablePackagesViewModelTests.cs
  19. 36
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/ExceptionThrowingPackageManagementService.cs
  20. 19
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/ExceptionThrowingPackageOperationResolver.cs
  21. 24
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakeMessageReporter.cs
  22. 2
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageOperationResolver.cs
  23. 15
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/TestablePackageViewModel.cs
  24. 2
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/TestablePackagesViewModel.cs
  25. 27
      src/AddIns/Misc/PackageManagement/Test/Src/InstalledPackagesViewModelTests.cs
  26. 3
      src/AddIns/Misc/PackageManagement/Test/Src/PackageUpdatesViewModelTests.cs
  27. 107
      src/AddIns/Misc/PackageManagement/Test/Src/PackageViewModelTests.cs
  28. 3
      src/AddIns/Misc/PackageManagement/Test/Src/RecentPackagesViewModelTests.cs

2
src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj

@ -85,6 +85,7 @@ @@ -85,6 +85,7 @@
<Compile Include="Src\Design\FakeProjectSystem.cs" />
<Compile Include="Src\Design\WpfDesigner.cs" />
<Compile Include="Src\ICompilerMessageView.cs" />
<Compile Include="Src\IMessageReporter.cs" />
<Compile Include="Src\IMessageViewCategory.cs" />
<Compile Include="Src\IPackageManagementOutputMessagesView.cs" />
<Compile Include="Src\IPackageRepositoryCache.cs" />
@ -198,6 +199,7 @@ @@ -198,6 +199,7 @@
<Resource Include="Resources\resultset_next.png" />
<Resource Include="Resources\resultset_previous.png" />
<Resource Include="Resources\accept.png" />
<Resource Include="Resources\exclamation.png" />
<None Include="Resources\license.txt" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />

BIN
src/AddIns/Misc/PackageManagement/Project/Resources/exclamation.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 701 B

29
src/AddIns/Misc/PackageManagement/Project/Src/AddPackageReferenceView.xaml

@ -15,6 +15,10 @@ @@ -15,6 +15,10 @@
<Window.Resources>
<Style TargetType="Button" BasedOn="{x:Static core:GlobalStyles.ButtonStyle}"/>
<pm:ViewModelLocator x:Key="ViewModelLocator"/>
<BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
<BitmapImage x:Key="ErrorIcon"
UriSource="pack://application:,,,/PackageManagement;component/Resources/exclamation.png"/>
</Window.Resources>
<DockPanel>
@ -23,15 +27,30 @@ @@ -23,15 +27,30 @@
<DockPanel.DataContext>
<Binding Source="{StaticResource ViewModelLocator}" Path="AddPackageReferenceViewModel"/>
</DockPanel.DataContext>
<StackPanel
<Grid
DockPanel.Dock="Bottom"
Margin="4, 4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel>
<Image
Margin="4, 4"
Visibility="{Binding Path=HasError, Converter={StaticResource BoolToVisibility}}"
Source="{StaticResource ErrorIcon}"/>
</StackPanel>
<TextBlock
Grid.Column="1"
Margin="4, 4"
HorizontalAlignment="Right">
TextTrimming="CharacterEllipsis"
Text="{Binding Path=Message}"/>
<Button
Grid.Column="2"
Content="{core:Localize Global.CloseButtonText}"
IsCancel="True">
</Button>
</StackPanel>
IsCancel="True"/>
</Grid>
<TabControl>
<TabItem Header="Available">
<pm:PackagesView

40
src/AddIns/Misc/PackageManagement/Project/Src/AddPackageReferenceViewModel.cs

@ -8,13 +8,15 @@ using NuGet; @@ -8,13 +8,15 @@ using NuGet;
namespace ICSharpCode.PackageManagement
{
public class AddPackageReferenceViewModel : ViewModelBase<AddPackageReferenceViewModel>
public class AddPackageReferenceViewModel : ViewModelBase<AddPackageReferenceViewModel>, IMessageReporter
{
IPackageManagementService packageManagementService;
InstalledPackagesViewModel installedPackagesViewModel;
AvailablePackagesViewModel availablePackagesViewModel;
PackageUpdatesViewModel packageUpdatesViewModel;
RecentPackagesViewModel recentPackagesViewModel;
string message;
bool hasError;
public AddPackageReferenceViewModel(
IPackageManagementService packageManagementService,
@ -23,10 +25,10 @@ namespace ICSharpCode.PackageManagement @@ -23,10 +25,10 @@ namespace ICSharpCode.PackageManagement
this.packageManagementService = packageManagementService;
this.packageManagementService.OutputMessagesView.Clear();
availablePackagesViewModel = new AvailablePackagesViewModel(packageManagementService, taskFactory);
installedPackagesViewModel = new InstalledPackagesViewModel(packageManagementService, taskFactory);
packageUpdatesViewModel = new PackageUpdatesViewModel(packageManagementService, taskFactory);
recentPackagesViewModel = new RecentPackagesViewModel(packageManagementService, taskFactory);
availablePackagesViewModel = new AvailablePackagesViewModel(packageManagementService, this, taskFactory);
installedPackagesViewModel = new InstalledPackagesViewModel(packageManagementService, this, taskFactory);
packageUpdatesViewModel = new PackageUpdatesViewModel(packageManagementService, this, taskFactory);
recentPackagesViewModel = new RecentPackagesViewModel(packageManagementService, this, taskFactory);
availablePackagesViewModel.ReadPackages();
installedPackagesViewModel.ReadPackages();
@ -49,5 +51,33 @@ namespace ICSharpCode.PackageManagement @@ -49,5 +51,33 @@ namespace ICSharpCode.PackageManagement
public RecentPackagesViewModel RecentPackagesViewModel {
get { return recentPackagesViewModel; }
}
public void ShowErrorMessage(string message)
{
this.Message = message;
this.HasError = true;
}
public string Message {
get { return message; }
set {
message = value;
OnPropertyChanged(model => model.Message);
}
}
public bool HasError {
get { return hasError; }
set {
hasError = value;
OnPropertyChanged(model => model.HasError);
}
}
public void ClearMessage()
{
this.Message = null;
this.HasError = false;
}
}
}

3
src/AddIns/Misc/PackageManagement/Project/Src/AvailablePackagesViewModel.cs

@ -15,8 +15,9 @@ namespace ICSharpCode.PackageManagement @@ -15,8 +15,9 @@ namespace ICSharpCode.PackageManagement
public AvailablePackagesViewModel(
IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: base(packageManagementService, taskFactory)
: base(packageManagementService, messageReporter, taskFactory)
{
IsSearchable = true;
ShowPackageSources = packageManagementService.HasMultiplePackageSources;

2
src/AddIns/Misc/PackageManagement/Project/Src/Design/DesignTimePackagesViewModel.cs

@ -11,7 +11,7 @@ namespace ICSharpCode.PackageManagement.Design @@ -11,7 +11,7 @@ namespace ICSharpCode.PackageManagement.Design
public class DesignTimePackagesViewModel : PackagesViewModel
{
public DesignTimePackagesViewModel()
: base(new DesignTimePackageManagementService(), new PackageManagementTaskFactory())
: base(new DesignTimePackageManagementService(), (IMessageReporter)null, new PackageManagementTaskFactory())
{
PageSize = 3;
AddPackageViewModels();

4
src/AddIns/Misc/PackageManagement/Project/Src/Design/FakePackageManagementOutputMessagesView.cs

@ -31,6 +31,10 @@ namespace ICSharpCode.PackageManagement.Design @@ -31,6 +31,10 @@ namespace ICSharpCode.PackageManagement.Design
}
}
public string SecondFormattedMessageLogged {
get { return FormattedMessagesLogged[1]; }
}
public void Clear()
{
IsClearCalled = true;

19
src/AddIns/Misc/PackageManagement/Project/Src/Design/FakePackageManagementService.cs

@ -55,16 +55,11 @@ namespace ICSharpCode.PackageManagement.Design @@ -55,16 +55,11 @@ namespace ICSharpCode.PackageManagement.Design
public IPackageRepository ActivePackageRepository { get; set; }
public IProjectManager ActiveProjectManager {
get {
if (ActiveProjectManagerExeptionToThrow != null) {
throw ActiveProjectManagerExeptionToThrow;
}
return FakeActiveProjectManager;
}
public virtual IProjectManager ActiveProjectManager {
get { return FakeActiveProjectManager; }
}
public void InstallPackage(IPackageRepository repository, IPackage package, IEnumerable<PackageOperation> operations)
public virtual void InstallPackage(IPackageRepository repository, IPackage package, IEnumerable<PackageOperation> operations)
{
IsInstallPackageCalled = true;
RepositoryPassedToInstallPackage = repository;
@ -72,7 +67,7 @@ namespace ICSharpCode.PackageManagement.Design @@ -72,7 +67,7 @@ namespace ICSharpCode.PackageManagement.Design
PackageOperationsPassedToInstallPackage.AddRange(operations);
}
public void UninstallPackage(IPackageRepository repository, IPackage package)
public virtual void UninstallPackage(IPackageRepository repository, IPackage package)
{
RepositoryPassedToUninstallPackage = repository;
PackagePassedToUninstallPackage = package;
@ -112,9 +107,7 @@ namespace ICSharpCode.PackageManagement.Design @@ -112,9 +107,7 @@ namespace ICSharpCode.PackageManagement.Design
public void AddPackageSources(IEnumerable<PackageSource> sources)
{
foreach (PackageSource source in sources) {
options.PackageSources.Add(source);
}
options.PackageSources.AddRange(sources);
}
public PackageSource ActivePackageSource { get; set; }
@ -126,8 +119,6 @@ namespace ICSharpCode.PackageManagement.Design @@ -126,8 +119,6 @@ namespace ICSharpCode.PackageManagement.Design
return FakeAggregateRepository;
}
public Exception ActiveProjectManagerExeptionToThrow { get; set; }
public FakePackageManagementOutputMessagesView FakeOutputMessagesView = new FakePackageManagementOutputMessagesView();
public IPackageManagementOutputMessagesView OutputMessagesView {

13
src/AddIns/Misc/PackageManagement/Project/Src/IMessageReporter.cs

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
namespace ICSharpCode.PackageManagement
{
public interface IMessageReporter
{
void ShowErrorMessage(string message);
void ClearMessage();
}
}

3
src/AddIns/Misc/PackageManagement/Project/Src/InstalledPackagesViewModel.cs

@ -16,8 +16,9 @@ namespace ICSharpCode.PackageManagement @@ -16,8 +16,9 @@ namespace ICSharpCode.PackageManagement
public InstalledPackagesViewModel(
IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: base(packageManagementService, taskFactory)
: base(packageManagementService, messageReporter, taskFactory)
{
packageManagementService.PackageInstalled += PackageInstalled;
packageManagementService.PackageUninstalled += PackageUninstalled;

3
src/AddIns/Misc/PackageManagement/Project/Src/PackageUpdatesViewModel.cs

@ -18,8 +18,9 @@ namespace ICSharpCode.PackageManagement @@ -18,8 +18,9 @@ namespace ICSharpCode.PackageManagement
public PackageUpdatesViewModel(
IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: base(packageManagementService, taskFactory)
: base(packageManagementService, messageReporter, taskFactory)
{
this.packageManagementService = packageManagementService;
}

47
src/AddIns/Misc/PackageManagement/Project/Src/PackageViewModel.cs

@ -16,6 +16,7 @@ namespace ICSharpCode.PackageManagement @@ -16,6 +16,7 @@ namespace ICSharpCode.PackageManagement
IPackageManagementService packageManagementService;
ILicenseAcceptanceService licenseAcceptanceService;
IMessageReporter messageReporter;
IPackage package;
IEnumerable<PackageOperation> packageOperations = new PackageOperation[0];
IPackageRepository sourcePackageRepository;
@ -25,12 +26,14 @@ namespace ICSharpCode.PackageManagement @@ -25,12 +26,14 @@ namespace ICSharpCode.PackageManagement
public PackageViewModel(
IPackage package,
IPackageManagementService packageManagementService,
ILicenseAcceptanceService licenseAcceptanceService)
ILicenseAcceptanceService licenseAcceptanceService,
IMessageReporter messageReporter)
{
this.package = package;
this.sourcePackageRepository = packageManagementService.ActivePackageRepository;
this.packageManagementService = packageManagementService;
this.licenseAcceptanceService = licenseAcceptanceService;
this.messageReporter = messageReporter;
CreateCommands();
}
@ -166,14 +169,17 @@ namespace ICSharpCode.PackageManagement @@ -166,14 +169,17 @@ namespace ICSharpCode.PackageManagement
public void AddPackage()
{
ClearReportedMessages();
LogAddingPackage();
GetPackageOperations();
if (CanInstallPackage()) {
InstallPackage();
}
TryInstallingPackage();
LogAfterPackageOperationCompletes();
}
void ClearReportedMessages()
{
messageReporter.ClearMessage();
}
void LogAddingPackage()
{
string message = GetFormattedStartPackageOperationMessage(AddingPackageMessageFormat);
@ -276,16 +282,35 @@ namespace ICSharpCode.PackageManagement @@ -276,16 +282,35 @@ namespace ICSharpCode.PackageManagement
return package.RequireLicenseAcceptance && !IsPackageInstalled(package);
}
void TryInstallingPackage()
{
try {
GetPackageOperations();
if (CanInstallPackage()) {
InstallPackage();
}
} catch (Exception ex) {
ReportError(ex);
Log(ex.ToString());
}
}
void InstallPackage()
{
packageManagementService.InstallPackage(sourcePackageRepository, package, packageOperations);
OnPropertyChanged(model => model.IsAdded);
}
void ReportError(Exception ex)
{
messageReporter.ShowErrorMessage(ex.Message);
}
public void RemovePackage()
{
ClearReportedMessages();
LogRemovingPackage();
packageManagementService.UninstallPackage(sourcePackageRepository, package);
TryUninstallingPackage();
LogAfterPackageOperationCompletes();
OnPropertyChanged(model => model.IsAdded);
@ -300,5 +325,15 @@ namespace ICSharpCode.PackageManagement @@ -300,5 +325,15 @@ namespace ICSharpCode.PackageManagement
protected virtual string RemovingPackageMessageFormat {
get { return "Uninstalling...{0}"; }
}
void TryUninstallingPackage()
{
try {
packageManagementService.UninstallPackage(sourcePackageRepository, package);
} catch (Exception ex) {
ReportError(ex);
Log(ex.ToString());
}
}
}
}

13
src/AddIns/Misc/PackageManagement/Project/Src/PackageViewModelFactory.cs

@ -10,18 +10,16 @@ namespace ICSharpCode.PackageManagement @@ -10,18 +10,16 @@ namespace ICSharpCode.PackageManagement
{
IPackageManagementService packageManagementService;
ILicenseAcceptanceService licenseAcceptanceService;
public PackageViewModelFactory(IPackageManagementService packageManagementService)
: this(packageManagementService, null)
{
}
IMessageReporter messageReporter;
public PackageViewModelFactory(
IPackageManagementService packageManagementService,
ILicenseAcceptanceService licenseAcceptanceService)
ILicenseAcceptanceService licenseAcceptanceService,
IMessageReporter messageReport)
{
this.packageManagementService = packageManagementService;
this.licenseAcceptanceService = licenseAcceptanceService;
this.messageReporter = messageReport;
}
public PackageViewModel CreatePackageViewModel(IPackage package)
@ -29,7 +27,8 @@ namespace ICSharpCode.PackageManagement @@ -29,7 +27,8 @@ namespace ICSharpCode.PackageManagement
return new PackageViewModel(
package,
packageManagementService,
licenseAcceptanceService);
licenseAcceptanceService,
messageReporter);
}
}
}

9
src/AddIns/Misc/PackageManagement/Project/Src/PackagesViewModel.cs

@ -33,10 +33,14 @@ namespace ICSharpCode.PackageManagement @@ -33,10 +33,14 @@ namespace ICSharpCode.PackageManagement
bool hasError;
string errorMessage = String.Empty;
public PackagesViewModel(IPackageManagementService packageManagementService, ITaskFactory taskFactory)
public PackagesViewModel(
IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: this(
packageManagementService,
new LicenseAcceptanceService(),
messageReporter,
taskFactory)
{
}
@ -44,10 +48,11 @@ namespace ICSharpCode.PackageManagement @@ -44,10 +48,11 @@ namespace ICSharpCode.PackageManagement
public PackagesViewModel(
IPackageManagementService packageManagementService,
ILicenseAcceptanceService licenseAcceptanceService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: this(
packageManagementService,
new PackageViewModelFactory(packageManagementService, licenseAcceptanceService),
new PackageViewModelFactory(packageManagementService, licenseAcceptanceService, messageReporter),
taskFactory)
{
}

7
src/AddIns/Misc/PackageManagement/Project/Src/RecentPackagesViewModel.cs

@ -11,8 +11,11 @@ namespace ICSharpCode.PackageManagement @@ -11,8 +11,11 @@ namespace ICSharpCode.PackageManagement
{
IPackageRepository recentPackageRepository;
public RecentPackagesViewModel(IPackageManagementService packageManagementService, ITaskFactory taskFactory)
: base(packageManagementService, taskFactory)
public RecentPackagesViewModel(
IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory)
: base(packageManagementService, messageReporter, taskFactory)
{
recentPackageRepository = packageManagementService.RecentPackageRepository;
packageManagementService.PackageInstalled += PackageInstalled;

3
src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj

@ -68,10 +68,13 @@ @@ -68,10 +68,13 @@
<Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Src\Helpers\ExceptionThrowingPackageManagementService.cs" />
<Compile Include="Src\Helpers\ExceptionThrowingPackageOperationResolver.cs" />
<Compile Include="Src\Helpers\FakeCompilerMessageView.cs" />
<Compile Include="Src\Helpers\FakeFileService.cs" />
<Compile Include="Src\Helpers\FakeLicenseAcceptanceService.cs" />
<Compile Include="Src\Helpers\FakeMessageCategoryView.cs" />
<Compile Include="Src\Helpers\FakeMessageReporter.cs" />
<Compile Include="Src\Helpers\FakePackageManager.cs" />
<Compile Include="Src\Helpers\FakePackageManagerFactory.cs" />
<Compile Include="Src\Helpers\FakePackageOperationResolver.cs" />

103
src/AddIns/Misc/PackageManagement/Test/Src/AddPackageReferenceViewModelTests.cs

@ -37,6 +37,27 @@ namespace PackageManagement.Tests @@ -37,6 +37,27 @@ namespace PackageManagement.Tests
taskFactory.ExecuteAllFakeTasks();
}
List<string> CallShowErrorMessageAndRecordPropertiesChanged(string message)
{
var propertyNamesChanged = RecordViewModelPropertiesChanged();
viewModel.ShowErrorMessage(message);
return propertyNamesChanged;
}
List<string> RecordViewModelPropertiesChanged()
{
var propertyNamesChanged = new List<string>();
viewModel.PropertyChanged += (sender, e) => propertyNamesChanged.Add(e.PropertyName);
return propertyNamesChanged;
}
List<string> CallClearMessageAndRecordPropertiesChanged()
{
var propertyNamesChanged = RecordViewModelPropertiesChanged();
viewModel.ClearMessage();
return propertyNamesChanged;
}
[Test]
public void InstalledPackagesViewModel_ProjectHasOneInstalledPackage_HasOnePackageViewModel()
{
@ -112,5 +133,87 @@ namespace PackageManagement.Tests @@ -112,5 +133,87 @@ namespace PackageManagement.Tests
PackageCollectionAssert.AreEqual(expectedPackages, viewModel.RecentPackagesViewModel.PackageViewModels);
}
[Test]
public void ShowErrorMessage_ErrorMessageToBeDisplayedToUser_MessageIsSet()
{
CreateViewModel();
viewModel.ShowErrorMessage("Test");
Assert.AreEqual("Test", viewModel.Message);
}
[Test]
public void ShowErrorMessage_ErrorMessageToBeDisplayedToUser_MessagePropertyIsChanged()
{
CreateViewModel();
List<string> propertyNamesChanged = CallShowErrorMessageAndRecordPropertiesChanged("Test");
bool result = propertyNamesChanged.Contains("Message");
Assert.IsTrue(result);
}
[Test]
public void ShowErrorMessage_ErrorMessageToBeDisplayedToUser_HasErrorIsTrue()
{
CreateViewModel();
viewModel.ShowErrorMessage("Test");
Assert.IsTrue(viewModel.HasError);
}
[Test]
public void ShowErrorMessage_ErrorMessageToBeDisplayedToUser_HasErrorPropertyIsChanged()
{
CreateViewModel();
List<string> propertyNamesChanged = CallShowErrorMessageAndRecordPropertiesChanged("Test");
bool result = propertyNamesChanged.Contains("HasError");
Assert.IsTrue(result);
}
[Test]
public void ClearMessage_ErrorMessageCurrentlyDisplayed_MessageIsCleared()
{
CreateViewModel();
viewModel.Message = "test";
viewModel.ClearMessage();
Assert.IsNull(viewModel.Message);
}
[Test]
public void ClearMessage_ErrorMessageCurrentlyDisplayed_MessagePropertyIsChanged()
{
CreateViewModel();
List<string> propertyNamesChanged = CallClearMessageAndRecordPropertiesChanged();
bool result = propertyNamesChanged.Contains("Message");
Assert.IsTrue(result);
}
[Test]
public void ClearMessages_ErrorMessageCurrentlyDisplayed_HasErrorPropertyIsChanged()
{
CreateViewModel();
List<string> propertyNamesChanged = CallClearMessageAndRecordPropertiesChanged();
bool result = propertyNamesChanged.Contains("HasError");
Assert.IsTrue(result);
}
[Test]
public void ClearMessage_ErrorMessageCurrentlyDisplayed_HasErrorIsFalse()
{
CreateViewModel();
viewModel.HasError = true;
viewModel.ClearMessage();
Assert.IsFalse(viewModel.HasError);
}
}
}

3
src/AddIns/Misc/PackageManagement/Test/Src/AvailablePackagesViewModelTests.cs

@ -32,7 +32,8 @@ namespace PackageManagement.Tests @@ -32,7 +32,8 @@ namespace PackageManagement.Tests
void CreateViewModel(IPackageManagementService packageManagementService)
{
taskFactory = new FakeTaskFactory();
viewModel = new AvailablePackagesViewModel(packageManagementService, taskFactory);
var messageReporter = new FakeMessageReporter();
viewModel = new AvailablePackagesViewModel(packageManagementService, messageReporter, taskFactory);
}
void CompleteReadPackagesTask()

36
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/ExceptionThrowingPackageManagementService.cs

@ -0,0 +1,36 @@ @@ -0,0 +1,36 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.PackageManagement.Design;
using NuGet;
namespace PackageManagement.Tests.Helpers
{
public class ExceptionThrowingPackageManagementService : FakePackageManagementService
{
public Exception ExeptionToThrowWhenActiveProjectManagerAccessed { get; set; }
public Exception ExeptionToThrowWhenInstallPackageCalled { get; set; }
public Exception ExeptionToThrowWhenUninstallPackageCalled { get; set; }
public override IProjectManager ActiveProjectManager {
get {
if (ExeptionToThrowWhenActiveProjectManagerAccessed != null) {
throw ExeptionToThrowWhenActiveProjectManagerAccessed;
}
return base.ActiveProjectManager;
}
}
public override void InstallPackage(IPackageRepository repository, IPackage package, IEnumerable<PackageOperation> operations)
{
throw ExeptionToThrowWhenInstallPackageCalled;
}
public override void UninstallPackage(IPackageRepository repository, IPackage package)
{
throw ExeptionToThrowWhenUninstallPackageCalled;
}
}
}

19
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/ExceptionThrowingPackageOperationResolver.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using NuGet;
namespace PackageManagement.Tests.Helpers
{
public class ExceptionThrowingPackageOperationResolver : FakePackageOperationResolver
{
public Exception ResolveOperationsExceptionToThrow;
public override IEnumerable<PackageOperation> ResolveOperations(IPackage package)
{
throw ResolveOperationsExceptionToThrow;
}
}
}

24
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakeMessageReporter.cs

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.PackageManagement;
namespace PackageManagement.Tests.Helpers
{
public class FakeMessageReporter : IMessageReporter
{
public string MessagePassedToShowErrorMessage;
public bool IsClearMessageCalled;
public void ShowErrorMessage(string message)
{
MessagePassedToShowErrorMessage = message;
}
public void ClearMessage()
{
IsClearMessageCalled = true;
}
}
}

2
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageOperationResolver.cs

@ -11,7 +11,7 @@ namespace PackageManagement.Tests.Helpers @@ -11,7 +11,7 @@ namespace PackageManagement.Tests.Helpers
{
public List<PackageOperation> PackageOperations = new List<PackageOperation>();
public IEnumerable<PackageOperation> ResolveOperations(IPackage package)
public virtual IEnumerable<PackageOperation> ResolveOperations(IPackage package)
{
return PackageOperations;
}

15
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/TestablePackageViewModel.cs

@ -14,31 +14,36 @@ namespace PackageManagement.Tests.Helpers @@ -14,31 +14,36 @@ namespace PackageManagement.Tests.Helpers
public FakePackageRepository FakeSourcePackageRepository;
public FakePackageManagementService FakePackageManagementService;
public FakeLicenseAcceptanceService FakeLicenseAcceptanceService;
public FakeMessageReporter FakeMessageReporter;
public FakePackage FakePackage;
public ILogger LoggerUsedWhenCreatingPackageResolver;
public string PackageViewModelAddingPackageMessageFormat = String.Empty;
public string PackageViewModelRemovingPackageMessageFormat = String.Empty;
public TestablePackageViewModel()
public TestablePackageViewModel(FakePackageManagementService packageManagementService)
: this(
new FakePackage(),
new FakePackageManagementService(),
new FakeLicenseAcceptanceService())
packageManagementService,
new FakeLicenseAcceptanceService(),
new FakeMessageReporter())
{
}
public TestablePackageViewModel(
FakePackage package,
FakePackageManagementService packageManagementService,
FakeLicenseAcceptanceService licenseAcceptanceService)
FakeLicenseAcceptanceService licenseAcceptanceService,
FakeMessageReporter messageReporter)
: base(
package,
packageManagementService,
licenseAcceptanceService)
licenseAcceptanceService,
messageReporter)
{
this.FakePackage = package;
this.FakePackageManagementService = packageManagementService;
this.FakeLicenseAcceptanceService = licenseAcceptanceService;
this.FakeMessageReporter = messageReporter;
this.FakeSourcePackageRepository = FakePackageManagementService.FakeActivePackageRepository;
}

2
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/TestablePackagesViewModel.cs

@ -31,7 +31,7 @@ namespace PackageManagement.Tests.Helpers @@ -31,7 +31,7 @@ namespace PackageManagement.Tests.Helpers
}
public TestablePackagesViewModel(FakePackageManagementService packageManagementService, FakeTaskFactory taskFactory)
: base(packageManagementService, taskFactory)
: base(packageManagementService, new FakeMessageReporter(), taskFactory)
{
FakePackageManagementService = packageManagementService;
FakeTaskFactory = taskFactory;

27
src/AddIns/Misc/PackageManagement/Test/Src/InstalledPackagesViewModelTests.cs

@ -15,6 +15,7 @@ namespace PackageManagement.Tests @@ -15,6 +15,7 @@ namespace PackageManagement.Tests
{
InstalledPackagesViewModel viewModel;
FakePackageManagementService packageManagementService;
ExceptionThrowingPackageManagementService exceptionThrowingPackageManagementService;
FakeTaskFactory taskFactory;
void CreateViewModel()
@ -28,10 +29,16 @@ namespace PackageManagement.Tests @@ -28,10 +29,16 @@ namespace PackageManagement.Tests
packageManagementService = new FakePackageManagementService();
}
void CreateExceptionThrowingPackageManagementService()
{
exceptionThrowingPackageManagementService = new ExceptionThrowingPackageManagementService();
}
void CreateViewModel(FakePackageManagementService packageManagementService)
{
taskFactory = new FakeTaskFactory();
viewModel = new InstalledPackagesViewModel(packageManagementService, taskFactory);
var messageReporter = new FakeMessageReporter();
viewModel = new InstalledPackagesViewModel(packageManagementService, messageReporter, taskFactory);
}
void CompleteReadPackagesTask()
@ -86,9 +93,9 @@ namespace PackageManagement.Tests @@ -86,9 +93,9 @@ namespace PackageManagement.Tests
[Test]
public void HasError_ActiveProjectManagerThrowsException_ReturnsTrue()
{
CreatePackageManagementService();
packageManagementService.ActiveProjectManagerExeptionToThrow = new Exception();
CreateViewModel(packageManagementService);
CreateExceptionThrowingPackageManagementService();
exceptionThrowingPackageManagementService.ExeptionToThrowWhenActiveProjectManagerAccessed = new Exception();
CreateViewModel(exceptionThrowingPackageManagementService);
Assert.IsTrue(viewModel.HasError);
}
@ -96,9 +103,9 @@ namespace PackageManagement.Tests @@ -96,9 +103,9 @@ namespace PackageManagement.Tests
[Test]
public void ErrorMessage_ActiveProjectManagerThrowsException_ReturnsErrorMessageFromException()
{
CreatePackageManagementService();
packageManagementService.ActiveProjectManagerExeptionToThrow = new Exception("Test");
CreateViewModel(packageManagementService);
CreateExceptionThrowingPackageManagementService();
exceptionThrowingPackageManagementService.ExeptionToThrowWhenActiveProjectManagerAccessed = new Exception("Test");
CreateViewModel(exceptionThrowingPackageManagementService);
Assert.AreEqual("Test", viewModel.ErrorMessage);
}
@ -106,9 +113,9 @@ namespace PackageManagement.Tests @@ -106,9 +113,9 @@ namespace PackageManagement.Tests
[Test]
public void ReadPackages_ActiveProjectManagerThrowsException_ErrorMessageFromExceptionNotOverriddenByReadPackagesCall()
{
CreatePackageManagementService();
packageManagementService.ActiveProjectManagerExeptionToThrow = new Exception("Test");
CreateViewModel(packageManagementService);
CreateExceptionThrowingPackageManagementService();
exceptionThrowingPackageManagementService.ExeptionToThrowWhenActiveProjectManagerAccessed = new Exception("Test");
CreateViewModel(exceptionThrowingPackageManagementService);
viewModel.ReadPackages();
ApplicationException ex = Assert.Throws<ApplicationException>(() => CompleteReadPackagesTask());

3
src/AddIns/Misc/PackageManagement/Test/Src/PackageUpdatesViewModelTests.cs

@ -20,7 +20,8 @@ namespace PackageManagement.Tests @@ -20,7 +20,8 @@ namespace PackageManagement.Tests
{
packageManagementService = new FakePackageManagementService();
taskFactory = new FakeTaskFactory();
viewModel = new PackageUpdatesViewModel(packageManagementService, taskFactory);
var messageReporter = new FakeMessageReporter();
viewModel = new PackageUpdatesViewModel(packageManagementService, messageReporter, taskFactory);
}
void CompleteReadPackagesTask()

107
src/AddIns/Misc/PackageManagement/Test/Src/PackageViewModelTests.cs

@ -18,14 +18,29 @@ namespace PackageManagement.Tests @@ -18,14 +18,29 @@ namespace PackageManagement.Tests
FakePackageManagementService packageManagementService;
FakePackageRepository sourcePackageRepository;
FakeLicenseAcceptanceService licenseAcceptanceService;
FakeMessageReporter messageReporter;
ExceptionThrowingPackageManagementService exceptionThrowingPackageManagementService;
void CreateViewModel()
{
viewModel = new TestablePackageViewModel();
packageManagementService = new FakePackageManagementService();
CreateViewModel(packageManagementService);
}
void CreateViewModelWithExceptionThrowingPackageManagementService()
{
exceptionThrowingPackageManagementService = new ExceptionThrowingPackageManagementService();
CreateViewModel(exceptionThrowingPackageManagementService);
}
void CreateViewModel(FakePackageManagementService packageManagementService)
{
viewModel = new TestablePackageViewModel(packageManagementService);
package = viewModel.FakePackage;
packageManagementService = viewModel.FakePackageManagementService;
this.packageManagementService = packageManagementService;
sourcePackageRepository = packageManagementService.FakeActivePackageRepository;
licenseAcceptanceService = viewModel.FakeLicenseAcceptanceService;
messageReporter = viewModel.FakeMessageReporter;
}
FakePackage AddPackageDependencyThatDoesNotRequireLicenseAcceptance(string packageId)
@ -466,7 +481,7 @@ namespace PackageManagement.Tests @@ -466,7 +481,7 @@ namespace PackageManagement.Tests
}
[Test]
public void AddPackage_PackageAddedSuccessfully_NextToLastMesssageLoggedMarksEndOfInstallation()
public void AddPackage_PackageAddedSuccessfully_NextToLastMessageLoggedMarksEndOfInstallation()
{
CreateViewModel();
viewModel.AddPackage();
@ -478,7 +493,7 @@ namespace PackageManagement.Tests @@ -478,7 +493,7 @@ namespace PackageManagement.Tests
}
[Test]
public void AddPackage_PackageAddedSuccessfully_LastMesssageLoggedIsEmptyLine()
public void AddPackage_PackageAddedSuccessfully_LastMessageLoggedIsEmptyLine()
{
CreateViewModel();
viewModel.AddPackage();
@ -505,7 +520,7 @@ namespace PackageManagement.Tests @@ -505,7 +520,7 @@ namespace PackageManagement.Tests
}
[Test]
public void RemovePackage_PackageRemovedSuccessfully_NextToLastMesssageLoggedMarksEndOfInstallation()
public void RemovePackage_PackageRemovedSuccessfully_NextToLastMessageLoggedMarksEndOfInstallation()
{
CreateViewModel();
viewModel.RemovePackage();
@ -517,7 +532,7 @@ namespace PackageManagement.Tests @@ -517,7 +532,7 @@ namespace PackageManagement.Tests
}
[Test]
public void RemovePackage_PackageRemovedSuccessfully_LastMesssageLoggedIsEmptyLine()
public void RemovePackage_PackageRemovedSuccessfully_LastMessageLoggedIsEmptyLine()
{
CreateViewModel();
viewModel.RemovePackage();
@ -527,5 +542,85 @@ namespace PackageManagement.Tests @@ -527,5 +542,85 @@ namespace PackageManagement.Tests
Assert.AreEqual(expectedMessage, actualMessage);
}
[Test]
public void AddPackage_ExceptionWhenInstallingPackage_ExceptionErrorMessageReported()
{
CreateViewModelWithExceptionThrowingPackageManagementService();
Exception ex = new Exception("Test");
exceptionThrowingPackageManagementService.ExeptionToThrowWhenInstallPackageCalled = ex;
viewModel.AddPackage();
Assert.AreEqual("Test", messageReporter.MessagePassedToShowErrorMessage);
}
[Test]
public void AddPackage_PackageAddedSuccessfully_MessagesReportedPreviouslyAreCleared()
{
CreateViewModel();
viewModel.AddPackage();
Assert.IsTrue(messageReporter.IsClearMessageCalled);
}
[Test]
public void AddPackage_ExceptionWhenInstallingPackage_ExceptionLogged()
{
CreateViewModelWithExceptionThrowingPackageManagementService();
Exception ex = new Exception("Exception error message");
exceptionThrowingPackageManagementService.ExeptionToThrowWhenInstallPackageCalled = ex;
viewModel.AddPackage();
string actualMessage = packageManagementService.FakeOutputMessagesView.SecondFormattedMessageLogged;
bool containsExceptionErrorMessage = actualMessage.Contains("Exception error message");
Assert.IsTrue(containsExceptionErrorMessage, actualMessage);
}
[Test]
public void RemovePackage_ExceptionWhenUninstallingPackage_ExceptionErrorMessageReported()
{
CreateViewModelWithExceptionThrowingPackageManagementService();
Exception ex = new Exception("Test");
exceptionThrowingPackageManagementService.ExeptionToThrowWhenUninstallPackageCalled = ex;
viewModel.RemovePackage();
Assert.AreEqual("Test", messageReporter.MessagePassedToShowErrorMessage);
}
[Test]
public void RemovePackage_PackageUninstalledSuccessfully_MessagesReportedPreviouslyAreCleared()
{
CreateViewModel();
viewModel.RemovePackage();
Assert.IsTrue(messageReporter.IsClearMessageCalled);
}
[Test]
public void RemovePackage_ExceptionWhenUninstallingPackage_ExceptionLogged()
{
CreateViewModelWithExceptionThrowingPackageManagementService();
Exception ex = new Exception("Exception error message");
exceptionThrowingPackageManagementService.ExeptionToThrowWhenUninstallPackageCalled = ex;
viewModel.RemovePackage();
string actualMessage = packageManagementService.FakeOutputMessagesView.SecondFormattedMessageLogged;
bool containsExceptionErrorMessage = actualMessage.Contains("Exception error message");
Assert.IsTrue(containsExceptionErrorMessage, actualMessage);
}
[Test]
public void AddPackage_ExceptionThrownWhenResolvingPackageOperations_ExceptionReported()
{
CreateViewModel();
var resolver = new ExceptionThrowingPackageOperationResolver();
viewModel.FakePackageOperationResolver = resolver;
resolver.ResolveOperationsExceptionToThrow = new Exception("Test");
viewModel.AddPackage();
Assert.AreEqual("Test", messageReporter.MessagePassedToShowErrorMessage);
}
}
}

3
src/AddIns/Misc/PackageManagement/Test/Src/RecentPackagesViewModelTests.cs

@ -30,7 +30,8 @@ namespace PackageManagement.Tests @@ -30,7 +30,8 @@ namespace PackageManagement.Tests
void CreateViewModel(FakePackageManagementService packageManagementService)
{
taskFactory = new FakeTaskFactory();
viewModel = new RecentPackagesViewModel(packageManagementService, taskFactory);
var messageReporter = new FakeMessageReporter();
viewModel = new RecentPackagesViewModel(packageManagementService, messageReporter, taskFactory);
}
void CompleteReadPackagesTask()

Loading…
Cancel
Save