test tool

This commit is contained in:
Xie Yanbo 2020-02-23 21:37:54 +08:00
parent 713b036f98
commit 75ab9bbb47
1 changed files with 55 additions and 1 deletions

View File

@ -1,6 +1,11 @@
import unittest, options, os, osproc, sequtils, strutils, sugar
import leveldb, leveldbpkg/raw
const
tmpDir = "/tmp/testleveldb"
tmpNimbleDir = tmpDir / "nimble"
tmpDbDir = tmpDir / "testdb"
template cd*(dir: string, body: untyped) =
## Sets the current dir to ``dir``, executes ``body`` and restores the
## previous working dir.
@ -11,7 +16,6 @@ template cd*(dir: string, body: untyped) =
setCurrentDir(lastDir)
proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] =
const tmpNimbleDir = "/tmp/testnimble"
var quotedArgs = @args
quotedArgs.insert("-y")
quotedArgs.insert("--nimbleDir:" & tmpNimbleDir)
@ -23,6 +27,21 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] =
checkpoint(cmd)
checkpoint(result.output)
proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] =
var quotedArgs = @args
quotedArgs.insert(tmpDbDir)
quotedArgs.insert("--database")
quotedArgs.insert(tmpNimbleDir / "bin" / "leveldb")
quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\""))
if not existsDir(tmpDbDir):
createDir(tmpDbDir)
let cmd = quotedArgs.join(" ")
result = execCmdEx(cmd)
checkpoint(cmd)
checkpoint(result.output)
suite "leveldb":
setup:
@ -198,3 +217,38 @@ suite "package":
checkpoint output
check exitCode == QuitSuccess
check output.contains("leveldb works.")
suite "tool":
test "leveldb tool":
var (output, exitCode) = execNimble("install")
check exitCode == QuitSuccess
check output.contains("Building")
check execTool("-v").exitCode == QuitSuccess
check execTool("create").exitCode == QuitSuccess
check execTool("list").exitCode == QuitSuccess
check execTool("put", "hello", "world").exitCode == QuitSuccess
(output, exitCode) = execTool("get", "hello")
check exitCode == QuitSuccess
check output == "world\L"
(output, exitCode) = execTool("list")
check exitCode == QuitSuccess
check output == "hello world\L"
check execTool("delete", "hello").exitCode == QuitSuccess
(output, exitCode) = execTool("get", "hello")
check exitCode == QuitSuccess
check output == ""
(output, exitCode) = execTool("list")
check exitCode == QuitSuccess
check output == ""
check execTool("put", "hello", "6130", "-x").exitCode == QuitSuccess
check execTool("get", "hello", "-x").output == "6130\L"
check execTool("get", "hello").output == "a0\L"
check execTool("list", "-x").output == "hello 6130\L"
check execTool("put", "hello", "0061", "-x").exitCode == QuitSuccess
check execTool("get", "hello", "-x").output == "0061\L"
check execTool("delete", "hello").exitCode == QuitSuccess