Googler | 40271d7 | 2023-12-29 11:14:05 -0800 | [diff] [blame] | 1 | // Copyright 2023 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.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Googler | fe3b2fb | 2024-06-12 14:45:34 -0700 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
Googler | 40271d7 | 2023-12-29 11:14:05 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 21 | import com.google.devtools.build.lib.packages.Globber.Operation; |
| 22 | import com.google.devtools.build.lib.skyframe.GlobsValue.GlobRequest; |
| 23 | import com.google.devtools.build.lib.skyframe.serialization.testutils.FsUtils; |
| 24 | import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester; |
| 25 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 26 | import com.google.devtools.build.lib.vfs.Root; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
| 31 | @RunWith(JUnit4.class) |
| 32 | public class GlobsValueTest { |
| 33 | |
| 34 | @Test |
| 35 | public void testSerialization() throws Exception { |
| 36 | PackageIdentifier packageId = PackageIdentifier.create("foo", PathFragment.create("//bar")); |
| 37 | Root packageRoot = Root.fromPath(FsUtils.TEST_FILESYSTEM.getPath("/packageRoot")); |
| 38 | |
| 39 | GlobRequest globRequest1 = GlobRequest.create("*", Operation.FILES_AND_DIRS); |
| 40 | GlobRequest globRequest2 = GlobRequest.create("foo/**", Operation.SUBPACKAGES); |
| 41 | GlobRequest globRequest3 = GlobRequest.create("**/*", Operation.FILES); |
| 42 | |
| 43 | SerializationTester serializationTester = |
| 44 | new SerializationTester( |
| 45 | GlobsValue.key(packageId, packageRoot, ImmutableSet.of(globRequest1, globRequest2)), |
| 46 | GlobsValue.key(packageId, packageRoot, ImmutableSet.of(globRequest2, globRequest3))) |
| 47 | .setVerificationFunction(GlobsValueTest::verifyEquivalent); |
| 48 | FsUtils.addDependencies(serializationTester); |
| 49 | serializationTester.runTests(); |
| 50 | } |
| 51 | |
| 52 | private static void verifyEquivalent(GlobsValue.Key orig, GlobsValue.Key deserialized) { |
| 53 | assertThat(deserialized).isSameInstanceAs(orig); |
| 54 | } |
Googler | fe3b2fb | 2024-06-12 14:45:34 -0700 | [diff] [blame] | 55 | |
| 56 | @Test |
| 57 | public void testPrintingDeterministic() throws Exception { |
| 58 | PackageIdentifier packageId = PackageIdentifier.create("foo", PathFragment.create("//bar")); |
| 59 | Root packageRoot = Root.fromPath(FsUtils.TEST_FILESYSTEM.getPath("/packageRoot")); |
| 60 | |
| 61 | GlobRequest globRequest1 = GlobRequest.create("*", Operation.FILES_AND_DIRS); |
| 62 | GlobRequest globRequest2 = GlobRequest.create("foo/**", Operation.SUBPACKAGES); |
| 63 | GlobRequest globRequest3 = GlobRequest.create("**/*", Operation.FILES); |
| 64 | |
| 65 | GlobsValue.Key key1 = |
| 66 | GlobsValue.key( |
| 67 | packageId, packageRoot, ImmutableSet.of(globRequest1, globRequest2, globRequest3)); |
| 68 | GlobsValue.Key key2 = |
| 69 | GlobsValue.key( |
| 70 | packageId, packageRoot, ImmutableSet.of(globRequest1, globRequest3, globRequest2)); |
| 71 | GlobsValue.Key key3 = |
| 72 | GlobsValue.key( |
| 73 | packageId, packageRoot, ImmutableSet.of(globRequest2, globRequest1, globRequest3)); |
| 74 | GlobsValue.Key key4 = |
| 75 | GlobsValue.key( |
| 76 | packageId, packageRoot, ImmutableSet.of(globRequest2, globRequest3, globRequest1)); |
| 77 | GlobsValue.Key key5 = |
| 78 | GlobsValue.key( |
| 79 | packageId, packageRoot, ImmutableSet.of(globRequest3, globRequest1, globRequest2)); |
| 80 | GlobsValue.Key key6 = |
| 81 | GlobsValue.key( |
| 82 | packageId, packageRoot, ImmutableSet.of(globRequest3, globRequest2, globRequest1)); |
| 83 | new EqualsTester() |
| 84 | .addEqualityGroup( |
| 85 | key1.toString(), |
| 86 | key2.toString(), |
| 87 | key3.toString(), |
| 88 | key4.toString(), |
| 89 | key5.toString(), |
| 90 | key6.toString(), |
| 91 | "<GlobsKey packageRoot = /packageRoot, packageIdentifier = @@foo///bar," |
| 92 | + " globRequests = [GlobRequest: * FILES_AND_DIRS,GlobRequest: **/* FILES," |
| 93 | + "GlobRequest: foo/** SUBPACKAGES]>") |
| 94 | .testEquals(); |
| 95 | } |
Googler | 40271d7 | 2023-12-29 11:14:05 -0800 | [diff] [blame] | 96 | } |