diff --git a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj index cfcfe19da2..0c00f98106 100644 --- a/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj +++ b/src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj @@ -170,6 +170,7 @@ + diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/IPackageScriptFileName.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/IPackageScriptFileName.cs new file mode 100644 index 0000000000..398d5bd24a --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/IPackageScriptFileName.cs @@ -0,0 +1,16 @@ +// 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.Scripting +{ + public interface IPackageScriptFileName + { + string PackageInstallDirectory { get; } + string ToString(); + bool ScriptDirectoryExists(); + bool FileExists(); + string GetScriptDirectory(); + } +} diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs index b8563585c2..1e1d4ccd13 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs @@ -9,7 +9,7 @@ namespace ICSharpCode.PackageManagement.Scripting public class PackageInitializeScript : PackageScript { public PackageInitializeScript( - PackageInitializeScriptFileName fileName, + IPackageScriptFileName fileName, IPackageScriptSession session) : base(fileName, session) { diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs index 7108fcf4bf..4d9c8c2abf 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs @@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting public class PackageInstallScript : PackageScript { public PackageInstallScript( - PackageInstallScriptFileName fileName, + IPackageScriptFileName fileName, IPackageScriptSession session) : base(fileName, session) { diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs index cbe4aa29ed..56f22e0fa5 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs @@ -10,14 +10,14 @@ namespace ICSharpCode.PackageManagement.Scripting public class PackageScript : IPackageScript { public PackageScript( - PackageScriptFileName fileName, + IPackageScriptFileName fileName, IPackageScriptSession session) { this.ScriptFileName = fileName; this.Session = session; } - protected PackageScriptFileName ScriptFileName { get; private set; } + protected IPackageScriptFileName ScriptFileName { get; private set; } protected IPackageScriptSession Session { get; private set; } public IPackage Package { get; set; } @@ -26,15 +26,22 @@ namespace ICSharpCode.PackageManagement.Scripting public void Execute() { BeforeExecute(); - AddSessionVariables(); - ExecuteScript(); - RemoveSessionVariables(); + if (ScriptFileExists()) { + AddSessionVariables(); + ExecuteScript(); + RemoveSessionVariables(); + } } protected virtual void BeforeExecute() { } + bool ScriptFileExists() + { + return ScriptFileName.FileExists(); + } + void AddSessionVariables() { Session.AddVariable("__rootPath", ScriptFileName.PackageInstallDirectory); diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs index 998aa788b2..17766c4374 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs @@ -7,7 +7,7 @@ using NuGet; namespace ICSharpCode.PackageManagement.Scripting { - public abstract class PackageScriptFileName + public abstract class PackageScriptFileName : IPackageScriptFileName { IFileSystem fileSystem; string relativeScriptFilePath; diff --git a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs index 717ea3afad..47263a5661 100644 --- a/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs +++ b/src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs @@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting public class PackageUninstallScript : PackageScript { public PackageUninstallScript( - PackageUninstallScriptFileName fileName, + IPackageScriptFileName fileName, IPackageScriptSession session) : base(fileName, session) { diff --git a/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj b/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj index 56a57b9168..e787ed89cf 100644 --- a/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj +++ b/src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj @@ -79,6 +79,7 @@ + diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageScriptFileName.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageScriptFileName.cs new file mode 100644 index 0000000000..bdcb2a0f61 --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageScriptFileName.cs @@ -0,0 +1,41 @@ +// 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.Scripting; + +namespace PackageManagement.Tests.Helpers +{ + public class FakePackageScriptFileName : IPackageScriptFileName + { + public string PackageInstallDirectory { get; set; } + + public bool ScriptDirectoryExistsReturnValue = true; + + public bool ScriptDirectoryExists() + { + return ScriptDirectoryExistsReturnValue; + } + + public bool FileExistsReturnValue = true; + + public bool FileExists() + { + return FileExistsReturnValue; + } + + public string GetScriptDirectoryReturnValue = String.Empty; + + public string GetScriptDirectory() + { + return GetScriptDirectoryReturnValue; + } + + public string ToStringReturnValue = String.Empty; + + public override string ToString() + { + return ToStringReturnValue; + } + } +} \ No newline at end of file diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePowerShellSession.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePowerShellSession.cs index 5ccf061808..2bfbf36f08 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePowerShellSession.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePowerShellSession.cs @@ -35,10 +35,12 @@ namespace PackageManagement.Tests.Helpers VariablesRemoved.Add(name); } + public bool IsScriptExecuted; public string ScriptPassedToInvokeScript; public void InvokeScript(string script) { + IsScriptExecuted = true; ScriptPassedToInvokeScript = script; } } diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs index aa71e2801d..c2783f69e4 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs @@ -13,32 +13,14 @@ namespace PackageManagement.Tests.Scripting public class PackageInitializeScriptTests { PackageInitializeScript script; - PackageInitializeScriptFileName scriptFileName; - FakeFileSystem fakeFileSystem; FakePackageScriptSession fakeSession; + FakePackageScriptFileName fakeScriptFileName; void CreateScript() { - fakeFileSystem = new FakeFileSystem(); - fakeFileSystem.FileExistsReturnValue = true; - fakeFileSystem.DirectoryExistsReturnValue = true; - scriptFileName = new PackageInitializeScriptFileName(fakeFileSystem); - - fakeSession = new FakePackageScriptSession(); - - script = new PackageInitializeScript(scriptFileName, fakeSession); - } - - void CreateScriptWithPhysicalFileSystem() - { - CreateScriptWithPhysicalFileSystem(@"d:\projects\myproject\packages\test"); - } - - void CreateScriptWithPhysicalFileSystem(string packageInstallDirectory) - { - scriptFileName = new PackageInitializeScriptFileName(packageInstallDirectory); + fakeScriptFileName = new FakePackageScriptFileName(); fakeSession = new FakePackageScriptSession(); - script = new PackageInitializeScript(scriptFileName, fakeSession); + script = new PackageInitializeScript(fakeScriptFileName, fakeSession); } void AssertSessionVariableIsRemoved(string variableName) @@ -51,13 +33,13 @@ namespace PackageManagement.Tests.Scripting public void Execute_ExistingEnvironmentPathIsEmptyString_PathToScriptAddedToEnvironmentPath() { CreateScript(); - fakeFileSystem.PathToReturnFromGetFullPath = @"d:\projects\myproject\packages\test"; + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; fakeSession.SetEnvironmentPath(String.Empty); script.Execute(); string actualEnvironmentPath = fakeSession.GetEnvironmentPath(); - string expectedEnvironmentPath = @"d:\projects\myproject\packages\test"; + string expectedEnvironmentPath = @"d:\projects\myproject\packages\test\tools"; Assert.AreEqual(expectedEnvironmentPath, actualEnvironmentPath); } @@ -66,13 +48,13 @@ namespace PackageManagement.Tests.Scripting public void Execute_OneItemInOriginalEnvironmentPath_PathToScriptAppendedToEnvironmentPath() { CreateScript(); - fakeFileSystem.PathToReturnFromGetFullPath = @"d:\projects\myproject\packages\test"; + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; fakeSession.SetEnvironmentPath(@"c:\users\sharpdevelop\ps;"); script.Execute(); string actualEnvironmentPath = fakeSession.GetEnvironmentPath(); - string expectedEnvironmentPath = @"c:\users\sharpdevelop\ps;d:\projects\myproject\packages\test"; + string expectedEnvironmentPath = @"c:\users\sharpdevelop\ps;d:\projects\myproject\packages\test\tools"; Assert.AreEqual(expectedEnvironmentPath, actualEnvironmentPath); } @@ -81,13 +63,13 @@ namespace PackageManagement.Tests.Scripting public void Execute_OneItemInOriginalEnvironmentPathMissingSemiColonAtEnd_PathToScriptAppendedToEnvironmentPathWithSemiColonAtStart() { CreateScript(); - fakeFileSystem.PathToReturnFromGetFullPath = @"d:\projects\myproject\packages\test"; + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; fakeSession.SetEnvironmentPath(@"c:\users\sharpdevelop\ps"); script.Execute(); string actualEnvironmentPath = fakeSession.GetEnvironmentPath(); - string expectedEnvironmentPath = @"c:\users\sharpdevelop\ps;d:\projects\myproject\packages\test"; + string expectedEnvironmentPath = @"c:\users\sharpdevelop\ps;d:\projects\myproject\packages\test\tools"; Assert.AreEqual(expectedEnvironmentPath, actualEnvironmentPath); } @@ -96,13 +78,13 @@ namespace PackageManagement.Tests.Scripting public void Execute_OriginalEnvironmentPathIsNull_PathToScriptAppendedToEnvironmentPath() { CreateScript(); - fakeFileSystem.PathToReturnFromGetFullPath = @"d:\projects\myproject\packages\test"; + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; fakeSession.SetEnvironmentPath(null); script.Execute(); string actualEnvironmentPath = fakeSession.GetEnvironmentPath(); - string expectedEnvironmentPath = @"d:\projects\myproject\packages\test"; + string expectedEnvironmentPath = @"d:\projects\myproject\packages\test\tools"; Assert.AreEqual(expectedEnvironmentPath, actualEnvironmentPath); } @@ -111,8 +93,8 @@ namespace PackageManagement.Tests.Scripting public void Execute_ScriptDirectoryDoesNotExist_PathIsNotUpdated() { CreateScript(); - fakeFileSystem.PathToReturnFromGetFullPath = @"d:\projects\myproject\packages\test"; - fakeFileSystem.DirectoryExistsReturnValue = false; + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; + fakeScriptFileName.ScriptDirectoryExistsReturnValue = false; fakeSession.SetEnvironmentPath(String.Empty); script.Execute(); @@ -126,7 +108,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageIsSet_PackageSessionVariableIsSet() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); var expectedPackage = new FakePackage("Test"); script.Package = expectedPackage; script.Execute(); @@ -139,8 +121,9 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_RootPathSessionVariableIsSet() { + CreateScript(); string expectedRootPath = @"d:\projects\myproject\packages\test"; - CreateScriptWithPhysicalFileSystem(expectedRootPath); + fakeScriptFileName.PackageInstallDirectory = expectedRootPath; script.Execute(); var rootPath = fakeSession.VariablesAdded["__rootPath"]; @@ -151,7 +134,8 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_ToolsPathSessionVariableIsSet() { - CreateScriptWithPhysicalFileSystem(@"d:\projects\myproject\packages\test"); + CreateScript(); + fakeScriptFileName.GetScriptDirectoryReturnValue = @"d:\projects\myproject\packages\test\tools"; script.Execute(); var toolsPath = fakeSession.VariablesAdded["__toolsPath"]; @@ -163,7 +147,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_ProjectSessionVariableIsSet() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); script.Execute(); var project = fakeSession.VariablesAdded["__project"]; @@ -174,7 +158,8 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_ScriptIsInvoked() { - CreateScriptWithPhysicalFileSystem(@"d:\projects\myproject\packages\test"); + CreateScript(); + fakeScriptFileName.ToStringReturnValue = @"d:\projects\myproject\packages\test\tools\init.ps1"; script.Execute(); string actualScript = fakeSession.ScriptPassedToInvokeScript; @@ -188,7 +173,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_PackageSessionVariableIsRemoved() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); script.Execute(); AssertSessionVariableIsRemoved("__package"); @@ -197,7 +182,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_RootPathSessionVariableIsRemoved() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); script.Execute(); AssertSessionVariableIsRemoved("__rootPath"); @@ -206,7 +191,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_ToolsPathSessionVariableIsRemoved() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); script.Execute(); AssertSessionVariableIsRemoved("__toolsPath"); @@ -215,7 +200,7 @@ namespace PackageManagement.Tests.Scripting [Test] public void Execute_PackageInstallDirectoryIsSet_ProjectSessionVariableIsRemoved() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); script.Execute(); AssertSessionVariableIsRemoved("__project"); diff --git a/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs b/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs index 02af693dc0..b7ad224d7e 100644 --- a/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs +++ b/src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs @@ -16,23 +16,24 @@ namespace PackageManagement.Tests.Scripting PackageInstallScriptFileName scriptFileName; FakePackageScriptSession fakeSession; PackageInstallScript script; + FakeFileSystem fakeFileSystem; - void CreateScriptWithPhysicalFileSystem() + void CreateScript() { - CreateScriptWithPhysicalFileSystem(@"d:\projects\myproject\packages\test"); - } - - void CreateScriptWithPhysicalFileSystem(string packageInstallDirectory) - { - scriptFileName = new PackageInstallScriptFileName(packageInstallDirectory); + fakeFileSystem = new FakeFileSystem(); + fakeFileSystem.FileExistsReturnValue = true; + fakeFileSystem.DirectoryExistsReturnValue = true; + scriptFileName = new PackageInstallScriptFileName(fakeFileSystem); + fakeSession = new FakePackageScriptSession(); + script = new PackageInstallScript(scriptFileName, fakeSession); } [Test] public void Execute_PackageInstallDirectoryIsSet_ProjectSessionVariableIsSet() { - CreateScriptWithPhysicalFileSystem(); + CreateScript(); var expectedProject = new Project(); var project = new FakePackageManagementProject(); project.DTEProject = expectedProject; @@ -43,5 +44,29 @@ namespace PackageManagement.Tests.Scripting Assert.AreEqual(expectedProject, projectVariable); } + + [Test] + public void Execute_ScriptDoesNotExist_ScriptIsNotExecuted() + { + CreateScript(); + fakeFileSystem.FileExistsReturnValue = false; + script.Execute(); + + bool executed = fakeSession.IsScriptExecuted; + + Assert.IsFalse(executed); + } + + [Test] + public void Execute_ScriptDoesNotExist_InstallScriptCheckedForExistence() + { + CreateScript(); + fakeFileSystem.FileExistsReturnValue = false; + script.Execute(); + + string fileChecked = fakeFileSystem.PathPassedToFileExists; + + Assert.AreEqual(@"tools\install.ps1", fileChecked); + } } }