{-# LANGUAGE OverloadedStrings #-} module GitHub.Types.ActionsLockSpec where import qualified Data.Map.Strict as Map import qualified Data.Text as Text import qualified Data.Text.Encoding as Text import Data.Yaml (decodeEither') import GitHub.Types.ActionsLock import Test.Hspec -- | A combined lock as `gh actions-lock` writes it, with a composite action. combinedLock :: Text.Text combinedLock = Text.unlines [ "# This file is machine-generated by `gh actions-lock`." , "# Do not edit by hand; run `gh actions-lock` to update." , "# Docs: https://gh.io/actions-lockfile" , "version: 'v0.0.3'" , "workflows:" , " '.github/workflows/ci.yml':" , " - 'actions/checkout@v6'" , " 'website/.github/workflows/draft.yml': []" , " 'website/.github/workflows/pages.yml':" , " - 'actions/upload-pages-artifact@v3'" , "dependencies:" , " 'actions/checkout@v6':" , " ref: 'v6'" , " commit: 'sha1-d23441a48e516b6c34aea4fa41551a30e30af803'" , " owner_id: 44036562" , " repo_id: 197814629" , " 'actions/upload-artifact@v4':" , " ref: 'v4'" , " commit: 'sha1-ea165f8d65b6e75b540449e92b4886f43607fa02'" , " owner_id: 44036562" , " repo_id: 192625955" , " 'actions/upload-pages-artifact@v3':" , " ref: 'v3'" , " commit: 'sha1-56afc609e74202658d3ffba0e8f6dda462b719fa'" , " owner_id: 44036562" , " repo_id: 496012378" , " uses:" , " - 'actions/upload-artifact@v4'" ] parse :: Text.Text -> ActionsLock parse = either (error . show) id . decodeEither' . Text.encodeUtf8 spec :: Spec spec = do describe "renderLock" $ it "writes a lock exactly as gh actions-lock does" $ renderLock (parse combinedLock) `shouldBe` combinedLock describe "actionKey" $ do it "names an action by its repository, lowercased, and ref" $ do actionKey "actions/checkout@v6" `shouldBe` Just "actions/checkout@v6" actionKey "google/clusterfuzzlite/actions/run_fuzzers@v1" `shouldBe` Just "google/clusterfuzzlite@v1" actionKey "SonarSource/sonarqube-scan-action@v6" `shouldBe` Just "sonarsource/sonarqube-scan-action@v6" it "doesn't lock local actions or containers" $ do actionKey "./.github/actions/setup" `shouldBe` Nothing actionKey "docker://alpine:3.20" `shouldBe` Nothing describe "repoLock" $ do it "takes a repository's workflows and what they need" $ repoLock "website" (parse combinedLock) `shouldBe` ActionsLock { lockWorkflows = Map.fromList [ (".github/workflows/draft.yml", []) , (".github/workflows/pages.yml", ["actions/upload-pages-artifact@v3"]) ] , lockDependencies = Map.delete "actions/checkout@v6" $ lockDependencies (parse combinedLock) } it "gives the workspace only its own workflows" $ Map.keys (lockWorkflows (repoLock "." (parse combinedLock))) `shouldBe` [".github/workflows/ci.yml"]