Skip to content

Commit

Permalink
fix tests; make fqname be starting from root of nimble package instea…
Browse files Browse the repository at this point in the history
…d of root of filesystem
  • Loading branch information
timotheecour committed Aug 14, 2018
1 parent ccb1e4a commit d2b9e8e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 43 deletions.
60 changes: 35 additions & 25 deletions compiler/packagehandling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,53 @@ iterator myParentDirs(p: string): string =
var current = p
while true:
current = current.parentDir
if current.len == 0: break
# IMPROVE:parentDir is buggy, "foo.nim".parentDir should be ".", not ""
if current.len == 0:
current = "."
yield current
break
yield current

proc resetPackageCache*(conf: ConfigRef) =
conf.packageCache = newPackageCache()
const packageSep = "@"

when false:
proc getPackageName*(conf: ConfigRef; path: string): string =
echo ("getPackageName",path)
var parents = 0
proc getPackageName*(conf: ConfigRef; path: string): string =
var dirs:seq[string] = @[]
var pkg = ""
var path_root = ""
block packageSearch:
for d in myParentDirs(path):
path_root = d
if conf.packageCache.hasKey(d):
#echo "from cache ", d, " |", packageCache[d], "|", path.splitFile.name
return conf.packageCache[d]
inc parents
pkg = conf.packageCache[d]
break packageSearch
for file in walkFiles(d / "*.nimble"):
result = file.splitFile.name
pkg = file.splitFile.name
break packageSearch
for file in walkFiles(d / "*.babel"):
result = file.splitFile.name
pkg = file.splitFile.name
break packageSearch
# we also store if we didn't find anything:
if result.isNil: result = ""
for d in myParentDirs(path):
#echo "set cache ", d, " |", result, "|", parents
conf.packageCache[d] = result
dec parents
if parents <= 0: break
dirs.add d.splitPath.tail

proc getPackageName*(conf: ConfigRef; path: string): string =
# at this point, path_root maps to pkg
for index in 0 .. dirs.len:
if index > 0:
let dir = dirs[^index]
path_root = path_root & DirSep & dir
pkg = pkg & packageSep & dir
if conf.packageCache.hasKey(path_root):
doAssert conf.packageCache[path_root] == pkg
else:
conf.packageCache[path_root] = pkg
result = pkg

proc fullyQualifiedName*(conf: ConfigRef; path: string): string =
let pkg = getPackageName(conf, path)
doAssert pkg.len > 0
let (p, file, ext) = path.splitFile
let sep = '@'
result = p.replace(DirSep, sep) & sep
result = pkg & packageSep & file

proc withPackageName*(conf: ConfigRef; path: string): string =
let x = getPackageName(conf, path)
doAssert x.len > 0
let fqname = fullyQualifiedName(conf, path)
let (p, file, ext) = path.splitFile
result = (p / (x & file)) & ext
# TODO: is `p/` part needed?
result = p / (fqname & ext)
9 changes: 5 additions & 4 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir

template mathop(op) {.dirty.} =
registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`)
registerCallback(c, "stdlib@pure.math." & astToStr(op), `op Wrapper`)

template osop(op) {.dirty.} =
registerCallback(c, "stdlib.os." & astToStr(op), `op Wrapper`)
registerCallback(c, "stdlib@pure.os." & astToStr(op), `op Wrapper`)

template ospathsop(op) {.dirty.} =
registerCallback(c, "stdlib.ospaths." & astToStr(op), `op Wrapper`)
registerCallback(c, "stdlib@pure.ospaths." & astToStr(op), `op Wrapper`)

template systemop(op) {.dirty.} =
registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)

template macrosop(op) {.dirty.} =
registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`)
registerCallback(c, "stdlib@core.macros." & astToStr(op), `op Wrapper`)

template wrap1f_math(op) {.dirty.} =
proc `op Wrapper`(a: VmArgs) {.nimcall.} =
Expand Down Expand Up @@ -116,6 +116,7 @@ proc registerAdditionalOps*(c: PCtx) =
wrap2svoid(writeFile, systemop)
wrap1s(readFile, systemop)
systemop getCurrentExceptionMsg
#TODO:handle
registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
systemop gorgeEx
Expand Down
2 changes: 1 addition & 1 deletion lib/system/nimscript.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ proc patchFile*(package, filename, replacement: string) =
##
## .. code-block:: nim
##
## patchFile("stdlib", "asyncdispatch", "patches/replacement")
## patchFile("stdlib@pure", "asyncdispatch", "patches/replacement")
discard

proc getCommand*(): string =
Expand Down
7 changes: 0 additions & 7 deletions tests/modules/tnotuniquename.nim

This file was deleted.

8 changes: 8 additions & 0 deletions tests/modules/tnotuniquename1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
discard """
output: "ok"
"""

# module names need not be unique (but must be renamed to avoid clashes)
import mnotuniquename
import tnotuniquename/mnotuniquename as mnotuniquename1
echo "ok"
4 changes: 2 additions & 2 deletions tests/modules/tnotuniquename2.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discard """
file: "tnotuniquename/mnotuniquename.nim"
errormsg: "module names need to be unique per Nimble package"
errormsg: "redefinition of 'mnotuniquename'"
"""

# avoid name clashes
import mnotuniquename
import tnotuniquename/mnotuniquename
2 changes: 1 addition & 1 deletion tests/newconfig/tfoo.nims
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ospaths
warning("uninit", off)
hint("processing", off)
#--verbosity:2
patchFile("stdlib", "math", "mymath")
patchFile("stdlib@pure", "math", "mymath")

task listDirs, "lists every subdirectory":
for x in listDirs("."):
Expand Down
14 changes: 11 additions & 3 deletions tests/testament/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,20 @@ proc cmpMsgs(r: var TResults, expected, given: TSpec, test: TTest, target: TTarg
r.addResult(test, target, expected.msg, given.msg, reSuccess)
inc(r.passed)

import compiler/options
var conf = newConfigRef()

proc generatedFile(test: TTest, target: TTarget): string =
let (_, name, _) = test.name.splitFile
let ext = targetToExt[target]
result = nimcacheDir(test.name, test.options, target) /
(if target == targetJS: "" else: "compiler_") &
name.changeFileExt(ext)

let fqname = fullyQualifiedName(conf, test.name)
# echo ("generatedFile", test.name, fqname, ext)
result = nimcacheDir(test.name, test.options, target)
if target == targetJS: #TODO:why?
result = result / name.changeFileExt(ext)
else:
result = result / fqname & "." & ext

proc needsCodegenCheck(spec: TSpec): bool =
result = spec.maxCodeSize > 0 or spec.ccodeCheck.len > 0
Expand Down

0 comments on commit d2b9e8e

Please sign in to comment.