blob: fc8fb6b1a7e826b9d8c9057f50009bc2fbe870be [file] [log] [blame]
Lukacs Berki14328eb2015-10-21 10:47:26 +00001// Copyright 2006 The Bazel Authors. All Rights Reserved.
Ulf Adams89f012d2015-02-26 13:39:28 +00002//
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.syntax;
15
Ulf Adams795895a2015-03-06 15:58:35 +000016import static com.google.common.truth.Truth.assertThat;
Ulf Adams89f012d2015-02-26 13:39:28 +000017import static com.google.devtools.build.lib.util.StringUtilities.joinLines;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000018import static org.junit.Assert.fail;
Ulf Adams89f012d2015-02-26 13:39:28 +000019
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000020import com.google.devtools.build.lib.testutil.Scratch;
tomlu67c84b12017-11-06 19:49:16 +010021import com.google.devtools.build.lib.vfs.FileSystemUtils;
Ulf Adams89f012d2015-02-26 13:39:28 +000022import com.google.devtools.build.lib.vfs.Path;
Lukacs Berki48338222015-06-12 11:37:46 +000023import com.google.devtools.build.lib.vfs.PathFragment;
lberkiaea56b32017-05-30 12:35:33 +020024import java.io.IOException;
25import java.nio.charset.StandardCharsets;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000026import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.junit.runners.JUnit4;
Ulf Adams89f012d2015-02-26 13:39:28 +000029
lberkiaea56b32017-05-30 12:35:33 +020030/** A test case for {@link ParserInputSource}. */
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000031@RunWith(JUnit4.class)
32public class ParserInputSourceTest {
Ulf Adams89f012d2015-02-26 13:39:28 +000033
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000034 private Scratch scratch = new Scratch();
Ulf Adams89f012d2015-02-26 13:39:28 +000035
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000036 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000037 public void testCreateFromFile() throws IOException {
38 String content = joinLines("Line 1", "Line 2", "Line 3", "");
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000039 Path file = scratch.file("/tmp/my/file.txt", content.getBytes(StandardCharsets.UTF_8));
tomlu67c84b12017-11-06 19:49:16 +010040 byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize());
41 ParserInputSource input = ParserInputSource.create(bytes, file.asFragment());
lberkiaea56b32017-05-30 12:35:33 +020042 assertThat(new String(input.getContent())).isEqualTo(content);
43 assertThat(input.getPath().toString()).isEqualTo("/tmp/my/file.txt");
Ulf Adams89f012d2015-02-26 13:39:28 +000044 }
45
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000046 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000047 public void testCreateFromString() {
48 String content = "Content provided as a string.";
49 String pathName = "/the/name/of/the/content.txt";
nharmatab4060b62017-04-04 17:11:39 +000050 ParserInputSource input = ParserInputSource.create(content, PathFragment.create(pathName));
lberkiaea56b32017-05-30 12:35:33 +020051 assertThat(new String(input.getContent())).isEqualTo(content);
52 assertThat(input.getPath().toString()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000053 }
54
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000055 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000056 public void testCreateFromCharArray() {
57 String content = "Content provided as a string.";
58 String pathName = "/the/name/of/the/content.txt";
Ulf Adams89f012d2015-02-26 13:39:28 +000059 char[] contentChars = content.toCharArray();
nharmatab4060b62017-04-04 17:11:39 +000060 ParserInputSource input = ParserInputSource.create(contentChars, PathFragment.create(pathName));
lberkiaea56b32017-05-30 12:35:33 +020061 assertThat(new String(input.getContent())).isEqualTo(content);
62 assertThat(input.getPath().toString()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000063 }
64
Ulf Adams89f012d2015-02-26 13:39:28 +000065
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000066 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000067 public void testIOExceptionIfInputFileDoesNotExistForSingleArgConstructor() {
68 try {
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000069 Path path = scratch.resolve("/does/not/exist");
tomlu67c84b12017-11-06 19:49:16 +010070 byte[] bytes = FileSystemUtils.readWithKnownFileSize(path, path.getFileSize());
71 ParserInputSource.create(bytes, path.asFragment());
Ulf Adams89f012d2015-02-26 13:39:28 +000072 fail();
73 } catch (IOException e) {
74 String expected = "/does/not/exist (No such file or directory)";
Ulf Adams795895a2015-03-06 15:58:35 +000075 assertThat(e).hasMessage(expected);
Ulf Adams89f012d2015-02-26 13:39:28 +000076 }
77 }
78
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000079 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000080 public void testWillNotTryToReadInputFileIfContentProvidedAsString() {
Lukacs Berki48338222015-06-12 11:37:46 +000081 ParserInputSource.create(
nharmatab4060b62017-04-04 17:11:39 +000082 "Content provided as string.", PathFragment.create("/will/not/try/to/read"));
Ulf Adams89f012d2015-02-26 13:39:28 +000083 }
84
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000085 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000086 public void testWillNotTryToReadInputFileIfContentProvidedAsChars() {
Ulf Adams89f012d2015-02-26 13:39:28 +000087 char[] content = "Content provided as char array.".toCharArray();
nharmatab4060b62017-04-04 17:11:39 +000088 ParserInputSource.create(content, PathFragment.create("/will/not/try/to/read"));
Ulf Adams89f012d2015-02-26 13:39:28 +000089 }
Ulf Adams89f012d2015-02-26 13:39:28 +000090}