Skip to content

Commit ef4065d

Browse files
fix: #355 Explicitly passing --project argument when starting emulator (#923)
* Running gcloud datastore emulator with --project argument * Allow user to pass a custom project id when creating a new LocalDatastoreHelper instance * Creating a local variable to increase the readability and emphasizing on the fact that project id not nullable now * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1e7a0a9 commit ef4065d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.datastore.testing;
1818

19+
import static com.google.common.base.MoreObjects.firstNonNull;
20+
1921
import com.google.api.core.InternalApi;
2022
import com.google.cloud.NoCredentials;
2123
import com.google.cloud.ServiceOptions;
@@ -68,7 +70,9 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
6870

6971
// Common settings
7072
private static final String CONSISTENCY_FLAG = "--consistency=";
73+
private static final String PROJECT_FLAG = "--project=";
7174
private static final double DEFAULT_CONSISTENCY = 0.9;
75+
private static final String DEFAULT_PROJECT_ID = PROJECT_ID_PREFIX + UUID.randomUUID();
7276

7377
private static final Logger LOGGER = Logger.getLogger(LocalDatastoreHelper.class.getName());
7478

@@ -90,6 +94,7 @@ public static class Builder {
9094
private int port;
9195
private Path dataDir;
9296
private boolean storeOnDisk = true;
97+
private String projectId;
9398

9499
private Builder() {}
95100

@@ -109,6 +114,11 @@ public Builder setPort(int port) {
109114
return this;
110115
}
111116

117+
public Builder setProjectId(String projectId) {
118+
this.projectId = projectId;
119+
return this;
120+
}
121+
112122
public Builder setDataDir(Path dataDir) {
113123
this.dataDir = dataDir;
114124
return this;
@@ -129,7 +139,8 @@ private LocalDatastoreHelper(Builder builder) {
129139
super(
130140
"datastore",
131141
builder.port > 0 ? builder.port : BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT),
132-
PROJECT_ID_PREFIX + UUID.randomUUID().toString());
142+
firstNonNull(builder.projectId, DEFAULT_PROJECT_ID));
143+
String projectId = firstNonNull(builder.projectId, DEFAULT_PROJECT_ID);
133144
this.consistency = builder.consistency > 0 ? builder.consistency : DEFAULT_CONSISTENCY;
134145
this.gcdPath = builder.dataDir;
135146
this.storeOnDisk = builder.storeOnDisk;
@@ -140,6 +151,7 @@ private LocalDatastoreHelper(Builder builder) {
140151
List<String> gcloudCommand = new ArrayList<>(Arrays.asList(GCLOUD_CMD_TEXT.split(" ")));
141152
gcloudCommand.add(GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort());
142153
gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency);
154+
gcloudCommand.add(PROJECT_FLAG + projectId);
143155
if (!builder.storeOnDisk) {
144156
gcloudCommand.add("--no-store-on-disk");
145157
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public void testCreateWithBuilder() {
8888
assertTrue(incompleteHelper.getProjectId().startsWith(PROJECT_ID_PREFIX));
8989
}
9090

91+
@Test
92+
public void testCreateWithCustomProjectId() {
93+
String customProjectId = "custom-project-id";
94+
LocalDatastoreHelper helper =
95+
LocalDatastoreHelper.newBuilder().setProjectId(customProjectId).build();
96+
assertEquals(customProjectId, helper.getProjectId());
97+
}
98+
9199
@Test
92100
public void testCreateWithToBuilder() throws IOException {
93101
LocalDatastoreHelper helper =

0 commit comments

Comments
 (0)