1#!/usr/bin/python3 2# 3# Copyright (C) 2021 The Android Open Source Project 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"""Tests for change_report.py.""" 17 18import os 19import pathlib 20import unittest 21 22import change_report 23 24 25class ChangeReportTest(unittest.TestCase): 26 """Tests for ChangeReport.""" 27 28 def setUp(self): 29 super().setUp() 30 old_dir = str(ChangeReportTest.get_resrouce_path('old_codebase')) 31 new_dir = str(ChangeReportTest.get_resrouce_path('new_codebase')) 32 self.change_report = change_report.ChangeReport( 33 old_dir, new_dir, state_filter='0,1,2,3,4') 34 35 def test_get_diff_stat_lines(self): 36 """Tests if the diff stat of new & old_codebase matches change_report-new_vs_old_codebase.csv.""" 37 diff_stat_lines = [] 38 diff_stat_lines.append(change_report.ChangeReport.get_diff_stat_header()) 39 diff_stat_lines.extend(self.change_report.get_diff_stat_lines()) 40 41 expected_diff_stat_lines = ChangeReportTest.get_expected_lines( 42 'change_report-new_vs_old_codebase.csv') 43 44 offending_line_indexes = ChangeReportTest.diff_lines( 45 expected_diff_stat_lines, diff_stat_lines) 46 self.assertEqual(len(offending_line_indexes), 0) 47 48 def test_get_diff_lines(self): 49 """Tests if the diff stat of new & old_codebase matches change_report_diff-new_vs_old_codebase.txt.""" 50 diff_lines = self.change_report.get_diff_lines() 51 52 expected_diff_lines = ChangeReportTest.get_expected_lines( 53 'change_report_diff-new_vs_old_codebase.txt') 54 55 offending_line_indexes = ChangeReportTest.diff_lines( 56 expected_diff_lines, diff_lines) 57 self.assertEqual(len(offending_line_indexes), 0) 58 59 @staticmethod 60 def get_resrouce_path(target): 61 # .../dev/change_report_test.py 62 this_path = pathlib.Path(os.path.abspath(__file__)).parents[0] 63 return pathlib.Path(this_path, 'resource', target) 64 65 @staticmethod 66 def get_expected_lines(target): 67 file = ChangeReportTest.get_resrouce_path(target) 68 with open(file, 'r') as f: 69 lines = f.readlines() 70 return lines 71 72 @staticmethod 73 def diff_lines(expected, actual): 74 expected_len = len(expected) 75 actual_len = len(actual) 76 offending_line_indexes = [] 77 78 if actual_len < expected_len: 79 l = actual_len 80 else: 81 l = expected_len 82 83 for i in range(l): 84 if expected[i] != actual[i]: 85 print('ERROR: line %d is not as expected' % i) 86 print(expected[i]) 87 print(actual[i]) 88 offending_line_indexes.append(i) 89 90 if actual_len < expected_len: 91 print('ERROR: Missing %d lines' % (expected_len - actual_len)) 92 for j in range(actual_len, expected_len): 93 print(expected[j]) 94 offending_line_indexes.append(j) 95 elif actual_len > expected_len: 96 print('ERROR: Extra %d lines' % (actual_len - expected_len)) 97 for k in range(expected_len, actual_len): 98 print(actual[k]) 99 offending_line_indexes.append(k) 100 return offending_line_indexes 101 102 103if __name__ == '__main__': 104 unittest.main() 105