Skip to content

Commit

Permalink
imp:check:recentassertions: improve the error message's suggestion
Browse files Browse the repository at this point in the history
The suggested sample balance assertion now uses the same commodity
symbol as in the failing posting (the first, if there are more than
one). Also the cleared mark has been removed.
  • Loading branch information
simonmichael committed Jul 13, 2023
1 parent 002773f commit ed456a3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
11 changes: 9 additions & 2 deletions hledger-lib/Hledger/Data/JournalChecks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Hledger.Data.JournalChecks.Ordereddates
import Hledger.Data.JournalChecks.Uniqueleafnames
import Hledger.Data.Posting (isVirtual, postingDate, transactionAllTags)
import Hledger.Data.Types
import Hledger.Data.Amount (amountIsZero, amountsRaw, missingamt)
import Hledger.Data.Amount (amountIsZero, amountsRaw, missingamt, amounts)
import Hledger.Data.Transaction (transactionPayee, showTransactionLineFirstPart, partitionAndCheckConversionPostings)
import Data.Time (Day, diffDays)
import Hledger.Utils
Expand Down Expand Up @@ -239,6 +239,11 @@ findRecentAssertionError today ps = do
let lag = diffDays (postingDate firsterrorp) latestassertdate
let acct = paccount firsterrorp
let (f,l,_mcols,ex) = makePostingAccountErrorExcerpt firsterrorp
let comm =
case map acommodity $ amounts $ pamount firsterrorp of
[] -> ""
(t:_) | T.length t == 1 -> t
(t:_) -> t <> " "
Just $ chomp $ printf
(unlines [
"%s:%d:",
Expand All @@ -250,7 +255,7 @@ findRecentAssertionError today ps = do
"",
"Consider adding a more recent balance assertion for this account. Eg:",
"",
"%s *\n %s $0 = $0 ; (adjust asserted amount)"
"%s\n %s %s0 = %s0 ; (adjust asserted amount)"
])
f
l
Expand All @@ -261,6 +266,8 @@ findRecentAssertionError today ps = do
(showDate latestassertdate)
(show today)
acct
comm
comm

-- -- | Print the last balance assertion date & status of all accounts with balance assertions.
-- printAccountLastAssertions :: Day -> [BalanceAssertionInfo] -> IO ()
Expand Down
2 changes: 1 addition & 1 deletion hledger/test/errors/recentassertions.j
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env -S hledger check recentassertions -f
# Postings more than 7 days after latest balance assertion.

2022-01-01 *
a 0 = 0

2022-01-08 good
a 0

# More than 7 days after latest balance assertion, fails this check.
2022-01-09 bad1
a 0

Expand Down
1 change: 1 addition & 0 deletions hledger/test/errors/recentassertions.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 1. The first posting more than 7 days after latest balance assertion causes this check to fail.
$ hledger check recentassertions -f recentassertions.j
>2 /Error: .*recentassertions.j:11:
| 2022-01-09 bad1
Expand Down

0 comments on commit ed456a3

Please sign in to comment.