运行 NUnit Mono 中的命令行参数
Running command line arguments in NUnit Mono
我在 OSX 到 运行 上使用 Xamarin 6.0.1、mono 4.4.1 和 NUnit 3.4.1 class 库 class 运行s 命令行参数
"which ios-deploy"。直接在终端上这个 returns: "/usr/local/bin/ios-deploy"
然而,在我的应用程序中,命令 returns "/usr/bin/which"
关于如何将应用程序发送到 return 终端 return 的任何想法?
请参阅下面的代码,感谢您的想法。
public class ProcesRunner
{
public string getProcess()
{
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
p.CreateNoWindow = true;
p.RedirectStandardOutput = true;
p.RedirectStandardError = true;
p.UseShellExecute = false;
Process pg = new Process();
pg.StartInfo = p;
pg.Start();
string strOutput = pg.StandardOutput.ReadToEnd();
string strError = pg.StandardError.ReadToEnd();
Console.WriteLine(strError);
pg.WaitForExit();
return strOutput;
}
}
单元测试
[TestFixture()]
public class Test
{
[Test()]
public void TestCase()
{
ProcesRunner pr = new ProcesRunner();
string outvar = pr.getProcess();
Console.WriteLine(outvar);
}
}
更新:刚刚从您的评论中意识到这是由于您 运行在 Xamarin Studio/MonoDevelop.
中进行了测试
当在一个应用程序中启动一个 Process
时,它本身还没有被赋予 shell 环境,即你点击一个图标来启动它,而不是从 cli 启动它,你需要告诉您的进程 运行 作为登录名 shell(man bash
了解详细信息)以便 运行 您的 bash 配置文件并选择您的路径设置等。 ..
-l Make bash act as if it had been invoked as a login shell
替换:
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
有:
ProcessStartInfo p = new ProcessStartInfo("/bin/bash", "-l -c 'which ios-deploy'");
输出:
nunit-console -nologo bin/Debug/WhichTest.dll
.
/usr/local/bin/ios-deploy
Tests run: 1, Failures: 0, Not run: 0, Time: 0.049 seconds
替换
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
和
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "ios-deploy");
您正在将 which
传递给 which
,这就是为什么您会看到通往 which
的路径 :)
编辑
如果从参数中删除 which
导致空字符串,则可能是因为 ios-deploy
在您的用户路径中。您可能需要设置 p.LoadUserProfile = true
,您可能需要升级到 NUnit 3.4.1 并使用命令行选项 --loaduserprofile
。结合 Charlie 的建议,它应该有效。
您正在捕获进程完成前的输出。您需要在 WaitForExit 之后移动这些行。
我在 OSX 到 运行 上使用 Xamarin 6.0.1、mono 4.4.1 和 NUnit 3.4.1 class 库 class 运行s 命令行参数
"which ios-deploy"。直接在终端上这个 returns: "/usr/local/bin/ios-deploy"
然而,在我的应用程序中,命令 returns "/usr/bin/which"
关于如何将应用程序发送到 return 终端 return 的任何想法?
请参阅下面的代码,感谢您的想法。
public class ProcesRunner
{
public string getProcess()
{
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
p.CreateNoWindow = true;
p.RedirectStandardOutput = true;
p.RedirectStandardError = true;
p.UseShellExecute = false;
Process pg = new Process();
pg.StartInfo = p;
pg.Start();
string strOutput = pg.StandardOutput.ReadToEnd();
string strError = pg.StandardError.ReadToEnd();
Console.WriteLine(strError);
pg.WaitForExit();
return strOutput;
}
}
单元测试
[TestFixture()]
public class Test
{
[Test()]
public void TestCase()
{
ProcesRunner pr = new ProcesRunner();
string outvar = pr.getProcess();
Console.WriteLine(outvar);
}
}
更新:刚刚从您的评论中意识到这是由于您 运行在 Xamarin Studio/MonoDevelop.
中进行了测试当在一个应用程序中启动一个 Process
时,它本身还没有被赋予 shell 环境,即你点击一个图标来启动它,而不是从 cli 启动它,你需要告诉您的进程 运行 作为登录名 shell(man bash
了解详细信息)以便 运行 您的 bash 配置文件并选择您的路径设置等。 ..
-l Make bash act as if it had been invoked as a login shell
替换:
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
有:
ProcessStartInfo p = new ProcessStartInfo("/bin/bash", "-l -c 'which ios-deploy'");
输出:
nunit-console -nologo bin/Debug/WhichTest.dll
.
/usr/local/bin/ios-deploy
Tests run: 1, Failures: 0, Not run: 0, Time: 0.049 seconds
替换
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "which ios-deploy");
和
ProcessStartInfo p = new ProcessStartInfo("/usr/bin/which", "ios-deploy");
您正在将 which
传递给 which
,这就是为什么您会看到通往 which
的路径 :)
编辑
如果从参数中删除 which
导致空字符串,则可能是因为 ios-deploy
在您的用户路径中。您可能需要设置 p.LoadUserProfile = true
,您可能需要升级到 NUnit 3.4.1 并使用命令行选项 --loaduserprofile
。结合 Charlie 的建议,它应该有效。
您正在捕获进程完成前的输出。您需要在 WaitForExit 之后移动这些行。