Skip to content

Commit 186a505

Browse files
committed
feat: updated tests to avoid rare bug with uninstalled versions
1 parent e121bcd commit 186a505

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

OasysGH/Helpers/RhinoResolver.cs

+21-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System;
66

77
public class RhinoResolver {
8+
private const string Coredllpath = "CoreDllPath";
89
private static string rhinoSystemDirectory;
10+
const string RhinoKey = "SOFTWARE\\McNeel\\Rhinoceros";
911

1012
public static string RhinoSystemDirectory {
1113
get {
@@ -20,6 +22,7 @@ public static string RhinoSystemDirectory {
2022

2123
public static int RhinoMajorVersion { get; set; }
2224

25+
2326
public static void Initialize() {
2427
if (IntPtr.Size != 8) {
2528
throw new Exception("Only 64 bit applications can use Rhino");
@@ -39,21 +42,20 @@ private static Assembly ResolveForRhinoAssemblies(object sender, ResolveEventArg
3942
return null;
4043
}
4144

42-
private static string FindRhinoSystemDirectory() {
45+
public static string FindRhinoSystemDirectory() {
4346
bool useLatest = RhinoMajorVersion < 0;
4447

45-
string name = "SOFTWARE\\McNeel\\Rhinoceros";
46-
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(name)) {
47-
string[] subKeyNames = registryKey.GetSubKeyNames();
48-
Array.Sort(subKeyNames);
49-
string text = "";
48+
string[] subKeyNames = GetSubKeys(RhinoKey);
49+
50+
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RhinoKey)) {
51+
string text = string.Empty;
5052
for (int num = subKeyNames.Length - 1; num >= 0; num--) {
5153
if (double.TryParse(subKeyNames[num], NumberStyles.Any, CultureInfo.InvariantCulture, out double result)) {
5254
text = subKeyNames[num];
5355
if (useLatest || (int)Math.Floor(result) == RhinoMajorVersion) {
5456
using RegistryKey registryKey2 = registryKey.OpenSubKey(text + "\\Install");
5557
try {
56-
object value = registryKey2.GetValue("CoreDllPath");
58+
object value = registryKey2.GetValue(Coredllpath);
5759
if (value == null)
5860
continue;
5961
if (value is string path && File.Exists(path)) {
@@ -70,4 +72,16 @@ private static string FindRhinoSystemDirectory() {
7072

7173
return null;
7274
}
75+
76+
public string[] GetRhinoSubKeys() {
77+
return GetSubKeys(RhinoKey);
78+
}
79+
80+
public static string[] GetSubKeys(string rhinoKey) {
81+
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(rhinoKey)) {
82+
string[] subKeyNames = registryKey.GetSubKeyNames();
83+
Array.Sort(subKeyNames);
84+
return subKeyNames;
85+
}
86+
}
7387
}

OasysGHTests/Helpers/ResolverTest.cs

+11-21
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,20 @@
1010
namespace OasysGHTests.Helpers {
1111
public class ResolverTest {
1212
[Fact]
13-
public void ResolverReturnLatestInstalledRhinoPath() {
14-
string name = "SOFTWARE\\McNeel\\Rhinoceros";
15-
int initialRhinoMajorVersion = RhinoResolver.RhinoMajorVersion;
16-
double rhinoMajorVersion = -1;
17-
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(name)) {
18-
string[] subKeyNames = registryKey.GetSubKeyNames();
19-
Array.Sort(subKeyNames);
20-
double.TryParse(subKeyNames[subKeyNames.Length - 1], NumberStyles.Any, CultureInfo.InvariantCulture, out rhinoMajorVersion);
21-
}
22-
RhinoResolver.Initialize();
23-
string expectedPath = $"C:\\Program Files\\Rhino {rhinoMajorVersion}\\System";
24-
Assert.Equal(RhinoResolver.RhinoSystemDirectory, expectedPath);
25-
RhinoResolver.RhinoMajorVersion = initialRhinoMajorVersion;
26-
RhinoResolver.RhinoSystemDirectory = "";
13+
public void ShouldReturnAValidPath() {
14+
string directory = RhinoResolver.FindRhinoSystemDirectory();
15+
Assert.True(File.Exists(directory));
2716
}
2817

2918
[Fact]
30-
public void ResolverReturnNoRhinoPathWhenRequestedVersionNotInstalled() {
31-
int initialRhinoMajorVersion = RhinoResolver.RhinoMajorVersion;
32-
RhinoResolver.Initialize();
33-
RhinoResolver.RhinoMajorVersion = 1;
34-
Assert.Null(RhinoResolver.RhinoSystemDirectory);
35-
RhinoResolver.RhinoMajorVersion = initialRhinoMajorVersion;
36-
RhinoResolver.RhinoSystemDirectory = "";
19+
public void SubKeyMissingShouldReturnFalse() {
20+
Assert.Throws<NullReferenceException>(() => RhinoResolver.GetSubKeys("INVALID_KEY"));
21+
}
22+
23+
[Fact]
24+
public void ShouldHaveSomeKeys() {
25+
var rhinoSolver = new RhinoResolver();
26+
Assert.True(rhinoSolver.GetRhinoSubKeys().Length > 0);
3727
}
3828
}
3929
}

0 commit comments

Comments
 (0)