Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP use product code to lookup product install when displayName returns null #11479

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Tools/DynamoInstallDetective/ProductLookUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ public virtual IEnumerable<string> GetProductNameList()
return s?.Contains(ProductLookUpName) ?? false;
});
}
//TODO add to IProductLookUp interface in Dynamo 3.0
//Returns product names and code tuples for products which have valid display name.
internal virtual IEnumerable<(string DisplayName, string ProductKey)> GetProductNameAndCodeList()
{
return RegUtils.GetInstalledProducts().Select(s => (s.Value.DisplayName,s.Key)).Where(s => {
return s.DisplayName?.Contains(ProductLookUpName) ?? false;
});
}

public virtual bool ExistsAtPath(string basePath)
{
Expand Down Expand Up @@ -383,10 +391,22 @@ public IInstalledProduct GetLatestProduct()

public virtual void LookUpAndInitProducts(IProductLookUp lookUp)
{
var newProducts = lookUp.GetProductNameList()
.Select(lookUp.GetProductFromProductName).Distinct()
.Where(p => p != null).OrderBy(p => p);
Products = Products == null ? newProducts : Products.Concat(newProducts);
var newProductTuples = lookUp.GetProductNameList().Select(x=>(DisplayName: x, ProductKey : string.Empty));
if (lookUp is InstalledProductLookUp lookupAsInstalledProduct)
{
newProductTuples = lookupAsInstalledProduct.GetProductNameAndCodeList();
}

var returnProducts = newProductTuples.Select(prod =>
{
var product = lookUp.GetProductFromProductName(prod.DisplayName);
if (product == null)
{
product = lookUp.GetProductFromProductCode(prod.ProductKey);
}
return product;
}).Distinct().Where(p => p != null).OrderBy(p => p);
Products = Products == null ? returnProducts : Products.Concat(returnProducts);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Tools/DynamoInstallDetective/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Reflection;
using System.Runtime.CompilerServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DynamoInstallDetective")]
[assembly: AssemblyCulture("")]

[assembly: InternalsVisibleTo("DynamoUtilitiesTests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
70 changes: 70 additions & 0 deletions test/Libraries/DynamoUtilitiesTests/ProductLookUpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ public void DynamoProductsOverrideWithDebugPath()
Assert.AreEqual("0.8.2.3", prod082.VersionString);
}

// Test ensures product initialization fallsback to using product code if product lookup with name fails.
[Test, Category("ProductLookUp"), Category("UnitTests")]
public void FindProductsWhenProductNameLookupFails()
{
var products = new Dictionary<string, (Version version, string installlocation, string displayName)>
{
{ "0000", (new Version(0, 1, 2, 3),"installLocation0", null) },
{ "0001", (new Version(0, 1, 2, 4), null,"prodname1XYZ") },
{ "0002", (new Version(0, 1, 2, 5),"installLocation2","prodname2") },
{ "0003", (new Version(0, 1, 2, 5),"installLocation3","prodnameXYZ") },
};

var locations = new Dictionary<string, Version>
{
{ "installLocation3", new Version(0,1,2,5) },
};

var lookUp = SetUpProductLookUpFailsToFindProductByName(products,locations);

var p = new InstalledProducts();
p.LookUpAndInitProducts(lookUp);
Assert.AreEqual(1, p.Products.Count());

}

[Test, Category("ProductLookUp"), Category("UnitTests")]
public void GetProductFromInstallPath()
{
Expand Down Expand Up @@ -268,6 +293,49 @@ public void CompareProducts()
Assert.IsTrue(p.CompareTo(p5) < 0);
}


private static InstalledProductLookUp SetUpProductLookUpFailsToFindProductByName(Dictionary<string, (Version version,string installlocation, string displayName)> products,
Dictionary<string,Version> mockedLocations)
{
string name;
(Version version, string installlocation, string displayName) product;
Version version;

var lookUp = new Mock<InstalledProductLookUp>(new object[] { "XYZ", "some.dll" }) { CallBase = true };

lookUp.Setup(l => l.ExistsAtPath(It.IsAny<string>()))
.Returns<string>(
(s) =>
{
if (string.IsNullOrEmpty(s))
return false;

if (products.TryGetValue(s, out product))
return product.version != null;
if (mockedLocations.TryGetValue(s, out version))
return version != null;

return false;
});

lookUp.Setup(l => l.GetInstallLocationFromProductName(It.IsAny<string>()))
.Returns<string>(null);
lookUp.Setup(l => l.GetProductNameAndCodeList()).Returns(
products.Select(s => (s.Value.displayName, s.Key)).Where(s2 =>
{
return s2.displayName?.Contains(lookUp.Object.ProductLookUpName) ?? false;
}));

lookUp.Setup(l => l.GetProductNameList()).Returns(products.Select(x=>x.Value.displayName));
lookUp.Setup(l => l.GetCoreFilePathFromInstallation(It.IsAny<string>()))
.Returns<string>(s => s);
lookUp.Setup(
l => l.GetInstallLocationFromProductCode(It.IsAny<string>(), out name))
.Returns<string,string>((s,s2)=> { return products[s].installlocation;});

return lookUp.Object;
}

private static InstalledProductLookUp SetUpProductLookUp(Dictionary<string, Tuple<int,int,int,int>> products,
Dictionary<string, Tuple<int, int, int, int>> locations)
{
Expand All @@ -293,6 +361,8 @@ private static InstalledProductLookUp SetUpProductLookUp(Dictionary<string, Tupl
lookUp.Setup(l => l.GetInstallLocationFromProductName(It.IsAny<string>()))
.Returns<string>(s => s);
lookUp.Setup(l => l.GetProductNameList()).Returns(products.Keys);
lookUp.Setup(l => l.GetProductNameAndCodeList()).Returns(
products.Select(s => (s.Key, "someCode")));
lookUp.Setup(l => l.GetCoreFilePathFromInstallation(It.IsAny<string>()))
.Returns<string>(s => s);
lookUp.Setup(l => l.GetVersionInfoFromFile(It.IsAny<string>()))
Expand Down