aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/syntax/BuildFileASTTest.java
blob: f1aa92f02af786eb07a71f994e6cee0cf76472f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2006 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.util.EvaluationTestCase;
import com.google.devtools.build.lib.testutil.MoreAsserts;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Unit tests for BuildFileAST.
 */
@RunWith(JUnit4.class)
public class BuildFileASTTest extends EvaluationTestCase {

  private Scratch scratch = new Scratch();

  @Override
  public Environment newEnvironment() throws Exception {
    return newBuildEnvironment();
  }

  /**
   * Parses the contents of the specified string (using DUMMY_PATH as the fake
   * filename) and returns the AST. Resets the error handler beforehand.
   */
  private BuildFileAST parseBuildFile(String... lines) throws IOException {
    Path file = scratch.file("/a/build/file/BUILD", lines);
    byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize());
    ParserInputSource inputSource = ParserInputSource.create(bytes, file.asFragment());
    return BuildFileAST.parseBuildFile(inputSource, getEventHandler());
  }

  @Test
  public void testParseBuildFileOK() throws Exception {
    BuildFileAST buildfile = parseBuildFile(
        "# a file in the build language",
        "",
        "x = [1,2,'foo',4] + [1,2, \"%s%d\" % ('foo', 1)]");

    assertThat(buildfile.exec(env, getEventHandler())).isTrue();

    // Test final environment is correctly modified:
    //
    // input1.BUILD contains:
    // x = [1,2,'foo',4] + [1,2, "%s%d" % ('foo', 1)]
    assertThat(env.lookup("x"))
        .isEqualTo(SkylarkList.createImmutable(Tuple.of(1, 2, "foo", 4, 1, 2, "foo1")));
  }

  @Test
  public void testEvalException() throws Exception {
    setFailFast(false);
    BuildFileAST buildfile = parseBuildFile(
        "x = 1",
        "y = [2,3]",
        "",
        "z = x + y");

    assertThat(buildfile.exec(env, getEventHandler())).isFalse();
    Event e = assertContainsError("unsupported operand type(s) for +: 'int' and 'list'");
    assertThat(e.getLocation().getStartLineAndColumn().getLine()).isEqualTo(4);
  }

  @Test
  public void testParsesFineWithNewlines() throws Exception {
    BuildFileAST buildFileAST = parseBuildFile("foo()", "bar()", "something = baz()", "bar()");
    assertThat(buildFileAST.getStatements()).hasSize(4);
  }

  @Test
  public void testFailsIfNewlinesAreMissing() throws Exception {
    setFailFast(false);

    BuildFileAST buildFileAST =
      parseBuildFile("foo() bar() something = baz() bar()");

    Event event = assertContainsError("syntax error at \'bar\': expected newline");
    assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD");
    assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(1);
    assertThat(buildFileAST.containsErrors()).isTrue();
  }

  @Test
  public void testImplicitStringConcatenationFails() throws Exception {
    setFailFast(false);
    BuildFileAST buildFileAST = parseBuildFile("a = 'foo' 'bar'");
    Event event = assertContainsError(
        "Implicit string concatenation is forbidden, use the + operator");
    assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD");
    assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(1);
    assertThat(event.getLocation().getStartLineAndColumn().getColumn()).isEqualTo(10);
    assertThat(buildFileAST.containsErrors()).isTrue();
  }

  @Test
  public void testImplicitStringConcatenationAcrossLinesIsIllegal() throws Exception {
    setFailFast(false);
    BuildFileAST buildFileAST = parseBuildFile("a = 'foo'\n  'bar'");

    Event event = assertContainsError("indentation error");
    assertThat(event.getLocation().getPath().toString()).isEqualTo("/a/build/file/BUILD");
    assertThat(event.getLocation().getStartLineAndColumn().getLine()).isEqualTo(2);
    assertThat(event.getLocation().getStartLineAndColumn().getColumn()).isEqualTo(2);
    assertThat(buildFileAST.containsErrors()).isTrue();
  }

  @Test
  public void testWithSyntaxErrorsDoesNotPrintDollarError() throws Exception {
    setFailFast(false);
    BuildFileAST buildFile = parseBuildFile(
        "abi = '$(ABI)-glibc-' + glibc_version + '-' + $(TARGET_CPU) + '-linux'",
        "libs = [abi + opt_level + '/lib/libcc.a']",
        "shlibs = [abi + opt_level + '/lib/libcc.so']",
        "+* shlibs", // syntax error at '+'
        "cc_library(name = 'cc',",
        "           srcs = libs,",
        "           includes = [ abi + opt_level + '/include' ])");
    assertThat(buildFile.containsErrors()).isTrue();
    assertContainsError("syntax error at '+': expected expression");
    assertThat(buildFile.exec(env, getEventHandler())).isFalse();
    MoreAsserts.assertDoesNotContainEvent(getEventCollector(), "$error$");
    // This message should not be printed anymore.
    MoreAsserts.assertDoesNotContainEvent(getEventCollector(), "contains syntax error(s)");
  }
}