From 6c9abfa9789608f9b1ec0129fe08f51b91aa3b2b Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Tue, 14 Mar 2017 16:30:51 -0700 Subject: [PATCH] Correctly sort legacy instances if no setup helper Fixes issue #40 and adds tests to make sure we lexigraphically sort installationVersion when the ISetupHelper is not available (since as no VS2017 or newer instances available). --- docker/Tests/legacy.tests.ps1 | 35 ++++++++++++++++++++++++++++ src/vswhere.lib/InstanceSelector.cpp | 7 +++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/docker/Tests/legacy.tests.ps1 b/docker/Tests/legacy.tests.ps1 index 038513f..1a0de2f 100644 --- a/docker/Tests/legacy.tests.ps1 +++ b/docker/Tests/legacy.tests.ps1 @@ -51,4 +51,39 @@ Describe 'vswhere -legacy' { $instances.Count | Should Be 2 } } + + Context 'no instances' { + BeforeEach { + New-Item HKLM:\Software\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 -Force | ForEach-Object { + foreach ($version in '10.0', '14.0') { + $_ | New-ItemProperty -Name $version -Value "C:\VisualStudio\$version" -Force + } + } + + Start-Process -Wait -FilePath C:\Windows\SysWOW64\regsvr32.exe -ArgumentList @( + '/s', + '/u', + 'C:\Downloads\Microsoft.VisualStudio.Setup.Configuration.Native\tools\x86\Microsoft.VisualStudio.Setup.Configuration.Native.dll' + ) + } + + AfterEach { + Start-Process -Wait -FilePath C:\Windows\SysWOW64\regsvr32.exe -ArgumentList @( + '/s', + 'C:\Downloads\Microsoft.VisualStudio.Setup.Configuration.Native\tools\x86\Microsoft.VisualStudio.Setup.Configuration.Native.dll' + ) + } + + It 'returns 2 instances' { + $instances = C:\bin\vswhere.exe -legacy -format json | ConvertFrom-Json + $instances.Count | Should Be 2 + } + + It '-latest returns latest instance' { + $instances = C:\bin\vswhere.exe -legacy -latest -format json | ConvertFrom-Json + $instances.Count | Should Be 1 + $instances[0].instanceId | Should Be 'VisualStudio.14.0' + $instances[0].installationPath | Should Be 'C:\VisualStudio\14.0' + } + } } diff --git a/src/vswhere.lib/InstanceSelector.cpp b/src/vswhere.lib/InstanceSelector.cpp index 0719cb8..450ebf1 100644 --- a/src/vswhere.lib/InstanceSelector.cpp +++ b/src/vswhere.lib/InstanceSelector.cpp @@ -33,7 +33,6 @@ InstanceSelector::InstanceSelector(_In_ const CommandArgs& args, _In_ ILegacyPro bool InstanceSelector::Less(const ISetupInstancePtr& a, const ISetupInstancePtr& b) const { - static ci_equal equal; static ci_less less; bstr_t bstrVersionA, bstrVersionB; @@ -62,6 +61,12 @@ bool InstanceSelector::Less(const ISetupInstancePtr& a, const ISetupInstancePtr& return SUCCEEDED(hrB); } } + else + { + // If ISetupHelper is not available we have only legacy products, or very early pre-releases of VS2017. + // For version 10.0 and newer either should lexigraphically sort correctly. + return less(wstring(bstrVersionA), wstring(bstrVersionB)); + } } else {