|
1 | 1 |
|
2 | 2 | module Test.Sandwich.WebDriver.Internal.Binaries.Common where |
3 | 3 |
|
4 | | -import Control.Exception |
5 | 4 | import Control.Monad |
6 | 5 | import Control.Monad.IO.Class |
7 | 6 | import Control.Monad.IO.Unlift |
8 | 7 | import Control.Monad.Logger |
9 | 8 | import Data.String.Interpolate |
10 | 9 | import qualified Data.Text as T |
11 | | -import System.Directory |
12 | 10 | import System.Exit |
13 | 11 | import System.FilePath |
14 | | -import System.Process |
15 | 12 | import Test.Sandwich.Expectations |
16 | 13 | import Test.Sandwich.Logging |
| 14 | +import Test.Sandwich.Misc (HasBaseContextMonad) |
17 | 15 | import Test.Sandwich.WebDriver.Internal.Util |
| 16 | +import UnliftIO.Async |
| 17 | +import UnliftIO.Directory |
| 18 | +import UnliftIO.Exception |
| 19 | +import UnliftIO.Process |
18 | 20 | import UnliftIO.Temporary |
19 | 21 |
|
20 | 22 |
|
21 | | -downloadAndUnzipToPath :: (MonadUnliftIO m, MonadLogger m) => T.Text -> FilePath -> m (Either T.Text ()) |
| 23 | +downloadAndUnzipToPath :: (MonadUnliftIO m, MonadLogger m, HasBaseContextMonad context m) => T.Text -> FilePath -> m (Either T.Text ()) |
22 | 24 | downloadAndUnzipToPath downloadPath localPath = leftOnException' $ do |
23 | 25 | info [i|Downloading #{downloadPath} to #{localPath}|] |
24 | | - liftIO $ createDirectoryIfMissing True (takeDirectory localPath) |
| 26 | + createDirectoryIfMissing True (takeDirectory localPath) |
25 | 27 | withSystemTempDirectory "sandwich-webdriver-tool-download" $ \dir -> do |
26 | 28 | curlDownloadToPath (T.unpack downloadPath) (dir </> "temp.zip") |
27 | 29 |
|
28 | 30 | createProcessWithLogging ((proc "unzip" ["temp.zip", "-d", "unzipped"]) { cwd = Just dir }) |
29 | | - >>= liftIO . waitForProcess >>= (`shouldBe` ExitSuccess) |
| 31 | + >>= \(ps, asy) -> finally (waitForProcess ps >>= (`shouldBe` ExitSuccess)) |
| 32 | + (cancel asy) |
30 | 33 | let unzipped = dir </> "unzipped" |
31 | 34 |
|
32 | 35 | executables <- (filter (/= "") . T.splitOn "\n" . T.pack) <$> readCreateProcessWithLogging (proc "find" [unzipped, "-executable", "-type", "f"]) "" |
33 | 36 | case executables of |
34 | | - [] -> liftIO $ throwIO $ userError [i|No executable found in file downloaded from #{downloadPath}|] |
| 37 | + [] -> throwIO $ userError [i|No executable found in file downloaded from #{downloadPath}|] |
35 | 38 | [x] -> do |
36 | | - liftIO $ copyFile (T.unpack x) localPath |
| 39 | + copyFile (T.unpack x) localPath |
37 | 40 | createProcessWithLogging (shell [i|chmod u+x #{localPath}|]) |
38 | | - >>= liftIO . waitForProcess >>= (`shouldBe` ExitSuccess) |
39 | | - xs -> liftIO $ throwIO $ userError [i|Found multiple executable found in file downloaded from #{downloadPath}: #{xs}|] |
| 41 | + >>= \(ps, asy) -> finally (waitForProcess ps >>= (`shouldBe` ExitSuccess)) |
| 42 | + (cancel asy) |
| 43 | + xs -> throwIO $ userError [i|Found multiple executable found in file downloaded from #{downloadPath}: #{xs}|] |
40 | 44 |
|
41 | | -downloadAndUntarballToPath :: (MonadUnliftIO m, MonadLogger m) => T.Text -> FilePath -> m (Either T.Text ()) |
| 45 | +downloadAndUntarballToPath :: (MonadUnliftIO m, MonadLogger m, HasBaseContextMonad context m) => T.Text -> FilePath -> m (Either T.Text ()) |
42 | 46 | downloadAndUntarballToPath downloadPath localPath = leftOnException' $ do |
43 | 47 | info [i|Downloading #{downloadPath} to #{localPath}|] |
44 | | - liftIO $ createDirectoryIfMissing True (takeDirectory localPath) |
| 48 | + createDirectoryIfMissing True (takeDirectory localPath) |
45 | 49 | createProcessWithLogging (shell [i|wget -qO- #{downloadPath} | tar xvz -C #{takeDirectory localPath}|]) |
46 | | - >>= liftIO . waitForProcess >>= (`shouldBe` ExitSuccess) |
| 50 | + >>= \(ps, asy) -> finally (liftIO $ waitForProcess ps >>= (`shouldBe` ExitSuccess)) |
| 51 | + (cancel asy) |
47 | 52 | createProcessWithLogging (shell [i|chmod u+x #{localPath}|]) |
48 | | - >>= liftIO . waitForProcess >>= (`shouldBe` ExitSuccess) |
| 53 | + >>= \(ps, asy) -> finally (liftIO $ waitForProcess ps >>= (`shouldBe` ExitSuccess)) |
| 54 | + (cancel asy) |
49 | 55 |
|
50 | | -curlDownloadToPath :: (MonadUnliftIO m, MonadLogger m) => String -> FilePath -> m () |
| 56 | +curlDownloadToPath :: (MonadUnliftIO m, MonadLogger m, HasBaseContextMonad context m) => String -> FilePath -> m () |
51 | 57 | curlDownloadToPath downloadPath localPath = do |
52 | 58 | info [i|Downloading #{downloadPath} to #{localPath}|] |
53 | | - liftIO $ createDirectoryIfMissing True (takeDirectory localPath) |
54 | | - p <- createProcessWithLogging (proc "curl" [downloadPath, "-o", localPath, "-s"]) |
55 | | - liftIO (waitForProcess p) >>= (`shouldBe` ExitSuccess) |
| 59 | + createDirectoryIfMissing True (takeDirectory localPath) |
| 60 | + (p, asy) <- createProcessWithLogging (proc "curl" [downloadPath, "-o", localPath, "-s"]) |
| 61 | + finally (liftIO (waitForProcess p) >>= (`shouldBe` ExitSuccess)) |
| 62 | + (cancel asy) |
56 | 63 |
|
57 | 64 | unlessM :: Monad m => m Bool -> m () -> m () |
58 | 65 | unlessM b s = b >>= (\t -> unless t s) |
0 commit comments