From c0b3c932ea6100e75c465669e585ccbc9790d679 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sat, 8 Mar 2025 16:34:45 +0000 Subject: [PATCH] ;run: --forecast with changed reporting interval fix (+test) [#2345] --- hledger/Hledger/Cli/Commands/Run.hs | 8 ++++---- hledger/test/run.test | 30 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Run.hs b/hledger/Hledger/Cli/Commands/Run.hs index 9d3a79ff6..31b740fec 100644 --- a/hledger/Hledger/Cli/Commands/Run.hs +++ b/hledger/Hledger/Cli/Commands/Run.hs @@ -43,6 +43,7 @@ import Hledger.Cli.DocFiles (runTldrForPage, runInfoForTopic, runManForTopic) import Hledger.Cli.Utils (journalTransform) import Text.Printf (printf) import System.Process (system) +import Data.Maybe (isJust) -- | Command line options for this command. runmode = hledgerCommandMode @@ -240,10 +241,9 @@ withJournalCached defaultJournalOverride cliopts cmd = do either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal where -- InputOptions contain reportspan_ that is used to calculare forecast period, - -- that is used by journalFinalise to insert forecast transactions.addHeaderBorders - -- For the purposes of caching, we want to ignore this, as it is only used for forecast - -- and it is sufficient to include forecast_ in the InputOptions that we use as a key. - ioptsWithoutReportSpan = iopts { reportspan_ = emptydatespan } + -- that is used by journalFinalise to insert forecast transactions. + -- For the purposes of caching, we want to ignore this when --forecast is not given. + ioptsWithoutReportSpan = if isJust (forecast_ iopts) then iopts else iopts { reportspan_ = emptydatespan } -- Read stdin, or if we read it alread, use a cache -- readStdin :: InputOpts -> ExceptT String IO Journal readStdin = do diff --git a/hledger/test/run.test b/hledger/test/run.test index 434ff7f91..7c05e8baf 100644 --- a/hledger/test/run.test +++ b/hledger/test/run.test @@ -164,3 +164,33 @@ readAndCacheJournalFile reading and caching -: readAndCacheJournalFile reading and caching sample.journal: readAndCacheJournalFile using cache for -: readAndCacheJournalFile using cache for sample.journal: + +# ** 12. Run commands with forecast properly honor reporting interval +< +2025-01-01 Buy beans + expenses:food $2 + assets -$2 + +~ every 14 days from 2025-01-01 Buy soylent green + expenses:food:not people $1 + assets -$1 +$ hledger run -f- -- echo "Till Feb" -- register --forecast -b 2025-01 -e 2025-02 -- echo "Till Mar" -- register --forecast -b 2025-01 -e 2025-03 +> +Till Feb +2025-01-01 Buy beans expenses:food $2 $2 + assets $-2 0 +2025-01-15 Buy soylent green ex:food:not people $1 $1 + assets $-1 0 +2025-01-29 Buy soylent green ex:food:not people $1 $1 + assets $-1 0 +Till Mar +2025-01-01 Buy beans expenses:food $2 $2 + assets $-2 0 +2025-01-15 Buy soylent green ex:food:not people $1 $1 + assets $-1 0 +2025-01-29 Buy soylent green ex:food:not people $1 $1 + assets $-1 0 +2025-02-12 Buy soylent green ex:food:not people $1 $1 + assets $-1 0 +2025-02-26 Buy soylent green ex:food:not people $1 $1 + assets $-1 0