Skip to content

Commit

Permalink
default locations (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo authored and chanseokoh committed Mar 7, 2018
1 parent 80caf03 commit a7ce77d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.cloud.tools.eclipse.test.util.MockSdkGenerator;
import com.google.cloud.tools.eclipse.test.util.ui.CompositeUtil;
import com.google.cloud.tools.eclipse.test.util.ui.ShellTestResource;
import java.io.IOException;
import java.nio.file.Path;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.preference.IPreferenceStore;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void testNonExistentPath() {
}

@Test
public void testVersion() throws Exception {
public void testVersion() throws IOException {
when(cloudSdkManager.isManagedSdkFeatureEnabled()).thenReturn(true);

Path mockSdk = MockSdkGenerator.createMockSdk("1.23.4");
Expand Down Expand Up @@ -155,6 +156,20 @@ public void testUi_sdkManagementFeature() {
assertNotNull(sdkLocation);
assertNotNull(sdkVersion);
}

@Test
// https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/2897
public void testSearchSdkIfSdkLocationIsEmpty() {
when(cloudSdkManager.isManagedSdkFeatureEnabled()).thenReturn(true);
when(preferences.getString(CloudSdkPreferences.CLOUD_SDK_PATH)).thenReturn("");
when(preferences.getString(CloudSdkPreferences.CLOUD_SDK_MANAGEMENT)).thenReturn("MANUAL");

createPreferenceArea();

assertTrue(sdkLocation.getText().endsWith("google-cloud-sdk"));
assertTrue(sdkVersion.getText().length() > 3);
assertEquals(sdkLocation.getText(), sdkVersion.getToolTipText());
}

@Test
public void testControlStates_automaticSdk() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Require-Bundle: org.eclipse.ui;bundle-version="3.107.0",
com.google.cloud.tools.eclipse.preferences
Import-Package: com.google.cloud.tools.eclipse.ui.util,
com.google.common.annotations;version="[21.0.0,22.0.0)",
com.google.common.base;version="[21.0.0,22.0.0)",
org.eclipse.core.runtime.jobs,
org.eclipse.osgi.util;version="1.1.0",
org.eclipse.ui.console,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkNotFoundException;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkOutOfDateException;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkVersionFileException;
import com.google.cloud.tools.appengine.cloudsdk.CloudSdkVersionFileNotFoundException;
import com.google.cloud.tools.eclipse.preferences.areas.PreferenceArea;
import com.google.cloud.tools.eclipse.sdk.CloudSdkManager;
import com.google.cloud.tools.eclipse.sdk.internal.CloudSdkPreferences;
Expand All @@ -30,6 +31,7 @@
import com.google.cloud.tools.managedcloudsdk.ManagedCloudSdk;
import com.google.cloud.tools.managedcloudsdk.UnsupportedOsException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -176,8 +178,25 @@ private void updateSelectedVersion() {
String location = null;
if (!cloudSdkManager.isManagedSdkFeatureEnabled() || chooseSdk.getSelection()) {
location = sdkLocation.getStringValue();
Path path = Paths.get(location);
version = getSdkVersion(path);
if (Strings.isNullOrEmpty(location)) {
try {
// look in default locations; see
// https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/2897
CloudSdk sdk = new CloudSdk.Builder().build();
location = sdk.getSdkPath().toString();
version = sdk.getVersion().toString();
// ends up calling this method again
sdkLocation.setStringValue(location);
} catch (CloudSdkNotFoundException | CloudSdkVersionFileNotFoundException ex) {
// surprising but here a CloudSdkVersionFileNotFoundException also means
// no SDK is found where expected, probably because it was moved or deleted
// behind Eclipse's back
version = Messages.getString("NoSdkFound");
}
} else {
Path path = Paths.get(location);
version = getSdkVersion(path);
}
} else if (cloudSdkManager.isManagedSdkFeatureEnabled()) {
try {
Path home = ManagedCloudSdk.newManagedSdk().getSdkHome();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Path createMockSdk() throws Exception {
return createMockSdk("184.0.0");
}

public static Path createMockSdk(String sdkVersion) throws Exception {
public static Path createMockSdk(String sdkVersion) throws IOException {
Path mockSdk = Files.createTempDirectory("mock-gcloud-sdk");
assertTrue(mockSdk.toFile().isDirectory());

Expand All @@ -61,11 +61,11 @@ public static void deleteMockSdk(Path mockSdk) throws IOException {
MoreFiles.deleteRecursively(mockSdk, RecursiveDeleteOption.ALLOW_INSECURE);
}

private static void createEmptyFile(Path path) throws Exception {
private static void createEmptyFile(Path path) throws IOException {
createFile(path, "");
}

private static void createFile(Path path, String content) throws Exception {
private static void createFile(Path path, String content) throws IOException {
Files.createDirectories(path.getParent());
assertTrue(path.getParent().toFile().isDirectory());
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
Expand Down

0 comments on commit a7ce77d

Please sign in to comment.