Regex bash grep-负匹配
我想在我的Python单元测试中显示一些标记位置,在这些位置我一直是懒惰的和去激活的测试 但我也有条件执行,这不是懒惰,它们是由测试时的性能或系统条件驱动的。这些是没有技巧的,我想完全忽略它们 让我们把一些输入放在一个文件中,test\u so\u bashregex.txt,并附上一些注释Regex bash grep-负匹配,regex,bash,regex-negation,Regex,Bash,Regex Negation,我想在我的Python单元测试中显示一些标记位置,在这些位置我一直是懒惰的和去激活的测试 但我也有条件执行,这不是懒惰,它们是由测试时的性能或系统条件驱动的。这些是没有技巧的,我想完全忽略它们 让我们把一些输入放在一个文件中,test\u so\u bashregex.txt,并附上一些注释 !ignore this, because skipUnless means I have an acceptable conditional flag @unittest.skipUnless(do_te
!ignore this, because skipUnless means I have an acceptable conditional flag
@unittest.skipUnless(do_test, do_test_msg)
def test_conditional_function():
xxx
!catch these 2, lazy test-passing
@unittest.skip("fb212.test_urls_security_usergroup Test_Detail.test_related fails with 302")
def sometest_function():
xxx
@unittest.expectedFailure
def test_another_function():
xxx
!bonus points... ignore things that are commented out
# @unittest.expectedFailure
此外,我不能在管道中使用grep-v skipUnless
,因为我确实想使用egrep-a 3 xxx*.py
来提供一些上下文,如:
grep-a3“@unittest\”*.py
test_backend_security_meta.py: @unittest.skip("rewrite - data can be legitimately missing")
test_backend_security_meta.py- def test_storage(self):
test_backend_security_meta.py- with getMultiDb() as mdb:
test_backend_security_meta.py-
我尝试过的:
努力@
我尝试了@unittest\(.+)(?!(除非\())
,但没有成功,因为它与前3个匹配
同上@unittest\.[a-zA-Z]+(?!(除非\())
@unittest\.skip(?)(除非\())
在使用skip的2上部分工作
尽管存在异常,所有这些都会进行部分匹配
在bash egrep上,事情看起来并没有多大好转,而这正是最终的结局
jluc@explore$egrep'@unittest\.*(?!(除非))'test\u so\u bashregex.txt
egrep:重复运算符操作数无效您可以:
(?
如果您不在乎是否出现“跳过”或“预期失败”,您可以简化它:
(?<!#\s)@unittest\.(?!skipUnless).*
(?好的,我会在sweaver2112的帮助下展示我的发现,但是如果有人有一个好的单阶段grep-ready regex,我就买它
bash的白鹭/grep不喜欢?!
(参考文献)。故事到此结束
相反,我所做的是通过管道将其传输到一些额外的过滤器:负grep-v skipUnless和另一个过滤器,以去除前导注释。这两个过滤器去除了不需要的行。但是,然后通过管道将其输出返回到另一个grep,并使用-A 3标志反复查找@unittest
如果负数greps清除了一行,它将不会显示在最后一个pipestage中,因此将从输入中删除。如果没有,我将立即返回上下文
egrep -A 3 -n '@unittest\.' test_so_bashregex.txt | egrep -v "^\s*#" | egrep -v "skipUnless\(" | grep @unittest -A 3
输出:
7:@unittest.skip("fb212.test_urls_security_usergroup Test_Detail.test_related fails with 302")
8-def sometest_function():
9- xxx
10:@unittest.expectedFailure
11-def test_another_function():
12- xxx
test_backend_security_meta.py:77: @unittest.skip("rewrite - data can be legitimately missing")
test_backend_security_meta.py-78- def test_storage(self):
test_backend_security_meta.py-79- with getMultiDb() as mdb:
test_backend_security_meta.py-80-
--
test_backend_security_meta.py:98: @unittest.skip("rewrite - data can be legitimately missing")
test_backend_security_meta.py-99- def test_get_li_tag_for_object(self):
test_backend_security_meta.py-100- li = self.mgr.get_li_tag()
test_backend_security_meta.py-101-
以及在**.py*上运行它的实际输出,而不是我的test.txt文件:
egrep -A 3 -n '@unittest\.' *.py | egrep -v "\d:\s*#" | egrep -v "skipUnless\(" | grep @unittest -A 3
输出:
7:@unittest.skip("fb212.test_urls_security_usergroup Test_Detail.test_related fails with 302")
8-def sometest_function():
9- xxx
10:@unittest.expectedFailure
11-def test_another_function():
12- xxx
test_backend_security_meta.py:77: @unittest.skip("rewrite - data can be legitimately missing")
test_backend_security_meta.py-78- def test_storage(self):
test_backend_security_meta.py-79- with getMultiDb() as mdb:
test_backend_security_meta.py-80-
--
test_backend_security_meta.py:98: @unittest.skip("rewrite - data can be legitimately missing")
test_backend_security_meta.py-99- def test_get_li_tag_for_object(self):
test_backend_security_meta.py-100- li = self.mgr.get_li_tag()
test_backend_security_meta.py-101-
像这样的东西怎么样?格雷普似乎有点拘束
items=$(find . -name "*.py")
for item in $items; do
cat $item | awk '
/^\@unittest.*expectedFailure/{seen_skip=1;}
/^\@unittest.*skip/{seen_skip=1;}
/^def/{
if (seen_skip == 1)
print "Being lazy at " $1
seen_skip=0;
}
'
done
不,跳过和预期的失败并不重要。我只关心没有技巧的人,为了避免它。@unittest\(?!skipUnless).*
在在线测试中运行良好。但在bash上,我发现重复运算符操作数无效。我要补充的是,OSX上的bash,因为我知道grep/egrep与Linux变体相比略有不同。在现实生活中,这些行不是放在test.txt文件中的,而是由grep xxx*.py拾取的。因此,我不会得到e文件名和行,除非我有一个额外的步骤将这些信息传送到awk。您的代码确实可以工作,打印出“在定义时懒惰”3次。更新为循环所有py-应该能够扩展该查找,但是您有测试文件结构,它显然选择了*.py,但输出在定义时仍然懒惰。所以$1 I始终只有“def”。没有@unittest指令、文件名、行,甚至没有实际的函数代码。感谢您的努力,这就是为什么我投了您一票,但它看起来比我的解决方案更不稳定和复杂。