Skip to content

Commit 0777f33

Browse files
authored
Fix getting download dir name for develop (#952)
* Fix getting download dir name for develop When getting the download directory name for a package that we want to put in develop mode, a trailing slash in the URL or the subdir leads to getting an empty string. To fix the problem we remove the trailing slash from them at the start of the `getDevelopDownloadDir` procedure. * Add tests for the `getDevelopDownloadDir` proc The `getDevelopDownloadDir` procedure is moved in the `download.nim` module and units tests for it are added. * Fix the tests on Windows Fix the `getDevelopDownloadDir` procedure tests on Windows by adding normalization of the expected paths.
1 parent 0a23c44 commit 0777f33

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/nimble.nim

-16
Original file line numberDiff line numberDiff line change
@@ -492,22 +492,6 @@ proc developWithDependencies(options: Options): bool =
492492
## with `--with-dependencies` flag.
493493
options.action.typ == actionDevelop and options.action.withDependencies
494494

495-
proc getDevelopDownloadDir(url, subdir: string, options: Options): string =
496-
## Returns the download dir for a develop mode dependency.
497-
assert isURL(url), "The string \"{url}\" is not a URL."
498-
499-
let downloadDirName =
500-
if subdir.len == 0:
501-
parseUri(url).path.splitFile.name
502-
else:
503-
subdir.splitFile.name
504-
505-
result =
506-
if options.action.path.isAbsolute:
507-
options.action.path / downloadDirName
508-
else:
509-
getCurrentDir() / options.action.path / downloadDirName
510-
511495
proc raiseCannotCloneInExistingDirException(downloadDir: string) =
512496
let msg = "Cannot clone into '$1': directory exists." % downloadDir
513497
const hint = "Remove the directory, or run this command somewhere else."

src/nimblepkg/download.nim

+59
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,28 @@ proc echoPackageVersions*(pkg: Package) =
511511
echo(" versions: (Remote tag retrieval not supported by " &
512512
$pkg.downloadMethod & ")")
513513

514+
proc removeTrailingSlash(s: string): string =
515+
s.strip(chars = {'/'}, leading = false)
516+
517+
proc getDevelopDownloadDir*(url, subdir: string, options: Options): string =
518+
## Returns the download dir for a develop mode dependency.
519+
assert isURL(url), &"The string \"{url}\" is not a URL."
520+
521+
let url = url.removeTrailingSlash
522+
let subdir = subdir.removeTrailingSlash
523+
524+
let downloadDirName =
525+
if subdir.len == 0:
526+
parseUri(url).path.splitFile.name
527+
else:
528+
subdir.splitFile.name
529+
530+
result =
531+
if options.action.path.isAbsolute:
532+
options.action.path / downloadDirName
533+
else:
534+
getCurrentDir() / options.action.path / downloadDirName
535+
514536
when isMainModule:
515537
import unittest
516538

@@ -536,3 +558,40 @@ when isMainModule:
536558
newVersion("0.1.1"): "v0.1.1",
537559
newVersion("0.1.0"): "v0.1.0",})
538560
check getVersionList(data) == expected
561+
562+
suite "getDevelopDownloadDir":
563+
let dummyOptionsWithoutPath = Options(action: Action(typ: actionDevelop))
564+
let dummyOptionsWithAbsolutePath = Options(
565+
action: Action(typ: actionDevelop, path: "/some/dir/"))
566+
let dummyOptionsWithRelativePath = Options(
567+
action: Action(typ: actionDevelop, path: "some/dir/"))
568+
569+
test "without subdir and without path":
570+
check getDevelopDownloadDir(
571+
"https://github.com/nimble-test/packagea/", "",
572+
dummyOptionsWithoutPath) == getCurrentDir() / "packagea"
573+
574+
test "without subdir and with absolute path":
575+
check getDevelopDownloadDir(
576+
"https://github.com/nimble-test/packagea", "",
577+
dummyOptionsWithAbsolutePath) == "/some/dir/packagea".normalizedPath
578+
579+
test "without subdir and with relative path":
580+
check getDevelopDownloadDir(
581+
"https://github.com/nimble-test/packagea/", "",
582+
dummyOptionsWithRelativePath) == getCurrentDir() / "some/dir/packagea"
583+
584+
test "with subdir and without path":
585+
check getDevelopDownloadDir(
586+
"https://github.com/nimble-test/multi", "beta",
587+
dummyOptionsWithoutPath) == getCurrentDir() / "beta"
588+
589+
test "with subdir and with absolute path":
590+
check getDevelopDownloadDir(
591+
"https://github.com/nimble-test/multi/", "alpha/",
592+
dummyOptionsWithAbsolutePath) == "/some/dir/alpha".normalizedPath
593+
594+
test "with subdir and with relative path":
595+
check getDevelopDownloadDir(
596+
"https://github.com/nimble-test/multi", "alpha/",
597+
dummyOptionsWithRelativePath) == getCurrentDir() / "some/dir/alpha"

0 commit comments

Comments
 (0)