Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja Lorente committed Jan 17, 2024
1 parent b139378 commit 4ac71a1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public RunConfigurationStateEditor getEditor(Project project) {
return new Editor();
}

public void setEnvVars(Map<String, String> vars) {
data = data.with(vars);
}

private static class Editor implements RunConfigurationStateEditor {
private final EnvironmentVariablesComponent component = new EnvironmentVariablesComponent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4ac71a1

Please sign in to comment.