starlark_doc_extract: match legacy Stardoc behavior for private aspect attrs and attr.license()
For private aspect propagation attrs, legacy Stardoc does not emit documentation.
For the deprecated attr.license() attributes, legacy Stardoc pretends the attribute
is a string list, so we do the same. (Note that we have to finally fix
License.repr() to return something sensible that can be parsed by
BuildType.LICENSE.convert() - i.e. by License.parseLicense() - because
starlark_doc_extract, unlike legacy Stardoc, correctly processes attribute default
values.)
PiperOrigin-RevId: 543457875
Change-Id: I5d01312e30ccdc8b1d71322f496ec8a237fa993d
diff --git a/src/test/java/com/google/devtools/build/lib/packages/LicenseTest.java b/src/test/java/com/google/devtools/build/lib/packages/LicenseTest.java
index 845f0b5..5db294d 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/LicenseTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/LicenseTest.java
@@ -15,8 +15,17 @@
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.packages.License.LicenseType;
import java.util.Arrays;
+import net.starlark.java.eval.Module;
+import net.starlark.java.eval.Mutability;
+import net.starlark.java.eval.Sequence;
+import net.starlark.java.eval.Starlark;
+import net.starlark.java.eval.StarlarkSemantics;
+import net.starlark.java.eval.StarlarkThread;
+import net.starlark.java.syntax.FileOptions;
+import net.starlark.java.syntax.ParserInput;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -35,4 +44,41 @@
assertThat(License.leastRestrictive(Arrays.<LicenseType>asList()))
.isEqualTo(LicenseType.BY_EXCEPTION_ONLY);
}
+
+ /** Evaluates a string as a Starlark expression returning a sequence of strings. */
+ private static Sequence<String> evalAsSequence(String string) throws Exception {
+ ParserInput input = ParserInput.fromLines(string);
+ Mutability mutability = Mutability.create("test");
+ Object parsedValue =
+ Starlark.execFile(
+ input,
+ FileOptions.DEFAULT,
+ Module.create(),
+ new StarlarkThread(mutability, StarlarkSemantics.DEFAULT));
+ mutability.freeze();
+ return Sequence.cast(parsedValue, String.class, "evalAsSequence() input");
+ }
+
+ @Test
+ public void repr() throws Exception {
+ assertThat(Starlark.repr(License.NO_LICENSE)).isEqualTo("[\"none\"]");
+ assertThat(License.parseLicense(evalAsSequence(Starlark.repr(License.NO_LICENSE))))
+ .isEqualTo(License.NO_LICENSE);
+
+ License withoutExceptions = License.parseLicense(ImmutableList.of("notice", "restricted"));
+ // License types sorted by LicenseType enum order.
+ assertThat(Starlark.repr(withoutExceptions)).isEqualTo("[\"restricted\", \"notice\"]");
+ assertThat(License.parseLicense(evalAsSequence(Starlark.repr(withoutExceptions))))
+ .isEqualTo(withoutExceptions);
+
+ License withExceptions =
+ License.parseLicense(
+ ImmutableList.of("notice", "restricted", "exception=//foo:bar", "exception=//baz:qux"));
+ // Exceptions sorted alphabetically.
+ assertThat(Starlark.repr(withExceptions))
+ .isEqualTo(
+ "[\"restricted\", \"notice\", \"exception=//baz:qux\", \"exception=//foo:bar\"]");
+ assertThat(License.parseLicense(evalAsSequence(Starlark.repr(withExceptions))))
+ .isEqualTo(withExceptions);
+ }
}