From d0c2b27975f2857ec8de6408a2d0659ed6c895c8 Mon Sep 17 00:00:00 2001 From: michael kirschner Date: Mon, 8 Feb 2021 16:58:14 -0500 Subject: [PATCH 1/3] wip use prod code when name returns null --- .../DynamoInstallDetective/ProductLookUp.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Tools/DynamoInstallDetective/ProductLookUp.cs b/src/Tools/DynamoInstallDetective/ProductLookUp.cs index c35ff923659..7c47a9e9efd 100644 --- a/src/Tools/DynamoInstallDetective/ProductLookUp.cs +++ b/src/Tools/DynamoInstallDetective/ProductLookUp.cs @@ -285,6 +285,14 @@ public virtual IEnumerable GetProductNameList() return s?.Contains(ProductLookUpName) ?? false; }); } + //TODO consider adding new interface. + //Returns product names and code tuples for products which have valid display name. + internal 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) { @@ -383,9 +391,17 @@ public IInstalledProduct GetLatestProduct() public virtual void LookUpAndInitProducts(IProductLookUp lookUp) { - var newProducts = lookUp.GetProductNameList() - .Select(lookUp.GetProductFromProductName).Distinct() - .Where(p => p != null).OrderBy(p => p); + //TODO fallback if this cast fails + var newProducts = (lookUp as InstalledProductLookUp).GetProductNameAndCodeList() + .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 ? newProducts : Products.Concat(newProducts); } } From f60532c711a089e6e1b100af04a3a839d3a0e027 Mon Sep 17 00:00:00 2001 From: michael kirschner Date: Tue, 9 Feb 2021 16:45:33 -0500 Subject: [PATCH 2/3] add new mock setup add friend assembly for moq add test fix broken tests add fallback for interface --- .../DynamoInstallDetective/ProductLookUp.cs | 16 +++-- .../Properties/AssemblyInfo.cs | 4 ++ .../ProductLookUpTests.cs | 70 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/Tools/DynamoInstallDetective/ProductLookUp.cs b/src/Tools/DynamoInstallDetective/ProductLookUp.cs index 7c47a9e9efd..885c7dca42a 100644 --- a/src/Tools/DynamoInstallDetective/ProductLookUp.cs +++ b/src/Tools/DynamoInstallDetective/ProductLookUp.cs @@ -285,9 +285,9 @@ public virtual IEnumerable GetProductNameList() return s?.Contains(ProductLookUpName) ?? false; }); } - //TODO consider adding new interface. + //TODO add to IProductLookUp interface in Dynamo 3.0 //Returns product names and code tuples for products which have valid display name. - internal IEnumerable<(string DisplayName, string ProductKey)> GetProductNameAndCodeList() + 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; @@ -391,9 +391,13 @@ public IInstalledProduct GetLatestProduct() public virtual void LookUpAndInitProducts(IProductLookUp lookUp) { - //TODO fallback if this cast fails - var newProducts = (lookUp as InstalledProductLookUp).GetProductNameAndCodeList() - .Select(prod => + var newProductTuples = lookUp.GetProductNameList().Select(x=>(DisplayName: x, ProductKey : string.Empty)); + if (lookUp is InstalledProductLookUp lookupAsInstalledProduct) + { + newProductTuples = (lookUp as InstalledProductLookUp).GetProductNameAndCodeList(); + } + + var returnProducts = newProductTuples.Select(prod => { var product = lookUp.GetProductFromProductName(prod.DisplayName); if (product == null) @@ -402,7 +406,7 @@ public virtual void LookUpAndInitProducts(IProductLookUp lookUp) } return product; }).Distinct().Where(p => p != null).OrderBy(p => p); - Products = Products == null ? newProducts : Products.Concat(newProducts); + Products = Products == null ? returnProducts : Products.Concat(returnProducts); } } diff --git a/src/Tools/DynamoInstallDetective/Properties/AssemblyInfo.cs b/src/Tools/DynamoInstallDetective/Properties/AssemblyInfo.cs index f6f675fffc9..6e1b757cd25 100644 --- a/src/Tools/DynamoInstallDetective/Properties/AssemblyInfo.cs +++ b/src/Tools/DynamoInstallDetective/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file diff --git a/test/Libraries/DynamoUtilitiesTests/ProductLookUpTests.cs b/test/Libraries/DynamoUtilitiesTests/ProductLookUpTests.cs index 88d9ed0340e..c49c561c1a9 100644 --- a/test/Libraries/DynamoUtilitiesTests/ProductLookUpTests.cs +++ b/test/Libraries/DynamoUtilitiesTests/ProductLookUpTests.cs @@ -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 + { + { "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 + { + { "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() { @@ -268,6 +293,49 @@ public void CompareProducts() Assert.IsTrue(p.CompareTo(p5) < 0); } + + private static InstalledProductLookUp SetUpProductLookUpFailsToFindProductByName(Dictionary products, + Dictionary mockedLocations) + { + string name; + (Version version, string installlocation, string displayName) product; + Version version; + + var lookUp = new Mock(new object[] { "XYZ", "some.dll" }) { CallBase = true }; + + lookUp.Setup(l => l.ExistsAtPath(It.IsAny())) + .Returns( + (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())) + .Returns(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())) + .Returns(s => s); + lookUp.Setup( + l => l.GetInstallLocationFromProductCode(It.IsAny(), out name)) + .Returns((s,s2)=> { return products[s].installlocation;}); + + return lookUp.Object; + } + private static InstalledProductLookUp SetUpProductLookUp(Dictionary> products, Dictionary> locations) { @@ -293,6 +361,8 @@ private static InstalledProductLookUp SetUpProductLookUp(Dictionary l.GetInstallLocationFromProductName(It.IsAny())) .Returns(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())) .Returns(s => s); lookUp.Setup(l => l.GetVersionInfoFromFile(It.IsAny())) From 6b31a468279ab41e7d2bae3df866c9294b1cab31 Mon Sep 17 00:00:00 2001 From: michael kirschner Date: Tue, 9 Feb 2021 16:49:38 -0500 Subject: [PATCH 3/3] remove as stmt --- src/Tools/DynamoInstallDetective/ProductLookUp.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tools/DynamoInstallDetective/ProductLookUp.cs b/src/Tools/DynamoInstallDetective/ProductLookUp.cs index 885c7dca42a..66e8937b2de 100644 --- a/src/Tools/DynamoInstallDetective/ProductLookUp.cs +++ b/src/Tools/DynamoInstallDetective/ProductLookUp.cs @@ -394,7 +394,7 @@ public virtual void LookUpAndInitProducts(IProductLookUp lookUp) var newProductTuples = lookUp.GetProductNameList().Select(x=>(DisplayName: x, ProductKey : string.Empty)); if (lookUp is InstalledProductLookUp lookupAsInstalledProduct) { - newProductTuples = (lookUp as InstalledProductLookUp).GetProductNameAndCodeList(); + newProductTuples = lookupAsInstalledProduct.GetProductNameAndCodeList(); } var returnProducts = newProductTuples.Select(prod =>