laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.buildtool; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | import static org.junit.Assert.assertThrows; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildtool.AqueryProcessor.AqueryActionFilterException; |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase; |
| 22 | import com.google.devtools.build.lib.query2.aquery.ActionGraphQueryEnvironment; |
janakr | ff186cc | 2020-11-16 12:23:53 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.query2.aquery.AqueryOptions; |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction; |
| 25 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
| 26 | import com.google.devtools.build.lib.query2.engine.QueryParser; |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.runtime.BlazeCommandResult; |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.runtime.commands.AqueryCommand; |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.server.FailureDetails.ActionQuery.Code; |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 36 | /** Integration tests for aquery. */ |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 37 | @RunWith(JUnit4.class) |
| 38 | public class AqueryBuildToolTest extends BuildIntegrationTestCase { |
| 39 | private ImmutableMap<String, QueryFunction> functions; |
| 40 | |
| 41 | @Before |
janakr | ff186cc | 2020-11-16 12:23:53 -0800 | [diff] [blame] | 42 | public final void setFunctions() { |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 43 | 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 | |
Googler | ba1ef92 | 2022-02-03 06:55:30 -0800 | [diff] [blame] | 53 | functions = builder.buildOrThrow(); |
janakr | ff186cc | 2020-11-16 12:23:53 -0800 | [diff] [blame] | 54 | runtimeWrapper.addOptionsClass(AqueryOptions.class); |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 58 | public void testConstructor_wrongAqueryFilterFormat_throwsError() throws Exception { |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 59 | QueryExpression expr = QueryParser.parse("deps(inputs('abc', //abc))", functions); |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 60 | |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 61 | assertThrows(AqueryActionFilterException.class, () -> new AqueryProcessor(expr)); |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 65 | public void testConstructor_wrongPatternSyntax_throwsError() throws Exception { |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 66 | QueryExpression expr = QueryParser.parse("inputs('*abc', //abc)", functions); |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 67 | |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 68 | AqueryActionFilterException thrown = |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 69 | assertThrows(AqueryActionFilterException.class, () -> new AqueryProcessor(expr)); |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 70 | assertThat(thrown).hasMessageThat().contains("Wrong query syntax:"); |
| 71 | } |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 72 | |
| 73 | @Test |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 74 | public void testDmpActionGraphFromSkyframe_wrongOutputFormat_returnsFailure() throws Exception { |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 75 | addOptions("--output=text"); |
| 76 | CommandEnvironment env = runtimeWrapper.newCommand(AqueryCommand.class); |
michajlo | 2c13ea7 | 2021-11-18 06:26:27 -0800 | [diff] [blame] | 77 | AqueryProcessor aqueryProcessor = new AqueryProcessor(null); |
Googler | 3916df6 | 2022-06-15 06:04:05 -0700 | [diff] [blame^] | 78 | BlazeCommandResult result = aqueryProcessor.dumpActionGraphFromSkyframe(env); |
leba | 207c140 | 2020-07-23 06:19:09 -0700 | [diff] [blame] | 79 | |
| 80 | assertThat(result.isSuccess()).isFalse(); |
| 81 | assertThat(result.getDetailedExitCode().getFailureDetail().getActionQuery().getCode()) |
| 82 | .isEqualTo(Code.SKYFRAME_STATE_PREREQ_UNMET); |
| 83 | } |
laszlocsomor | 4d6ae2c | 2020-02-28 06:57:42 -0800 | [diff] [blame] | 84 | } |