调试熊¶
本文档概述了Coala的调试接口。调试界面将帮助用户调试Bear代码,并使用PDB界面逐步调试代码。
在调用Coala的调试器之后,它将进入 run()
一只熊的方法,不管用什么方法 yield
或者不是,一旦熊市完成分析,就会立即退出。
备注
PDB的退出命令 (q
)已重新映射,因此Coala继续正常执行而不中止。所以, quit
或 q
将首先清除所有断点,然后继续执行。
下面是为简单的 HelloWorldBear
这将为每个文件打印一条调试消息,并生成一条消息“A HelloworldBear”。
import logging
from coalib.bears.LocalBear import LocalBear
class HelloWorldBear(LocalBear):
def run(self, filename, file):
logging.debug('Hello World! Checking file {}.'.
format(filename))
yield self.new_result(message="A HelloworldBear",
file=filename)
调用之后,调试器将单步执行 run()
一种通过PDB承载的方法 runcall()
方法,并在 run()
就在左边。
例如,调试会话如下所示:
[DEBUG][15:58:27] Platform Linux -- Python 3.6.5, coalib
0.12.0.dev99999999999999
Executing section cli...
[DEBUG][15:58:27] Files that will be checked:
/home/Voldemort/test/mytest.py
[DEBUG][15:58:27] coala is run only on changed files, bears' log messages
from previous runs may not appear. You may use the `--flush-cache` flag to
see them.
[DEBUG][15:58:27] Running bear HelloWorldBear...
> /home/Voldemort/coala-bears/bears/general/HelloWorldBear.py(8)run()
-> logging.debug('Hello World! Checking file {}.'.
(Pdb) l
3 from coalib.bears.LocalBear import LocalBear
4
5
6 class HelloWorldBear(LocalBear):
7 def run(self, filename, file):
8 -> logging.debug('Hello World! Checking file {}.'.
9 format(filename))
10
11 yield self.new_result(message="A HelloworldBear.",
12 file=filename)
[EOF]
(Pdb) c
[DEBUG][15:58:30] Hello World! Checking file /home/Voldemort/test/mytest.py.
--Return--
> /home/Voldemort/coala-bears/bears/general/HelloWorldBear.py(8)run()->None
-> logging.debug('Hello World! Checking file {}.'.
(Pdb) c
mytest.py
**** HelloWorldBear [Section: cli | Severity: NORMAL] ****
! ! A HelloworldBear
[ ] *0. Do (N)othing
[ ] 1. (O)pen file
[ ] 2. Add (I)gnore comment
[ ] Enter number (Ctrl-D to exit):
使用¶
命令行界面¶
用户可以指定要使用的调试熊 --debug-bears
,即
$coala -b PEP8Bear,HelloWorldBear -f <filename> --debug-bears HelloWorldBear
如果没有为 --debug-bears
然后,它将默认调试通过的所有熊 --bears
或 -b
争论。
$ coala --bears HelloWorldBear -files <filename> --debug-bears
备注
一只熊可能取决于不同熊的结果。调试器还将调试熊所依赖的所有熊。
Coafile¶
用户可以指定使用 .coafile
:
[all]
bears = PEP8Bear,MypyBear
files = <filename>
debug_bears = PEP8Bear
或调试由 bears
设置:
[all]
bears = PEP8Bear,MypyBear
files = <filename>
debug_bears = True
调试器功能¶
检查Bear设置¶
新命令 settings
包含在Coala的调试界面中,用于检查调试环境中的Bear设置。它显示熊的所有已传递设置及其值,以便您可以快速检查它们。
下面是一个Bear示例,它检测文件是否有超过 max_number_of_lines
线。
from coalib.results.Result import Result
from coalib.bears.LocalBear import LocalBear
class TooManyLinesBear(LocalBear):
def run(self, filename, file, max_number_of_lines: int):
"""
Detects if a file has more than ``max_number_of_lines`` lines.
:param max_number_of_lines:
Maximum number of lines to be allowed for a file.
"""
if len(file) > max_number_of_lines:
yield Result.from_values(
origin=self,
message=('This file has {} lines, while {} lines'
' are allowed'
.format(len(file), max_number_of_lines)),
file=filename)
在上面的示例中 max_number_of_lines
是非可选设置。在TooManyLinesBear上调用调试器之后,Coala将首先询问所有未传递的非可选设置的值,即 max_number_of_lines
在此之后,将调用调试器。然后是 settings
命令用于显示Bear在调试环境中更新后的可选和非可选设置,即
[WARNING][17:50:57] acquire_settings: section parameter is deprecated.
Please enter a value for the setting "max_number_of_lines" (Maximum number
of lines to be allowed for a file.) needed by TooManyLinesBear for
section "cli":
5
[DEBUG][17:50:59] Platform Linux -- Python 3.6.5,
coalib 0.12.0.dev99999999999999
[DEBUG][17:50:59] The file cache was successfully flushed.
Executing section cli...
[DEBUG][17:50:59] Files that will be checked:
/home/vaibhav/test/mytest.py
[DEBUG][17:50:59] coala is run only on changed files, bears' log messages
from previous runs may not appear. You may use the `--flush-cache` flag to
see them.
[DEBUG][17:50:59] Running bear TooManyLinesBear...
> /home/vaibhav/coala-bears/bears/general/TooManyLinesBear.py(15)run()
-> if len(file) > max_number_of_lines:
(Pdb) l
10
11 :param max_number_of_lines:
12 Maximum number of lines to be allowed for a file.
13 """
14
15 -> if len(file) > max_number_of_lines:
16 yield Result.from_values(
17 origin=self,
18 message=('This file has {} lines, while {} lines'
19 ' are allowed'
20 .format(len(file), max_number_of_lines)),
(Pdb) settings
max_number_of_lines = 5
> /home/vaibhav/coala-bears/bears/general/TooManyLinesBear.py(16)run()
-> yield Result.from_values(
(Pdb) q
--Return--
> /home/vaibhav/coala-bears/bears/general/TooManyLinesBear.py(16)run()->None
-> yield Result.from_values(
(Pdb) q