Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
2ad63fd0
提交
2ad63fd0
authored
6月 25, 2016
作者:
Thomas Gardner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: improve test coverage for FileArg parsing
License: MIT Signed-off-by:
Thomas Gardner
<
tmg@fastmail.com
>
上级
ddc8d0c6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
20 行删除
+81
-20
parse.go
commands/cli/parse.go
+5
-3
parse_test.go
commands/cli/parse_test.go
+76
-17
没有找到文件。
commands/cli/parse.go
浏览文件 @
2ad63fd0
...
...
@@ -11,8 +11,8 @@ import (
cmds
"github.com/ipfs/go-ipfs/commands"
files
"github.com/ipfs/go-ipfs/commands/files"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
logging
"gx/ipfs/QmYtB7Qge8cJpXc4irsEp8zRqfnZMBeB7aTrMEkPk67DRv/go-log"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
var
log
=
logging
.
Logger
(
"commands/cli"
)
...
...
@@ -305,7 +305,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
var
err
error
if
fpath
==
"-"
{
if
err
=
printReadInfo
(
stdin
,
msgStdinInfo
);
err
==
nil
{
file
=
files
.
NewReaderFile
(
""
,
""
,
stdin
,
nil
)
fpath
=
stdin
.
Name
()
file
=
files
.
NewReaderFile
(
""
,
fpath
,
stdin
,
nil
)
}
}
else
{
file
,
err
=
appendFile
(
fpath
,
argDef
,
recursive
,
hidden
)
...
...
@@ -321,7 +322,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
if
err
:=
printReadInfo
(
stdin
,
msgStdinInfo
);
err
!=
nil
{
return
nil
,
nil
,
err
}
fileArgs
[
""
]
=
files
.
NewReaderFile
(
""
,
""
,
stdin
,
nil
)
fpath
:=
stdin
.
Name
()
fileArgs
[
fpath
]
=
files
.
NewReaderFile
(
""
,
fpath
,
stdin
,
nil
)
}
else
{
break
}
...
...
commands/cli/parse_test.go
浏览文件 @
2ad63fd0
...
...
@@ -179,7 +179,12 @@ func TestArgumentParsing(t *testing.T) {
},
"FileArg"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
FileArg
(
"a"
,
false
,
false
,
"some arg"
),
commands
.
FileArg
(
"a"
,
true
,
false
,
"some arg"
),
},
},
"FileArg+Variadic"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
FileArg
(
"a"
,
true
,
true
,
"some arg"
),
},
},
"FileArg+Stdin"
:
{
...
...
@@ -187,10 +192,34 @@ func TestArgumentParsing(t *testing.T) {
commands
.
FileArg
(
"a"
,
true
,
true
,
"some arg"
)
.
EnableStdin
(),
},
},
"StringArg+FileArg"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
FileArg
(
"a"
,
true
,
false
,
"some arg"
),
},
},
"StringArg+FileArg+Stdin"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
FileArg
(
"a"
,
true
,
true
,
"some arg"
)
.
EnableStdin
(),
},
},
"StringArg+FileArg+Variadic"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
FileArg
(
"a"
,
true
,
true
,
"some arg"
),
},
},
"StringArg+FileArg+Variadic+Stdin"
:
{
Arguments
:
[]
commands
.
Argument
{
commands
.
StringArg
(
"a"
,
true
,
false
,
"some arg"
),
commands
.
FileArg
(
"a"
,
true
,
true
,
"some arg"
),
},
},
},
}
test
:=
func
(
cmd
words
,
f
*
os
.
File
,
res
words
)
{
test
:=
func
(
cmd
words
,
f
*
os
.
File
,
exp
words
)
{
if
f
!=
nil
{
if
_
,
err
:=
f
.
Seek
(
0
,
os
.
SEEK_SET
);
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -200,8 +229,18 @@ func TestArgumentParsing(t *testing.T) {
if
err
!=
nil
{
t
.
Errorf
(
"Command '%v' should have passed parsing: %v"
,
cmd
,
err
)
}
if
!
sameWords
(
req
.
Arguments
(),
res
)
{
t
.
Errorf
(
"Arguments parsed from '%v' are '%v' instead of '%v'"
,
cmd
,
req
.
Arguments
(),
res
)
parsedWords
:=
make
([]
string
,
len
(
req
.
Arguments
()))
copy
(
parsedWords
,
req
.
Arguments
())
if
files
:=
req
.
Files
();
files
!=
nil
{
for
file
,
err
:=
files
.
NextFile
();
err
!=
io
.
EOF
;
file
,
err
=
files
.
NextFile
()
{
parsedWords
=
append
(
parsedWords
,
file
.
FullPath
())
}
}
if
!
sameWords
(
parsedWords
,
exp
)
{
t
.
Errorf
(
"Arguments parsed from '%v' are '%v' instead of '%v'"
,
cmd
,
parsedWords
,
exp
)
}
}
...
...
@@ -241,23 +280,43 @@ func TestArgumentParsing(t *testing.T) {
testFail
([]
string
{
"reversedoptional"
},
nil
,
"didn't provide any args, 1 required"
)
testFail
([]
string
{
"reversedoptional"
,
"value1"
,
"value2"
,
"value3"
},
nil
,
"provided too many args, only takes 1"
)
// Use a temp file to simulate stdin
fileToSimulateStdin
:=
func
(
t
*
testing
.
T
,
content
string
)
*
os
.
File
{
fstdin
,
err
:=
ioutil
.
TempFile
(
""
,
""
)
// Since FileArgs are presently stored ordered by Path, the enum string
// is used to construct a predictably ordered sequence of filenames.
tmpFile
:=
func
(
t
*
testing
.
T
,
enum
string
)
*
os
.
File
{
f
,
err
:=
ioutil
.
TempFile
(
""
,
enum
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
_
,
err
:=
io
.
WriteString
(
fstdin
,
content
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
fstdin
return
f
}
fstdin
:=
fileToSimulateStdin
(
t
,
"stdin1"
)
defer
os
.
Remove
(
fstdin
.
Name
())
file1
:=
tmpFile
(
t
,
"1"
)
file2
:=
tmpFile
(
t
,
"2"
)
file3
:=
tmpFile
(
t
,
"3"
)
defer
os
.
Remove
(
file3
.
Name
())
defer
os
.
Remove
(
file2
.
Name
())
defer
os
.
Remove
(
file1
.
Name
())
test
([]
string
{
"noarg"
},
fstdin
,
[]
string
{})
test
([]
string
{
"FileArg"
,
fstdin
.
Name
()},
nil
,
[]
string
{})
test
([]
string
{
"FileArg+Stdin"
},
fstdin
,
[]
string
{})
test
([]
string
{
"FileArg+Stdin"
,
"-"
},
fstdin
,
[]
string
{})
test
([]
string
{
"noarg"
},
file1
,
[]
string
{})
test
([]
string
{
"FileArg"
,
file1
.
Name
()},
nil
,
[]
string
{
file1
.
Name
()})
test
([]
string
{
"FileArg+Variadic"
,
file1
.
Name
(),
file2
.
Name
()},
nil
,
[]
string
{
file1
.
Name
(),
file2
.
Name
()})
test
([]
string
{
"FileArg+Stdin"
},
file1
,
[]
string
{
file1
.
Name
()})
test
([]
string
{
"FileArg+Stdin"
,
"-"
},
file1
,
[]
string
{
file1
.
Name
()})
test
([]
string
{
"FileArg+Stdin"
,
file1
.
Name
(),
"-"
},
file2
,
[]
string
{
file1
.
Name
(),
file2
.
Name
()})
test
([]
string
{
"StringArg+FileArg"
,
"foo"
,
file1
.
Name
()},
nil
,
[]
string
{
"foo"
,
file1
.
Name
()})
test
([]
string
{
"StringArg+FileArg+Variadic"
,
"foo"
,
file1
.
Name
(),
file2
.
Name
()},
nil
,
[]
string
{
"foo"
,
file1
.
Name
(),
file2
.
Name
()})
test
([]
string
{
"StringArg+FileArg+Stdin"
,
"foo"
,
file1
.
Name
(),
"-"
},
file2
,
[]
string
{
"foo"
,
file1
.
Name
(),
file2
.
Name
()})
test
([]
string
{
"StringArg+FileArg+Variadic+Stdin"
,
"foo"
,
file1
.
Name
(),
file2
.
Name
()},
file3
,
[]
string
{
"foo"
,
file1
.
Name
(),
file2
.
Name
()})
test
([]
string
{
"StringArg+FileArg+Variadic+Stdin"
,
"foo"
,
file1
.
Name
(),
file2
.
Name
(),
"-"
},
file3
,
[]
string
{
"foo"
,
file1
.
Name
(),
file2
.
Name
(),
file3
.
Name
()})
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论