Skip to content

Make cabal init conform with --no-comments flag for commented fields #7770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions cabal-install/src/Distribution/Client/Init/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ fieldD
-> WriteOpts
-> PrettyField FieldAnnotation
fieldD fieldName fieldContents fieldComments includeField opts
| fieldContents == empty =
-- If there is no content, optionally produce a commented out field.
fieldSEmptyContents $ CommentBefore fieldComments
| otherwise =
-- If the "--no-comments" or "--minimal" flag is set, strip comments.
let comments
| isMinimal = []
| hasNoComments = []
| otherwise = fieldComments

-- If the "--minimal" flag is set, strip comments.
in fieldSWithContents comments
-- If the "--no-comments" or "--minimal" flag is set, strip comments.
| hasNoComments || isMinimal = contents NoComment
| otherwise = contents $ commentPositionFor fieldName fieldComments
where
commentPositionFor fn
| fn == "cabal-version" = CommentAfter
Expand All @@ -84,6 +75,11 @@ fieldD fieldName fieldContents fieldComments includeField opts
isMinimal = _optMinimal opts
hasNoComments = _optNoComments opts

contents
-- If there is no content, optionally produce a commented out field.
| fieldContents == empty = fieldSEmptyContents
| otherwise = fieldSWithContents

fieldSEmptyContents cs
| not includeField || isMinimal = PrettyEmpty
| otherwise = PrettyField
Expand All @@ -92,7 +88,7 @@ fieldD fieldName fieldContents fieldComments includeField opts
empty

fieldSWithContents cs =
PrettyField (withComments . commentPositionFor fieldName $ map ("-- " ++) cs) fieldName fieldContents
PrettyField (withComments cs) fieldName fieldContents


-- | A field annotation instructing the pretty printer to comment out the field
Expand All @@ -104,7 +100,9 @@ commentedOutWithComments NoComment = FieldAnnotation True NoComment

-- | A field annotation with the specified comment lines.
withComments :: CommentPosition -> FieldAnnotation
withComments = FieldAnnotation False
withComments (CommentBefore cs) = FieldAnnotation False . CommentBefore $ map ("-- " ++) cs
withComments (CommentAfter cs) = FieldAnnotation False . CommentAfter $ map ("-- " ++) cs
withComments NoComment = FieldAnnotation False NoComment

-- | A field annotation with no comments.
annNoComments :: FieldAnnotation
Expand Down
19 changes: 10 additions & 9 deletions cabal-install/tests/UnitTests/Distribution/Client/Init/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ goldenExeTests
-> PackageName
-> TestTree
goldenExeTests v pkgIx pkgDir pkgName = testGroup "exe golden tests"
[ goldenVsString "Empty flags, not simple, no options"
(goldenExe "exe.golden") $
[ goldenVsString "Empty flags, not simple, no options, no comments"
(goldenExe "exe-no-comments.golden") $
let opts = WriteOpts False False True v pkgDir Executable pkgName defaultCabalVersion
in runGoldenExe opts exeArgs emptyFlags

Expand Down Expand Up @@ -148,12 +148,13 @@ goldenLibTests
-> PackageName
-> TestTree
goldenLibTests v pkgIx pkgDir pkgName = testGroup "lib golden tests"
[ goldenVsString "Empty flags, not simple, no options"
(goldenLib "lib.golden") $
[ goldenVsString "Empty flags, not simple, no options, no comments"
(goldenLib "lib-no-comments.golden") $
let opts = WriteOpts False False True v pkgDir Library pkgName defaultCabalVersion
in runGoldenLib opts libArgs emptyFlags

, goldenVsString "Empty flags, simple, no options" (goldenLib "lib-simple.golden") $
, goldenVsString "Empty flags, simple, no options, no comments"
(goldenLib "lib-simple-no-comments.golden") $
let opts = WriteOpts False False True v pkgDir Library pkgName defaultCabalVersion
in runGoldenLib opts libArgs emptyFlags

Expand Down Expand Up @@ -190,8 +191,8 @@ goldenTestTests
-> PackageName
-> TestTree
goldenTestTests v pkgIx pkgDir pkgName = testGroup "test golden tests"
[ goldenVsString "Empty flags, not simple, no options"
(goldenTest "test.golden") $
[ goldenVsString "Empty flags, not simple, no options, no comments"
(goldenTest "test-no-comments.golden") $
let opts = WriteOpts False False True v pkgDir Library pkgName defaultCabalVersion
in runGoldenTest opts testArgs emptyFlags

Expand All @@ -215,8 +216,8 @@ goldenTestTests v pkgIx pkgDir pkgName = testGroup "test golden tests"
let opts = WriteOpts False False False v pkgDir Library pkgName defaultCabalVersion
in runGoldenTest opts testArgs (emptyFlags {buildTools = Flag ["happy"]})

, goldenVsString "Standalone tests, empty flags, not simple, no options"
(goldenTest "standalone-test.golden") $
, goldenVsString "Standalone tests, empty flags, not simple, no options, no comments"
(goldenTest "standalone-test-no-comments.golden") $
let opts = WriteOpts False False True v pkgDir TestSuite pkgName defaultCabalVersion
in runGoldenTest opts testArgs emptyFlags

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ cabal-version: 3.0
name: y
version: 0.1.0.0
synopsis: synopsis

-- A longer description of the package.
-- description:
homepage: home
license: BSD-3-Clause
license-file: LICENSE
author: foo-kmett
maintainer: [email protected]

-- A copyright notice.
-- copyright:
category: Data
build-type: Simple
extra-doc-files: CHANGELOG.md

-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:

common warnings
Expand All @@ -26,11 +20,7 @@ common warnings
library
import: warnings
exposed-modules: MyLib

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base
hs-source-dirs: src
Expand All @@ -39,11 +29,7 @@ library
executable y
import: warnings
main-is: Main.hs

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends:
base,
Expand All @@ -55,11 +41,7 @@ executable y
test-suite y-test
import: warnings
default-language: Haskell2010

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ cabal-version: 3.0
name: y
version: 0.1.0.0
synopsis: synopsis

-- A longer description of the package.
-- description:
homepage: home
license: BSD-3-Clause
license-file: LICENSE
author: foo-kmett
maintainer: [email protected]

-- A copyright notice.
-- copyright:
category: Data
build-type: Simple
extra-doc-files: CHANGELOG.md

-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:

common warnings
Expand All @@ -26,11 +20,7 @@ common warnings
library
import: warnings
exposed-modules: MyLib

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base
hs-source-dirs: src
Expand All @@ -39,11 +29,7 @@ library
test-suite y-test
import: warnings
default-language: Haskell2010

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ cabal-version: 3.0
name: y
version: 0.1.0.0
synopsis: synopsis

-- A longer description of the package.
-- description:
homepage: home
license: BSD-3-Clause
license-file: LICENSE
author: foo-kmett
maintainer: [email protected]

-- A copyright notice.
-- copyright:
category: Data
build-type: Simple
extra-doc-files: CHANGELOG.md

-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
-- extra-source-files:

common warnings
Expand All @@ -26,11 +20,7 @@ common warnings
test-suite y-test
import: warnings
default-language: Haskell2010

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
executable y
import: warnings
main-is: Main.hs

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base
hs-source-dirs: exe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
library
import: warnings
exposed-modules: MyLib

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base
hs-source-dirs: src
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
library
import: warnings
exposed-modules: MyLib

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base
hs-source-dirs: src
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
test-suite y-test
import: warnings
default-language: Haskell2010

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
test-suite y-test
import: warnings
default-language: Haskell2010

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down