use of com.facebook.buck.util.HumanReadableException in project buck by facebook.
the class RunShTestAndRecordResultStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
TestResultSummary summary;
if (context.getPlatform() == Platform.WINDOWS) {
// Ignore sh_test on Windows.
summary = new TestResultSummary(getShortName(), "sh_test", /* type */
ResultType.SUCCESS, /* duration*/
0, /* message */
"sh_test ignored on Windows", /* stacktrace */
null, /* stdout */
null, /* stderr */
null);
} else {
ShellStep test = new ShellStep(filesystem.getRootPath()) {
boolean timedOut = false;
@Override
public String getShortName() {
return pathToShellScript.toString();
}
@Override
protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
return ImmutableList.<String>builder().add(pathToShellScript.toString()).addAll(args).build();
}
@Override
public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
return ImmutableMap.<String, String>builder().put("NO_BUCKD", "1").putAll(env).build();
}
@Override
protected boolean shouldPrintStderr(Verbosity verbosity) {
// Do not stream this output because we want to capture it.
return false;
}
@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
StepExecutionResult executionResult = super.execute(context);
if (timedOut) {
throw new HumanReadableException("Timed out running test: " + testCaseName + ", with exitCode: " + executionResult.getExitCode());
}
return executionResult;
}
@Override
protected Optional<Consumer<Process>> getTimeoutHandler(final ExecutionContext context) {
return Optional.of(process -> timedOut = true);
}
@Override
protected Optional<Long> getTimeout() {
return testRuleTimeoutMs;
}
@Override
protected boolean shouldPrintStdout(Verbosity verbosity) {
// Do not stream this output because we want to capture it.
return false;
}
};
StepExecutionResult executionResult = test.execute(context);
// Write test result.
boolean isSuccess = executionResult.isSuccess();
summary = new TestResultSummary(getShortName(), "sh_test", /* type */
isSuccess ? ResultType.SUCCESS : ResultType.FAILURE, test.getDuration(), /* message */
null, /* stacktrace */
null, test.getStdout(), test.getStderr());
}
ObjectMapper mapper = context.getObjectMapper();
try (OutputStream outputStream = filesystem.newFileOutputStream(pathToTestResultFile)) {
mapper.writeValue(outputStream, summary);
}
// should be zero.
return StepExecutionResult.SUCCESS;
}
use of com.facebook.buck.util.HumanReadableException in project buck by facebook.
the class WorkerProcessProtocolZero method receiveCommandResponse.
/*
Expects a message that looks like this if the job was successful:
,{
id: <messageID>,
type: 'result',
exit_code: 0
}
of a message that looks like this if message was correct but job failed due to various reasons:
,{
id: <messageID>,
type: 'result',
exit_code: <exitCode>
}
or a message that looks like this if the external tool received a message type it cannot
interpret:
,{
id: <messageID>,
type: 'error',
exit_code: 1
}
or a message that looks like this if the external tool received a valid message type but other
attributes of the message were in an inconsistent state:
,{
id: <messageID>,
type: 'error',
exit_code: 2
}
*/
@Override
public int receiveCommandResponse(int messageID) throws IOException {
int id = -1;
int exitCode = -1;
String type = "";
try {
processStdoutReader.beginObject();
while (processStdoutReader.hasNext()) {
String property = processStdoutReader.nextName();
if (property.equals("id")) {
id = processStdoutReader.nextInt();
} else if (property.equals("type")) {
type = processStdoutReader.nextString();
} else if (property.equals("exit_code")) {
exitCode = processStdoutReader.nextInt();
} else {
processStdoutReader.skipValue();
}
}
processStdoutReader.endObject();
} catch (IOException e) {
throw new HumanReadableException(e, "Error receiving command response from external process.\n" + "Stderr from external process:\n%s", getStdErrorOutput());
}
if (id != messageID) {
throw new HumanReadableException(String.format("Expected response's \"id\" value to be " + "\"%d\", got \"%d\" instead.", messageID, id));
}
if (!type.equals(TYPE_RESULT) && !type.equals(TYPE_ERROR)) {
throw new HumanReadableException(String.format("Expected response's \"type\" " + "to be one of [\"%s\",\"%s\"], got \"%s\" instead.", TYPE_RESULT, TYPE_ERROR, type));
}
return exitCode;
}
use of com.facebook.buck.util.HumanReadableException in project buck by facebook.
the class RuleKeyBuilder method setReflectively.
/** Recursively serializes the value. Serialization of the key is handled outside. */
protected RuleKeyBuilder<RULE_KEY> setReflectively(@Nullable Object val) {
if (val instanceof RuleKeyAppendable) {
return setAppendableRuleKey((RuleKeyAppendable) val);
}
if (val instanceof BuildRule) {
return setBuildRule((BuildRule) val);
}
if (val instanceof Supplier) {
try (Scope containerScope = scopedHasher.wrapperScope(Wrapper.SUPPLIER)) {
Object newVal = ((Supplier<?>) val).get();
return setReflectively(newVal);
}
}
if (val instanceof Optional) {
Object o = ((Optional<?>) val).orElse(null);
try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.OPTIONAL)) {
return setReflectively(o);
}
}
if (val instanceof Either) {
Either<?, ?> either = (Either<?, ?>) val;
if (either.isLeft()) {
try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_LEFT)) {
return setReflectively(either.getLeft());
}
} else {
try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_RIGHT)) {
return setReflectively(either.getRight());
}
}
}
// Note {@link java.nio.file.Path} implements "Iterable", so we explicitly exclude it here.
if (val instanceof Iterable && !(val instanceof Path)) {
try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
for (Object element : (Iterable<?>) val) {
try (Scope elementScope = containerScope.elementScope()) {
setReflectively(element);
}
}
return this;
}
}
if (val instanceof Iterator) {
Iterator<?> iterator = (Iterator<?>) val;
try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
while (iterator.hasNext()) {
try (Scope elementScope = containerScope.elementScope()) {
setReflectively(iterator.next());
}
}
}
return this;
}
if (val instanceof Map) {
if (!(val instanceof SortedMap || val instanceof ImmutableMap)) {
logger.warn("Adding an unsorted map to the rule key. " + "Expect unstable ordering and caches misses: %s", val);
}
try (ContainerScope containerScope = scopedHasher.containerScope(Container.MAP)) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) val).entrySet()) {
try (Scope elementScope = containerScope.elementScope()) {
setReflectively(entry.getKey());
}
try (Scope elementScope = containerScope.elementScope()) {
setReflectively(entry.getValue());
}
}
}
return this;
}
if (val instanceof Path) {
throw new HumanReadableException("It's not possible to reliably disambiguate Paths. They are disallowed from rule keys");
}
if (val instanceof SourcePath) {
try {
return setSourcePath((SourcePath) val);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (val instanceof NonHashableSourcePathContainer) {
SourcePath sourcePath = ((NonHashableSourcePathContainer) val).getSourcePath();
return setNonHashingSourcePath(sourcePath);
}
if (val instanceof SourceWithFlags) {
SourceWithFlags source = (SourceWithFlags) val;
try {
setSourcePath(source.getSourcePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
setReflectively(source.getFlags());
return this;
}
return setSingleValue(val);
}
use of com.facebook.buck.util.HumanReadableException in project buck by facebook.
the class QueryMacroExpander method extractTargets.
private Stream<BuildTarget> extractTargets(BuildTarget target, CellPathResolver cellNames, Optional<BuildRuleResolver> resolver, T input) {
String queryExpression = CharMatcher.anyOf("\"'").trimFrom(input.getQuery().getQuery());
final GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(resolver, targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
try {
QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
HashSet<String> targetLiterals = new HashSet<>();
parsedExp.collectTargetPatterns(targetLiterals);
return targetLiterals.stream().flatMap(pattern -> {
try {
return env.getTargetsMatchingPattern(pattern, executorService).stream();
} catch (Exception e) {
throw new HumanReadableException(e, "Error parsing target expression %s for target %s", pattern, target);
}
}).map(queryTarget -> {
Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
return ((QueryBuildTarget) queryTarget).getBuildTarget();
});
} catch (QueryException e) {
throw new HumanReadableException("Error executing query in macro for target %s", target, e);
}
}
use of com.facebook.buck.util.HumanReadableException in project buck by facebook.
the class ExportFileDescription method createBuildRule.
@Override
public <A extends Arg> ExportFile createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) {
BuildTarget target = params.getBuildTarget();
Mode mode = args.mode.orElse(Mode.COPY);
String name;
if (args.out.isPresent()) {
if (mode == ExportFileDescription.Mode.REFERENCE) {
throw new HumanReadableException("%s: must not set `out` for `export_file` when using `REFERENCE` mode", params.getBuildTarget());
}
name = args.out.get();
} else {
name = target.getShortNameAndFlavorPostfix();
}
SourcePath src;
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
if (args.src.isPresent()) {
if (mode == ExportFileDescription.Mode.REFERENCE && !pathResolver.getFilesystem(args.src.get()).equals(params.getProjectFilesystem())) {
throw new HumanReadableException("%s: must use `COPY` mode for `export_file` when source (%s) uses a different cell", target, args.src.get());
}
src = args.src.get();
} else {
src = new PathSourcePath(params.getProjectFilesystem(), target.getBasePath().resolve(target.getShortNameAndFlavorPostfix()));
}
return new ExportFile(params, ruleFinder, pathResolver, name, mode, src);
}
Aggregations