I'm either missing something or this is inconsistent.
I'm calling currentSourcePath() in combination with parentDir() and it appears it yields different results whether I call the code directly or inside a template.
# source_path/src/source_path.nim
template mySourcePath*(): string =
currentSourcePath()
template mySourceDir*(): string =
currentSourcePath().parentDir()
# source_path/tests/test_source_path.nim
import os
import unittest
import source_path
test "currentSourcePath template inside a template":
let path = currentSourcePath() # RESULT: The path to testing file
let path2 = mySourcePath() # RESULT: The path to testing file
check path == path2
let dir = currentSourcePath().parentDir() # RESULT: The path to tests dir
let dir2 = mySourceDir() # RESULT: The path to src dir
check dir == dir2 # Fails
Shouldn't mySourceDir() return the same result as currentSourcePath().parentDir(), since the former is just a template for the latter chained call? (Tested with both Nim 1.6 and 2.0.)
Here's my repo: walkr/source_path
... system.currentSourcePath which returns the path of the source file containing that template call.
but that doesn't clarify why I ought to get different results.
The call to my template mySourcePath() on the line let path2 = mySourcePath(), injects the content of that template (mySourcePath) at the call site, thus the injected currentSourcePath() will return the path to the test file, because that's where the injected currentSourcePath() ultimately gets called. And it returns just that.
However, my second template mySourceDir(), ought to inject at its call site, the one identified by the line let dir2 = mySourceDir(), the same code as the function mySourcePath() except with an additional transformation, namely .parentDir(), but from the example I've provided it appears that the currentSourcePath() inside mySourcePath, and the currentSourcePath() inside mySourceDir, behave differently.
Put it another way, if these two templates return the same paths,
template mySourcePath*(): string =
currentSourcePath()
template mySourceDir*(): string =
currentSourcePath()
shouldn't these two – notice the addition of the .parentDir() – still return the same paths, except that the mySourceDir will only return the parent directory of whatever that path is?
template mySourcePath*(): string =
currentSourcePath()
template mySourceDir*(): string =
currentSourcePath().parentDir()