Can't write to pen

I am writing a chat server in Haskell. My executable has the following code:

data Client = Client {idx :: Int, hClient :: Handle} deriving (Show, Eq)

tellClient :: String -> Client -> IO ()
tellClient = flip $ hPutStrLn . hClient

askClient :: Client -> IO (String)
askClient = hGetLine . hClient

      

which I expect to use to write and read from a file. I am writing tests against this code using HSpec. I have the following function to create temporary objects Client

(which rely on the "temporary") library:

withTempClient :: Int -> (Client -> IO a) -> IO a
withTempClient cIdx f = withTempFile "tests/generated" "clientFile.tmp" axn
  where axn _ handle = f $ Client cIdx handle

      

I use this to test the above code like this:

main = hspec $  do
  describe "client IO" $ do
    it "can ask and tell" $ withTempClient 1 $ \c -> do
      tellClient "hello" c
      hFlush $ hClient c
      askClient c `shouldReturn` "hello"

      

But the test fails with the following error:

   uncaught exception: IOException of type EOF (test/generated
   /clientFile11587.tmp: hGetLine: end of file)

      

I thought that maybe I was doing something wrong in withTempClient

, so I added the following test case:

it "opens a handle corectly" $ withTempClient 1 $ \(Client _ h) -> do
  hIsOpen h `shouldReturn` True
  hIsWritable h `shouldReturn` True

      

but that got through, so I'm not sure what the problem is. What am I missing?

+3


source to share


1 answer


When writing to a file, the OS keeps track of the position you are in the specified file. You are getting an error EOF

because you are at the end of the file (it was empty to start with and everything you wrote is before the current position).

To fix this, you need to use hSeek

to move yourself at the beginning of the file like this:



hSeek (hClient c) AbsoluteSeek 0

      

For more information on searching, see cf. this bit is the real world haskell .

+4


source







All Articles