Skip to content

Commit

Permalink
Merge #971
Browse files Browse the repository at this point in the history
971: tests: Fix ServerSpec and SqliteSpec on windows r=KtorZ a=rvl

Relates to #703.

# Overview

Windows unit test fixes.

-  Let ServerSpec error path tests pass.
-  Close temp file so that SqliteSpec passes.

# Comments

Cross-compiled tests are not yet automatically run under Haskell.nix with Wine (cc @angerman @hamishmack). So am testing with:
```
nix-env -f '<nixpkgs>' -iA pkgs.wineWowPackages.minimal
wine $(nix-build release.nix -A x86_64-pc-mingw32.tests.cardano-wallet-core.unit.x86_64-linux)/cardano-wallet-core-2019.11.6/unit.exe
```


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: KtorZ <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2019
2 parents 3797c0a + c8b1e58 commit 977ceff
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ import Data.Generics.Internal.VL.Lens
import Data.Generics.Labels
()
import Data.List
( isSubsequenceOf, sortOn )
( isInfixOf, isSubsequenceOf, sortOn )
import Data.Maybe
( fromMaybe, isJust )
import Data.Proxy
Expand Down Expand Up @@ -270,6 +270,7 @@ import System.IO.Error
, isAlreadyInUseError
, isDoesNotExistError
, isPermissionError
, isUserError
)
import System.Random
( getStdRandom, random )
Expand Down Expand Up @@ -371,21 +372,33 @@ ioToListenError hostPreference portOpt e
-- Usually caused by trying to listen on a privileged port
| isPermissionError e =
Just ListenErrorOperationNotPermitted
-- Bad hostname
-- Bad hostname -- Linux and Darwin
| isDoesNotExistError e =
Just (ListenErrorHostDoesNotExist hostPreference)
-- Bad hostname (bind: WSAEOPNOTSUPP) -- Windows
| isUserError e && hasDescription "10045" =
Just (ListenErrorHostDoesNotExist hostPreference)
-- Address is valid, but can't be used for listening -- Linux
| show (ioeGetErrorType e) == "invalid argument" =
Just (ListenErrorInvalidAddress hostPreference)
-- Address is valid, but can't be used for listening -- Darwin
| show (ioeGetErrorType e) == "unsupported operation" =
Just (ListenErrorInvalidAddress hostPreference)
-- Address is valid, but can't be used for listening -- Windows
| isOtherError e && hasDescription "WSAEINVAL" =
Just (ListenErrorInvalidAddress hostPreference)
-- Listening on an unavailable or privileged port -- Windows
| isOtherError e && hasDescription "WSAEACCESS" =
Just (ListenErrorAddressAlreadyInUse (listenPort portOpt))
| otherwise =
Nothing
where
listenPort (ListenOnPort port) = Just port
listenPort ListenOnRandomPort = Nothing

isOtherError ex = show (ioeGetErrorType ex) == "failed"
hasDescription text = text `isInfixOf` show e

{-------------------------------------------------------------------------------
Core API
-------------------------------------------------------------------------------}
Expand Down
3 changes: 3 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Network.Socket
( SockAddr (..), getSocketName, tupleToHostAddress )
import Test.Hspec
( Spec, describe, it, shouldBe, shouldReturn )
import Test.Utils.Windows
( skipOnWindows )

spec :: Spec
spec = describe "API Server" $ do
Expand Down Expand Up @@ -40,6 +42,7 @@ spec = describe "API Server" $ do

-- assuming we are not running the tests as root
it "handles privileged ports" $ do
skipOnWindows "Impossible to uniquely detect this error case"
withListeningSocket "127.0.0.1" (ListenOnPort 23) $ \res ->
res `shouldBe` Left ListenErrorOperationNotPermitted

Expand Down
5 changes: 4 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ import GHC.Conc
( TVar, atomically, newTVarIO, readTVarIO, writeTVar )
import System.Directory
( doesFileExist, removeFile )
import System.IO
( hClose )
import System.IO.Error
( isUserError )
import System.IO.Temp
Expand Down Expand Up @@ -550,7 +552,8 @@ withTestDBFile
withTestDBFile action expectations = do
logConfig <- defaultConfigTesting
trace <- setupTrace (Right logConfig) "connectionSpec"
withSystemTempFile "spec.db" $ \fp _handle -> do
withSystemTempFile "spec.db" $ \fp handle -> do
hClose handle
removeFile fp
withDBLayer logConfig trace (Just fp) action
expectations fp
Expand Down
3 changes: 3 additions & 0 deletions lib/test-utils/cardano-wallet-test-utils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ library
-Werror
build-depends:
base
, hspec-core
, hspec-expectations
, network
, QuickCheck
, random-shuffle
Expand All @@ -40,3 +42,4 @@ library
exposed-modules:
Test.Utils.Ports
Test.Utils.Time
Test.Utils.Windows
27 changes: 27 additions & 0 deletions lib/test-utils/src/Test/Utils/Windows.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE FlexibleContexts #-}

-- |
-- Copyright: © 2018-2019 IOHK
-- License: Apache-2.0
--
-- Utility function for making test suites pass on Windows.

module Test.Utils.Windows
( skipOnWindows
) where

import Prelude

import Control.Exception
( throwIO )
import Control.Monad
( when )
import System.Info
( os )
import Test.Hspec.Core.Spec
( ResultStatus (..) )
import Test.Hspec.Expectations
( Expectation, HasCallStack )

skipOnWindows :: HasCallStack => String -> Expectation
skipOnWindows _reason = when (os == "mingw32") $ throwIO Success
2 changes: 2 additions & 0 deletions nix/.stack.nix/cardano-wallet-test-utils.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 977ceff

Please sign in to comment.