Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
后端镜像
rust镜像
Ripgrep
提交
adff43fb
R
Ripgrep
项目概览
后端镜像
/
rust镜像
/
Ripgrep
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Ripgrep
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
adff43fb
编写于
3月 01, 2017
作者:
M
Marc Tiehuis
提交者:
Andrew Gallant
3月 08, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove clap validator + add max-filesize integration tests
上级
71585f6d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
60 addition
and
34 deletion
+60
-34
build.rs
build.rs
+0
-1
src/app.rs
src/app.rs
+1
-24
src/args.rs
src/args.rs
+7
-9
tests/tests.rs
tests/tests.rs
+45
-0
tests/workdir.rs
tests/workdir.rs
+7
-0
未找到文件。
build.rs
浏览文件 @
adff43fb
...
...
@@ -2,7 +2,6 @@
extern
crate
clap
;
#[macro_use]
extern
crate
lazy_static
;
extern
crate
regex
;
use
std
::
env
;
use
std
::
fs
;
...
...
src/app.rs
浏览文件 @
adff43fb
use
std
::
collections
::
HashMap
;
use
clap
::{
App
,
AppSettings
,
Arg
,
ArgSettings
};
use
regex
::
Regex
;
const
ABOUT
:
&
'static
str
=
"
ripgrep (rg) recursively searches your current directory for a regex pattern.
...
...
@@ -147,8 +146,7 @@ fn app<F>(next_line_help: bool, doc: F) -> App<'static, 'static>
.short
(
"m"
)
.value_name
(
"NUM"
)
.takes_value
(
true
)
.validator
(
validate_number
))
.arg
(
flag
(
"max-filesize"
)
.value_name
(
"NUM+SUFFIX?"
)
.takes_value
(
true
)
.validator
(
validate_max_filesize
))
.value_name
(
"NUM+SUFFIX?"
)
.takes_value
(
true
))
.arg
(
flag
(
"maxdepth"
)
.value_name
(
"NUM"
)
.takes_value
(
true
)
.validator
(
validate_number
))
...
...
@@ -502,24 +500,3 @@ lazy_static! {
fn
validate_number
(
s
:
String
)
->
Result
<
(),
String
>
{
s
.parse
::
<
usize
>
()
.map
(|
_
|())
.map_err
(|
err
|
err
.to_string
())
}
fn
validate_max_filesize
(
s
:
String
)
->
Result
<
(),
String
>
{
let
re
=
Regex
::
new
(
r#"^(\d+)([KMG])?$"#
)
.unwrap
();
let
caps
=
try!
(
re
.captures
(
&
s
)
.ok_or
(
"invalid format for max-filesize argument"
));
let
value
=
caps
.get
(
1
);
let
suffix
=
caps
.get
(
2
)
.map
(|
x
|
x
.as_str
());
match
value
{
Some
(
value
)
=>
{
try!
(
value
.as_str
()
.parse
::
<
u64
>
()
.map_err
(|
err
|
err
.to_string
()));
}
None
=>
()
}
match
suffix
{
None
|
Some
(
"K"
)
|
Some
(
"M"
)
|
Some
(
"G"
)
=>
Ok
(()),
_
=>
Err
(
From
::
from
(
"invalid suffix for max-filesize argument"
))
}
}
src/args.rs
浏览文件 @
adff43fb
...
...
@@ -791,20 +791,18 @@ impl<'a> ArgMatches<'a> {
None
=>
return
Ok
(
None
)
};
let
re
=
Regex
::
new
(
r#"^(\d+)([KMG])?$"#
)
.unwrap
();
let
re
=
Regex
::
new
(
"^([0-9]+)([KMG])?$"
)
.unwrap
();
let
caps
=
try!
(
re
.captures
(
&
max_filesize
)
.ok_or
(
"invalid format for max-filesize argument"
));
let
value
=
match
caps
.get
(
1
)
{
Some
(
value
)
=>
Some
(
try!
(
value
.as_str
()
.parse
::
<
u64
>
())),
None
=>
None
};
let
value
=
try!
(
caps
[
1
]
.parse
::
<
u64
>
()
.map_err
(|
err
|
err
.to_string
()));
let
suffix
=
caps
.get
(
2
)
.map
(|
x
|
x
.as_str
());
match
suffix
{
None
=>
Ok
(
value
),
Some
(
"K"
)
=>
Ok
(
value
.map
(|
x
|
x
*
1024
)),
Some
(
"M"
)
=>
Ok
(
value
.map
(|
x
|
x
*
1024
*
1024
)),
Some
(
"G"
)
=>
Ok
(
value
.map
(|
x
|
x
*
1024
*
1024
*
1024
)),
None
=>
Ok
(
Some
(
value
)
),
Some
(
"K"
)
=>
Ok
(
Some
(
value
*
1024
)),
Some
(
"M"
)
=>
Ok
(
Some
(
value
*
1024
*
1024
)),
Some
(
"G"
)
=>
Ok
(
Some
(
value
*
1024
*
1024
*
1024
)),
_
=>
Err
(
From
::
from
(
"invalid suffix for max-filesize argument"
))
}
}
...
...
tests/tests.rs
浏览文件 @
adff43fb
...
...
@@ -444,6 +444,51 @@ sherlock!(max_filesize_parse_error_suffix, "Sherlock", ".",
wd
.assert_err
(
&
mut
cmd
);
});
sherlock!
(
max_filesize_parse_no_suffix
,
"Sherlock"
,
"."
,
|
wd
:
WorkDir
,
mut
cmd
:
Command
|
{
wd
.remove
(
"sherlock"
);
wd
.create_size
(
"foo"
,
40
);
wd
.create_size
(
"bar"
,
60
);
cmd
.arg
(
"--max-filesize"
)
.arg
(
"50"
)
.arg
(
"--files"
);
let
lines
:
String
=
wd
.stdout
(
&
mut
cmd
);
let
expected
=
"
\
foo
"
;
assert_eq!
(
lines
,
expected
);
});
sherlock!
(
max_filesize_parse_k_suffix
,
"Sherlock"
,
"."
,
|
wd
:
WorkDir
,
mut
cmd
:
Command
|
{
wd
.remove
(
"sherlock"
);
wd
.create_size
(
"foo"
,
3048
);
wd
.create_size
(
"bar"
,
4100
);
cmd
.arg
(
"--max-filesize"
)
.arg
(
"4K"
)
.arg
(
"--files"
);
let
lines
:
String
=
wd
.stdout
(
&
mut
cmd
);
let
expected
=
"
\
foo
"
;
assert_eq!
(
lines
,
expected
);
});
sherlock!
(
max_filesize_parse_m_suffix
,
"Sherlock"
,
"."
,
|
wd
:
WorkDir
,
mut
cmd
:
Command
|
{
wd
.remove
(
"sherlock"
);
wd
.create_size
(
"foo"
,
1000000
);
wd
.create_size
(
"bar"
,
1400000
);
cmd
.arg
(
"--max-filesize"
)
.arg
(
"1M"
)
.arg
(
"--files"
);
let
lines
:
String
=
wd
.stdout
(
&
mut
cmd
);
let
expected
=
"
\
foo
"
;
assert_eq!
(
lines
,
expected
);
});
sherlock!
(
ignore_hidden
,
"Sherlock"
,
"."
,
|
wd
:
WorkDir
,
mut
cmd
:
Command
|
{
wd
.remove
(
"sherlock"
);
wd
.create
(
".sherlock"
,
hay
::
SHERLOCK
);
...
...
tests/workdir.rs
浏览文件 @
adff43fb
...
...
@@ -46,6 +46,13 @@ impl WorkDir {
self
.create_bytes
(
name
,
contents
.as_bytes
());
}
/// Create a new file with the given name and size.
pub
fn
create_size
<
P
:
AsRef
<
Path
>>
(
&
self
,
name
:
P
,
filesize
:
u64
)
{
let
path
=
self
.dir
.join
(
name
);
let
file
=
nice_err
(
&
path
,
File
::
create
(
&
path
));
nice_err
(
&
path
,
file
.set_len
(
filesize
));
}
/// Create a new file with the given name and contents in this directory.
pub
fn
create_bytes
<
P
:
AsRef
<
Path
>>
(
&
self
,
name
:
P
,
contents
:
&
[
u8
])
{
let
path
=
self
.dir
.join
(
name
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录