Merge pull request #15 from xyb/packagecompile

fix package compile issue
This commit is contained in:
Michał Zieliński 2020-02-23 17:18:47 +01:00 committed by GitHub
commit 54480c9a89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 3 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
nimcache/
/test.db/
/leveldb.e
tests/packagetest/packagetest
src/leveldb
tests/test

View File

@ -1,4 +1,4 @@
import options, strutils, leveldbpkg/raw
import options, os, strutils, leveldbpkg/raw
type
LevelDb* = ref object
@ -20,7 +20,15 @@ type
const
version* = block:
let content = staticRead"../leveldb.nimble"
const configFile = "leveldb.nimble"
const sourcePath = currentSourcePath()
const parentConfig = sourcePath.parentDir.parentDir / configFile
const localConfig = sourcePath.parentDir / configFile
var content: string
if fileExists(parentConfig):
content = staticRead(parentConfig)
else:
content = staticRead(localConfig)
var version_line: string
for line in content.split("\L"):
if line.startsWith("version"):

View File

@ -0,0 +1,15 @@
# Package
version = "0.1.0"
author = "Xie Yanbo"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"
bin = @["packagetest"]
# Dependencies
requires "nim >= 0.18.0"
requires "leveldb"

View File

@ -0,0 +1,10 @@
import options
import leveldb
when isMainModule:
let db = leveldb.open("/tmp/testleveldb/tooldb")
db.put("hello", "world")
let val = db.get("hello")
if val.isSome() and val.get() == "world":
echo "leveldb works."
db.close()

View File

@ -1,6 +1,47 @@
import unittest, options, sequtils
import unittest, options, os, osproc, sequtils, strutils
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.
block:
let lastDir = getCurrentDir()
setCurrentDir(dir)
body
setCurrentDir(lastDir)
proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] =
var quotedArgs = @args
quotedArgs.insert("-y")
quotedArgs.insert("--nimbleDir:" & tmpNimbleDir)
quotedArgs.insert("nimble")
quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"")
let cmd = quotedArgs.join(" ")
result = execCmdEx(cmd)
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(proc (x: string): string = "\"" & x & "\"")
if not existsDir(tmpDbDir):
createDir(tmpDbDir)
let cmd = quotedArgs.join(" ")
result = execCmdEx(cmd)
checkpoint(cmd)
checkpoint(result.output)
suite "leveldb":
setup:
@ -159,3 +200,55 @@ suite "leveldb":
defer: nc.close()
nc.put("a", "1")
check(toSeq(nc.iter()) == @[("a", "1")])
suite "package":
test "import as package":
let (output, exitCode) = execNimble("install")
check exitCode == QuitSuccess
check output.contains("leveldb installed successfully.")
cd "tests/packagetest":
var (output, exitCode) = execNimble("build")
check exitCode == QuitSuccess
check output.contains("Building")
(output, exitCode) = execCmdEx("./packagetest")
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