8000 Add testing util functions (for internal use). · coderabbit-test/bazel@2f345ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f345ca

Browse files
Googlercopybara-github
authored andcommitted
Add testing util functions (for internal use).
PiperOrigin-RevId: 746090446 Change-Id: I5eb8dc3acc6621ffd15ff31149f33e2010ce009a
1 parent 1bf547e commit 2f345ca

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.google.devtools.common.options.OptionsBase;
7777
import com.google.devtools.common.options.OptionsParser;
7878
import com.google.devtools.common.options.OptionsParsingException;
79+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
7980
import com.google.protobuf.Any;
8081
import com.google.protobuf.Message;
8182
import java.util.ArrayList;
@@ -172,19 +173,25 @@ public final CommandEnvironment newCommand(Class<? extends BlazeCommand> command
172173

173174
/**
174175
* Creates a new command environment with additional proto extensions as if they were passed to
175-
* the blaze server.
176+
* the Blaze server.
177+
*
178+
* @param command the command instance for which to create a new environment.
179+
* @param extensions additional proto extensions to pass to the command.
180+
* @return the new command environment.
176181
*/
177-
public final CommandEnvironment newCommandWithExtensions(
178-
Class<? extends BlazeCommand> command, List<Message> extensions) throws Exception {
182+
@CanIgnoreReturnValue
183+
public final CommandEnvironment newCustomCommandWithExtensions(
184+
BlazeCommand command, List<Message> extensions) throws Exception {
179185
Command commandAnnotation =
180186
checkNotNull(
181-
command.getAnnotation(Command.class),
187+
command.getClass().getAnnotation(Command.class),
182188
"BlazeCommand %s missing command annotation",
183-
command);
184-
this.command = command.getDeclaredConstructor().newInstance();
189+
command.getClass());
190+
this.command = command;
191+
185192
additionalOptionsClasses.addAll(
186193
BlazeCommandUtils.getOptions(
187-
command, runtime.getBlazeModules(), runtime.getRuleClassProvider()));
194+
command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider()));
188195
initializeOptionsParser(commandAnnotation);
189196

190197
checkNotNull(
@@ -211,6 +218,22 @@ public final CommandEnvironment newCommandWithExtensions(
211218
return env;
212219
}
213220

221+
/**
222+
* Creates a new command environment with additional proto extensions as if they were passed to
223+
* the Blaze server. This method creates a new instance of the provided command class via its
224+
* default constructor. For command classes with constructor parameters, use {@link
225+
* #newCustomCommandWithExtensions} and pass in a pre-existing {@link BlazeCommand} instance.
226+
*
227+
* @param command the command class for which to create a new environment. This class must have a
228+
* default constructor or this method will throw an exception.
229+
* @param extensions additional proto extensions to pass to the command.
230+
*/
231+
public final CommandEnvironment newCommandWithExtensions(
232+
Class<? extends BlazeCommand> command, List<Message> extensions) throws Exception {
233+
return newCustomCommandWithExtensions(
234+
command.getDeclaredConstructor().newInstance(), extensions);
235+
}
236+
214237
/**
215238
* Returns the command environment. You must call {@link #newCommand()} before calling this
216239
* method.
@@ -236,6 +259,14 @@ public void addOptions(List<String> args) {
236259
optionsToParse.addAll(args);
237260
}
238261

262+
public void setOptionsParserResidue(List<String> residue, List<String> postDoubleDashResidue) {
263+
optionsParser.setResidue(residue, postDoubleDashResidue);
264+
}
265+
266+
public void setConfiguration(BuildConfigurationValue configuration) {
267+
this.configuration = configuration;
268+
}
269+
239270
public void addStarlarkOption(String label, Object value) {
240271
starlarkOptions.put(Label.parseCanonicalUnchecked(label).getCanonicalForm(), value);
241272
}
@@ -319,10 +350,10 @@ private OptionsParser createOptionsParser(Command commandAnnotation) {
319350
return OptionsParser.builder().optionsClasses(options).ignoreUserOptions().build();
320351
}
321352

322-
void executeNonBuildCommand() throws Exception {
353+
public void executeCustomCommand() throws Exception {
323354
checkNotNull(command, "No command created, try calling newCommand()");
324355
checkState(
325-
env.getCommand().buildPhase() == NONE,
356+
env.getCommand().buildPhase() == NONE || env.getCommandName().equals("run"),
326357
"%s is a build command, did you mean to call executeBuild()?",
327358
env.getCommandName());
328359

@@ -337,6 +368,11 @@ void executeNonBuildCommand() throws Exception {
337368
try {
338369
Crash crash = null;
339370
try {
371+
if (env.getCommandName().equals("run")) {
372+
try (SilentCloseable c = Profiler.instance().profile("syncPackageLoading")) {
373+
env.syncPackageLoading(optionsParser);
374+
}
375+
}
340376
result = command.exec(env, optionsParser);
341377
} catch (RuntimeException | Error e) {
342378
crash = Crash.from(e);

src/test/java/com/google/devtools/build/lib/buildtool/util/BuildIntegrationTestCase.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.google.devtools.build.lib.bugreport.CrashContext;
6666
import com.google.devtools.build.lib.buildtool.BuildRequest;
6767
import com.google.devtools.build.lib.buildtool.BuildResult;
68-
import com.google.devtools.build.lib.buildtool.BuildTool;
6968
import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
7069
import com.google.devtools.build.lib.cmdline.Label;
7170
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
@@ -861,14 +860,14 @@ public final BuildResult buildTarget(List<String> targets) throws Exception {
861860
protected void info() throws Exception {
862861
events.setOutErr(outErr);
863862
runtimeWrapper.newCommand(InfoCommand.class);
864-
runtimeWrapper.executeNonBuildCommand();
863+
runtimeWrapper.executeCustomCommand();
865864
}
866865

867866
/** Runs the {@code clean} command. */
868867
public void clean() throws Exception {
< 2364 /code>
869868
events.setOutErr(outErr);
870869
runtimeWrapper.newCommand(CleanCommand.class);
871-
runtimeWrapper.executeNonBuildCommand();
870+
runtimeWrapper.executeCustomCommand();
872871
}
873872

874873
/** Utility function: parse a string as a label. */

0 commit comments

Comments
 (0)
0