[bcl] Put xunit tests in mcs/class/lib/PROFILE/tests too (#10928)
[mono-project.git] / mcs / class / Mono.Profiler.Log / Test / ProfilerTestRun.cs
blob5e1155d60b9b22ac4a837c8f988416fc7b3801d2
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.IO;
9 using Mono.Profiler.Log;
11 namespace MonoTests.Mono.Profiler.Log {
13 sealed class ProfilerTestRun {
15 sealed class ProfilerTestVisitor : LogEventVisitor {
17 public ProfilerTestRun Run { get; }
19 public List<LogEvent> Events { get; } = new List<LogEvent> ();
21 public ProfilerTestVisitor (ProfilerTestRun run)
23 Run = run;
26 public override void VisitBefore (LogEvent ev)
28 Events.Add (ev);
32 public string Name { get; }
34 public string Options { get; }
36 readonly string _output;
38 static volatile int _id;
40 static string _testAssemblyPath;
41 static Process _currentProcess;
43 public ProfilerTestRun (string name, string options)
45 _testAssemblyPath = Path.Combine (Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location), "log-profiler-test.exe");
46 _currentProcess = Process.GetCurrentProcess();
47 Name = name;
48 Options = options;
49 _output = $"test-{_id++}.mlpd";
52 public void Run (Action<IReadOnlyList<LogEvent>> action)
54 RunTest ();
55 var events = ParseFile ();
57 action (events);
60 void RunTest ()
62 using (var proc = new Process ()) {
63 proc.StartInfo = new ProcessStartInfo {
64 UseShellExecute = false,
65 FileName = _currentProcess.MainModule.FileName,
66 Arguments = $"--debug --profile=log:nodefaults,output=\"{_output}\",{Options} {_testAssemblyPath} {Name}",
69 proc.Start ();
70 proc.WaitForExit ();
72 if (proc.ExitCode != 0)
73 throw new Exception ($"Profiler test process exited with code: {proc.ExitCode}");
77 IReadOnlyList<LogEvent> ParseFile ()
79 var visitor = new ProfilerTestVisitor (this);
81 using (var stream = new LogStream (File.OpenRead (_output)))
82 new LogProcessor (stream, null, visitor).Process ();
84 return visitor.Events;