From ba45990676035cd1d32a843d16256a3a727b98fd Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Sun, 23 Feb 2020 20:13:32 +0800 Subject: [PATCH 1/4] test package --- tests/packagetest/packagetest.nimble | 15 ++++++++++ tests/packagetest/src/packagetest.nim | 10 +++++++ tests/test.nim | 41 ++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/packagetest/packagetest.nimble create mode 100644 tests/packagetest/src/packagetest.nim diff --git a/tests/packagetest/packagetest.nimble b/tests/packagetest/packagetest.nimble new file mode 100644 index 0000000..a3e1c7d --- /dev/null +++ b/tests/packagetest/packagetest.nimble @@ -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" diff --git a/tests/packagetest/src/packagetest.nim b/tests/packagetest/src/packagetest.nim new file mode 100644 index 0000000..45c0b79 --- /dev/null +++ b/tests/packagetest/src/packagetest.nim @@ -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() diff --git a/tests/test.nim b/tests/test.nim index 28b1aca..6ef481e 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,6 +1,28 @@ -import unittest, options, sequtils +import unittest, options, os, osproc, sequtils, strutils, sugar import leveldb, leveldbpkg/raw +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] = + const tmpNimbleDir = "/tmp/testnimble" + var quotedArgs = @args + quotedArgs.insert("-y") + quotedArgs.insert("--nimbleDir:" & tmpNimbleDir) + quotedArgs.insert("nimble") + quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\"")) + + let cmd = quotedArgs.join(" ") + result = execCmdEx(cmd) + checkpoint(cmd) + checkpoint(result.output) + suite "leveldb": setup: @@ -159,3 +181,20 @@ 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.") From 713b036f986503751a6aa8bfb3769a7985b3075a Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Sun, 23 Feb 2020 15:43:12 +0800 Subject: [PATCH 2/4] fix package compile issue Error message: "Error: cannot open file: ../leveldb.nimble" --- .gitignore | 3 +++ src/leveldb.nim | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f010be6..cbbffb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ nimcache/ /test.db/ /leveldb.e +tests/packagetest/packagetest +src/leveldb +tests/test diff --git a/src/leveldb.nim b/src/leveldb.nim index 9b937f9..6644f54 100644 --- a/src/leveldb.nim +++ b/src/leveldb.nim @@ -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"): From 75ab9bbb4774a7c47100f82cb60908ef20a81516 Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Sun, 23 Feb 2020 21:37:54 +0800 Subject: [PATCH 3/4] test tool --- tests/test.nim | 56 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/test.nim b/tests/test.nim index 6ef481e..cc56d9a 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -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 From 39d911be0c18a4c3bd785b1f18b2e6c9d58f482f Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Sun, 23 Feb 2020 22:03:47 +0800 Subject: [PATCH 4/4] sugar not in nim 0.18.0 --- tests/test.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test.nim b/tests/test.nim index cc56d9a..15756bb 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,4 +1,4 @@ -import unittest, options, os, osproc, sequtils, strutils, sugar +import unittest, options, os, osproc, sequtils, strutils import leveldb, leveldbpkg/raw const @@ -20,7 +20,7 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] = quotedArgs.insert("-y") quotedArgs.insert("--nimbleDir:" & tmpNimbleDir) quotedArgs.insert("nimble") - quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\"")) + quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"") let cmd = quotedArgs.join(" ") result = execCmdEx(cmd) @@ -32,7 +32,7 @@ proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] = quotedArgs.insert(tmpDbDir) quotedArgs.insert("--database") quotedArgs.insert(tmpNimbleDir / "bin" / "leveldb") - quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\"")) + quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"") if not existsDir(tmpDbDir): createDir(tmpDbDir)