aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java
blob: 3424be94944a79c2ca0da24bd875438b7efa51f2 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2017 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.skylark.skylint;

import com.google.common.truth.Truth;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.syntax.BuildFileAST;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ControlFlowCheckerTest {
  private static List<Issue> findIssues(EventHandler eventHandler, String... lines) {
    String content = String.join("\n", lines);
    BuildFileAST ast = BuildFileAST.parseString(eventHandler, content);
    return ControlFlowChecker.check(ast);
  }

  private static List<Issue> findIssues(String... lines) {
    return findIssues(
        event -> {
          throw new IllegalArgumentException(event.getMessage());
        },
        lines);
  }

  @Test
  public void testAnalyzerToleratesTopLevelFail() throws Exception {
    Truth.assertThat(
            findIssues("fail(\"fail is considered a return, but not at the top level\")"))
        .isEmpty();
  }

  @Test
  public void testIfElseReturnMissing() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def some_function(x):",
                    "  if x:",
                    "    print('foo')",
                    "  else:",
                    "    return x")
                .toString())
        .contains(
            "1:1-5:12: some but not all execution paths of 'some_function' return a value."
                + " If it is intentional, make it explicit using 'return None'."
                + " If you know these cannot happen,"
                + " add the statement `fail('unreachable')` to them."
                + " For more details, have a look at the documentation. [missing-return-value]");
  }

  @Test
  public void testNestedFunction() {
    Truth.assertThat(
            findIssues(event -> {}, "def foo():", "  def bar():", "    pass", "  return")
                .toString())
        .contains(
            "2:3-3:8: bar is a nested function which is not allowed."
                + " Consider inlining it or moving it to top-level."
                + " For more details, have a look at the Skylark documentation."
                + " [nested-function]");
  }

  @Test
  public void testIfElseReturnValueMissing() throws Exception {
    String messages =
        findIssues(
                "def some_function(x):",
                "  if x:",
                "    return x",
                "  else:",
                "    return # missing value")
            .toString();
    Truth.assertThat(messages)
        .contains(
            "1:1-5:10: some but not all execution paths of 'some_function' return a value."
                + " If it is intentional, make it explicit using 'return None'."
                + " If you know these cannot happen,"
                + " add the statement `fail('unreachable')` to them."
                + " For more details, have a look at the documentation. [missing-return-value]");
    Truth.assertThat(messages)
        .contains(
            "5:5-5:10: return value missing (you can `return None` if this is desired)"
                + " [missing-return-value]");
  }

  @Test
  public void testIfElifElseReturnMissing() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def f(x):",
                    "  if x:",
                    "    return x",
                    "  elif not x:",
                    "    pass",
                    "  else:",
                    "    return not x")
                .toString())
        .contains(
            "1:1-7:16: some but not all execution paths of 'f' return a value."
                + " If it is intentional, make it explicit using 'return None'."
                + " If you know these cannot happen,"
                + " add the statement `fail('unreachable')` to them."
                + " For more details, have a look at the documentation. [missing-return-value]");
  }

  @Test
  public void testNestedIfElseReturnMissing() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def f(x, y):",
                    "  if x:",
                    "    if y:",
                    "      return y",
                    "    else:",
                    "      print('foo')",
                    "  else:",
                    "    return x")
                .toString())
        .contains(
            "1:1-8:12: some but not all execution paths of 'f' return a value."
                + " If it is intentional, make it explicit using 'return None'."
                + " If you know these cannot happen,"
                + " add the statement `fail('unreachable')` to them."
                + " For more details, have a look at the documentation. [missing-return-value]");
  }

  @Test
  public void testElseBranchMissing() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def some_function(x):",
                    "  if x:",
                    "    return x",
                    "  elif not x:",
                    "    return not x")
                .toString())
        .containsMatch(
            "1:1-5:16: some but not all execution paths of 'some_function' return a value."
                + " .+ \\[missing-return-value\\]");
  }

  @Test
  public void testIfAndFallOffEnd() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def some_function(x):",
                    "  if x:",
                    "    return x",
                    "  print('foo')",
                    "  # return missing here")
                .toString())
        .containsMatch(
            "1:1-4:14: some but not all execution paths of 'some_function' return a value."
                + " .+ \\[missing-return-value\\]");
  }

  @Test
  public void testForAndFallOffEnd() throws Exception {
    Truth.assertThat(
            findIssues(
                    "def some_function():",
                    "  for x in []:",
                    "    return x",
                    "  print('foo')",
                    "  # return missing here")
                .toString())
        .containsMatch(
            "1:1-4:14: some but not all execution paths of 'some_function' return a value."
                + " .+ \\[missing-return-value\\]");
  }

  @Test
  public void testAlwaysReturnButSometimesWithoutValue() throws Exception {
    String messages =
        findIssues(
                "def some_function(x):",
                "  if x:",
                "    return # returns without value here",
                "  return x")
            .toString();
    Truth.assertThat(messages)
        .containsMatch(
            "1:1-4:10: some but not all execution paths of 'some_function' return a value."
                + " .+ \\[missing-return-value\\]");
    Truth.assertThat(messages)
        .contains(
            "3:5-3:10: return value missing (you can `return None` if this is desired)"
                + " [missing-return-value]");
  }

  @Test
  public void testUnreachableAfterIf() throws Exception {
    String messages =
        findIssues(
                "def some_function(x):",
                "  if x:",
                "    return",
                "  else:",
                "    fail('fail')",
                "  print('This line is unreachable')")
            .toString();
    Truth.assertThat(messages).contains("6:3-6:35: unreachable statement [unreachable-statement]");
  }

  @Test
  public void testNoUnreachableDuplicates() throws Exception {
    List<Issue> messages =
        findIssues(
            "def some_function():",
            "  return",
            "  print('unreachable1')",
            "  print('unreachable2')");
    Truth.assertThat(messages).hasSize(1);
  }

  @Test
  public void testUnreachableAfterBreakContinue() throws Exception {
    String messages =
        findIssues(
                "def some_function(x):",
                "  for y in x:",
                "    if y:",
                "      break",
                "    else:",
                "      continue",
                "    print('unreachable')")
            .toString();
    Truth.assertThat(messages).contains("7:5-7:24: unreachable statement [unreachable-statement]");
  }

  @Test
  public void testReachableStatements() throws Exception {
    Truth.assertThat(
            findIssues(
                "def some_function(x):",
                "  if x:",
                "    return",
                "  for y in []:",
                "    if y:",
                "      continue",
                "    else:",
                "      fail('fail')",
                "  return"))
        .isEmpty();
  }

  @Test
  public void testIfBeforeReturn() throws Exception {
    Truth.assertThat(
            findIssues(
                "def f(x, y):",
                "  if x:",
                "    return x",
                "  elif not y:",
                "    print('foo')",
                "  print('bar')",
                "  return y"))
        .isEmpty();
  }

  @Test
  public void testReturnInAllBranches() throws Exception {
    Truth.assertThat(
            findIssues(
                "def f(x, y):",
                "  if x:",
                "    return x",
                "  elif not y:",
                "    return None",
                "  else:",
                "    return y"))
        .isEmpty();
  }

  @Test
  public void testReturnInNestedIf() throws Exception {
    Truth.assertThat(
            findIssues(
                "def f(x,y):",
                "  if x:",
                "    if y:",
                "      return y",
                "    else:",
                "      return not y",
                "  else:",
                "    return not x"))
        .isEmpty();
  }

  @Test
  public void testIfStatementSequence() throws Exception {
    Truth.assertThat(
            findIssues(
                "def f(x,y):",
                "  if x:",
                "    print('foo')",
                "  else:",
                "    return x",
                "  print('bar')",
                "  if y:",
                "    return x",
                "  else:",
                "    return y"))
        .isEmpty();
    List<Issue> issues =
        findIssues(
            "def f(x,y):",
            "  if x:",
            "    return x",
            "  else:",
            "    return x",
            "  # from now on everything's unreachable",
            "  print('bar')",
            "  if y:",
            "    return x",
            "  # no else branch but doesn't matter since it's unreachable");
    Truth.assertThat(issues).hasSize(1);
    Truth.assertThat(issues.toString())
        .contains("7:3-7:14: unreachable statement [unreachable-statement]");
  }

  @Test
  public void testCallToFail() throws Exception {
    Truth.assertThat(
            findIssues(
                "def some_function_name(x):",
                "  if x:",
                "    fail('bar')",
                "  else:",
                "    return x"))
        .isEmpty();
  }
}