Skip to content

Commit

Permalink
Defensively protect against a broken WindowsRegistry service
Browse files Browse the repository at this point in the history
Native-platform is sometimes unavailable.  Without native-platform, we're
unable to look-up things from the Windows registry.
  • Loading branch information
big-guy committed May 8, 2024
1 parent 591afee commit a8931dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.Lists;
import net.rubygrapefruit.platform.MissingRegistryEntryException;
import net.rubygrapefruit.platform.WindowsRegistry;
import org.gradle.internal.nativeintegration.NativeIntegrationUnavailableException;
import org.gradle.internal.os.OperatingSystem;

import java.io.File;
Expand Down Expand Up @@ -67,9 +68,9 @@ private Set<InstallationLocation> findInstallationsInRegistry() {

private List<String> find(String sdkSubkey, String path, String value) {
try {
return getVersions(sdkSubkey).stream()
.map(version -> getValue(sdkSubkey, path, value, version)).collect(Collectors.toList());
} catch (MissingRegistryEntryException e) {
List<String> versions = getVersions(sdkSubkey);
return versions.stream().map(version -> getValue(sdkSubkey, path, value, version)).collect(Collectors.toList());
} catch (MissingRegistryEntryException | NativeIntegrationUnavailableException e) {
// Ignore
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ package org.gradle.jvm.toolchain.internal

import net.rubygrapefruit.platform.MissingRegistryEntryException
import net.rubygrapefruit.platform.WindowsRegistry
import org.gradle.api.internal.provider.Providers
import org.gradle.api.provider.ProviderFactory
import org.gradle.internal.nativeintegration.NativeIntegrationUnavailableException
import org.gradle.internal.os.OperatingSystem
import spock.lang.Specification

Expand Down Expand Up @@ -74,8 +73,20 @@ class WindowsInstallationSupplierTest extends Specification {

def "handles absent adoptopenjdk keys"() {
given:
registry.getSubkeys(WindowsRegistry.Key.HKEY_LOCAL_MACHINE, "SOFTWARE\\AdoptOpenJDK\\JDK") >> { throw new MissingRegistryEntryException() }
def supplier = createSupplier(OperatingSystem.MAC_OS)
registry.getSubkeys(WindowsRegistry.Key.HKEY_LOCAL_MACHINE, _) >> { throw new MissingRegistryEntryException() }
def supplier = createSupplier()

when:
def locations = supplier.get()

then:
locations.isEmpty()
}

def "gracefully handles unavailable native integration"() {
given:
registry.getSubkeys(WindowsRegistry.Key.HKEY_LOCAL_MACHINE, _) >> { throw new NativeIntegrationUnavailableException() }
def supplier = createSupplier()

when:
def locations = supplier.get()
Expand Down Expand Up @@ -114,11 +125,4 @@ class WindowsInstallationSupplierTest extends Specification {
WindowsInstallationSupplier createSupplier(OperatingSystem os = OperatingSystem.WINDOWS) {
new WindowsInstallationSupplier(registry, os)
}

ProviderFactory createProviderFactory(String propertyValue) {
def providerFactory = Mock(ProviderFactory)
providerFactory.gradleProperty("org.gradle.java.installations.auto-detect") >> Providers.notDefined()
providerFactory
}

}

0 comments on commit a8931dc

Please sign in to comment.