k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/hack/boilerplate/verify_boilerplate_test.py (about) 1 #!/usr/bin/env python3 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import io 18 import os 19 import sys 20 import unittest 21 22 import verify_boilerplate 23 24 class TestBoilerplate(unittest.TestCase): 25 26 def setUp(self): 27 self.old_cwd = os.getcwd() 28 if os.getenv('TEST_WORKSPACE'): # Running in bazel 29 os.chdir('verify/boilerplate') 30 os.chdir(os.path.join(os.path.dirname(__file__), 'test/')) 31 self.old_out = sys.stdout 32 sys.stdout = io.StringIO() 33 34 def tearDown(self): 35 sys.stdout = self.old_out 36 os.chdir(self.old_cwd) 37 38 def test_boilerplate(self): 39 40 class Args(): 41 def __init__(self): 42 self.filenames = [] 43 self.rootdir = '.' 44 self.boilerplate_dir = '../' 45 self.skip = [] 46 self.verbose = True 47 48 verify_boilerplate.ARGS = Args() 49 with self.assertRaises(SystemExit): 50 verify_boilerplate.main() 51 52 output = sys.stdout.getvalue() 53 expected = '\n'.join(verify_boilerplate.nonconforming_lines([ 54 './fail.go', 55 './fail.py', 56 ])) + '\n' # add trailing newline 57 58 self.assertEqual(output, expected) 59 60 61 if __name__ == '__main__': 62 unittest.main()