diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index aa479bfa9..556b3feb9 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -314,14 +314,8 @@ journalFinalise iopts@InputOpts{auto_,infer_equity_,balancingopts_,strict_} f tx & journalReverse -- convert all lists to the order they were parsed where checkAddAndBalance d j = do - when strict_ $ do - -- If in strict mode, check all postings are to declared accounts - journalCheckAccountsDeclared j - -- and using declared commodities - journalCheckCommoditiesDeclared j - -- Add forecast transactions if enabled - journalAddForecast (forecastPeriod iopts j) j + newj <- journalAddForecast (forecastPeriod iopts j) j -- Add auto postings if enabled & (if auto_ && not (null $ jtxnmodifiers j) then journalAddAutoPostings d balancingopts_ else pure) -- Balance all transactions and maybe check balance assertions. @@ -331,6 +325,14 @@ journalFinalise iopts@InputOpts{auto_,infer_equity_,balancingopts_,strict_} f tx -- infer market prices from commodity-exchanging transactions <&> journalInferMarketPricesFromTransactions + when strict_ $ do + -- If in strict mode, check all postings are to declared accounts + journalCheckAccountsDeclared newj + -- and using declared commodities + journalCheckCommoditiesDeclared newj + + return newj + journalAddAutoPostings :: Day -> BalancingOpts -> Journal -> Either String Journal journalAddAutoPostings d bopts = -- Balance all transactions without checking balance assertions, diff --git a/hledger/test/check-accounts.test b/hledger/test/check-accounts.test index 8f84a18fb..3b74c1703 100644 --- a/hledger/test/check-accounts.test +++ b/hledger/test/check-accounts.test @@ -1,14 +1,49 @@ -# check accounts succeeds when all accounts are declared +# 1. check accounts succeeds when all accounts are declared < account a 2020-01-01 (a) 1 $ hledger -f- check accounts -# and otherwise fails: +# 2. and otherwise fails: < 2020-01-01 (a) 1 $ hledger -f- check accounts >2 /undeclared account "a"/ >=1 + +# 3. also fails for forecast accounts +< +account a +~ 2022-01-31 + a $1 + b +$ hledger -f- --forecast check accounts +>2 /undeclared account "b"/ +>=1 + +# 4. also fails in --strict mode +$ hledger -f- --forecast --strict bal +>2 /undeclared account "b"/ +>=1 + +# 5. also fails for auto accounts +< +account a + += a + (b) $1 + +2022-01-31 + (a) $1 + +2022-02-01 +$ hledger -f- --auto check accounts +>2 /undeclared account "b"/ +>=1 + +# 6. also fails in --strict mode +$ hledger -f- --auto --strict bal +>2 /undeclared account "b"/ +>=1