Skip to content

Commit 3ad1276

Browse files
committed
[FEATURE] Added error counter on makefile check. This fixes #31
1 parent ccbf7f0 commit 3ad1276

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

PyChecker/utils/makefile.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
def check_makefile_clean_dir(project_path: str, binary_name: str, root_path: str):
12+
error_count = 0
1213
with open(root_path + "/.mymakefile", 'w+') as file:
1314
file.write("Cleaning Directory\n")
1415
print("Cleaning Directory")
@@ -24,14 +25,18 @@ def check_makefile_clean_dir(project_path: str, binary_name: str, root_path: str
2425
" It should have removed {}\n".format(binary_name))
2526
print("-> Error when processing rule `fclean':"
2627
" It should have removed {}".format(binary_name))
28+
error_count += 1
2729
if glob.glob(project_path + '*.o'):
2830
file.write("-> Error when processing rule `fclean':"
2931
" It should have removed *.o\n")
3032
print("-> Error when processing rule `fclean':"
3133
" It should have removed *.o")
34+
error_count += 1
35+
return error_count
3236

3337

3438
def check_makefile_all(project_path: str, binary_name: str, root_path: str):
39+
error_count = 0
3540
makefile_path = project_path + '/Makefile'
3641
with open(root_path + "/.mymakefile", 'a') as file:
3742
file.write("*------------------------------------------------------*\n")
@@ -40,9 +45,13 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
4045
if 'all: ' not in open(makefile_path).read():
4146
file.write("-> Error: rule `all' not found in the Makefile.\n")
4247
print("-> Error: rule `all' not found in the Makefile.")
48+
error_count += 1
49+
return error_count
4350
if 'all: $(NAME)' not in open(makefile_path).read():
4451
file.write("-> Error: rule `all' should call the rule `$(NAME)'\n")
4552
print("-> Error: rule `all' should call the rule `$(NAME)'")
53+
error_count += 1
54+
return error_count
4655
result = subprocess.run('make ' + '-C ' + project_path + ' all',
4756
shell=True, stdout=subprocess.PIPE,
4857
stderr=subprocess.STDOUT).stdout.decode('utf-8')
@@ -53,15 +62,19 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
5362
" It should have created {}\n".format(binary_name))
5463
print("-> Error when processing rule `all':"
5564
" It should have created {}".format(binary_name))
65+
error_count += 1
5666
if not glob.glob(project_path + '/*.o'):
5767
file.write("-> Error when processing rule `all':"
5868
" It should NOT have removed *.o\n")
5969
print("-> Error when processing rule `all':"
6070
" It should NOT have removed *.o")
71+
error_count += 1
6172
# @todo [ -z "$(echo ${MAKEALLTWICE} | grep -i "Nothing to be done")" -a -z "$(echo ${MAKEALLTWICE} | grep -i "is up to date")" ] && printf "%s\n" "-> Failing rule: Processing the rule 'all' twice in a row should result in nothing to be done" && RET=1
73+
return error_count
6274

6375

6476
def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
77+
error_count = 0
6578
makefile_path = project_path + '/Makefile'
6679
with open(root_path + "/.mymakefile", 'a') as file:
6780
file.write("*------------------------------------------------------*\n")
@@ -70,12 +83,15 @@ def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
7083
if 'clean: ' not in open(makefile_path).read():
7184
file.write("-> Error: rule `clean' not found in the Makefile.\n")
7285
print("-> Error: rule `clean' not found in the Makefile.")
86+
error_count += 1
87+
return error_count
7388
if not os.path.exists(project_path + '/' + binary_name):
7489
file.write("-> Error: Cannot test rule `clean' because rule"
7590
" `all' failed\n")
7691
print("-> Error: Cannot test rule `clean' because rule"
7792
" `all' failed")
78-
# @todo Stop the makefile test here
93+
error_count += 1
94+
return error_count
7995
result = subprocess.run('make ' + '-C ' + project_path + ' clean',
8096
shell=True, stdout=subprocess.PIPE,
8197
stderr=subprocess.STDOUT).stdout.decode('utf-8')
@@ -86,12 +102,16 @@ def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
86102
"cleaned the binary named {}.\n".format(binary_name))
87103
print("-> Error: Failing Rule: It should not have "
88104
"cleaned the binary named {}.".format(binary_name))
105+
error_count += 1
89106
if glob.glob(project_path + '/*.o'):
90107
file.write("-> Error: Failing Rule: It should have cleaned the *.o\n")
91108
print("-> Error: Failing Rule: It should have cleaned the *.o")
109+
error_count += 1
110+
return error_count
92111

93112

94113
def check_makefile_re(project_path: str, binary_name: str, root_path: str):
114+
error_count = 0
95115
makefile_path = project_path + '/Makefile'
96116
with open(root_path + "/.mymakefile", 'a') as file:
97117
file.write("*------------------------------------------------------*\n")
@@ -100,11 +120,15 @@ def check_makefile_re(project_path: str, binary_name: str, root_path: str):
100120
if 're: ' not in open(makefile_path).read():
101121
file.write("-> Error: rule `re' not found in the Makefile.\n")
102122
print("-> Error: rule `re' not found in the Makefile.")
123+
error_count += 1
124+
return error_count
103125
if not os.path.exists(project_path + '/' + binary_name):
104126
file.write("-> Error: Cannot test rule `re' because rule "
105127
"`all' failed\n")
106128
print("-> Error: Cannot test rule `re' because rule "
107129
"`all' failed")
130+
error_count += 1
131+
return error_count
108132
inode1 = os.stat(project_path + '/' + binary_name).st_ino
109133
file.write("-- Before running rule `re', the {} inode is {}\n".format(binary_name, inode1))
110134
print("-- Before running rule `re', the {} inode is {}".format(binary_name, inode1))
@@ -121,19 +145,24 @@ def check_makefile_re(project_path: str, binary_name: str, root_path: str):
121145
" have built the object file `*.o'\n")
122146
print("-> Error when processing rule `re': It should"
123147
" have built the object file `*.o'")
148+
error_count += 1
124149
if not os.path.exists(project_path + '/' + binary_name):
125150
file.write("--> Error when processing rule `re': It should"
126151
" have compiled a binary named {}\n".format(binary_name))
127152
print("--> Error when processing rule `re': It should"
128153
" have compiled a binary named {}".format(binary_name))
154+
error_count += 1
129155
if inode1 == inode2:
130156
file.write("-> Failing rule `re': It should have compiled"
131157
" again the binary named {} (inode unchanged)\n".format(binary_name))
132158
print("-> Failing rule `re': It should have compiled"
133159
" again the binary named {} (inode unchanged)".format(binary_name))
160+
error_count += 1
161+
return error_count
134162

135163

136164
def check_makefile_fclean(project_path: str, binary_name: str, root_path: str):
165+
error_count = 0
137166
makefile_path = project_path + '/Makefile'
138167
with open(root_path + "/.mymakefile", 'a') as file:
139168
file.write("*------------------------------------------------------*\n")
@@ -142,6 +171,8 @@ def check_makefile_fclean(project_path: str, binary_name: str, root_path: str):
142171
if 'fclean: ' not in open(makefile_path).read():
143172
file.write("-> Error: rule `fclean' not found in the Makefile.\n")
144173
print("-> Error: rule `fclean' not found in the Makefile.")
174+
error_count += 1
175+
return error_count
145176
result = subprocess.run('make ' + '-C ' + project_path + ' fclean',
146177
shell=True, stdout=subprocess.PIPE,
147178
stderr=subprocess.STDOUT).stdout.decode('utf-8')
@@ -152,15 +183,20 @@ def check_makefile_fclean(project_path: str, binary_name: str, root_path: str):
152183
" have removed the binary named {}\n".format(binary_name))
153184
print("--> Error when processing rule `re': It should"
154185
" have removed the binary named {}".format(binary_name))
186+
error_count += 1
155187
if glob.glob(project_path + '/*.o'):
156188
file.write("-> Error: Failing Rule: It should have cleaned the *.o\n")
157189
print("-> Error: Failing Rule: It should have cleaned the *.o")
190+
error_count += 1
158191
if glob.glob(project_path + '/*.a'):
159192
file.write("-> Error: Failing Rule: It should have cleaned the *.a\n")
160193
print("-> Error: Failing Rule: It should have cleaned the *.a")
194+
error_count += 1
195+
return error_count
161196

162197

163198
def check_makefile_name(project_path: str, binary_name: str, root_path: str):
199+
error_count = 0
164200
makefile_path = project_path + '/Makefile'
165201
with open(root_path + "/.mymakefile", 'a') as file:
166202
file.write("*------------------------------------------------------*\n")
@@ -169,6 +205,8 @@ def check_makefile_name(project_path: str, binary_name: str, root_path: str):
169205
if '$(NAME):' not in open(makefile_path).read():
170206
file.write("-> Error: rule `$(NAME)' not found in the Makefile.\n")
171207
print("-> Error: rule `$(NAME)' not found in the Makefile.")
208+
error_count += 1
209+
return error_count
172210
result = subprocess.run('make ' + '-C ' + project_path + ' ' + binary_name,
173211
shell=True, stdout=subprocess.PIPE,
174212
stderr=subprocess.STDOUT).stdout.decode('utf-8')
@@ -179,15 +217,19 @@ def check_makefile_name(project_path: str, binary_name: str, root_path: str):
179217
" have compiled a binary named {}\n".format(binary_name))
180218
print("--> Error when processing rule `re': It should"
181219
" have compiled a binary named {}".format(binary_name))
220+
error_count += 1
182221
if not glob.glob(project_path + '/*.o'):
183222
file.write("-> Error when processing rule `fclean': It should"
184223
" NOT have removed *.o\n")
185224
print("-> Error when processing rule `fclean': It should"
186225
" NOT have removed *.o\n")
226+
error_count += 1
187227
# @todo [ -z "$(echo ${MAKEALLTWICE} | grep -i "Nothing to be done")" -a -z "$(echo ${MAKEALLTWICE} | grep -i "is up to date")" ] && printf "%s\n" "-> Failing rule: Processing the rule 'all' twice in a row should result in nothing to be done" && RET=1
228+
return error_count
188229

189230

190231
def check_makefile_phony(project_path: str, binary_name: str, root_path: str):
232+
error_count = 0
191233
makefile_path = project_path + '/Makefile'
192234
with open(root_path + "/.mymakefile", 'a') as file:
193235
file.write("*------------------------------------------------------*\n")
@@ -196,11 +238,15 @@ def check_makefile_phony(project_path: str, binary_name: str, root_path: str):
196238
if '.PHONY:' not in open(makefile_path).read():
197239
file.write("-> Error: rule `.PHONY' not found in the Makefile.\n")
198240
print("-> Error: rule `.PHONY' not found in the Makefile.")
241+
error_count += 1
242+
return error_count
199243
if not os.path.exists(project_path + '/' + binary_name):
200-
file.write("-> Error: Cannot test rule `re' because rule "
244+
file.write("-> Error: Cannot test rule `.PHONY' because rule "
201245
"`$(NAME)' failed\n")
202-
print("-> Error: Cannot test rule `re' because rule "
246+
print("-> Error: Cannot test rule `.PHONY' because rule "
203247
"`$(NAME)' failed")
248+
error_count += 1
249+
return error_count
204250
result = subprocess.run('make ' + '-C ' + project_path + ' .PHONY',
205251
shell=True, stdout=subprocess.PIPE,
206252
stderr=subprocess.STDOUT).stdout.decode('utf-8')
@@ -211,9 +257,12 @@ def check_makefile_phony(project_path: str, binary_name: str, root_path: str):
211257
" not have cleaned the binary named {}\n".format(binary_name))
212258
print("--> Error when processing rule `.PHONY': It should"
213259
" not have cleaned the binary named {}\n".format(binary_name))
260+
error_count += 1
214261
if glob.glob(project_path + '/*.o'):
215262
file.write("-> Error: Failing Rule: It should have cleaned the *.o\n")
216263
print("-> Error: Failing Rule: It should have cleaned the *.o")
264+
error_count += 1
265+
return error_count
217266

218267

219268
def check(project_path: str, binary_name: str, root_path: str):
@@ -232,12 +281,15 @@ def check(project_path: str, binary_name: str, root_path: str):
232281
if not os.path.exists(makefile_path):
233282
print("--> Error: Makefile not found.")
234283
return 1
235-
# @todo Add an error counter
236-
check_makefile_clean_dir(project_path, binary_name, root_path)
237-
check_makefile_all(project_path, binary_name, root_path)
238-
check_makefile_clean(project_path, binary_name, root_path)
239-
check_makefile_re(project_path, binary_name, root_path)
240-
check_makefile_fclean(project_path, binary_name, root_path)
241-
check_makefile_name(project_path, binary_name, root_path)
242-
check_makefile_phony(project_path, binary_name, root_path)
284+
error_count = 0
285+
error_count += check_makefile_clean_dir(project_path, binary_name, root_path)
286+
error_count += check_makefile_all(project_path, binary_name, root_path)
287+
error_count += check_makefile_clean(project_path, binary_name, root_path)
288+
error_count += check_makefile_re(project_path, binary_name, root_path)
289+
error_count += check_makefile_fclean(project_path, binary_name, root_path)
290+
error_count += check_makefile_name(project_path, binary_name, root_path)
291+
error_count += check_makefile_phony(project_path, binary_name, root_path)
292+
print("\n\n\t\tFound {} errors on your makefile".format(error_count))
293+
with open(root_path + "/.mymakefile", 'a') as file:
294+
file.write("\n\n\t\tFound {} errors on your makefile\n".format(error_count))
243295
return 0

0 commit comments

Comments
 (0)