Browse Source

Check PowerShell scripts exist before attempting to run them.

pull/15/head
Matt Ward 15 years ago
parent
commit
a3b896b2e6
  1. 1
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 16
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/IPackageScriptFileName.cs
  3. 2
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs
  4. 2
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs
  5. 17
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs
  6. 2
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs
  7. 2
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs
  8. 1
      src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
  9. 41
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageScriptFileName.cs
  10. 2
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePowerShellSession.cs
  11. 65
      src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs
  12. 41
      src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs

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

@ -170,6 +170,7 @@ @@ -170,6 +170,7 @@
<Compile Include="Src\ProcessPackageAction.cs" />
<Compile Include="Src\Scripting\IPackageScript.cs" />
<Compile Include="Src\Scripting\IPackageScriptFactory.cs" />
<Compile Include="Src\Scripting\IPackageScriptFileName.cs" />
<Compile Include="Src\Scripting\IPackageScriptSession.cs" />
<Compile Include="Src\Scripting\PackageInitializeScript.cs" />
<Compile Include="Src\Scripting\PackageInitializeScriptFileName.cs" />

16
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/IPackageScriptFileName.cs

@ -0,0 +1,16 @@ @@ -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();
}
}

2
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInitializeScript.cs

@ -9,7 +9,7 @@ namespace ICSharpCode.PackageManagement.Scripting @@ -9,7 +9,7 @@ namespace ICSharpCode.PackageManagement.Scripting
public class PackageInitializeScript : PackageScript
{
public PackageInitializeScript(
PackageInitializeScriptFileName fileName,
IPackageScriptFileName fileName,
IPackageScriptSession session)
: base(fileName, session)
{

2
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageInstallScript.cs

@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting @@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting
public class PackageInstallScript : PackageScript
{
public PackageInstallScript(
PackageInstallScriptFileName fileName,
IPackageScriptFileName fileName,
IPackageScriptSession session)
: base(fileName, session)
{

17
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScript.cs

@ -10,14 +10,14 @@ namespace ICSharpCode.PackageManagement.Scripting @@ -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 @@ -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);

2
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageScriptFileName.cs

@ -7,7 +7,7 @@ using NuGet; @@ -7,7 +7,7 @@ using NuGet;
namespace ICSharpCode.PackageManagement.Scripting
{
public abstract class PackageScriptFileName
public abstract class PackageScriptFileName : IPackageScriptFileName
{
IFileSystem fileSystem;
string relativeScriptFilePath;

2
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PackageUninstallScript.cs

@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting @@ -8,7 +8,7 @@ namespace ICSharpCode.PackageManagement.Scripting
public class PackageUninstallScript : PackageScript
{
public PackageUninstallScript(
PackageUninstallScriptFileName fileName,
IPackageScriptFileName fileName,
IPackageScriptSession session)
: base(fileName, session)
{

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

@ -79,6 +79,7 @@ @@ -79,6 +79,7 @@
<Compile Include="Src\Helpers\FakePackageManagementProjectFactory.cs" />
<Compile Include="Src\Helpers\FakePackageScript.cs" />
<Compile Include="Src\Helpers\FakePackageScriptFactory.cs" />
<Compile Include="Src\Helpers\FakePackageScriptFileName.cs" />
<Compile Include="Src\Helpers\FakePackageViewModelFactory.cs" />
<Compile Include="Src\Helpers\FakePowerShellSession.cs" />
<Compile Include="Src\Helpers\TestableAddPackageReferenceCommand.cs" />

41
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/FakePackageScriptFileName.cs

@ -0,0 +1,41 @@ @@ -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;
}
}
}

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

@ -35,10 +35,12 @@ namespace PackageManagement.Tests.Helpers @@ -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;
}
}

65
src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializeScriptTests.cs

@ -13,32 +13,14 @@ namespace PackageManagement.Tests.Scripting @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -215,7 +200,7 @@ namespace PackageManagement.Tests.Scripting
[Test]
public void Execute_PackageInstallDirectoryIsSet_ProjectSessionVariableIsRemoved()
{
CreateScriptWithPhysicalFileSystem();
CreateScript();
script.Execute();
AssertSessionVariableIsRemoved("__project");

41
src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInstallScriptTests.cs

@ -16,23 +16,24 @@ namespace PackageManagement.Tests.Scripting @@ -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 @@ -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);
}
}
}

Loading…
Cancel
Save