fw4spl
test_codingstyle.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 
4 import filecmp
5 import os
6 import shutil
7 import unittest
8 
9 import codingstyle
10 import common
11 
12 
13 class TestCodingstyle(unittest.TestCase):
14  @classmethod
15  def __are_directory_identical(cls, dircmp):
16  if len(dircmp.diff_files) > 0 or len(dircmp.funny_files) > 0:
17  return False
18  else:
19  for sub_dircmp in dircmp.subdirs.values():
20  sub_return = cls.__are_directory_identical(sub_dircmp)
21  if not sub_return:
22  return False
23  return True
24 
25  @classmethod
26  def __execute_codingstyle(cls, test, enableReformat, *args, **kwargs):
27  # Be verbose by default
28  common.g_trace = True
29 
30  # Be sure to use a fixed year
31  codingstyle.YEAR = 2999
32 
33  # Construct the base path
34  dir_path = os.path.dirname(os.path.realpath(__file__))
35  test_data_path = dir_path + '/' + test
36 
37  if enableReformat:
38  # Save the test data
39  test_data_path_copy = test_data_path + str(os.getpid())
40  shutil.copytree(test_data_path, test_data_path_copy)
41 
42  try:
43  # Find the files to check
44  files = common.directory_on_disk(test_data_path_copy)
45 
46  # Apply the hook
47  result, reformatted = codingstyle.codingstyle(files, True, True, False)
48 
49  except:
50  shutil.rmtree(test_data_path_copy)
51  raise
52 
53  # Compare the result with a verbatim
54  verbatim = kwargs.get('verbatim', None)
55 
56  if verbatim is not None:
57  verbatim_data_path = dir_path + '/' + verbatim
58  dircmp = filecmp.dircmp(verbatim_data_path, test_data_path_copy)
59  if not cls.__are_directory_identical(dircmp):
60  result = True
61 
62  # Cleanup
63  shutil.rmtree(test_data_path_copy)
64  else:
65  # Find the files to check
66  files = common.directory_on_disk(test_data_path)
67 
68  # Apply the hook on source directly
69  result, reformatted = codingstyle.codingstyle(files, False, True, False)
70 
71  return result, reformatted
72 
73  def test_codingstyle_formatted(self):
74  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Formatted', False)
75 
76  # Check result
77  self.assertFalse(result, "Codingstyle function should return no error.")
78  self.assertFalse(len(reformatted) > 0, "No file should have been reformatted.")
79 
80  def test_codingstyle_lgpl_check(self):
81  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Lgpl', False)
82 
83  # Check result
84  self.assertTrue(result, "Codingstyle function should return True as one file at least contains one error.")
85  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
86 
87  def test_codingstyle_lgpl_reformat(self):
88  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Lgpl', True)
89 
90  # Check result
91  self.assertFalse(result, "Codingstyle function should return no error.")
92  self.assertTrue(len(reformatted) > 0, "Some files should have been fixed.")
93 
94  def test_codingstyle_sort_includes_check(self):
95  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Sort_includes', False)
96 
97  # Check result
98  self.assertTrue(result, "Codingstyle function should return True as one file at least contains one error.")
99  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
100 
101  def test_codingstyle_sort_includes_reformat(self):
102  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Sort_includes', True)
103 
104  # Check result
105  self.assertFalse(result, "Codingstyle function should return no error.")
106  self.assertTrue(len(reformatted) > 0, "Some files should have been fixed.")
107 
108  def test_codingstyle_header_guards_typo_check(self):
109  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Header_guards_typo', False)
110 
111  # Check result
112  self.assertTrue(result, "Codingstyle function should return True as one file at least contains one error.")
113  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
114 
115  def test_codingstyle_header_guards_typo_reformat(self):
116  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Header_guards_typo', True)
117 
118  # Check result
119  self.assertFalse(result, "Codingstyle function should return no error.")
120  self.assertTrue(len(reformatted) > 0, "Some files should have been fixed.")
121 
122  def test_codingstyle_header_guards_forgotten_check(self):
123  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Header_guards_forgotten', False)
124 
125  # Check result
126  self.assertTrue(result, "Codingstyle function should return True as one file at least contains one error.")
127  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
128 
129  def test_codingstyle_uncrusitfy_formatted_check(self):
130  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Uncrustify_formatted', False)
131 
132  # Check result
133  self.assertFalse(result, "Codingstyle function should return True as no file contains error.")
134  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
135 
136  def test_codingstyle_uncrusitfy_unformatted_check(self):
137  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Uncrustify_unformatted', False)
138 
139  # Check result
140  self.assertTrue(result, "Codingstyle function should return False as one file at least contains one error.")
141  self.assertFalse(len(reformatted) > 0, "No file should have been fixed.")
142 
143  def test_codingstyle_uncrustify_unformatted_reformat(self):
144  result, reformatted = self.__execute_codingstyle('data/Codingstyle/Uncrustify_unformatted',
145  True,
146  verbatim='data/Codingstyle/Uncrustify_formatted')
147 
148  # Check result
149  self.assertFalse(result, "Codingstyle function should return no error.")
150  self.assertTrue(len(reformatted) > 0, "Some files should have been fixed.")
151 
152 
153 if __name__ == '__main__':
154  unittest.main()
def codingstyle(files, enable_reformat, check_lgpl, check_commits_date)
Definition: codingstyle.py:52
def directory_on_disk(path)
Definition: common.py:334
def __are_directory_identical(cls, dircmp)
def __execute_codingstyle(cls, test, enableReformat, args, kwargs)