Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.packages; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 17 | |
Googler | 5ede15c | 2023-06-26 09:19:45 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.packages.License.LicenseType; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 20 | import java.util.Arrays; |
Googler | 5ede15c | 2023-06-26 09:19:45 -0700 | [diff] [blame] | 21 | import net.starlark.java.eval.Module; |
| 22 | import net.starlark.java.eval.Mutability; |
| 23 | import net.starlark.java.eval.Sequence; |
| 24 | import net.starlark.java.eval.Starlark; |
| 25 | import net.starlark.java.eval.StarlarkSemantics; |
| 26 | import net.starlark.java.eval.StarlarkThread; |
| 27 | import net.starlark.java.syntax.FileOptions; |
| 28 | import net.starlark.java.syntax.ParserInput; |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.junit.runners.JUnit4; |
| 32 | |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 33 | @RunWith(JUnit4.class) |
| 34 | public class LicenseTest { |
| 35 | |
| 36 | @Test |
| 37 | public void testLeastRestrictive() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 38 | 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 Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 46 | } |
Googler | 5ede15c | 2023-06-26 09:19:45 -0700 | [diff] [blame] | 47 | |
| 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(), |
Googler | 11f0620 | 2024-04-11 20:50:39 -0700 | [diff] [blame^] | 57 | StarlarkThread.createTransient(mutability, StarlarkSemantics.DEFAULT)); |
Googler | 5ede15c | 2023-06-26 09:19:45 -0700 | [diff] [blame] | 58 | 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 Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 84 | } |