blob: 8f1429d46eb48a24c00175237d71baa26faf22e7 [file] [log] [blame]
laszlocsomor4d6ae2c2020-02-28 06:57:42 -08001// Copyright 2020 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.buildtool;
15
16import static com.google.common.truth.Truth.assertThat;
17import static org.junit.Assert.assertThrows;
18
19import com.google.common.collect.ImmutableMap;
michajlo2c13ea72021-11-18 06:26:27 -080020import com.google.devtools.build.lib.buildtool.AqueryProcessor.AqueryActionFilterException;
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080021import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
22import com.google.devtools.build.lib.query2.aquery.ActionGraphQueryEnvironment;
janakrff186cc2020-11-16 12:23:53 -080023import com.google.devtools.build.lib.query2.aquery.AqueryOptions;
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080024import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction;
25import com.google.devtools.build.lib.query2.engine.QueryExpression;
26import com.google.devtools.build.lib.query2.engine.QueryParser;
leba207c1402020-07-23 06:19:09 -070027import com.google.devtools.build.lib.runtime.BlazeCommandResult;
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080028import com.google.devtools.build.lib.runtime.CommandEnvironment;
leba207c1402020-07-23 06:19:09 -070029import com.google.devtools.build.lib.runtime.commands.AqueryCommand;
leba207c1402020-07-23 06:19:09 -070030import com.google.devtools.build.lib.server.FailureDetails.ActionQuery.Code;
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080031import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.junit.runners.JUnit4;
35
leba207c1402020-07-23 06:19:09 -070036/** Integration tests for aquery. */
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080037@RunWith(JUnit4.class)
38public class AqueryBuildToolTest extends BuildIntegrationTestCase {
39 private ImmutableMap<String, QueryFunction> functions;
40
41 @Before
janakrff186cc2020-11-16 12:23:53 -080042 public final void setFunctions() {
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080043 ImmutableMap.Builder<String, QueryFunction> builder = ImmutableMap.builder();
44
45 for (QueryFunction queryFunction : ActionGraphQueryEnvironment.FUNCTIONS) {
46 builder.put(queryFunction.getName(), queryFunction);
47 }
48
49 for (QueryFunction queryFunction : ActionGraphQueryEnvironment.AQUERY_FUNCTIONS) {
50 builder.put(queryFunction.getName(), queryFunction);
51 }
52
Googlerba1ef922022-02-03 06:55:30 -080053 functions = builder.buildOrThrow();
janakrff186cc2020-11-16 12:23:53 -080054 runtimeWrapper.addOptionsClass(AqueryOptions.class);
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080055 }
56
57 @Test
michajlo2c13ea72021-11-18 06:26:27 -080058 public void testConstructor_wrongAqueryFilterFormat_throwsError() throws Exception {
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080059 QueryExpression expr = QueryParser.parse("deps(inputs('abc', //abc))", functions);
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080060
michajlo2c13ea72021-11-18 06:26:27 -080061 assertThrows(AqueryActionFilterException.class, () -> new AqueryProcessor(expr));
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080062 }
63
64 @Test
michajlo2c13ea72021-11-18 06:26:27 -080065 public void testConstructor_wrongPatternSyntax_throwsError() throws Exception {
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080066 QueryExpression expr = QueryParser.parse("inputs('*abc', //abc)", functions);
jhorvitz4182af42020-12-28 09:57:34 -080067
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080068 AqueryActionFilterException thrown =
michajlo2c13ea72021-11-18 06:26:27 -080069 assertThrows(AqueryActionFilterException.class, () -> new AqueryProcessor(expr));
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080070 assertThat(thrown).hasMessageThat().contains("Wrong query syntax:");
71 }
leba207c1402020-07-23 06:19:09 -070072
73 @Test
michajlo2c13ea72021-11-18 06:26:27 -080074 public void testDmpActionGraphFromSkyframe_wrongOutputFormat_returnsFailure() throws Exception {
leba207c1402020-07-23 06:19:09 -070075 addOptions("--output=text");
76 CommandEnvironment env = runtimeWrapper.newCommand(AqueryCommand.class);
michajlo2c13ea72021-11-18 06:26:27 -080077 AqueryProcessor aqueryProcessor = new AqueryProcessor(null);
Googler3916df62022-06-15 06:04:05 -070078 BlazeCommandResult result = aqueryProcessor.dumpActionGraphFromSkyframe(env);
leba207c1402020-07-23 06:19:09 -070079
80 assertThat(result.isSuccess()).isFalse();
81 assertThat(result.getDetailedExitCode().getFailureDetail().getActionQuery().getCode())
82 .isEqualTo(Code.SKYFRAME_STATE_PREREQ_UNMET);
83 }
laszlocsomor4d6ae2c2020-02-28 06:57:42 -080084}