[#163] Add tests: commit msgs. and branch names (#187)

Resolves #163
This commit is contained in:
Veronika Romashkina 2020-07-07 13:20:46 +01:00 committed by GitHub
parent 7082774b7f
commit e6939b5c81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 15 deletions

View File

@ -119,6 +119,7 @@ test-suite hit-on-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
other-modules: Test.Hit.Names
build-depends: text
, hspec ^>= 2.7.1

View File

@ -14,6 +14,11 @@ All functionality related to the branch creation and manipulation.
module Hit.Git.Branch
( runNew
, runRename
-- * Branch naming helpers
, BranchDescription (..)
, assignAndDisplayBranchDescription
, mkBranchDescription
) where
import Data.Char (isAlphaNum, isDigit, isSpace)
@ -95,7 +100,7 @@ mkBranchName doCreateIssue issueOrName = do
login <- getUsername
maybeIssue <- if doCreateIssue then tryCreateNewIssue login else pure Nothing
let branchDescription = mkBranchDescription maybeIssue issueOrName
title <- assignAndDisplayBranchDescription login branchDescription
title <- assignAndDisplayBranchDescription True login branchDescription
pure $ login <> "/" <> title
where
tryCreateNewIssue :: Text -> IO (Maybe IssueNumber)
@ -134,8 +139,8 @@ mkBranchDescription Nothing issueOrName = case readMaybe @Int $ toString issueOr
Just issueNum -> FromIssueNumber issueNum
Nothing -> FromText issueOrName
{- | Assigns the user to the issue if applicable (it current design, if the issue
already exists and user creates the branch for it: 'FromIssueNumber').
{- | Assigns the user to the issue if applicable (in the current design, if the
issue already exists and user creates the branch for it: 'FromIssueNumber').
Displays 'BranchDescription' in format:
@ -143,14 +148,19 @@ Displays 'BranchDescription' in format:
123-short-issue-title
@
-}
assignAndDisplayBranchDescription :: Text -> BranchDescription -> IO Text
assignAndDisplayBranchDescription username = \case
assignAndDisplayBranchDescription
:: Bool -- ^ To assign the given user to the issue?
-> Text -- ^ User name
-> BranchDescription
-> IO Text
assignAndDisplayBranchDescription doAssign username = \case
FromText text -> pure $ mkShortDesc text
FromNewIssue issueNum issueTitle -> pure $ nameWithNumber issueNum issueTitle
FromIssueNumber issueNum -> do
issue <- fetchIssue $ mkIssueId issueNum
assignIssue issue username
showIssueLink issue
when doAssign $ do
assignIssue issue username
showIssueLink issue
pure $ nameWithNumber issueNum $ issueTitle issue
where
nameWithNumber :: Int -> Text -> Text

View File

@ -11,6 +11,9 @@ Portability : Portable
module Hit.Git.Commit
( runCommit
-- * Commit message helpers
, toCommitMessage
) where
import Colourista (errorMessage)
@ -48,18 +51,20 @@ runCommit CommitOptions{..} = case coName of
commitCmds :: Text -> Maybe Int -> IO ()
commitCmds msg issueNum = do
"git" ["add", "."]
"git" ["commit", "-m", showMsg msg $ guard hasIssue *> issueNum]
"git" ["commit", "-m", toCommitMessage hasIssue msg issueNum]
when (coPush || coIsForcePush == Force) $ runPush coIsForcePush
getCurrentIssue :: IO (Maybe Int)
getCurrentIssue = issueFromBranch <$> getCurrentBranch
showMsg :: Text -> Maybe Int -> Text
showMsg (stripRfc -> msg) = \case
Nothing -> msg
Just n ->
let issue = "#" <> show n
in "[" <> issue <> "] " <> msg <> "\n\nResolves " <> issue
hasIssue :: Bool
hasIssue = not coNoIssueNumber
toCommitMessage :: Bool -> Text -> Maybe Int -> Text
toCommitMessage hasIssue (stripRfc -> msg) issueNum
| not hasIssue = msg
| otherwise = case issueNum of
Nothing -> msg
Just n ->
let issue = "#" <> show n
in "[" <> issue <> "] " <> msg <> "\n\nResolves " <> issue

View File

@ -4,10 +4,12 @@ import GitHub.Data.Name (Name (..))
import Test.Hspec (describe, hspec, it, shouldBe)
import Hit.GitHub (parseOwnerRepo)
import Test.Hit.Names (namesSpec)
main :: IO ()
main = hspec $ do
namesSpec
describe "parseOwnerRepo" $ do
let expectedOwnerName = N "kowainik"
let expectedRepoName = N "hit-on"

54
test/Test/Hit/Names.hs Normal file
View File

@ -0,0 +1,54 @@
module Test.Hit.Names
( namesSpec
) where
import GitHub (IssueNumber (..))
import Test.Hspec (Expectation, Spec, describe, it, shouldBe)
import Hit.Git.Branch (assignAndDisplayBranchDescription, mkBranchDescription)
import Hit.Git.Commit (toCommitMessage)
namesSpec :: Spec
namesSpec = describe "Names for branches and commit messages" $ do
branchNamesSpec
commitMessagesSpec
branchNamesSpec :: Spec
branchNamesSpec = describe "Branch naming" $ do
describe "Existing issue as argument" $ do
it "from issue with RFC" $
checkName Nothing "117" "117-hit-go-for-branch-switching"
it "from issue with dots and symbols" $
checkName Nothing "163" "163-Add-tests-commit-msgs-and"
it "from issue with preserved special symbols" $
checkName Nothing "175" "175-Update-header_information/-in-the-library"
describe "Title without issue" $ do
it "Simple title" $
checkName Nothing "this-should-be-branch-name" "this-should-be-branch-name"
it "Simple title with spaces" $
checkName Nothing "this should be branch name" "this-should-be-branch-name"
it "Simple title (more than 5 words)" $
checkName Nothing "this-should-be-branch-name-and-not-this" "this-should-be-branch-name-and-not-this"
it "Simple title with spaces (more than 5 words)" $
checkName Nothing "this should be branch name and not this" "this-should-be-branch-name"
describe "Newly created issues" $ do
it "from IssueNumber and title" $
checkName (Just $ IssueNumber 100000) "I am a new issue" "100000-I-am-a-new-issue"
it "from IssueNumber and title (more than 5 words)" $
checkName (Just $ IssueNumber 100000) "I am a new issue in here" "100000-I-am-a-new-issue"
where
checkName :: Maybe IssueNumber -> Text -> Text -> Expectation
checkName mIssue title expectedName =
assignAndDisplayBranchDescription False "no-user-name" (mkBranchDescription mIssue title) >>= \n ->
n `shouldBe` expectedName
commitMessagesSpec :: Spec
commitMessagesSpec = describe "Commit messages naming" $ do
it "with issue and 'resolve' addition" $
toCommitMessage True "Commit message" (Just 42) `shouldBe` "[#42] Commit message\n\nResolves #42"
it "with issue and without 'resolve' addition" $
toCommitMessage False "Commit message" (Just 42) `shouldBe` "Commit message"
it "without issue" $
toCommitMessage True "Commit message" Nothing `shouldBe` "Commit message"