Skip to content

Commit d5095db

Browse files
gregestrencopybara-github
authored andcommitted
"bazel config" output tests: skip noconfig.
The affected test does basic sanity testing on the expected structure of "bazel config <configHash>" output. Including checking that some basic bits of structure are in that output. noconfig is a special configuration that strips out most structure, so isn't a good candidate for this test. PiperOrigin-RevId: 504598184 Change-Id: Ia9dc80770c8b7f1edf5245d6a0a75b913539da86
1 parent 1262540 commit d5095db

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

src/test/java/com/google/devtools/build/lib/runtime/commands/ConfigCommandTest.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
package com.google.devtools.build.lib.runtime.commands;
1616

17+
import static com.google.common.collect.ImmutableList.toImmutableList;
1718
import static com.google.common.truth.Truth.assertThat;
19+
import static com.google.common.truth.Truth.assertWithMessage;
1820

21+
import com.google.common.base.Predicates;
1922
import com.google.common.collect.ImmutableList;
2023
import com.google.common.collect.ImmutableSet;
2124
import com.google.common.collect.Iterables;
@@ -195,23 +198,54 @@ public void showConfigIds() throws Exception {
195198
assertThat(fullJson.get("configuration-IDs").getAsJsonArray().size()).isEqualTo(3);
196199
}
197200

201+
private boolean skipNoConfig(JsonElement configHash) {
202+
try {
203+
return !new Gson()
204+
.fromJson(
205+
callConfigCommand(configHash.getAsString()).outAsLatin1(),
206+
ConfigurationForOutput.class)
207+
.mnemonic
208+
.contains("-noconfig");
209+
} catch (Exception e) {
210+
assertWithMessage("Failed to retrieve %s: %s", configHash.getAsString(), e.getMessage())
211+
.fail();
212+
return false;
213+
}
214+
}
215+
216+
/**
217+
* Calls the config command to return all config hashes currently available.
218+
*
219+
* @param includeNoConfig if true, include the "noconfig" configuration (see {@link
220+
* com.google.devtools.build.lib.analysis.config.transitions.NoConfigTransition}. Else filter
221+
* it out.
222+
*/
223+
private ImmutableList<String> getConfigHashes(boolean includeNoConfig) throws Exception {
224+
return JsonParser.parseString(callConfigCommand().outAsLatin1())
225+
.getAsJsonObject()
226+
.get("configuration-IDs")
227+
.getAsJsonArray()
228+
.asList()
229+
.stream()
230+
.filter(includeNoConfig ? Predicates.alwaysTrue() : this::skipNoConfig)
231+
.map(c -> c.getAsString())
232+
.collect(toImmutableList());
233+
}
234+
198235
@Test
199236
public void showSingleConfig() throws Exception {
200237
analyzeTarget();
201-
String configHash1 =
202-
JsonParser.parseString(callConfigCommand().outAsLatin1())
203-
.getAsJsonObject()
204-
.get("configuration-IDs")
205-
.getAsJsonArray()
206-
.get(0)
207-
.getAsString();
238+
// Find the first non-noconfig configuration (see NoConfigTransition). noconfig is a special
239+
// configuration that strips away most of its structure, so not a good candidate for this test.
240+
String configHash = getConfigHashes(/* includeNoConfig= */ false).get(0);
208241
ConfigurationForOutput config =
209242
new Gson()
210-
.fromJson(callConfigCommand(configHash1).outAsLatin1(), ConfigurationForOutput.class);
243+
.fromJson(callConfigCommand(configHash).outAsLatin1(), ConfigurationForOutput.class);
244+
211245
assertThat(config).isNotNull();
212246
// Verify config metadata:
213-
assertThat(config.configHash).isEqualTo(configHash1);
214-
assertThat(config.skyKey).isEqualTo(String.format("BuildConfigurationKey[%s]", configHash1));
247+
assertThat(config.configHash).isEqualTo(configHash);
248+
assertThat(config.skyKey).isEqualTo(String.format("BuildConfigurationKey[%s]", configHash));
215249
// Verify the existence of a couple of expected fragments:
216250
assertThat(
217251
config.fragments.stream()

0 commit comments

Comments
 (0)