1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2024-09-16 05:30:09 +00:00
MpcNET/Sources/MpcNET.Test/MpdMock.cs

119 lines
3.9 KiB
C#
Raw Normal View History

2017-04-12 10:11:27 +00:00
namespace MpcNET.Test
2017-04-12 09:21:29 +00:00
{
2018-05-18 13:14:20 +00:00
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
2017-04-12 09:21:29 +00:00
public class MpdMock : IDisposable
{
public void Start()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
2018-05-18 13:14:20 +00:00
this.SendCommand("/usr/bin/pkill mpd");
2017-04-12 09:21:29 +00:00
}
MpdConf.Create(Path.Combine(AppContext.BaseDirectory, "Server"));
2018-05-18 13:14:20 +00:00
var server = this.GetServer();
2017-04-12 09:21:29 +00:00
2018-05-18 13:14:20 +00:00
this.Process = new Process
2017-04-12 09:21:29 +00:00
{
StartInfo = new ProcessStartInfo
{
FileName = server.FileName,
WorkingDirectory = server.WorkingDirectory,
Arguments = server.Arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
2018-05-18 13:14:20 +00:00
TestOutput.WriteLine($"Starting Server: {this.Process.StartInfo.FileName} {this.Process.StartInfo.Arguments}");
2017-04-12 09:21:29 +00:00
2018-05-18 13:14:20 +00:00
this.Process.Start();
TestOutput.WriteLine($"Output: {this.Process.StandardOutput.ReadToEnd()}");
TestOutput.WriteLine($"Error: {this.Process.StandardError.ReadToEnd()}");
2017-04-12 09:21:29 +00:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
2018-05-18 13:14:20 +00:00
this.SendCommand("/bin/netstat -ntpl");
2017-04-12 09:21:29 +00:00
}
}
public Process Process { get; private set; }
private Server GetServer()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Server.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Server.Windows;
}
throw new NotSupportedException("OS not supported");
}
private void SendCommand(string command)
{
var netcat = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
WorkingDirectory = "/bin/",
Arguments = $"-c \"sudo {command}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
netcat.Start();
netcat.WaitForExit();
TestOutput.WriteLine(command);
TestOutput.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}");
TestOutput.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}");
}
public void Dispose()
{
2018-05-18 13:14:20 +00:00
this.Process?.Kill();
this.Process?.Dispose();
2017-04-12 09:21:29 +00:00
TestOutput.WriteLine("Server Stopped.");
}
private class Server
{
public static Server Linux = new Server(
fileName: "/bin/bash",
workingDirectory: "/bin/",
arguments: $"-c \"sudo /usr/bin/mpd {Path.Combine(AppContext.BaseDirectory, "Server", "mpd.conf")} -v\"");
public static Server Windows = new Server(
fileName: Path.Combine(AppContext.BaseDirectory, "Server", "mpd.exe"),
workingDirectory: Path.Combine(AppContext.BaseDirectory, "Server"),
arguments: $"{Path.Combine(AppContext.BaseDirectory, "Server", "mpd.conf")} -v");
private Server(string fileName, string workingDirectory, string arguments)
{
2018-05-18 13:14:20 +00:00
this.FileName = fileName;
this.WorkingDirectory = workingDirectory;
this.Arguments = arguments;
2017-04-12 09:21:29 +00:00
}
public string FileName { get; }
public string WorkingDirectory { get; }
public string Arguments { get; }
}
}
}