xref: /aosp_15_r20/prebuilts/checkstyle/tests.py (revision 387726c4b5c67c6b48512fa4a28a3b8997d21b0d)
1#!/usr/bin/env python3
2
3#
4# Copyright 2015, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""Tests the Checkstyle script used to run style checks on Java files."""
20
21try:
22  from StringIO import StringIO
23except ImportError:
24  # python3 use io instead of StringIO
25  from io import StringIO
26import unittest
27import checkstyle
28
29
30TEST_RULE = u'com.puppycrawl.tools.checkstyle.checks.BANANAS'
31TEST_SHA = u'0000deadbeef000000deadbeef00deadbeef0000'
32TEST_ROOT = u'/usr/local/android/master/framework/support'
33TEST_FILE1 = TEST_ROOT + u'/Blarg.java'
34TEST_FILE2 = TEST_ROOT + u'/Blarg2.java'
35TEST_FILE_NON_JAVA = TEST_ROOT + u'/blarg.cc'
36FILE_ADDED = u'A '
37FILE_MODIFIED = u'M '
38FILE_UNTRACKED = u'??'
39
40
41def mock_repository_root():
42  return TEST_ROOT
43
44
45def mock_last_commit():
46  return TEST_SHA
47
48
49def mock_modified_files_good(root, tracked_only=False, commit=None):
50  if commit:
51    return {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED}
52  return {}
53
54
55def mock_modified_files_uncommitted(root, tracked_only=False, commit=None):
56  if tracked_only and not commit:
57    return {TEST_FILE1: FILE_MODIFIED}
58  if commit:
59    return {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED}
60  return {}
61
62
63def mock_modified_files_untracked(root, tracked_only=False, commit=None):
64  if not tracked_only:
65    return {TEST_FILE1: FILE_UNTRACKED}
66  if commit:
67    return {TEST_FILE2: FILE_ADDED}
68  return {}
69
70
71def mock_modified_files_non_java(root, tracked_only=False, commit=None):
72  if commit:
73    return {TEST_FILE1: FILE_MODIFIED, TEST_FILE_NON_JAVA: FILE_ADDED}
74  return {}
75
76
77class TestCheckstyle(unittest.TestCase):
78
79  def setUp(self):
80    checkstyle.git.repository_root = mock_repository_root
81    checkstyle.git.last_commit = mock_last_commit
82
83  def test_ShouldSkip(self):
84    # Skip checks for explicit git commit.
85    self.assertFalse(checkstyle._ShouldSkip(True, None, 1, TEST_RULE))
86    self.assertTrue(checkstyle._ShouldSkip(True, [], 1, TEST_RULE))
87    self.assertFalse(checkstyle._ShouldSkip(True, [1], 1, TEST_RULE))
88    self.assertFalse(checkstyle._ShouldSkip(True, [1, 2, 3], 1, TEST_RULE))
89    self.assertTrue(checkstyle._ShouldSkip(True, [1, 2, 3], 4, TEST_RULE))
90    for rule in checkstyle.FORCED_RULES:
91      self.assertFalse(checkstyle._ShouldSkip(True, [1, 2, 3], 1, rule))
92      self.assertFalse(checkstyle._ShouldSkip(True, [1, 2, 3], 4, rule))
93
94    # Skip checks for explicitly checked files.
95    self.assertFalse(checkstyle._ShouldSkip(False, None, 1, TEST_RULE))
96    self.assertFalse(checkstyle._ShouldSkip(False, [], 1, TEST_RULE))
97    self.assertFalse(checkstyle._ShouldSkip(False, [1], 1, TEST_RULE))
98    self.assertFalse(checkstyle._ShouldSkip(False, [1, 2, 3], 1, TEST_RULE))
99    self.assertFalse(checkstyle._ShouldSkip(False, [1, 2, 3], 4, TEST_RULE))
100    for rule in checkstyle.FORCED_RULES:
101      self.assertFalse(checkstyle._ShouldSkip(False, [1, 2, 3], 1, rule))
102      self.assertFalse(checkstyle._ShouldSkip(False, [1, 2, 3], 4, rule))
103
104    # Skip checks for test classes.
105    self.assertFalse(checkstyle._ShouldSkip(True, None, 1, TEST_RULE, True))
106    self.assertTrue(checkstyle._ShouldSkip(True, [], 1, TEST_RULE, True))
107    self.assertFalse(checkstyle._ShouldSkip(True, [1], 1, TEST_RULE, True))
108    self.assertFalse(checkstyle._ShouldSkip(True, [1, 2, 3], 1, TEST_RULE, True))
109    self.assertTrue(checkstyle._ShouldSkip(True, [1, 2, 3], 4, TEST_RULE, True))
110    for rule in checkstyle.SKIPPED_RULES_FOR_TEST_FILES:
111      self.assertTrue(checkstyle._ShouldSkip(True, [1, 2, 3], 1, rule, True))
112      self.assertTrue(checkstyle._ShouldSkip(True, [1, 2, 3], 4, rule, True))
113
114  def test_GetModifiedFiles(self):
115    checkstyle.git.modified_files = mock_modified_files_good
116    out = StringIO()
117    files = checkstyle._GetModifiedFiles(mock_last_commit(), out=out)
118    output = out.getvalue()
119    self.assertEqual(output, '')
120    self.assertEqual(files, {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED})
121
122  def test_GetModifiedFilesUncommitted(self):
123    checkstyle.git.modified_files = mock_modified_files_uncommitted
124    with self.assertRaises(SystemExit):
125      out = StringIO()
126      checkstyle._GetModifiedFiles(mock_last_commit(), out=out)
127    self.assertEqual(out.getvalue(), checkstyle.ERROR_UNCOMMITTED)
128
129  def test_GetModifiedFilesUncommittedExplicitCommit(self):
130    checkstyle.git.modified_files = mock_modified_files_uncommitted
131    out = StringIO()
132    files = checkstyle._GetModifiedFiles(mock_last_commit(), True, out=out)
133    output = out.getvalue()
134    self.assertEqual(output, '')
135    self.assertEqual(files, {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED})
136
137  def test_GetModifiedFilesNonJava(self):
138    checkstyle.git.modified_files = mock_modified_files_non_java
139    out = StringIO()
140    files = checkstyle._GetModifiedFiles(mock_last_commit(), out=out)
141    output = out.getvalue()
142    self.assertEqual(output, '')
143    self.assertEqual(files, {TEST_FILE1: FILE_MODIFIED})
144
145  def test_WarnIfUntrackedFiles(self):
146    checkstyle.git.modified_files = mock_modified_files_untracked
147    out = StringIO()
148    checkstyle._WarnIfUntrackedFiles(out=out)
149    output = out.getvalue()
150    self.assertEqual(output, checkstyle.ERROR_UNTRACKED + TEST_FILE1 + '\n\n')
151
152  def test_WarnIfUntrackedFilesNoUntracked(self):
153    checkstyle.git.modified_files = mock_modified_files_good
154    out = StringIO()
155    checkstyle._WarnIfUntrackedFiles(out=out)
156    output = out.getvalue()
157    self.assertEqual(output, '')
158
159  def test_FilterFiles(self):
160    files = {TEST_FILE1: FILE_MODIFIED, TEST_FILE2: FILE_ADDED}
161    output = checkstyle._FilterFiles(files, None)
162    self.assertEqual(files, output)
163    output = checkstyle._FilterFiles(files, ['Blarg2'])
164    self.assertEqual({TEST_FILE2: FILE_ADDED}, output)
165    output = checkstyle._FilterFiles(files, ['Blarg'])
166    self.assertEqual(files, output)
167    output = checkstyle._FilterFiles(files, ['FunkyTown'])
168    self.assertEqual({}, output)
169
170if __name__ == '__main__':
171  unittest.main()
172