Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Move common IronRuby and IronPython console application code to Scrip…
Browse files Browse the repository at this point in the history
…ting project.
  • Loading branch information
mrward committed Sep 25, 2010
1 parent ee134f9 commit fc508ad
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 221 deletions.
Expand Up @@ -4,97 +4,28 @@
using System;
using System.Diagnostics;
using System.Text;
using ICSharpCode.Scripting;

namespace ICSharpCode.PythonBinding
{
public class PythonConsoleApplication
public class PythonConsoleApplication : ScriptingConsoleApplication
{
PythonAddInOptions options;
StringBuilder arguments;
bool debug;
string pythonScriptFileName = String.Empty;
string pythonScriptCommandLineArguments = String.Empty;
string workingDirectory = String.Empty;

public PythonConsoleApplication(PythonAddInOptions options)
{
this.options = options;
}

public string FileName {
public override string FileName {
get { return options.PythonFileName; }
}

public bool Debug {
get { return debug; }
set { debug = value; }
}

public string PythonScriptFileName {
get { return pythonScriptFileName; }
set { pythonScriptFileName = value; }
}

public string PythonScriptCommandLineArguments {
get { return pythonScriptCommandLineArguments; }
set { pythonScriptCommandLineArguments = value; }
}

public string WorkingDirectory {
get { return workingDirectory; }
set { workingDirectory = value; }
}

public ProcessStartInfo GetProcessStartInfo()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = FileName;
processStartInfo.Arguments = GetArguments();
processStartInfo.WorkingDirectory = workingDirectory;
return processStartInfo;
}

public string GetArguments()
{
arguments = new StringBuilder();

AppendBooleanOptionIfTrue("-X:Debug", debug);
AppendQuotedStringIfNotEmpty(pythonScriptFileName);
AppendStringIfNotEmpty(pythonScriptCommandLineArguments);

return arguments.ToString().TrimEnd();
}

void AppendBooleanOptionIfTrue(string option, bool flag)
{
if (flag) {
AppendOption(option);
}
}

void AppendOption(string option)
{
arguments.Append(option + " ");
}

void AppendQuotedStringIfNotEmpty(string option)
{
if (!String.IsNullOrEmpty(option)) {
AppendQuotedString(option);
}
}

void AppendQuotedString(string option)
{
string quotedOption = String.Format("\"{0}\"", option);
AppendOption(quotedOption);
}

void AppendStringIfNotEmpty(string option)

protected override void AddArguments(ScriptingCommandLineBuilder commandLine)
{
if (!String.IsNullOrEmpty(option)) {
AppendOption(option);
}
commandLine.AppendBooleanOptionIfTrue("-X:Debug", Debug);
commandLine.AppendQuotedStringIfNotEmpty(ScriptFileName);
commandLine.AppendStringIfNotEmpty(ScriptCommandLineArguments);
}
}
}
Expand Up @@ -83,8 +83,8 @@ void WriteTests(SelectedTests selectedTests)

public ProcessStartInfo CreateProcessStartInfo(SelectedTests selectedTests)
{
consoleApplication.PythonScriptFileName = GetSharpDevelopTestPythonScriptFileName();
consoleApplication.PythonScriptCommandLineArguments = GetResponseFileNameCommandLineArgument();
consoleApplication.ScriptFileName = GetSharpDevelopTestPythonScriptFileName();
consoleApplication.ScriptCommandLineArguments = GetResponseFileNameCommandLineArgument();
consoleApplication.WorkingDirectory = selectedTests.Project.Directory;
return consoleApplication.GetProcessStartInfo();
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ public override void Run()
ProcessStartInfo GetProcessStartInfo()
{
string scriptFileName = workbench.ActiveViewContent.PrimaryFileName;
ipy.PythonScriptFileName = scriptFileName;
ipy.ScriptFileName = scriptFileName;
ipy.WorkingDirectory = Path.GetDirectoryName(scriptFileName);
return ipy.GetProcessStartInfo();
}
Expand Down
Expand Up @@ -397,7 +397,7 @@
<Compile Include="Resolver\ResolveTextBoxFromSystemWindowsFormsImportTextBoxTests.cs" />
<Compile Include="Resolver\ResolveUnknownNamespaceTests.cs" />
<Compile Include="Testing\CreatePythonTestRunnerTestFixture.cs" />
<Compile Include="Testing\PythonConsoleApplicationTestFixture.cs" />
<Compile Include="Testing\PythonConsoleApplicationTests.cs" />
<Compile Include="Testing\PythonStandardLibraryPathTests.cs" />
<Compile Include="Testing\PythonTestDebuggerRunsSelectedTestMethodTestFixture.cs" />
<Compile Include="Testing\PythonTestFrameworkIsTestClassTests.cs" />
Expand Down
Expand Up @@ -11,7 +11,7 @@
namespace PythonBinding.Tests.Testing
{
[TestFixture]
public class PythonConsoleApplicationTestFixture
public class PythonConsoleApplicationTests
{
PythonConsoleApplication app;
PythonAddInOptions options;
Expand All @@ -25,44 +25,48 @@ public void Init()
}

[Test]
public void FileNameIsPythonFileNameFromAddInOptions()
public void FileName_NewInstance_FileNameIsPythonFileNameFromAddInOptions()
{
string fileName = app.FileName;
string expectedFileName = @"C:\IronPython\ipy.exe";
Assert.AreEqual(expectedFileName, app.FileName);
Assert.AreEqual(expectedFileName, fileName);
}

[Test]
public void GetArgumentsReturnsDebugOptionWhenDebugIsTrue()
public void GetArguments_DebugIsTrue_ReturnsDebugOption()
{
app.Debug = true;
string args = app.GetArguments();
string expectedCommandLine = "-X:Debug";

Assert.AreEqual(expectedCommandLine, app.GetArguments());
Assert.AreEqual(expectedCommandLine, args);
}

[Test]
public void GetArgumentsReturnsQuotedPythonScriptFileName()
public void GetArguments_ScriptFileNameSet_ReturnsQuotedPythonScriptFileName()
{
app.PythonScriptFileName = @"d:\projects\my ipy\test.py";
app.ScriptFileName = @"d:\projects\my ipy\test.py";
string args = app.GetArguments();
string expectedCommandLine = "\"d:\\projects\\my ipy\\test.py\"";

Assert.AreEqual(expectedCommandLine, app.GetArguments());
Assert.AreEqual(expectedCommandLine, args);
}

[Test]
public void GetArgumentsReturnsQuotedPythonScriptFileNameAndItsCommandLineArguments()
public void GetArguments_ScriptFileNameAndScriptCommandLineArgsSet_ReturnsQuotedPythonScriptFileNameAndItsCommandLineArguments()
{
app.Debug = true;
app.PythonScriptFileName = @"d:\projects\my ipy\test.py";
app.PythonScriptCommandLineArguments = "@responseFile.txt -def";
app.ScriptFileName = @"d:\projects\my ipy\test.py";
app.ScriptCommandLineArguments = "@responseFile.txt -def";
string args = app.GetArguments();
string expectedCommandLine =
"-X:Debug \"d:\\projects\\my ipy\\test.py\" @responseFile.txt -def";

Assert.AreEqual(expectedCommandLine, app.GetArguments());
Assert.AreEqual(expectedCommandLine, args);
}

[Test]
public void GetProcessStartInfoHasFileNameThatEqualsIronPythonConsoleApplicationExeFileName()
public void GetProcessStartInfo_NewInstance_ReturnsProcessStartInfoWithFileNameThatEqualsIronPythonConsoleApplicationExeFileName()
{
ProcessStartInfo startInfo = app.GetProcessStartInfo();
string expectedFileName = @"C:\IronPython\ipy.exe";
Expand All @@ -71,7 +75,7 @@ public void GetProcessStartInfoHasFileNameThatEqualsIronPythonConsoleApplication
}

[Test]
public void GetProcessStartInfoHasDebugFlagSetInArguments()
public void GetProcessStartInfo_DebugIsTrue_ReturnsProcessStartInfoWithDebugFlagSetInArguments()
{
app.Debug = true;
ProcessStartInfo startInfo = app.GetProcessStartInfo();
Expand All @@ -81,20 +85,21 @@ public void GetProcessStartInfoHasDebugFlagSetInArguments()
}

[Test]
public void GetProcessStartInfoHasWorkingDirectoryIfSet()
public void GetProcessStartInfo_WorkingDirectorySet_ReturnsProcessStartInfoWithMatchingWorkingDirectory()
{
app.WorkingDirectory = @"d:\temp";
ProcessStartInfo startInfo = app.GetProcessStartInfo();
Assert.AreEqual(@"d:\temp", startInfo.WorkingDirectory);
string expectedDirectory = @"d:\temp";
Assert.AreEqual(expectedDirectory, startInfo.WorkingDirectory);
}

[Test]
public void ChangingOptionsPythonFileNameChangesProcessStartInfoFileName()
public void GetProcessStartInfo_ChangingPythonOptionsFileName_ProcessStartInfoFileNameMatchesChange()
{
options.PythonFileName = @"d:\temp\test\ipy.exe";
ProcessStartInfo startInfo = app.GetProcessStartInfo();

Assert.AreEqual(@"d:\temp\test\ipy.exe", startInfo.FileName);
string expectedFileName = @"d:\temp\test\ipy.exe";
Assert.AreEqual(expectedFileName, startInfo.FileName);
}
}
}
Expand Up @@ -6,110 +6,41 @@
using System.Diagnostics;
using System.Text;

using ICSharpCode.Scripting;

namespace ICSharpCode.RubyBinding
{
public class RubyConsoleApplication
public class RubyConsoleApplication : ScriptingConsoleApplication
{
RubyAddInOptions options;
bool debug;
List<string> loadPaths = new List<string>();
string rubyScriptFileName = String.Empty;
string rubyScriptCommandLineArguments = String.Empty;
string workingDirectory = String.Empty;
string loadPath = String.Empty;
StringBuilder arguments;

public RubyConsoleApplication(RubyAddInOptions options)
{
this.options = options;
}

public string FileName {
public override string FileName {
get { return options.RubyFileName; }
}

public bool Debug {
get { return debug; }
set { debug = value; }
}

public void AddLoadPath(string path)
{
loadPaths.Add(path);
}

public string RubyScriptFileName {
get { return rubyScriptFileName; }
set { rubyScriptFileName = value; }
}

public string RubyScriptCommandLineArguments {
get { return rubyScriptCommandLineArguments; }
set { rubyScriptCommandLineArguments = value; }
}

public string WorkingDirectory {
get { return workingDirectory; }
set { workingDirectory = value; }
}

public ProcessStartInfo GetProcessStartInfo()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = FileName;
processStartInfo.Arguments = GetArguments();
processStartInfo.WorkingDirectory = workingDirectory;
return processStartInfo;
}

public string GetArguments()
{
arguments = new StringBuilder();

AppendBooleanOptionIfTrue("-D", debug);
AppendLoadPaths();
AppendQuotedStringIfNotEmpty(rubyScriptFileName);
AppendStringIfNotEmpty(rubyScriptCommandLineArguments);

return arguments.ToString().TrimEnd();
}

void AppendBooleanOptionIfTrue(string option, bool flag)
protected override void AddArguments(ScriptingCommandLineBuilder commandLine)
{
if (flag) {
AppendOption(option);
}
}

void AppendOption(string option)
{
arguments.Append(option + " ");
commandLine.AppendBooleanOptionIfTrue("-D", Debug);
AppendLoadPaths(commandLine);
commandLine.AppendQuotedStringIfNotEmpty(ScriptFileName);
commandLine.AppendStringIfNotEmpty(ScriptCommandLineArguments);
}

void AppendLoadPaths()
void AppendLoadPaths(ScriptingCommandLineBuilder commandLine)
{
foreach (string path in loadPaths) {
AppendQuotedString("-I" + path);
}
}

void AppendQuotedStringIfNotEmpty(string option)
{
if (!String.IsNullOrEmpty(option)) {
AppendQuotedString(option);
}
}

void AppendQuotedString(string option)
{
string quotedOption = String.Format("\"{0}\"", option);
AppendOption(quotedOption);
}

void AppendStringIfNotEmpty(string option)
{
if (!String.IsNullOrEmpty(option)) {
AppendOption(option);
commandLine.AppendQuotedString("-I" + path);
}
}
}
Expand Down
Expand Up @@ -66,9 +66,9 @@ void WriteTests(SelectedTests selectedTests)

public ProcessStartInfo CreateProcessStartInfo(SelectedTests selectedTests)
{
consoleApplication.RubyScriptFileName = GetSharpDevelopTestRubyScriptFileName();
consoleApplication.ScriptFileName = GetSharpDevelopTestRubyScriptFileName();
AddLoadPaths(selectedTests.Project);
consoleApplication.RubyScriptCommandLineArguments = GetCommandLineArguments(selectedTests);
consoleApplication.ScriptCommandLineArguments = GetCommandLineArguments(selectedTests);
consoleApplication.WorkingDirectory = selectedTests.Project.Directory;
return consoleApplication.GetProcessStartInfo();
}
Expand All @@ -84,7 +84,7 @@ void AddLoadPathForRubyStandardLibrary()
if (options.HasRubyLibraryPath) {
consoleApplication.AddLoadPath(options.RubyLibraryPath);
}
string testRunnerLoadPath = Path.GetDirectoryName(consoleApplication.RubyScriptFileName);
string testRunnerLoadPath = Path.GetDirectoryName(consoleApplication.ScriptFileName);
consoleApplication.AddLoadPath(testRunnerLoadPath);
}

Expand Down

0 comments on commit fc508ad

Please sign in to comment.