diff --git a/base/src/com/google/idea/blaze/base/run/state/EnvironmentVariablesState.java b/base/src/com/google/idea/blaze/base/run/state/EnvironmentVariablesState.java index 4984a78f5f6..a62aa863594 100644 --- a/base/src/com/google/idea/blaze/base/run/state/EnvironmentVariablesState.java +++ b/base/src/com/google/idea/blaze/base/run/state/EnvironmentVariablesState.java @@ -70,6 +70,10 @@ public RunConfigurationStateEditor getEditor(Project project) { return new Editor(); } + public void setEnvVars(Map vars) { + data = data.with(vars); + } + private static class Editor implements RunConfigurationStateEditor { private final EnvironmentVariablesComponent component = new EnvironmentVariablesComponent(); diff --git a/base/tests/integrationtests/com/google/idea/blaze/base/run/BlazeCommandRunConfigurationGenericHandlerIntegrationTest.java b/base/tests/integrationtests/com/google/idea/blaze/base/run/BlazeCommandRunConfigurationGenericHandlerIntegrationTest.java index fb9bebd1080..f2dd41ffe1f 100644 --- a/base/tests/integrationtests/com/google/idea/blaze/base/run/BlazeCommandRunConfigurationGenericHandlerIntegrationTest.java +++ b/base/tests/integrationtests/com/google/idea/blaze/base/run/BlazeCommandRunConfigurationGenericHandlerIntegrationTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.idea.blaze.base.BlazeIntegrationTestCase; import com.google.idea.blaze.base.command.BlazeCommandName; import com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder; @@ -88,6 +89,7 @@ public void testReadAndWriteMatches() throws Exception { state.getBlazeFlagsState().setRawFlags(ImmutableList.of("--flag1", "--flag2")); state.getExeFlagsState().setRawFlags(ImmutableList.of("--exeFlag1")); state.getBlazeBinaryState().setBlazeBinary("/usr/bin/blaze"); + state.getUserEnvVarsState().setEnvVars(ImmutableMap.of("HELLO", "world")); Element element = new Element("test"); configuration.writeExternal(element); @@ -107,6 +109,7 @@ public void testReadAndWriteMatches() throws Exception { .inOrder(); assertThat(readState.getExeFlagsState().getRawFlags()).containsExactly("--exeFlag1"); assertThat(readState.getBlazeBinaryState().getBlazeBinary()).isEqualTo("/usr/bin/blaze"); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()).isEqualTo(ImmutableMap.of("HELLO", "world")); } @Test @@ -155,6 +158,8 @@ public void testEditorApplyToAndResetFromMatches() throws ConfigurationException .isEqualTo(state.getExeFlagsState().getRawFlags()); assertThat(readState.getBlazeBinaryState().getBlazeBinary()) .isEqualTo(state.getBlazeBinaryState().getBlazeBinary()); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()) + .isEqualTo(state.getUserEnvVarsState().getData().getEnvs()); Disposer.dispose(editor); } @@ -185,6 +190,7 @@ public void testEditorApplyToAndResetFromHandlesNulls() throws ConfigurationExce readState.getBlazeFlagsState().setRawFlags(ImmutableList.of("--flag1", "--flag2")); readState.getExeFlagsState().setRawFlags(ImmutableList.of("--exeFlag1")); readState.getBlazeBinaryState().setBlazeBinary("/usr/bin/blaze"); + readState.getUserEnvVarsState().setEnvVars(ImmutableMap.of("HELLO", "world")); editor.applyEditorTo(readConfiguration); @@ -201,6 +207,8 @@ public void testEditorApplyToAndResetFromHandlesNulls() throws ConfigurationExce .isEqualTo(state.getExeFlagsState().getRawFlags()); assertThat(readState.getBlazeBinaryState().getBlazeBinary()) .isEqualTo(state.getBlazeBinaryState().getBlazeBinary()); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()) + .isEqualTo(state.getUserEnvVarsState().getData().getEnvs()); Disposer.dispose(editor); } diff --git a/base/tests/unittests/com/google/idea/blaze/base/run/state/BlazeCommandRunConfigurationCommonStateTest.java b/base/tests/unittests/com/google/idea/blaze/base/run/state/BlazeCommandRunConfigurationCommonStateTest.java index 42a1962a667..5e9db6f6832 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/run/state/BlazeCommandRunConfigurationCommonStateTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/run/state/BlazeCommandRunConfigurationCommonStateTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.idea.blaze.base.BlazeTestCase; import com.google.idea.blaze.base.command.BlazeCommandName; @@ -59,6 +60,7 @@ public void readAndWriteShouldMatch() throws Exception { state.getBlazeFlagsState().setRawFlags(ImmutableList.of("--flag1", "--flag2")); state.getExeFlagsState().setRawFlags(ImmutableList.of("--exeFlag1")); state.getBlazeBinaryState().setBlazeBinary("/usr/bin/blaze"); + state.getUserEnvVarsState().setEnvVars(ImmutableMap.of("HELLO", "world")); Element element = new Element("test"); state.writeExternal(element); @@ -72,6 +74,7 @@ public void readAndWriteShouldMatch() throws Exception { .inOrder(); assertThat(readState.getExeFlagsState().getRawFlags()).containsExactly("--exeFlag1"); assertThat(readState.getBlazeBinaryState().getBlazeBinary()).isEqualTo("/usr/bin/blaze"); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()).isEqualTo(ImmutableMap.of("HELLO", "world")); } @Test @@ -90,6 +93,8 @@ public void readAndWriteShouldHandleNulls() throws Exception { .isEqualTo(state.getExeFlagsState().getRawFlags()); assertThat(readState.getBlazeBinaryState().getBlazeBinary()) .isEqualTo(state.getBlazeBinaryState().getBlazeBinary()); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()) + .isEqualTo(state.getUserEnvVarsState().getData().getEnvs()); } @Test @@ -155,6 +160,8 @@ public void editorApplyToAndResetFromShouldMatch() throws Exception { .isEqualTo(state.getExeFlagsState().getRawFlags()); assertThat(readState.getBlazeBinaryState().getBlazeBinary()) .isEqualTo(state.getBlazeBinaryState().getBlazeBinary()); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()) + .isEqualTo(state.getUserEnvVarsState().getData().getEnvs()); } @Test @@ -174,5 +181,7 @@ public void editorApplyToAndResetFromShouldHandleNulls() throws Exception { .isEqualTo(state.getExeFlagsState().getRawFlags()); assertThat(readState.getBlazeBinaryState().getBlazeBinary()) .isEqualTo(state.getBlazeBinaryState().getBlazeBinary()); + assertThat(readState.getUserEnvVarsState().getData().getEnvs()) + .isEqualTo(state.getUserEnvVarsState().getData().getEnvs()); } } diff --git a/base/tests/unittests/com/google/idea/blaze/base/run/state/EnvironmentVariablesStateTest.java b/base/tests/unittests/com/google/idea/blaze/base/run/state/EnvironmentVariablesStateTest.java new file mode 100644 index 00000000000..87064906cf6 --- /dev/null +++ b/base/tests/unittests/com/google/idea/blaze/base/run/state/EnvironmentVariablesStateTest.java @@ -0,0 +1,45 @@ +package com.google.idea.blaze.base.run.state; + + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Map; + +/** Unit tests for {@link EnvironmentVariablesState}. */ +@RunWith(JUnit4.class) +public class EnvironmentVariablesStateTest { + + @Test + public void testSetEnvVarsReadWrite() { + Map env = ImmutableMap.of("HELLO", "world", "HI", "friends"); + EnvironmentVariablesState state = new EnvironmentVariablesState(); + + assertThat(state.getData().getEnvs()).isEmpty(); + state.setEnvVars(env); + + RunConfigurationStateEditor editor = state.getEditor(null); + editor.resetEditorFrom(state); + editor.applyEditorTo(state); + + assertThat(state.getData().getEnvs()).isEqualTo(env); + assertThat(state.asBlazeTestEnvFlags()) + .containsExactly("--test_env", "HELLO=world", "--test_env", "HI=friends") + .inOrder(); + } + + @Test + public void testAsBlazeTestFlags() { + EnvironmentVariablesState state = new EnvironmentVariablesState(); + assertThat(state.asBlazeTestEnvFlags()).isEmpty(); + state.setEnvVars(ImmutableMap.of("HELLO", "world", "HI", "friends")); + assertThat(state.asBlazeTestEnvFlags()) + .containsExactly("--test_env", "HELLO=world", "--test_env", "HI=friends") + .inOrder(); + } +} diff --git a/java/tests/unittests/com/google/idea/blaze/java/run/BlazeJavaRunProfileStateTest.java b/java/tests/unittests/com/google/idea/blaze/java/run/BlazeJavaRunProfileStateTest.java index 79711a1ed6e..b4f445dde33 100644 --- a/java/tests/unittests/com/google/idea/blaze/java/run/BlazeJavaRunProfileStateTest.java +++ b/java/tests/unittests/com/google/idea/blaze/java/run/BlazeJavaRunProfileStateTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.Futures; import com.google.idea.blaze.base.BlazeTestCase; import com.google.idea.blaze.base.bazel.BuildSystemProvider; @@ -163,6 +164,53 @@ public void flagsShouldBeAppendedIfPresent() { "//label:rule")); } + @Test + public void envVarsAppearAsTestEnvWhenCommandIsTest() { + configuration.setTargetInfo( + TargetInfo.builder(Label.create("//label:rule"), "java_test").build()); + BlazeCommandRunConfigurationCommonState handlerState = + (BlazeCommandRunConfigurationCommonState) configuration.getHandler().getState(); + + handlerState.getCommandState().setCommand(BlazeCommandName.TEST); + handlerState.getUserEnvVarsState().setEnvVars(ImmutableMap.of("HELLO", "world")); + + // Regular Run + assertThat(BlazeJavaRunProfileState + .getBlazeCommandBuilder( + project, + configuration, + ImmutableList.of(), + ExecutorType.RUN, + null) + .build().toList()) + .containsExactly( + "/usr/bin/blaze", + "test", + BlazeFlags.getToolTagFlag(), + "--test_env", "HELLO=world", + "--", + "//label:rule" + ).inOrder(); + + // Fast build + assertThat(BlazeJavaRunProfileState + .getBlazeCommandBuilder( + project, + configuration, + ImmutableList.of(), + ExecutorType.FAST_BUILD_RUN, + null) + .build().toList()) + .containsExactly( + "/usr/bin/blaze", + "test", + BlazeFlags.getToolTagFlag(), + "--test_env", "HELLO=world", + "--", + "//label:rule" + ).inOrder(); + } + @Test public void debugFlagShouldBeIncludedForJavaTest() { configuration.setTargetInfo(