Skip to content

Commit

Permalink
Increase purview of --dry-run and --only-download flags (haskell#7379)
Browse files Browse the repository at this point in the history
Changes the behavior of the following commands under the `--dry-run` and
`--only-download` flags
- `v2-configure` and `v2-freeze` do not write their respective files
- `v2-exec` and `v2-run` do not run the target executable

Adds package tests for the configure and freeze commands.
  • Loading branch information
aaronallen8455 committed Jun 3, 2021
1 parent bd713cd commit 043e809
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 28 deletions.
27 changes: 16 additions & 11 deletions cabal-install/src/Distribution/Client/CmdConfigure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ configureAction flags@NixStyleFlags {..} _extraArgs globalFlags = do
if exists
then firstFreeBackup' (i + 1)
else return backup

-- If cabal.project.local already exists, back up to cabal.project.local~[n]
exists <- doesFileExist localFile
when exists $ do
backup <- firstFreeBackup
notice verbosity $
quote (takeFileName localFile) <> " already exists, backing it up to "
<> quote (takeFileName backup) <> "."
copyFile localFile backup
writeProjectLocalExtraConfig (distDirLayout baseCtx)
cliConfig
dryRun = buildSettingDryRun (buildSettings baseCtx)
|| buildSettingOnlyDownload (buildSettings baseCtx)

if dryRun
then notice verbosity "Config file not written due to flag(s)."
else do
-- If cabal.project.local already exists, back up to cabal.project.local~[n]
exists <- doesFileExist localFile
when exists $ do
backup <- firstFreeBackup
notice verbosity $
quote (takeFileName localFile) <> " already exists, backing it up to "
<> quote (takeFileName backup) <> "."
copyFile localFile backup
writeProjectLocalExtraConfig (distDirLayout baseCtx)
cliConfig

buildCtx <-
runProjectPreBuildPhase verbosity baseCtx $ \elaboratedPlan ->
Expand Down
9 changes: 8 additions & 1 deletion cabal-install/src/Distribution/Client/CmdExec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Distribution.Client.ProjectOrchestration
, distDirLayout
, commandLineFlagsToProjectConfig
, ProjectBaseContext(..)
, BuildTimeSettings(..)
)
import Distribution.Client.ProjectPlanOutput
( updatePostBuildProjectStatus
Expand Down Expand Up @@ -81,6 +82,7 @@ import Distribution.Simple.Utils
, createDirectoryIfMissingVerbose
, withTempDirectory
, wrapText
, notice
)
import Distribution.Verbosity
( normal
Expand Down Expand Up @@ -185,7 +187,12 @@ execAction flags@NixStyleFlags {..} extraArgs globalFlags = do
argOverrides'
program
invocation = programInvocation program' args
runProgramInvocation verbosity invocation
dryRun = buildSettingDryRun (buildSettings baseCtx)
|| buildSettingOnlyDownload (buildSettings baseCtx)

if dryRun
then notice verbosity "Running of executable suppressed by flag(s)"
else runProgramInvocation verbosity invocation
where
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
cliConfig = commandLineFlagsToProjectConfig globalFlags flags
Expand Down
17 changes: 11 additions & 6 deletions cabal-install/src/Distribution/Client/CmdFreeze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ freezeAction flags@NixStyleFlags {..} extraArgs globalFlags = do
distDirLayout,
cabalDirLayout,
projectConfig,
localPackages
localPackages,
buildSettings
} <- establishProjectBaseContext verbosity cliConfig OtherCommand

(_, elaboratedPlan, _, totalIndexState, activeRepos) <-
Expand All @@ -116,17 +117,21 @@ freezeAction flags@NixStyleFlags {..} extraArgs globalFlags = do
localPackages

let freezeConfig = projectFreezeConfig elaboratedPlan totalIndexState activeRepos
writeProjectLocalFreezeConfig distDirLayout freezeConfig
notice verbosity $
"Wrote freeze file: " ++ distProjectFile distDirLayout "freeze"
dryRun = buildSettingDryRun buildSettings
|| buildSettingOnlyDownload buildSettings

if dryRun
then notice verbosity "Freeze file not written due to flag(s)"
else do
writeProjectLocalFreezeConfig distDirLayout freezeConfig
notice verbosity $
"Wrote freeze file: " ++ distProjectFile distDirLayout "freeze"

where
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
cliConfig = commandLineFlagsToProjectConfig globalFlags flags
mempty -- ClientInstallFlags, not needed here



-- | Given the install plan, produce a config value with constraints that
-- freezes the versions of packages used in the plan.
--
Expand Down
26 changes: 16 additions & 10 deletions cabal-install/src/Distribution/Client/CmdRun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import Distribution.CabalSpecVersion (CabalSpecVersion (..), cabalSpecLatest)
import Distribution.Verbosity
( normal )
import Distribution.Simple.Utils
( wrapText, warn, die', info
( wrapText, warn, die', info, notice
, createTempDirectory, handleDoesNotExist )
import Distribution.Client.ProjectConfig
( ProjectConfig(..), ProjectConfigShared(..)
Expand Down Expand Up @@ -283,15 +283,21 @@ runAction flags@NixStyleFlags {..} targetStrings globalFlags = do
exeName
</> exeName
let args = drop 1 targetStrings
runProgramInvocation
verbosity
emptyProgramInvocation {
progInvokePath = exePath,
progInvokeArgs = args,
progInvokeEnv = dataDirsEnvironmentForPlan
(distDirLayout baseCtx)
elaboratedPlan
}
dryRun = buildSettingDryRun (buildSettings baseCtx)
|| buildSettingOnlyDownload (buildSettings baseCtx)

if dryRun
then notice verbosity "Running of executable suppressed by flag(s)"
else
runProgramInvocation
verbosity
emptyProgramInvocation {
progInvokePath = exePath,
progInvokeArgs = args,
progInvokeEnv = dataDirsEnvironmentForPlan
(distDirLayout baseCtx)
elaboratedPlan
}

handleDoesNotExist () (removeDirectoryRecursive tmpDir)
where
Expand Down
1 change: 1 addition & 0 deletions cabal-install/src/Distribution/Client/ProjectConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Distribution.Client.ProjectConfig (
readProjectConfig,
readGlobalConfig,
readProjectLocalFreezeConfig,
showProjectConfig,
withProjectOrGlobalConfig,
writeProjectLocalExtraConfig,
writeProjectLocalFreezeConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: ConfigFile
version: 0.1.0.0
author: Foo Bar
maintainer: cabal-dev@haskell.org
build-type: Simple
cabal-version: >=1.10

2 changes: 2 additions & 0 deletions cabal-testsuite/PackageTests/NewConfigure/ConfigFile/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
15 changes: 15 additions & 0 deletions cabal-testsuite/PackageTests/NewConfigure/ConfigFile/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# cabal v2-configure
Config file not written due to flag(s).
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- ConfigFile-0.1.0.0 (first run)
# cabal v2-configure
Config file not written due to flag(s).
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- ConfigFile-0.1.0.0 (first run)
# cabal v2-configure
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- ConfigFile-0.1.0.0 (first run)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
18 changes: 18 additions & 0 deletions cabal-testsuite/PackageTests/NewConfigure/ConfigFile/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Test.Cabal.Prelude

-- Test that 'cabal v2-configure' generates the config file appropriately
main = withShorterPathForNewBuildStore $ \storeDir ->
cabalTest . withSourceCopy $ do
cwd <- fmap testCurrentDir getTestEnv
let configFile = cwd </> "cabal.project.local"

shouldNotExist configFile

-- should not create config file with --dry-run or --only-download
cabalG ["--store-dir=" ++ storeDir] "v2-configure" ["--dry-run"]
cabalG ["--store-dir=" ++ storeDir] "v2-configure" ["--only-download"]
shouldNotExist configFile

-- should create the config file
cabalG ["--store-dir=" ++ storeDir] "v2-configure" []
shouldExist configFile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ In order, the following would be built:
- my-library-dep-2.0 (lib) (requires build)
- my-local-package-1.0 (exe:my-exe) (first run)
# cabal v2-freeze
Freeze file not written due to flag(s)
# cabal v2-freeze
Freeze file not written due to flag(s)
# cabal v2-freeze
Resolving dependencies...
Wrote freeze file: <ROOT>/new_freeze.dist/source/cabal.project.freeze
# cabal v2-build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ main = withShorterPathForNewBuildStore $ \storeDir ->
-- v2-build should choose the latest version for the dependency.
cabalG' ["--store-dir=" ++ storeDir] "v2-build" ["--dry-run"] >>= assertUsesLatestDependency

-- should not create freeze file with --dry-run or --only-download flags
cabalG' ["--store-dir=" ++ storeDir] "v2-freeze" ["--dry-run"]
cabalG' ["--store-dir=" ++ storeDir] "v2-freeze" ["--only-download"]
shouldNotExist freezeFile

-- Freeze a dependency on the older version.
cabalG ["--store-dir=" ++ storeDir] "v2-freeze" ["--constraint=my-library-dep==1.0"]

Expand Down
4 changes: 4 additions & 0 deletions changelog.d/pr-7407
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
synopsis: --dry-run and --only-download effect v2-configure, v2-freeze, v2-run, and v2-exec
pr: #7407
issues: #7379
decription: { v2-configure, v2-freeze, v2-run, and v2-exec now behave expectedly under the --dry-run and --only-download flags }

0 comments on commit 043e809

Please sign in to comment.