Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasfloescher-geberit committed Mar 7, 2022
1 parent 714031c commit ce0d1bb
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 166 deletions.
69 changes: 29 additions & 40 deletions src/Revit.TestRunner.Console/Commands/AbstractTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,28 @@ public abstract class AbstractTestCommand : ICommand
/// <summary>
/// Preferred Revit Version.
/// </summary>
[Option('r', "revit", Default = 2020, HelpText = "Start Revit in Version")]
[Option( 'r', "revit", Default = 2020, HelpText = "Start Revit in Version" )]
public int RevitVersion { get; set; }

/// <summary>
/// Execute Command.
/// </summary>
public virtual void Execute()
{
System.Console.WriteLine($"App dir '{FileNames.WatchDirectory}'");
System.Console.WriteLine( $"App dir '{FileNames.WatchDirectory}'" );
}

/// <summary>
/// Validate File existance. Write to console if not.
/// </summary>
protected bool FileExist(string filePath)
protected bool FileExist( string filePath )
{
bool result = false;
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
if( !string.IsNullOrEmpty( filePath ) && File.Exists( filePath ) ) {
result = true;
}
else
{
System.Console.WriteLine("Input file not found!");
else {
System.Console.WriteLine( "Input file not found!" );
}

return result;
Expand All @@ -51,59 +49,50 @@ protected bool FileExist(string filePath)
/// <summary>
/// Run specified Tests.
/// </summary>
protected async Task RunTests(IEnumerable<TestCaseDto> cases, TestRunnerClient client = null)
protected async Task RunTests( IEnumerable<TestCaseDto> cases, TestRunnerClient client = null )
{
System.Console.WriteLine($"Start test run {DateTime.Now}; preferred on Revit {RevitVersion}");
System.Console.WriteLine( $"Start test run {DateTime.Now}; preferred on Revit {RevitVersion}" );
System.Console.WriteLine();
TimeSpan duration = TimeSpan.Zero;

var complete = new List<TestCaseDto>();
if (client == null)
client = new TestRunnerClient(ConsoleConstants.ProgramName, ConsoleConstants.ProgramVersion);
if( client == null )
client = new TestRunnerClient( ConsoleConstants.ProgramName, ConsoleConstants.ProgramVersion );

await client.StartTestRunAsync(cases, RevitVersion.ToString(), result =>
{
try
{
if (result.StateDto != null)
{
foreach (var test in result.StateDto.Cases.Where(c => c.State != TestState.Unknown))
{
if (complete.All(t => t.Id != test.Id))
{
complete.Add(test);
await client.StartTestRunAsync( cases, RevitVersion.ToString(), result => {
try {
if( result.StateDto != null ) {
foreach( var test in result.StateDto.Cases.Where( c => c.State != TestState.Unknown ) ) {
if( complete.All( t => t.Id != test.Id ) ) {
complete.Add( test );
string testString = $"{test.Id,-8} Test {test.State,-7} - {test.TestClass}.{test.MethodName}";
System.Console.WriteLine(testString);
if (!string.IsNullOrEmpty(test.Message)) System.Console.WriteLine($"\t{test.Message}");
if (!string.IsNullOrEmpty(test.StackTrace)) System.Console.WriteLine($"\t{test.StackTrace}");
System.Console.WriteLine( testString );
if( !string.IsNullOrEmpty( test.Message ) ) System.Console.WriteLine( $"\t{test.Message}" );
if( !string.IsNullOrEmpty( test.StackTrace ) ) System.Console.WriteLine( $"\t{test.StackTrace}" );
duration = result.Duration;
}
}
}
if (result.IsCompleted && !string.IsNullOrEmpty(result.Message))
{
System.Console.WriteLine(result.Message);
if( result.IsCompleted && !string.IsNullOrEmpty( result.Message ) ) {
System.Console.WriteLine( result.Message );
}
}
catch (Exception e)
{
System.Console.WriteLine($"Callback Exception: {e}");
catch( Exception e ) {
System.Console.WriteLine( $"Callback Exception: {e}" );
}
if (!string.IsNullOrEmpty(result.Message)) System.Console.WriteLine(result.Message);
}, CancellationToken.None);
if( !string.IsNullOrEmpty( result.Message ) ) System.Console.WriteLine( result.Message );
}, CancellationToken.None );

int passedCount = complete.Count(t => t.State == TestState.Passed);
int completeCount = complete.Count(t => t.State == TestState.Passed || t.State == TestState.Failed);
int passedCount = complete.Count( t => t.State == TestState.Passed );
int completeCount = complete.Count( t => t.State == TestState.Passed || t.State == TestState.Failed );

System.Console.WriteLine();
System.Console.WriteLine($"Run finished - duration {duration:g} - {passedCount} of {completeCount} Tests passed ({Math.Round(100 * (double)passedCount / completeCount)}%)");

System.Console.WriteLine( $"Run finished - duration {duration:g} - {passedCount} of {completeCount} Tests passed ({Math.Round( 100 * (double)passedCount / completeCount )}%)" );
}
}
}
}
32 changes: 15 additions & 17 deletions src/Revit.TestRunner.Console/Commands/AssemblyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Revit.TestRunner.Console.Commands
/// <summary>
/// Execute Test Command
/// </summary>
[Verb("assembly", HelpText = "Execute all UnitTests from a specified assembly")]
[Verb( "assembly", HelpText = "Execute all UnitTests from a specified assembly" )]
public class AssemblyCommand : AbstractTestCommand
{
/// <summary>
/// Request File Path
/// </summary>
[Value(0, HelpText = "Assembly path containing Tests to execute")]
[Value( 0, HelpText = "Assembly path containing Tests to execute" )]
public string AssemblyPath { get; set; }

/// <summary>
Expand All @@ -27,31 +27,29 @@ public override void Execute()
{
base.Execute();

if (FileExist(AssemblyPath))
{
RunAll(AssemblyPath).GetAwaiter().GetResult();
if( FileExist( AssemblyPath ) ) {
RunAll( AssemblyPath ).GetAwaiter().GetResult();
}
}

/// <summary>
/// Run all tests in assembly.
/// </summary>
private async Task RunAll(string assemblyPath)
private async Task RunAll( string assemblyPath )
{
System.Console.WriteLine("Run all tests in assembly");
System.Console.WriteLine($"Explore assembly '{AssemblyPath}'");
System.Console.WriteLine( "Run all tests in assembly" );
System.Console.WriteLine( $"Explore assembly '{AssemblyPath}'" );

TestRunnerClient client = new TestRunnerClient(ConsoleConstants.ProgramName, ConsoleConstants.ProgramVersion);
var explore = await client.ExploreAssemblyAsync(assemblyPath, RevitVersion.ToString(), CancellationToken.None);
TestRunnerClient client = new TestRunnerClient( ConsoleConstants.ProgramName, ConsoleConstants.ProgramVersion );
var explore = await client.ExploreAssemblyAsync( assemblyPath, RevitVersion.ToString(), CancellationToken.None );

if (explore != null)
{
System.Console.WriteLine("Get tests from assembly");
var root = ModelHelper.ToNodeTree(explore.ExploreFile);
var cases = root.DescendantsAndMe.Where(n => n.Type == TestType.Case).ToArray();
if( explore != null ) {
System.Console.WriteLine( "Get tests from assembly" );
var root = ModelHelper.ToNodeTree( explore.ExploreFile );
var cases = root.DescendantsAndMe.Where( n => n.Type == TestType.Case ).ToArray();

await RunTests(cases.Select(ModelHelper.ToTestCase), client);
await RunTests( cases.Select( ModelHelper.ToTestCase ), client );
}
}
}
}
}
12 changes: 6 additions & 6 deletions src/Revit.TestRunner.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ namespace Revit.TestRunner.Console
/// </summary>
public class Program
{
public static void Main(string[] args)
public static void Main( string[] args )
{
if (Debugger.IsAttached) args = new[] { "--help" };
if( Debugger.IsAttached ) args = new[] { "--help" };
//if( Debugger.IsAttached ) args = new [] { "request", @"C:\temp\App.json", "-r", "2020" };
if( Debugger.IsAttached ) {
var assembly = new FileInfo( Assembly.GetExecutingAssembly().Location );
var testAssemblyPath = Path.Combine( assembly.Directory.Parent.FullName, "Revit.TestRunner.SampleTestProject2.dll" );
args = new [] { "assembly", testAssemblyPath, "-r", "2020", };
args = new[] { "assembly", testAssemblyPath, "-r", "2020", };
}

Parser.Default.ParseArguments<RequestCommand, AssemblyCommand, HelloCommand>(args)
.WithParsed<ICommand>(t => t.Execute());
Parser.Default.ParseArguments<RequestCommand, AssemblyCommand, HelloCommand>( args )
.WithParsed<ICommand>( t => t.Execute() );

//System.Console.ReadKey();
}
}
}
}
68 changes: 29 additions & 39 deletions src/Revit.TestRunner.Shared/RevitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public static class RevitHelper
public static IEnumerable<string> GetInstalledRevitApplications()
{
var installedPrograms = GetInstalledApplications();
var revitInstallers = installedPrograms.Where(s => s.Contains(Revit));
var revitInstallers = installedPrograms.Where( s => s.Contains( Revit ) );
var revitInstalled = revitInstallers
.Where(s => int.TryParse(s.Substring(s.Length - 4), out int i))
.Where(s => s.StartsWith(Revit))
.Where(s => s.Length == Revit.Length + 5)
.Where( s => int.TryParse( s.Substring( s.Length - 4 ), out int i ) )
.Where( s => s.StartsWith( Revit ) )
.Where( s => s.Length == Revit.Length + 5 )
.Distinct();


return revitInstalled.OrderBy(s => s);
return revitInstalled.OrderBy( s => s );
}

/// <summary>
Expand All @@ -36,23 +36,17 @@ private static IEnumerable<string> GetInstalledApplications()
var result = new List<string>();
const string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
{
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
var obj = sk.GetValue("DisplayName");

if (obj != null)
{
result.Add(obj.ToString());
using( RegistryKey rk = Registry.LocalMachine.OpenSubKey( uninstallKey ) ) {
foreach( string skName in rk.GetSubKeyNames() ) {
using( RegistryKey sk = rk.OpenSubKey( skName ) ) {
try {
var obj = sk.GetValue( "DisplayName" );

if( obj != null ) {
result.Add( obj.ToString() );
}
}
catch (Exception)
{
catch( Exception ) {
// ignored
}
}
Expand All @@ -67,48 +61,44 @@ private static IEnumerable<string> GetInstalledApplications()
/// If any Revit already run, no more Revit will be started.
/// Returns the process id and if it was new started.
/// </summary>
public static (int ProcessId, bool IsNew) StartRevit(string version, string language = "")
public static (int ProcessId, bool IsNew) StartRevit( string version, string language = "" )
{
Process process = null;
bool isNew = false;

var processes = Process.GetProcessesByName("Revit");
var processes = Process.GetProcessesByName( "Revit" );

if (processes.Length == 0)
{
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var executablePath = Path.Combine(programFiles, "Autodesk", $"Revit {version}", "Revit.exe");
if( processes.Length == 0 ) {
var programFiles = Environment.GetFolderPath( Environment.SpecialFolder.ProgramFiles );
var executablePath = Path.Combine( programFiles, "Autodesk", $"Revit {version}", "Revit.exe" );

var argument = !string.IsNullOrEmpty(language)
var argument = !string.IsNullOrEmpty( language )
? $" /language {language}"
: string.Empty;

if (File.Exists(executablePath))
{
Console.WriteLine($"Starting Revit {version}");
if( File.Exists( executablePath ) ) {
Console.WriteLine( $"Starting Revit {version}" );

process = Process.Start(executablePath, argument);
process = Process.Start( executablePath, argument );
isNew = true;
}
}
else if (processes.Length == 1)
{
else if( processes.Length == 1 ) {
process = processes.Single();
//Console.WriteLine($"Using Revit ProcessId[{process.Id}]");
}
else
{
throw new ApplicationException("Too many Revit applications already running! Max 1 allowed.");
else {
throw new ApplicationException( "Too many Revit applications already running! Max 1 allowed." );
}

return (process.Id, isNew);
}

public static void KillRevit(int processId)
public static void KillRevit( int processId )
{
//Console.WriteLine($"Killing Revit");
var process = Process.GetProcessById(processId);
var process = Process.GetProcessById( processId );
process?.Kill();
}
}
}
}
Loading

0 comments on commit ce0d1bb

Please sign in to comment.