mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 20:13:52 +08:00
#17273: Making the default install action hook up into the upgrade code if the package is already installed. Merging the code paths into the packageinstaller. Adding some UT coverage to the package installer experience.
--HG-- branch : 1.x
This commit is contained in:
@@ -54,6 +54,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
if (Directory.Exists(_basePath)) {
|
||||
Directory.Delete(_basePath, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(_basePath);
|
||||
_contentDefinitionManager = new Mock<IContentDefinitionManager>();
|
||||
_appDataFolder = AppDataFolderTests.CreateAppDataFolder(_basePath);
|
||||
@@ -227,6 +228,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
}
|
||||
|
||||
#region Stubs
|
||||
|
||||
public class ThingHandler : ContentHandler {
|
||||
public ThingHandler() {
|
||||
Filters.Add(new ActivatingFilter<Thing>(ThingDriver.ContentTypeName));
|
||||
@@ -278,6 +280,7 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,8 @@
|
||||
<Compile Include="Indexing\LuceneIndexProviderTests.cs" />
|
||||
<Compile Include="Indexing\LuceneSearchBuilderTests.cs" />
|
||||
<Compile Include="Media\Services\MediaServiceTests.cs" />
|
||||
<Compile Include="Packaging\Services\FolderUpdaterTests.cs" />
|
||||
<Compile Include="Packaging\Services\PackageInstallerTests.cs" />
|
||||
<Compile Include="Recipes\RecipeHandlers\ModuleRecipeHandlerTest.cs" />
|
||||
<Compile Include="Recipes\RecipeHandlers\ThemeRecipeHandlerTest.cs" />
|
||||
<Compile Include="Recipes\Services\RecipeManagerTests.cs" />
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
using System.IO;
|
||||
using Autofac;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Packaging.Services;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Tests.Modules.Packaging.Services {
|
||||
[TestFixture]
|
||||
public class FolderUpdaterTests {
|
||||
protected IContainer _container;
|
||||
|
||||
private readonly string _basePath = Path.Combine(Path.GetTempPath(), "FolderUpdaterTests");
|
||||
|
||||
[SetUp]
|
||||
public virtual void Init() {
|
||||
if (Directory.Exists(_basePath)) {
|
||||
Directory.Delete(_basePath, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(_basePath);
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
builder.RegisterType<FolderUpdater>().As<IFolderUpdater>();
|
||||
builder.RegisterInstance(new Mock<INotifier>().Object);
|
||||
|
||||
_container = builder.Build();
|
||||
}
|
||||
|
||||
[TestFixtureTearDown]
|
||||
public void Clean() {
|
||||
if (Directory.Exists(_basePath)) {
|
||||
Directory.Delete(_basePath, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BackupTest() {
|
||||
DirectoryInfo sourceDirectoryInfo = Directory.CreateDirectory(Path.Combine(_basePath, "Source"));
|
||||
File.CreateText(Path.Combine(sourceDirectoryInfo.FullName, "file1.txt")).Close();
|
||||
File.CreateText(Path.Combine(sourceDirectoryInfo.FullName, "file2.txt")).Close();
|
||||
|
||||
IFolderUpdater folderUpdater = _container.Resolve<IFolderUpdater>();
|
||||
|
||||
DirectoryInfo targetDirectoryInfo = new DirectoryInfo(Path.Combine(_basePath, "Target"));
|
||||
folderUpdater.Backup(sourceDirectoryInfo, targetDirectoryInfo);
|
||||
|
||||
Assert.That(File.Exists(Path.Combine(targetDirectoryInfo.FullName, "file1.txt")), Is.True);
|
||||
Assert.That(File.Exists(Path.Combine(targetDirectoryInfo.FullName, "file2.txt")), Is.True);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using System.IO;
|
||||
using Autofac;
|
||||
using Moq;
|
||||
using NuGet;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Caching;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using Orchard.FileSystems.VirtualPath;
|
||||
using Orchard.FileSystems.WebSite;
|
||||
using Orchard.Packaging.Services;
|
||||
using Orchard.Tests.Stubs;
|
||||
using Orchard.UI.Notify;
|
||||
using IPackageBuilder = Orchard.Packaging.Services.IPackageBuilder;
|
||||
using PackageBuilder = Orchard.Packaging.Services.PackageBuilder;
|
||||
|
||||
namespace Orchard.Tests.Modules.Packaging.Services {
|
||||
[TestFixture]
|
||||
public class PackageInstallerTests : ContainerTestBase {
|
||||
private const string PackageIdentifier = "Hello.World";
|
||||
|
||||
private readonly string _basePath = Path.Combine(Path.GetTempPath(), "PackageInstallerTests");
|
||||
|
||||
private Mock<IVirtualPathProvider> _mockedVirtualPathProvider;
|
||||
|
||||
protected override void Register(ContainerBuilder builder) {
|
||||
builder.RegisterType<PackageBuilder>().As<IPackageBuilder>();
|
||||
builder.RegisterType<PackageInstaller>().As<IPackageInstaller>();
|
||||
builder.RegisterType<ExtensionManager>().As<IExtensionManager>();
|
||||
builder.RegisterType<FolderUpdater>().As<IFolderUpdater>();
|
||||
builder.RegisterInstance(new Mock<INotifier>().Object);
|
||||
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
|
||||
|
||||
_mockedVirtualPathProvider = new Mock<IVirtualPathProvider>();
|
||||
builder.RegisterInstance(_mockedVirtualPathProvider.Object).As<IVirtualPathProvider>();
|
||||
builder.RegisterType<DefaultOrchardFrameworkAssemblies>().As<IOrchardFrameworkAssemblies>();
|
||||
builder.RegisterType<InMemoryWebSiteFolder>().As<IWebSiteFolder>()
|
||||
.As<InMemoryWebSiteFolder>().InstancePerLifetimeScope();
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public override void Init() {
|
||||
base.Init();
|
||||
|
||||
if (Directory.Exists(_basePath)) {
|
||||
Directory.Delete(_basePath, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(_basePath);
|
||||
}
|
||||
|
||||
[TestFixtureTearDown]
|
||||
public void Clean() {
|
||||
if (Directory.Exists(_basePath)) {
|
||||
Directory.Delete(_basePath, true);
|
||||
}
|
||||
}
|
||||
|
||||
private Stream BuildHelloWorld(IPackageBuilder packageBuilder) {
|
||||
// add some content because NuGet requires it
|
||||
var folder = _container.Resolve<InMemoryWebSiteFolder>();
|
||||
using (var sourceStream = GetType().Assembly.GetManifestResourceStream(GetType(), "Hello.World.csproj.txt")) {
|
||||
folder.AddFile("~/Modules/Hello.World/Hello.World.csproj", new StreamReader(sourceStream).ReadToEnd());
|
||||
}
|
||||
|
||||
return packageBuilder.BuildPackage(new ExtensionDescriptor {
|
||||
ExtensionType = DefaultExtensionTypes.Module,
|
||||
Id = PackageIdentifier,
|
||||
Version = "1.0",
|
||||
Description = "a",
|
||||
Author = "b"
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InstallTest() {
|
||||
IPackageBuilder packageBuilder = _container.Resolve<IPackageBuilder>();
|
||||
Stream stream = BuildHelloWorld(packageBuilder);
|
||||
|
||||
string filename = Path.Combine(_basePath, "package.nupkg");
|
||||
using (var fileStream = File.Create(filename)) {
|
||||
stream.CopyTo(fileStream);
|
||||
}
|
||||
|
||||
ZipPackage zipPackage = new ZipPackage(filename);
|
||||
IPackageInstaller packageInstaller = _container.Resolve<IPackageInstaller>();
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.MapPath(It.IsAny<string>()))
|
||||
.Returns<string>(path => Path.Combine(_basePath, path.Replace("~\\", "")));
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.Combine(It.IsAny<string[]>()))
|
||||
.Returns<string[]>(Path.Combine);
|
||||
|
||||
PackageInfo packageInfo = packageInstaller.Install(zipPackage, _basePath, _basePath);
|
||||
Assert.That(packageInfo, Is.Not.Null);
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/Hello.World")));
|
||||
Assert.That(File.Exists(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InstallUpgradeTest() {
|
||||
IPackageBuilder packageBuilder = _container.Resolve<IPackageBuilder>();
|
||||
Stream stream = BuildHelloWorld(packageBuilder);
|
||||
|
||||
string filename = Path.Combine(_basePath, "package.nupkg");
|
||||
using (var fileStream = File.Create(filename)) {
|
||||
stream.CopyTo(fileStream);
|
||||
}
|
||||
|
||||
ZipPackage zipPackage = new ZipPackage(filename);
|
||||
IPackageInstaller packageInstaller = _container.Resolve<IPackageInstaller>();
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.MapPath(It.IsAny<string>()))
|
||||
.Returns<string>(path => Path.Combine(_basePath, path.Replace("~\\", "").Replace("~/", "")));
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.Combine(It.IsAny<string[]>()))
|
||||
.Returns<string[]>(Path.Combine);
|
||||
|
||||
PackageInfo packageInfo = packageInstaller.Install(zipPackage, _basePath, _basePath);
|
||||
Assert.That(packageInfo, Is.Not.Null);
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/Hello.World")));
|
||||
Assert.That(File.Exists(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj")));
|
||||
|
||||
// Modify one of the files and install again and check that backup worked and file content is updated
|
||||
string[] lines = File.ReadAllLines(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj"));
|
||||
string originalLine = lines[lines.Length - 1];
|
||||
lines[lines.Length - 1] = "modified";
|
||||
File.WriteAllLines(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj"), lines);
|
||||
|
||||
packageInfo = packageInstaller.Install(zipPackage, _basePath, _basePath);
|
||||
Assert.That(packageInfo, Is.Not.Null);
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/Hello.World")));
|
||||
Assert.That(File.Exists(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj")));
|
||||
lines = File.ReadAllLines(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj"));
|
||||
Assert.That(lines[lines.Length - 1], Is.EqualTo(originalLine));
|
||||
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/_Backup/Hello.World")));
|
||||
Assert.That(File.Exists(Path.Combine(_basePath, "Modules/_Backup/Hello.World/Hello.World.csproj")));
|
||||
lines = File.ReadAllLines(Path.Combine(_basePath, "Modules/_Backup/Hello.World/Hello.World.csproj"));
|
||||
Assert.That(lines[lines.Length - 1], Is.EqualTo("modified"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UninstallTest() {
|
||||
IPackageBuilder packageBuilder = _container.Resolve<IPackageBuilder>();
|
||||
Stream stream = BuildHelloWorld(packageBuilder);
|
||||
|
||||
string filename = Path.Combine(_basePath, "package.nupkg");
|
||||
using (var fileStream = File.Create(filename)) {
|
||||
stream.CopyTo(fileStream);
|
||||
}
|
||||
|
||||
ZipPackage zipPackage = new ZipPackage(filename);
|
||||
IPackageInstaller packageInstaller = _container.Resolve<IPackageInstaller>();
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.MapPath(It.IsAny<string>()))
|
||||
.Returns<string>(path => Path.Combine(_basePath, path.Replace("~\\", "").Replace("~/", "")));
|
||||
|
||||
_mockedVirtualPathProvider.Setup(v => v.Combine(It.IsAny<string[]>()))
|
||||
.Returns<string[]>(Path.Combine);
|
||||
|
||||
PackageInfo packageInfo = packageInstaller.Install(zipPackage, _basePath, _basePath);
|
||||
Assert.That(packageInfo, Is.Not.Null);
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/Hello.World")));
|
||||
Assert.That(File.Exists(Path.Combine(_basePath, "Modules/Hello.World/Hello.World.csproj")));
|
||||
|
||||
// Uninstall and check that the installation will be gone
|
||||
packageInstaller.Uninstall(zipPackage.Id, _basePath);
|
||||
Assert.That(Directory.Exists(Path.Combine(_basePath, "Modules/Hello.World")), Is.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ Features:
|
||||
return Enumerable.Empty<PackagingSource>();
|
||||
}
|
||||
|
||||
public void AddSource(string feedTitle, string feedUrl) {
|
||||
public int AddSource(string feedTitle, string feedUrl) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user