Skip to content

Commit 14020d5

Browse files
committed
Fix tests
- Add a Mercurial install step to the CI on Mac OS because it is not installed by default but it is required by the `vcstools.nim` module tests. - Add workaround for the `Resource temporarily unavailable (code: 11)` error on Linux systems. The error is most probably caused by a bug in the `asynctools` or the Nim standard library but currently, the workaround is an easier solution than a proper fix. - Add debug priority printing of the command to be executed by the `doCmdEx` and` doCmdExAsync` procedures. - Fix `vcstools.getBranchesOnWhichVcsRevisionIsPresent` procedure which had a wrong output on Windows. - Configure user name and email for the local testing repositories to avoid VCS operations failure when tests are executed on the CI. - Fix expected character for root directory in `getVcsTypeAndSpecialDirPath` test in` vcstools.nim` module. - Use a proper module name in the `dependency.nim` test file when the tests are executed on Mac OS. - Fix the expected main repository path on Mac OS to start with "/private" in "cannot lock because develop dependency is out of range" test in `tlockfile.nim`. Most probably the list is incomplete because I'm missing something. :) Related to #127
1 parent 794ee44 commit 14020d5

File tree

8 files changed

+73
-26
lines changed

8 files changed

+73
-26
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
with:
2525
version: ${{ matrix.nimversion }}
2626
- run: nim --version
27+
- name: Install Mercurial on macOS
28+
if: matrix.os == 'macos-latest'
29+
run: brew install mercurial
2730
- name: Run nim c -r tester
2831
run: |
2932
cd tests

src/nimblepkg/asynctools/asyncproc.nim

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ else:
2424
const STILL_ACTIVE = 259
2525
import posix
2626

27-
when defined(linux):
28-
import linux
29-
3027
type
3128
ProcessOption* = enum ## options that can be passed `startProcess`
3229
poEchoCmd, ## echo the command before execution
@@ -510,7 +507,7 @@ else:
510507
result = cast[cstringArray](alloc0((counter + 1) * sizeof(cstring)))
511508
var i = 0
512509
for key, val in envPairs():
513-
var x = key.string & "=" & val.string
510+
var x = key & "=" & val
514511
result[i] = cast[cstring](alloc(x.len+1))
515512
copyMem(result[i], addr(x[0]), x.len+1)
516513
inc(i)
@@ -889,7 +886,17 @@ proc execProcess(command: string, args: seq[string] = @[],
889886
var data = newString(bufferSize)
890887
var p = startProcess(command, args = args, env = env, options = options)
891888

892-
let future = p.waitForExit()
889+
# Here the code path for Linux systems is a workaround for a bug eighter in
890+
# the `asynctools` library or the Nim standard library which causes `Resource
891+
# temporarily unavailable (code: 11)` error when executing multiple
892+
# asynchronous operations.
893+
#
894+
# TODO: Add a proper fix that does not use a different code ordering on the
895+
# different systems.
896+
897+
when not defined(linux):
898+
let future = p.waitForExit
899+
893900
while true:
894901
let res = await p.outputHandle.readInto(addr data[0], bufferSize)
895902
if res > 0:
@@ -898,12 +905,16 @@ proc execProcess(command: string, args: seq[string] = @[],
898905
data.setLen(bufferSize)
899906
else:
900907
break
901-
result.exitcode = await future
908+
909+
result.exitcode =
910+
when not defined(linux):
911+
await future
912+
else:
913+
await p.waitForExit
914+
902915
close(p)
903916

904917
when isMainModule:
905-
import os
906-
907918
when defined(windows):
908919
var data = waitFor(execProcess("cd"))
909920
else:

src/nimblepkg/tools.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ proc doCmd*(cmd: string) =
4747
[$exitCode, cmd, output])
4848

4949
proc doCmdEx*(cmd: string): ProcessOutput =
50+
displayDebug("Executing", cmd)
5051
let bin = extractBin(cmd)
5152
if findExe(bin) == "":
5253
raise nimbleError("'" & bin & "' not in PATH.")
@@ -57,6 +58,7 @@ proc removeQuotes(cmd: string): string =
5758

5859
proc doCmdExAsync*(cmd: string, args: seq[string] = @[]):
5960
Future[ProcessOutput] {.async.} =
61+
displayDebug("Executing", join(concat(@[cmd], args), " "))
6062
let bin = extractBin(cmd)
6163
if findExe(bin) == "":
6264
raise nimbleError("'" & bin & "' not in PATH.")

src/nimblepkg/vcstools.nim

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,15 @@ proc getBranchesOnWhichVcsRevisionIsPresent*(
410410

411411
let vcsType = path.getVcsType
412412
for line in output.strip.splitLines:
413-
var line = line.strip(chars = Whitespace + {'*'})
413+
var line = line.strip(chars = Whitespace + {'*', '\''})
414414
if vcsType == vcsTypeGit and branchType == btBoth:
415415
# For "git branch -a" remote branches are starting with "remotes" which
416416
# have to be removed for uniformity with "git branch -r".
417417
const prefix = "remotes/"
418418
if line.startsWith(prefix):
419419
line = line[prefix.len .. ^1]
420-
result.incl line
420+
if line.len > 0:
421+
result.incl line
421422

422423
proc isVcsRevisionPresentOnSomeBranch*(path: Path, vcsRevision: Sha1Hash):
423424
bool =
@@ -624,7 +625,7 @@ proc fastForwardMerge*(path: Path, remoteBranch, localBranch: string) =
624625
tryDoCmdEx(&"{git(path)} checkout {currentBranch}")
625626

626627
when isMainModule:
627-
import unittest, std/sha1, sequtils, os
628+
import unittest, std/sha1, sequtils
628629

629630
type
630631
NameToVcsRevision = OrderedTable[string, Sha1Hash]
@@ -635,11 +636,11 @@ when isMainModule:
635636
testGitDir = tempDir / "testGitDir"
636637
testHgDir = tempDir / "testHgDir"
637638
testNoVcsDir = tempDir / "testNoVcsDir"
638-
testSubDir = "./testSubDir"
639+
testSubDir = "testSubDir"
639640
testFile = "test.txt"
640641
testFile2 = "test2.txt"
641642
testFileContent = "This is a test file.\n"
642-
testSubDirFile = testSubDir / testFile
643+
testSubDirFile = &"{testSubDir}/{testFile}"
643644
testRemotes: seq[tuple[name, url: string]] = @[
644645
("origin", "testRemote1Dir"),
645646
("other", "testRemote2Dir"),
@@ -654,12 +655,20 @@ when isMainModule:
654655

655656
var nameToVcsRevision: NameToVcsRevision
656657

657-
proc getMercurialPathsDesc(): string =
658-
result = "[paths]\n"
658+
proc getMercurialRcFileContent(): string =
659+
result = """
660+
[ui]
661+
username = John Doe <[email protected]>
662+
[paths]
663+
"""
659664
for remote in testRemotes:
660-
result &= &"{remote.name} = {remote.url.absolutePath}\n"
665+
result &= &"{remote.name} = {testHgDir / remote.url}\n"
661666

662-
proc initRepo(vcsType: VcsType) = tryDoCmdEx(&"{vcsType} init")
667+
proc initRepo(vcsType: VcsType, url = ".") =
668+
tryDoCmdEx(&"{vcsType} init {url}")
669+
if vcsType == vcsTypeGit:
670+
tryDoCmdEx(&"git -C {url} config user.name \"John Doe\"")
671+
tryDoCmdEx(&"git -C {url} config user.email \"[email protected]\"")
663672

664673
proc collectFiles(files: varargs[string]): string =
665674
for file in files: result &= file & " "
@@ -688,13 +697,13 @@ when isMainModule:
688697
for remote in testRemotes:
689698
tryDoCmdEx(&"git remote add {remote.name} {remote.url}")
690699
of vcsTypeHg:
691-
writeFile(".hg/hgrc", getMercurialPathsDesc())
700+
writeFile(".hg/hgrc", getMercurialRcFileContent())
692701
of vcsTypeNone:
693702
assert false, "VCS type must not be 'vcsTypeNone'."
694703

695704
proc setupRemoteRepo(vcsType: VcsType, remoteUrl: string) =
696705
createDir remoteUrl
697-
tryDoCmdEx(&"{vcsType} init {remoteUrl}")
706+
initRepo(vcsType, remoteUrl)
698707
if vcsType == vcsTypeGit:
699708
cd remoteUrl:
700709
tryDoCmdEx("git config receive.denyCurrentBranch ignore")
@@ -726,6 +735,9 @@ when isMainModule:
726735
tryDoCmdEx(&"{vcsType} branch {branchName}")
727736

728737
proc pushToRemote(vcsType: VcsType, remoteName: string) =
738+
if vcsType == vcsTypeGit and remoteName == gitDefaultRemote:
739+
let branchName = getCurrentBranch(".")
740+
tryDoCmdEx(&"git push --set-upstream {remoteName} {branchName}")
729741
tryDoCmdEx(&"{vcsType} push {remoteName}")
730742

731743
proc pushToTestRemotes(vcsType: VcsType) =
@@ -1033,13 +1045,13 @@ when isMainModule:
10331045

10341046
suite "Mercurial":
10351047
suiteTestCode(vcsTypeHg, testHgDir):
1036-
(testHgDir / remote.url).absolutePath
1048+
testHgDir / remote.url
10371049

10381050
suite "no version control":
10391051
setupNoVcsSuite()
10401052

10411053
test "getVcsTypeAndSpecialDirPath":
1042-
const rootDir = when defined(windows): '\\' else: '/'
1054+
const rootDir = when defined(windows): ':' else: '/'
10431055
let (vcsType, specialDirPath) = getVcsTypeAndSpecialDirPath(testNoVcsDir)
10441056
check vcsType == vcsTypeNone
10451057
check ($specialDirPath)[^1] == rootDir
@@ -1048,6 +1060,7 @@ when isMainModule:
10481060
check not isValidSha1Hash($getVcsRevision(testNoVcsDir))
10491061

10501062
test "getPackageFileList":
1051-
check getPackageFileList(testNoVcsDir) == @[testFile, testSubDirFile]
1063+
check getPackageFileList(testNoVcsDir) ==
1064+
@[testFile, testSubDirFile.normalizedPath]
10521065

10531066
tearDownSuite(testNoVcsDir)

tests/develop/dependency/dependency.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import packagea
22

33
proc test*(): string =
4-
when defined(windows):
4+
when defined(windows) or defined(macosx):
55
$PackageA.test(6, 9)
66
elif defined(unix):
77
$packagea.test(6, 9)

tests/tester.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (C) Dominik Picheta. All rights reserved.
22
# BSD License. Look at license.txt for more info.
33

4-
# import suites
4+
import testscommon
5+
6+
# suits imports
7+
58
import tcheckcommand
69
import tdevelopfeature
710
import tissues

tests/testscommon.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (C) Dominik Picheta. All rights reserved.
22
# BSD License. Look at license.txt for more info.
33

4+
{.used.}
5+
46
import sequtils, strutils, strformat, os, osproc, sugar, unittest, macros,
57
std/sha1
68

tests/tlockfile.nim

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,18 @@ requires "nim >= 1.5.1"
119119
proc addRemote(remoteName, remoteUrl: string) =
120120
tryDoCmdEx(&"git remote add {remoteName} {remoteUrl}")
121121

122+
proc configUserAndEmail =
123+
tryDoCmdEx("git config user.name \"John Doe\"")
124+
tryDoCmdEx("git config user.email \"[email protected]\"")
125+
122126
proc initRepo(isBare = false) =
123127
let bare = if isBare: "--bare" else: ""
124128
tryDoCmdEx("git init " & bare)
129+
configUserAndEmail()
125130

126131
proc clone(urlFrom, pathTo: string) =
127132
tryDoCmdEx(&"git clone {urlFrom} {pathTo}")
133+
cd pathTo: configUserAndEmail()
128134

129135
proc branch(branchName: string) =
130136
tryDoCmdEx(&"git branch {branchName}")
@@ -183,7 +189,7 @@ requires "nim >= 1.5.1"
183189
else:
184190
check fileExists(lockFileName)
185191

186-
let (output, exitCode) = execNimbleYes("lock")
192+
let (output, exitCode) = execNimbleYes("lock", "--debug")
187193
check exitCode == QuitSuccess
188194

189195
var lines = output.processOutput
@@ -245,6 +251,13 @@ requires "nim >= 1.5.1"
245251

246252
let (output, exitCode) = execNimbleYes("lock")
247253
check exitCode == QuitFailure
254+
let mainPkgRepoPath =
255+
when defined(macosx):
256+
# This is a workaround for the added `/private` prefix to the main
257+
# repository Nimble file path when executing the test on macOS.
258+
"/private" / mainPkgRepoPath
259+
else:
260+
mainPkgRepoPath
248261
let errors = @[
249262
notInRequiredRangeMsg(dep1PkgName, dep1PkgRepoPath, "0.1.0",
250263
mainPkgName, mainPkgRepoPath, "> 0.1.0"),
@@ -266,7 +279,7 @@ requires "nim >= 1.5.1"
266279
(dep2PkgName, dep2PkgRepoPath)],
267280
isNew = true)
268281
removeDir installDir
269-
let (output, exitCode) = execNimbleYes("install")
282+
let (output, exitCode) = execNimbleYes("install", "--debug")
270283
check exitCode == QuitSuccess
271284
let lines = output.processOutput
272285
check lines.inLines(&"Downloading {dep1PkgOriginRepoPath} using git")

0 commit comments

Comments
 (0)