Skip to content
GitLab
探索
项目
群组
代码片段
项目
群组
代码片段
/
帮助
帮助
支持
社区论坛
快捷键
?
提交反馈
登录
切换导航
菜单
打开侧边栏
ExternalRepo
pre-commit-hooks
提交
a21def36
提交
a21def36
编辑于
7年前
作者:
Anthony Sottile
浏览文件
操作
下载
电子邮件补丁
差异文件
Add an `--unsafe` option to `check-yaml`
上级
e80813e7
main
remove-fix-encoding-pragma
v4.6.0
v4.5.0
v4.4.0
v4.3.0
v4.2.0
v4.1.0
v4.0.1
v4.0.0
v3.4.0
v3.3.0
v3.2.0
v3.1.0
v3.0.1
v3.0.0
v2.5.0
v2.4.0
v2.3.0
v2.2.3
v2.2.2
v2.2.1
v2.2.0
v2.1.0
v2.0.0
v1.4.0
v1.4.0-1
v1.3.0
无相关合并请求
变更
3
Hide whitespace changes
Inline
Side-by-side
显示
3 个更改的文件
README.md
+5
-0
README.md
pre_commit_hooks/check_yaml.py
+34
-5
pre_commit_hooks/check_yaml.py
tests/check_yaml_test.py
+20
-1
tests/check_yaml_test.py
有
59 个添加
和
6 个删除
+59
-6
README.md
+
5
-
0
浏览文件 @
a21def36
...
...
@@ -52,6 +52,11 @@ Add this to your `.pre-commit-config.yaml`
-
`check-yaml`
- Attempts to load all yaml files to verify syntax.
-
`--allow-multiple-documents`
- allow yaml files which use the
[
multi-document syntax
](
http://www.yaml.org/spec/1.2/spec.html#YAML
)
-
`--unsafe`
- Instaed of loading the files, simply parse them for syntax.
A syntax-only check enables extensions and unsafe constructs which would
otherwise be forbidden. Using this option removes all guarantees of
portability to other yaml implementations.
Implies
`--allow-multiple-documents`
.
-
`debug-statements`
- Check for pdb / ipdb / pudb statements in code.
-
`detect-aws-credentials`
- Checks for the existence of AWS secrets that you
have set up with the AWS CLI.
...
...
This diff is collapsed.
Click to expand it.
pre_commit_hooks/check_yaml.py
+
34
-
5
浏览文件 @
a21def36
from
__future__
import
print_function
import
argparse
import
collections
import
sys
import
yaml
...
...
@@ -11,24 +12,52 @@ except ImportError: # pragma: no cover (no libyaml-dev / pypy)
Loader
=
yaml
.
SafeLoader
def
_exhaust
(
gen
):
for
_
in
gen
:
pass
def
_parse_unsafe
(
*
args
,
**
kwargs
):
_exhaust
(
yaml
.
parse
(
*
args
,
**
kwargs
))
def
_load_all
(
*
args
,
**
kwargs
):
# need to exhaust the generator
return
tuple
(
yaml
.
load_all
(
*
args
,
**
kwargs
))
_exhaust
(
yaml
.
load_all
(
*
args
,
**
kwargs
))
Key
=
collections
.
namedtuple
(
'Key'
,
(
'multi'
,
'unsafe'
))
LOAD_FNS
=
{
Key
(
multi
=
False
,
unsafe
=
False
):
yaml
.
load
,
Key
(
multi
=
False
,
unsafe
=
True
):
_parse_unsafe
,
Key
(
multi
=
True
,
unsafe
=
False
):
_load_all
,
Key
(
multi
=
True
,
unsafe
=
True
):
_parse_unsafe
,
}
def
check_yaml
(
argv
=
None
):
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'-m'
,
'--allow-multiple-documents'
,
dest
=
'yaml_load_fn'
,
action
=
'store_const'
,
const
=
_load_all
,
default
=
yaml
.
load
,
'-m'
,
'--multi'
,
'--allow-multiple-documents'
,
action
=
'store_true'
,
)
parser
.
add_argument
(
'--unsafe'
,
action
=
'store_true'
,
help
=
(
'Instead of loading the files, simply parse them for syntax. '
'A syntax-only check enables extensions and unsafe contstructs '
'which would otherwise be forbidden. Using this option removes '
'all guarantees of portability to other yaml implementations. '
'Implies --allow-multiple-documents'
),
)
parser
.
add_argument
(
'filenames'
,
nargs
=
'*'
,
help
=
'Yaml filenames to check.'
)
args
=
parser
.
parse_args
(
argv
)
load_fn
=
LOAD_FNS
[
Key
(
multi
=
args
.
multi
,
unsafe
=
args
.
unsafe
)]
retval
=
0
for
filename
in
args
.
filenames
:
try
:
args
.
yaml_
load_fn
(
open
(
filename
),
Loader
=
Loader
)
load_fn
(
open
(
filename
),
Loader
=
Loader
)
except
yaml
.
YAMLError
as
exc
:
print
(
exc
)
retval
=
1
...
...
This diff is collapsed.
Click to expand it.
tests/check_yaml_test.py
+
20
-
1
浏览文件 @
a21def36
...
...
@@ -22,7 +22,7 @@ def test_check_yaml_allow_multiple_documents(tmpdir):
f
=
tmpdir
.
join
(
'test.yaml'
)
f
.
write
(
'---
\n
foo
\n
---
\n
bar
\n
'
)
# should fail
w
without the setting
# should fail without the setting
assert
check_yaml
((
f
.
strpath
,))
# should pass when we allow multiple documents
...
...
@@ -33,3 +33,22 @@ def test_fails_even_with_allow_multiple_documents(tmpdir):
f
=
tmpdir
.
join
(
'test.yaml'
)
f
.
write
(
'['
)
assert
check_yaml
((
'--allow-multiple-documents'
,
f
.
strpath
))
def
test_check_yaml_unsafe
(
tmpdir
):
f
=
tmpdir
.
join
(
'test.yaml'
)
f
.
write
(
'some_foo: !vault |
\n
'
' $ANSIBLE_VAULT;1.1;AES256
\n
'
' deadbeefdeadbeefdeadbeef
\n
'
,
)
# should fail "safe" check
assert
check_yaml
((
f
.
strpath
,))
# should pass when we allow unsafe documents
assert
not
check_yaml
((
'--unsafe'
,
f
.
strpath
))
def
test_check_yaml_unsafe_still_fails_on_syntax_errors
(
tmpdir
):
f
=
tmpdir
.
join
(
'test.yaml'
)
f
.
write
(
'['
)
assert
check_yaml
((
'--unsafe'
,
f
.
strpath
))
This diff is collapsed.
Click to expand it.
编辑
预览
支持
Markdown
0%
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录
菜单
探索
项目
群组
代码片段