Skip to content

Commit

Permalink
cabal init -i should autodetect author name and maintainer email (#8267)
Browse files Browse the repository at this point in the history
* cabal init -i should autodetect author name and maintainer email (fix #8255)

Name and email are fetched from git config in the non-interactive init
already, and we plug that utility into the interactive one in this commit.

Testing becomes clunkier: the list of inputs have to hold an additional
string that will be used as the return value of
`Interactive.readProcessWithExitCode`, and here is why. Recall that this
method is used to call `git config` to perform autodetection. In the
real scenario (the `IO` instance of `Interactive`), it actually calls
out to `git`. In the testing scenario (the `PurePrompt` instance of
`Interactive`), the method simply returns the next element of the list
of pre-defined inputs. So, the list of inputs to every relevant test has
to be adjusted.

* Add test checking that username/email can be autodetected
  • Loading branch information
ulysses4ever authored Jul 8, 2022
1 parent 2e9af5b commit 1b8bf8c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Distribution.Client.Init.FlagExtractors
import Distribution.Client.Init.Prompt
import Distribution.Client.Init.Types
import Distribution.Client.Init.Utils
import Distribution.Client.Init.NonInteractive.Heuristics (guessAuthorName, guessAuthorEmail)
import Distribution.FieldGrammar.Newtypes (SpecLicense(..))
import Distribution.Simple.Setup (Flag(..), fromFlagOrDefault)
import Distribution.Simple.PackageIndex (InstalledPackageIndex)
Expand Down Expand Up @@ -355,12 +356,14 @@ licensePrompt flags = getLicense flags $ do
else fmap prettyShow knownLicenses

authorPrompt :: Interactive m => InitFlags -> m String
authorPrompt flags = getAuthor flags $
promptStr "Author name" OptionalPrompt
authorPrompt flags = getAuthor flags $ do
name <- guessAuthorName
promptStr "Author name" (DefaultPrompt name)

emailPrompt :: Interactive m => InitFlags -> m String
emailPrompt flags = getEmail flags $
promptStr "Maintainer email" OptionalPrompt
emailPrompt flags = getEmail flags $ do
email' <- guessAuthorEmail
promptStr "Maintainer email" (DefaultPrompt email')

homepagePrompt :: Interactive m => InitFlags -> m String
homepagePrompt flags = getHomepage flags $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ pkgArgs = fromList
, "y"
, "0.1.0.0"
, "2"
, "git username"
, "foo-kmett"
, "git email"
, "foo-kmett@kmett.kmett"
, "home"
, "synopsis"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -249,8 +251,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -338,8 +342,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -414,8 +420,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -504,8 +512,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -579,8 +589,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -660,8 +672,10 @@ createProjectTest pkgIx srcDb = testGroup "createProject tests"
-- license
, "3"
-- author
, "git username"
, "Foobar"
-- email
, "git email"
, "foobar@qux.com"
-- homepage
, "qux.com"
Expand Down Expand Up @@ -728,7 +742,9 @@ fileCreatorTests pkgIx srcDb _pkgName = testGroup "generators"
, "y" -- "yes to prompt internal to package name"
, "0.2.0.1" -- package version
, "2" -- pick the second license in the list
, "git username" -- name guessed by calling "git config user.name"
, "Foobar" -- author name
, "git email" -- email guessed by calling "git config user.email"
, "foobar@qux.com" -- maintainer email
, "qux.com" -- package homepage
, "Qux's package" -- package synopsis
Expand Down Expand Up @@ -834,10 +850,14 @@ interactiveTests srcDb = testGroup "Check top level getter functions"
, testSimplePrompt "2" synopsisPrompt
"Resistance is futile, you will be assimilated" ["Resistance is futile, you will be assimilated"]
]
, testSimplePrompt "Check authorPrompt output" authorPrompt
"Foobar" ["Foobar"]
, testSimplePrompt "Check emailPrompt output" emailPrompt
"foobar@qux.com" ["foobar@qux.com"]
, testSimplePrompt "Check authorPrompt output (name supplied by the user)" authorPrompt
"Foobar" ["git username", "Foobar"]
, testSimplePrompt "Check authorPrompt output (name guessed from git config)" authorPrompt
"git username" ["git username", ""]
, testSimplePrompt "Check emailPrompt output (email supplied by the user)" emailPrompt
"foobar@qux.com" ["git email", "foobar@qux.com"]
, testSimplePrompt "Check emailPrompt output (email guessed from git config)" emailPrompt
"git@email" ["git@email", ""]
, testSimplePrompt "Check homepagePrompt output" homepagePrompt
"qux.com" ["qux.com"]
, testSimplePrompt "Check testDirsPrompt output" testDirsPrompt
Expand Down
4 changes: 4 additions & 0 deletions changelog.d/issue-8255
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
synopsis: cabal init -i should autodetect author name and maintainer email (fix #8255)
packages: cabal-install
issues: #8255
prs: #8267

0 comments on commit 1b8bf8c

Please sign in to comment.