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

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

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

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

@ -8,13 +8,15 @@ using NuGet;
namespace ICSharpCode.PackageManagement namespace ICSharpCode.PackageManagement
{ {
public class AddPackageReferenceViewModel : ViewModelBase<AddPackageReferenceViewModel> public class AddPackageReferenceViewModel : ViewModelBase<AddPackageReferenceViewModel>, IMessageReporter
{ {
IPackageManagementService packageManagementService; IPackageManagementService packageManagementService;
InstalledPackagesViewModel installedPackagesViewModel; InstalledPackagesViewModel installedPackagesViewModel;
AvailablePackagesViewModel availablePackagesViewModel; AvailablePackagesViewModel availablePackagesViewModel;
PackageUpdatesViewModel packageUpdatesViewModel; PackageUpdatesViewModel packageUpdatesViewModel;
RecentPackagesViewModel recentPackagesViewModel; RecentPackagesViewModel recentPackagesViewModel;
string message;
bool hasError;
public AddPackageReferenceViewModel( public AddPackageReferenceViewModel(
IPackageManagementService packageManagementService, IPackageManagementService packageManagementService,
@ -23,10 +25,10 @@ namespace ICSharpCode.PackageManagement
this.packageManagementService = packageManagementService; this.packageManagementService = packageManagementService;
this.packageManagementService.OutputMessagesView.Clear(); this.packageManagementService.OutputMessagesView.Clear();
availablePackagesViewModel = new AvailablePackagesViewModel(packageManagementService, taskFactory); availablePackagesViewModel = new AvailablePackagesViewModel(packageManagementService, this, taskFactory);
installedPackagesViewModel = new InstalledPackagesViewModel(packageManagementService, taskFactory); installedPackagesViewModel = new InstalledPackagesViewModel(packageManagementService, this, taskFactory);
packageUpdatesViewModel = new PackageUpdatesViewModel(packageManagementService, taskFactory); packageUpdatesViewModel = new PackageUpdatesViewModel(packageManagementService, this, taskFactory);
recentPackagesViewModel = new RecentPackagesViewModel(packageManagementService, taskFactory); recentPackagesViewModel = new RecentPackagesViewModel(packageManagementService, this, taskFactory);
availablePackagesViewModel.ReadPackages(); availablePackagesViewModel.ReadPackages();
installedPackagesViewModel.ReadPackages(); installedPackagesViewModel.ReadPackages();
@ -49,5 +51,33 @@ namespace ICSharpCode.PackageManagement
public RecentPackagesViewModel RecentPackagesViewModel { public RecentPackagesViewModel RecentPackagesViewModel {
get { return 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
public AvailablePackagesViewModel( public AvailablePackagesViewModel(
IPackageManagementService packageManagementService, IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory) ITaskFactory taskFactory)
: base(packageManagementService, taskFactory) : base(packageManagementService, messageReporter, taskFactory)
{ {
IsSearchable = true; IsSearchable = true;
ShowPackageSources = packageManagementService.HasMultiplePackageSources; ShowPackageSources = packageManagementService.HasMultiplePackageSources;

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

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

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

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

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

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

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

@ -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
public InstalledPackagesViewModel( public InstalledPackagesViewModel(
IPackageManagementService packageManagementService, IPackageManagementService packageManagementService,
IMessageReporter messageReporter,
ITaskFactory taskFactory) ITaskFactory taskFactory)
: base(packageManagementService, taskFactory) : base(packageManagementService, messageReporter, taskFactory)
{ {
packageManagementService.PackageInstalled += PackageInstalled; packageManagementService.PackageInstalled += PackageInstalled;
packageManagementService.PackageUninstalled += PackageUninstalled; packageManagementService.PackageUninstalled += PackageUninstalled;

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

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

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

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

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

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

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

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

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

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

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

@ -37,6 +37,27 @@ namespace PackageManagement.Tests
taskFactory.ExecuteAllFakeTasks(); 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] [Test]
public void InstalledPackagesViewModel_ProjectHasOneInstalledPackage_HasOnePackageViewModel() public void InstalledPackagesViewModel_ProjectHasOneInstalledPackage_HasOnePackageViewModel()
{ {
@ -112,5 +133,87 @@ namespace PackageManagement.Tests
PackageCollectionAssert.AreEqual(expectedPackages, viewModel.RecentPackagesViewModel.PackageViewModels); 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
void CreateViewModel(IPackageManagementService packageManagementService) void CreateViewModel(IPackageManagementService packageManagementService)
{ {
taskFactory = new FakeTaskFactory(); taskFactory = new FakeTaskFactory();
viewModel = new AvailablePackagesViewModel(packageManagementService, taskFactory); var messageReporter = new FakeMessageReporter();
viewModel = new AvailablePackagesViewModel(packageManagementService, messageReporter, taskFactory);
} }
void CompleteReadPackagesTask() void CompleteReadPackagesTask()

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

@ -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 @@
// 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 @@
// 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
{ {
public List<PackageOperation> PackageOperations = new List<PackageOperation>(); public List<PackageOperation> PackageOperations = new List<PackageOperation>();
public IEnumerable<PackageOperation> ResolveOperations(IPackage package) public virtual IEnumerable<PackageOperation> ResolveOperations(IPackage package)
{ {
return PackageOperations; return PackageOperations;
} }

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

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

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

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

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

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

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

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

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

@ -18,14 +18,29 @@ namespace PackageManagement.Tests
FakePackageManagementService packageManagementService; FakePackageManagementService packageManagementService;
FakePackageRepository sourcePackageRepository; FakePackageRepository sourcePackageRepository;
FakeLicenseAcceptanceService licenseAcceptanceService; FakeLicenseAcceptanceService licenseAcceptanceService;
FakeMessageReporter messageReporter;
ExceptionThrowingPackageManagementService exceptionThrowingPackageManagementService;
void CreateViewModel() 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; package = viewModel.FakePackage;
packageManagementService = viewModel.FakePackageManagementService; this.packageManagementService = packageManagementService;
sourcePackageRepository = packageManagementService.FakeActivePackageRepository; sourcePackageRepository = packageManagementService.FakeActivePackageRepository;
licenseAcceptanceService = viewModel.FakeLicenseAcceptanceService; licenseAcceptanceService = viewModel.FakeLicenseAcceptanceService;
messageReporter = viewModel.FakeMessageReporter;
} }
FakePackage AddPackageDependencyThatDoesNotRequireLicenseAcceptance(string packageId) FakePackage AddPackageDependencyThatDoesNotRequireLicenseAcceptance(string packageId)
@ -466,7 +481,7 @@ namespace PackageManagement.Tests
} }
[Test] [Test]
public void AddPackage_PackageAddedSuccessfully_NextToLastMesssageLoggedMarksEndOfInstallation() public void AddPackage_PackageAddedSuccessfully_NextToLastMessageLoggedMarksEndOfInstallation()
{ {
CreateViewModel(); CreateViewModel();
viewModel.AddPackage(); viewModel.AddPackage();
@ -478,7 +493,7 @@ namespace PackageManagement.Tests
} }
[Test] [Test]
public void AddPackage_PackageAddedSuccessfully_LastMesssageLoggedIsEmptyLine() public void AddPackage_PackageAddedSuccessfully_LastMessageLoggedIsEmptyLine()
{ {
CreateViewModel(); CreateViewModel();
viewModel.AddPackage(); viewModel.AddPackage();
@ -505,7 +520,7 @@ namespace PackageManagement.Tests
} }
[Test] [Test]
public void RemovePackage_PackageRemovedSuccessfully_NextToLastMesssageLoggedMarksEndOfInstallation() public void RemovePackage_PackageRemovedSuccessfully_NextToLastMessageLoggedMarksEndOfInstallation()
{ {
CreateViewModel(); CreateViewModel();
viewModel.RemovePackage(); viewModel.RemovePackage();
@ -517,7 +532,7 @@ namespace PackageManagement.Tests
} }
[Test] [Test]
public void RemovePackage_PackageRemovedSuccessfully_LastMesssageLoggedIsEmptyLine() public void RemovePackage_PackageRemovedSuccessfully_LastMessageLoggedIsEmptyLine()
{ {
CreateViewModel(); CreateViewModel();
viewModel.RemovePackage(); viewModel.RemovePackage();
@ -527,5 +542,85 @@ namespace PackageManagement.Tests
Assert.AreEqual(expectedMessage, actualMessage); 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
void CreateViewModel(FakePackageManagementService packageManagementService) void CreateViewModel(FakePackageManagementService packageManagementService)
{ {
taskFactory = new FakeTaskFactory(); taskFactory = new FakeTaskFactory();
viewModel = new RecentPackagesViewModel(packageManagementService, taskFactory); var messageReporter = new FakeMessageReporter();
viewModel = new RecentPackagesViewModel(packageManagementService, messageReporter, taskFactory);
} }
void CompleteReadPackagesTask() void CompleteReadPackagesTask()

Loading…
Cancel
Save