blob: ee7f5f18fbbca965d054ecb42347d2c6fdb9b529 [file] [log] [blame]
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.packages;
15
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000017
Googler5ede15c2023-06-26 09:19:45 -070018import com.google.common.collect.ImmutableList;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000019import com.google.devtools.build.lib.packages.License.LicenseType;
lberkiaea56b32017-05-30 12:35:33 +020020import java.util.Arrays;
Googler5ede15c2023-06-26 09:19:45 -070021import net.starlark.java.eval.Module;
22import net.starlark.java.eval.Mutability;
23import net.starlark.java.eval.Sequence;
24import net.starlark.java.eval.Starlark;
25import net.starlark.java.eval.StarlarkSemantics;
26import net.starlark.java.eval.StarlarkThread;
27import net.starlark.java.syntax.FileOptions;
28import net.starlark.java.syntax.ParserInput;
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000029import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.junit.runners.JUnit4;
32
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000033@RunWith(JUnit4.class)
34public class LicenseTest {
35
36 @Test
37 public void testLeastRestrictive() {
lberkiaea56b32017-05-30 12:35:33 +020038 assertThat(License.leastRestrictive(Arrays.asList(LicenseType.RESTRICTED)))
39 .isEqualTo(LicenseType.RESTRICTED);
40 assertThat(
41 License.leastRestrictive(
42 Arrays.asList(LicenseType.RESTRICTED, LicenseType.BY_EXCEPTION_ONLY)))
43 .isEqualTo(LicenseType.RESTRICTED);
44 assertThat(License.leastRestrictive(Arrays.<LicenseType>asList()))
45 .isEqualTo(LicenseType.BY_EXCEPTION_ONLY);
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000046 }
Googler5ede15c2023-06-26 09:19:45 -070047
48 /** Evaluates a string as a Starlark expression returning a sequence of strings. */
49 private static Sequence<String> evalAsSequence(String string) throws Exception {
50 ParserInput input = ParserInput.fromLines(string);
51 Mutability mutability = Mutability.create("test");
52 Object parsedValue =
53 Starlark.execFile(
54 input,
55 FileOptions.DEFAULT,
56 Module.create(),
Googler11f06202024-04-11 20:50:39 -070057 StarlarkThread.createTransient(mutability, StarlarkSemantics.DEFAULT));
Googler5ede15c2023-06-26 09:19:45 -070058 mutability.freeze();
59 return Sequence.cast(parsedValue, String.class, "evalAsSequence() input");
60 }
61
62 @Test
63 public void repr() throws Exception {
64 assertThat(Starlark.repr(License.NO_LICENSE)).isEqualTo("[\"none\"]");
65 assertThat(License.parseLicense(evalAsSequence(Starlark.repr(License.NO_LICENSE))))
66 .isEqualTo(License.NO_LICENSE);
67
68 License withoutExceptions = License.parseLicense(ImmutableList.of("notice", "restricted"));
69 // License types sorted by LicenseType enum order.
70 assertThat(Starlark.repr(withoutExceptions)).isEqualTo("[\"restricted\", \"notice\"]");
71 assertThat(License.parseLicense(evalAsSequence(Starlark.repr(withoutExceptions))))
72 .isEqualTo(withoutExceptions);
73
74 License withExceptions =
75 License.parseLicense(
76 ImmutableList.of("notice", "restricted", "exception=//foo:bar", "exception=//baz:qux"));
77 // Exceptions sorted alphabetically.
78 assertThat(Starlark.repr(withExceptions))
79 .isEqualTo(
80 "[\"restricted\", \"notice\", \"exception=//baz:qux\", \"exception=//foo:bar\"]");
81 assertThat(License.parseLicense(evalAsSequence(Starlark.repr(withExceptions))))
82 .isEqualTo(withExceptions);
83 }
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +000084}