encapsule-0.6.1: test/CompletionSpec.hs
-- SPDX-License-Identifier: Apache-2.0
module CompletionSpec (spec) where
import Control.Exception (bracket)
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import System.Directory (canonicalizePath, findExecutable, removeDirectoryRecursive)
import System.Environment (lookupEnv, setEnv, unsetEnv)
import System.FilePath ((</>))
import System.Posix.Files (setFileMode)
import System.Posix.Temp (mkdtemp)
import Test.Hspec
import EncapsuleTest (encapsuleChecked)
spec :: Spec
spec = describe "image completion" $ do
it "completes local names and tags once, filtering dangling images and prefixes" $
withPodman (Just imageScript) $
mapM_ (\subcmd -> do
complete [subcmd, ""] `shouldReturn`
["alpine:latest", "dev:latest", "dev:v1"]
complete [subcmd, "dev:"] `shouldReturn`
["dev:latest", "dev:v1"]
complete [subcmd, "unknown"] `shouldReturn` []) ["run", "create"]
it "completes the image after options and stops suggesting images for commands" $
withPodman (Just imageScript) $
mapM_ (\subcmd -> do
complete [subcmd, "--user", "root", "alp"] `shouldReturn`
["alpine:latest"]
complete [subcmd, "alpine:latest", "alp"] `shouldReturn` [])
["run", "create"]
it "strips registries and repository paths, and deduplicates shortened names" $
withPodman (Just $ unlines
[ "#!/bin/sh"
, "echo registry.example:5000/team/app:v1"
, "echo quay.io/team/app:v1"
, "echo quay.io/other/nested/app:v1"
, "echo '[::1]:5000/team/app:v2'"
, "echo team/app:v3"
, "echo standalone:latest"
]) $
mapM_ (\subcmd -> do
complete [subcmd, ""] `shouldReturn`
["app:v1", "app:v2", "app:v3", "standalone:latest"]
complete [subcmd, "app:"] `shouldReturn`
["app:v1", "app:v2", "app:v3"])
["run", "create"]
it "returns no suggestions or diagnostics when Podman fails" $
withPodman (Just "#!/bin/sh\necho storage-error >&2\necho partial:latest\nexit 1\n") $
mapM_ (\subcmd -> complete [subcmd, ""] `shouldReturn` []) ["run", "create"]
it "removes one leading encapsule prefix while preserving other occurrences and tags" $
withPodman (Just $ unlines
[ "#!/bin/sh"
, "echo localhost/encapsule-fedora-toolbox:45"
, "echo localhost/fedora-toolbox:45"
, "echo localhost/my-encapsule-dev:latest"
, "echo localhost/encapsule-encapsule-dev:encapsule-tag"
]) $
mapM_ (\subcmd -> do
complete [subcmd, ""] `shouldReturn`
["encapsule-dev:encapsule-tag", "fedora-toolbox:45", "my-encapsule-dev:latest"]
complete [subcmd, "fedora"] `shouldReturn` ["fedora-toolbox:45"])
["run", "create"]
it "returns no suggestions when Podman is missing" $
withPodman Nothing $
mapM_ (\subcmd -> complete [subcmd, ""] `shouldReturn` []) ["run", "create"]
imageScript :: String
imageScript = unlines
[ "#!/bin/sh"
, "[ \"$#\" = 3 ] && [ \"$1\" = images ] && [ \"$2\" = --format ] && [ \"$3\" = '{{.Repository}}:{{.Tag}}' ] || exit 1"
, "echo ignored-warning >&2"
, "echo localhost/encapsule-dev:v1"
, "echo docker.io/library/alpine:latest"
, "echo localhost/encapsule-dev:latest"
, "echo localhost/encapsule-dev:v1"
, "echo '<none>:<none>'"
, "echo 'localhost/untagged:<none>'"
, "echo"
]
complete :: [String] -> IO [String]
complete words' =
filter (not . isPrefixOf "-") . lines <$> encapsuleChecked
(["--bash-completion-index", show (length words')] ++
concatMap (\word -> ["--bash-completion-word", word]) ("encapsule" : words'))
-- Resolve encapsule before restricting PATH, so even the missing-Podman case
-- works on hosts where Podman is installed.
withPodman :: Maybe String -> IO a -> IO a
withPodman script action = do
name <- fromMaybe "encapsule" <$> lookupEnv "ENCAPSULE"
exe <- findExecutable name >>= maybe (fail "encapsule executable not found") canonicalizePath
bracket (mkdtemp "/tmp/encapsule-completion-test-XXXXXX") removeDirectoryRecursive $ \dir -> do
case script of
Nothing -> return ()
Just contents -> do
writeFile (dir </> "podman") contents
setFileMode (dir </> "podman") 0o700
withEnv "ENCAPSULE" exe $ withEnv "PATH" dir action
withEnv :: String -> String -> IO a -> IO a
withEnv key value action =
bracket (lookupEnv key) restore $ \_ -> setEnv key value >> action
where
restore Nothing = unsetEnv key
restore (Just old) = setEnv key old