diff options
author | 2017-08-22 18:52:48 +0200 | |
---|---|---|
committer | 2017-08-23 13:28:48 +0200 | |
commit | 141cb88676e618772ade52ada2e0c676c7cf3ff6 (patch) | |
tree | f6be4b398ce49e42bacc162677c85ea4ab2161c2 /src/tools/skylark/javatests/com | |
parent | 16ac80005bf9edf882b49ad0f1f3ca7327bc7da1 (diff) |
Start work on the Skylark linter (skylint).
This commit introduces checking of naming conventions,
i.e. upper and lower snake case.
RELNOTES: None
PiperOrigin-RevId: 166073284
Diffstat (limited to 'src/tools/skylark/javatests/com')
3 files changed, 112 insertions, 0 deletions
diff --git a/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/BUILD b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/BUILD new file mode 100644 index 0000000000..61b7a8ad7c --- /dev/null +++ b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/BUILD @@ -0,0 +1,23 @@ +# Tests for the Skylark linter + +java_test( + name = "SkylintTests", + srcs = glob(["*.java"]), + test_class = "com.google.devtools.skylark.skylint.SkylintTests", + deps = [ + "//src/main/java/com/google/devtools/build/lib:packages", + "//src/test/java/com/google/devtools/build/lib:testutil", + "//src/tools/skylark/java/com/google/devtools/skylark/skylint:skylint-lib", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) + +filegroup( + name = "srcs", + srcs = glob( + ["**"], + ), + visibility = ["//src:__pkg__"], +) diff --git a/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/NamingConventionsCheckerTest.java b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/NamingConventionsCheckerTest.java new file mode 100644 index 0000000000..b8fefb305c --- /dev/null +++ b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/NamingConventionsCheckerTest.java @@ -0,0 +1,67 @@ +// 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.syntax.BuildFileAST; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests the lint done by {@link com.google.devtools.skylark.skylint.NamingConventionsChecker}. */ +@RunWith(JUnit4.class) +public class NamingConventionsCheckerTest { + private static List<Issue> findIssues(String... lines) { + String content = String.join("\n", lines); + BuildFileAST ast = + BuildFileAST.parseSkylarkString( + event -> { + throw new IllegalArgumentException(event.getMessage()); + }, + content); + return NamingConventionsChecker.check(ast); + } + + @Test + public void testMessages() throws Exception { + String errorMessage = + findIssues( + "badGlobalVariableName = 0", + "def badFunctionName(BadParameterName):", + " BAD_LOCAL_VARIABLE_NAME = 1") + .toString(); + Truth.assertThat(errorMessage) + .contains("'badGlobalVariableName' should be lower_snake_case or UPPER_SNAKE_CASE"); + Truth.assertThat(errorMessage).contains("'badFunctionName' should be lower_snake_case"); + Truth.assertThat(errorMessage).contains("'BadParameterName' should be lower_snake_case"); + Truth.assertThat(errorMessage).contains("'BAD_LOCAL_VARIABLE_NAME' should be lower_snake_case"); + } + + @Test + public void testNoIssues() throws Exception { + Truth.assertThat( + findIssues( + "GOOD_GLOBAL_VARIABLE_NAME = 0", + "def good_function_name(good_parameter_name):", + " good_local_variable_name = 1")) + .isEmpty(); + } + + @Test + public void testNoDuplicates() throws Exception { + Truth.assertThat(findIssues("def foo():", " BAD = 1", " BAD += 1")).hasSize(1); + } +} diff --git a/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/SkylintTests.java b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/SkylintTests.java new file mode 100644 index 0000000000..8a1e705d6d --- /dev/null +++ b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/SkylintTests.java @@ -0,0 +1,22 @@ +// 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.devtools.build.lib.testutil.ClasspathSuite; +import org.junit.runner.RunWith; + +/** All tests for skylint. */ +@RunWith(ClasspathSuite.class) +public class SkylintTests {} |