blob: a4386adf0c94f7446b53a6b295e960c1be21c3a6 [file] [log] [blame]
nharmataccc42952018-10-25 15:39:26 -07001// 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.
14package com.google.devtools.build.lib.runtime;
15
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.common.collect.ImmutableList;
19import com.google.devtools.build.lib.events.Location;
20import com.google.devtools.build.lib.events.Location.LineAndColumn;
21import com.google.devtools.build.lib.vfs.FileSystem;
22import com.google.devtools.build.lib.vfs.PathFragment;
23import com.google.devtools.build.lib.vfs.Root;
24import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
nharmata4da45912018-11-29 08:42:15 -080029/** Tests for {@link LocationPrinter} static methods. */
nharmataccc42952018-10-25 15:39:26 -070030@RunWith(JUnit4.class)
nharmata4da45912018-11-29 08:42:15 -080031public class LocationPrinterTest {
nharmataccc42952018-10-25 15:39:26 -070032 private final FileSystem fileSystem = new InMemoryFileSystem();
33
34 @Test
35 public void getRelativeLocationString_PathIsAlreadyRelative() {
36 assertThat(
nharmata4da45912018-11-29 08:42:15 -080037 LocationPrinter.getRelativeLocationString(
nharmataccc42952018-10-25 15:39:26 -070038 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(
nharmata4da45912018-11-29 08:42:15 -080049 LocationPrinter.getRelativeLocationString(
nharmataccc42952018-10-25 15:39:26 -070050 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(
nharmata4da45912018-11-29 08:42:15 -080061 LocationPrinter.getRelativeLocationString(
nharmataccc42952018-10-25 15:39:26 -070062 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(
nharmata4da45912018-11-29 08:42:15 -080073 LocationPrinter.getRelativeLocationString(
nharmataccc42952018-10-25 15:39:26 -070074 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(
nharmata4da45912018-11-29 08:42:15 -080088 LocationPrinter.getRelativeLocationString(
nharmataccc42952018-10-25 15:39:26 -070089 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}