Skip to content

Commit

Permalink
"bazel config" output tests: skip noconfig.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gregestren authored and copybara-github committed Jan 25, 2023
1 parent 1262540 commit d5095db
Showing 1 changed file with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

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

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -195,23 +198,54 @@ public void showConfigIds() throws Exception {
assertThat(fullJson.get("configuration-IDs").getAsJsonArray().size()).isEqualTo(3);
}

private boolean skipNoConfig(JsonElement configHash) {
try {
return !new Gson()
.fromJson(
callConfigCommand(configHash.getAsString()).outAsLatin1(),
ConfigurationForOutput.class)
.mnemonic
.contains("-noconfig");
} catch (Exception e) {
assertWithMessage("Failed to retrieve %s: %s", configHash.getAsString(), e.getMessage())
.fail();
return false;
}
}

/**
* Calls the config command to return all config hashes currently available.
*
* @param includeNoConfig if true, include the "noconfig" configuration (see {@link
* com.google.devtools.build.lib.analysis.config.transitions.NoConfigTransition}. Else filter
* it out.
*/
private ImmutableList<String> getConfigHashes(boolean includeNoConfig) throws Exception {
return JsonParser.parseString(callConfigCommand().outAsLatin1())
.getAsJsonObject()
.get("configuration-IDs")
.getAsJsonArray()
.asList()
.stream()
.filter(includeNoConfig ? Predicates.alwaysTrue() : this::skipNoConfig)
.map(c -> c.getAsString())
.collect(toImmutableList());
}

@Test
public void showSingleConfig() throws Exception {
analyzeTarget();
String configHash1 =
JsonParser.parseString(callConfigCommand().outAsLatin1())
.getAsJsonObject()
.get("configuration-IDs")
.getAsJsonArray()
.get(0)
.getAsString();
// Find the first non-noconfig configuration (see NoConfigTransition). noconfig is a special
// configuration that strips away most of its structure, so not a good candidate for this test.
String configHash = getConfigHashes(/* includeNoConfig= */ false).get(0);
ConfigurationForOutput config =
new Gson()
.fromJson(callConfigCommand(configHash1).outAsLatin1(), ConfigurationForOutput.class);
.fromJson(callConfigCommand(configHash).outAsLatin1(), ConfigurationForOutput.class);

assertThat(config).isNotNull();
// Verify config metadata:
assertThat(config.configHash).isEqualTo(configHash1);
assertThat(config.skyKey).isEqualTo(String.format("BuildConfigurationKey[%s]", configHash1));
assertThat(config.configHash).isEqualTo(configHash);
assertThat(config.skyKey).isEqualTo(String.format("BuildConfigurationKey[%s]", configHash));
// Verify the existence of a couple of expected fragments:
assertThat(
config.fragments.stream()
Expand Down

0 comments on commit d5095db

Please sign in to comment.