nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 1 | // Copyright 2018 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.runtime; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.devtools.build.lib.events.Location; |
| 20 | import com.google.devtools.build.lib.events.Location.LineAndColumn; |
| 21 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 22 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 23 | import com.google.devtools.build.lib.vfs.Root; |
| 24 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 29 | /** Tests for {@link LocationPrinter} static methods. */ |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 30 | @RunWith(JUnit4.class) |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 31 | public class LocationPrinterTest { |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 32 | private final FileSystem fileSystem = new InMemoryFileSystem(); |
| 33 | |
| 34 | @Test |
| 35 | public void getRelativeLocationString_PathIsAlreadyRelative() { |
| 36 | assertThat( |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 37 | LocationPrinter.getRelativeLocationString( |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 38 | Location.fromPathAndStartColumn( |
| 39 | PathFragment.create("relative/path"), 0, 0, new LineAndColumn(4, 2)), |
| 40 | PathFragment.create("/this/is/the/workspace"), |
| 41 | ImmutableList.of( |
| 42 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root"))))) |
| 43 | .isEqualTo("relative/path:4:2"); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void getRelativeLocationString_PathIsAbsoluteAndWorkspaceIsNull() { |
| 48 | assertThat( |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 49 | LocationPrinter.getRelativeLocationString( |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 50 | Location.fromPathAndStartColumn( |
| 51 | PathFragment.create("/absolute/path"), 0, 0, new LineAndColumn(4, 2)), |
| 52 | null, |
| 53 | ImmutableList.of( |
| 54 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root"))))) |
| 55 | .isEqualTo("/absolute/path:4:2"); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void getRelativeLocationString_PathIsAbsoluteButNotUnderWorkspaceOrPackagePathRoots() { |
| 60 | assertThat( |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 61 | LocationPrinter.getRelativeLocationString( |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 62 | Location.fromPathAndStartColumn( |
| 63 | PathFragment.create("/absolute/path"), 0, 0, new LineAndColumn(4, 2)), |
| 64 | PathFragment.create("/this/is/the/workspace"), |
| 65 | ImmutableList.of( |
| 66 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root"))))) |
| 67 | .isEqualTo("/absolute/path:4:2"); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void getRelativeLocationString_PathIsAbsoluteAndUnderWorkspace() { |
| 72 | assertThat( |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 73 | LocationPrinter.getRelativeLocationString( |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 74 | Location.fromPathAndStartColumn( |
| 75 | PathFragment.create("/this/is/the/workspace/blah.txt"), |
| 76 | 0, |
| 77 | 0, |
| 78 | new LineAndColumn(4, 2)), |
| 79 | PathFragment.create("/this/is/the/workspace"), |
| 80 | ImmutableList.of( |
| 81 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root"))))) |
| 82 | .isEqualTo("blah.txt:4:2"); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void getRelativeLocationString_PathIsAbsoluteAndUnderPackagePathRoot() { |
| 87 | assertThat( |
nharmata | 4da4591 | 2018-11-29 08:42:15 -0800 | [diff] [blame] | 88 | LocationPrinter.getRelativeLocationString( |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 89 | Location.fromPathAndStartColumn( |
| 90 | PathFragment.create("/this/is/a/package/path/root3/blah.txt"), |
| 91 | 0, |
| 92 | 0, |
| 93 | new LineAndColumn(4, 2)), |
| 94 | PathFragment.create("/this/is/the/workspace"), |
| 95 | ImmutableList.of( |
| 96 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root1")), |
| 97 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root2")), |
| 98 | Root.fromPath(fileSystem.getPath("/this/is/a/package/path/root3"))))) |
| 99 | .isEqualTo("blah.txt:4:2"); |
| 100 | } |
| 101 | } |