From a00dc04e0df1028ca4c3732e780d8069c88922c7 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 24 Apr 2022 18:51:47 -1000 Subject: [PATCH] imp: check: uniqueleafnames: use the standard error format (#1436) Hledger.Read.Common: export makePostingErrorExcerpt --- hledger-lib/Hledger/Read/Common.hs | 1 + .../Cli/Commands/Check/Uniqueleafnames.hs | 29 ++++++++++++------- hledger/test/check-uniqueleafnames.test | 2 +- hledger/test/errors/uniqueleafnames.test | 13 ++++----- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 971eb8a84..a10dd973e 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -113,6 +113,7 @@ module Hledger.Read.Common ( skipNonNewlineSpaces1, aliasesFromOpts, makeTransactionErrorExcerpt, + makePostingErrorExcerpt, -- * tests tests_Common, diff --git a/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs b/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs index 8e66a3641..66baace69 100755 --- a/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs +++ b/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs @@ -12,6 +12,7 @@ import Data.Text (Text) import qualified Data.Text as T import Hledger import Text.Printf (printf) +import Data.Maybe (fromMaybe) -- | Check that all the journal's postings are to accounts with a unique leaf name. -- Otherwise, return an error message for the first offending posting. @@ -38,16 +39,22 @@ journalLeafAndFullAccountNames = map leafAndAccountName . journalAccountNamesUse where leafAndAccountName a = (accountLeafName a, a) checkposting :: [(Text,[AccountName])] -> Posting -> Either String () -checkposting leafandfullnames Posting{paccount,ptransaction} = - case [lf | lf@(_,fs) <- leafandfullnames, paccount `elem` fs] of +checkposting leafandfullnames p@Posting{paccount=a} = + case [lf | lf@(_,fs) <- leafandfullnames, a `elem` fs] of [] -> Right () (leaf,fulls):_ -> Left $ printf - "account leaf names are not unique\nleaf name \"%s\" appears in account names: %s%s" - leaf - (T.intercalate ", " $ map (("\""<>).(<>"\"")) fulls) - (case ptransaction of - Nothing -> "" - Just t -> printf "\nseen in \"%s\" in transaction at: %s\n\n%s" - paccount - (sourcePosPairPretty $ tsourcepos t) - (linesPrepend "> " . (<>"\n") . textChomp $ showTransaction t) :: String) + "%s:%d:%d-%d:\n%saccount leaf name \"%s\" is not unique\nit is used in account names: %s" + f l col col2 ex leaf accts + where + -- t = fromMaybe nulltransaction ptransaction -- XXX sloppy + col = maybe 0 fst mcols + col2 = maybe 0 (fromMaybe 0 . snd) mcols + (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols + where + finderrcols p _ _ = Just (col, Just col2) + where + alen = T.length $ paccount p + llen = T.length $ accountLeafName a + col = 5 + (if isVirtual p then 1 else 0) + alen - llen + col2 = col + llen - 1 + accts = T.intercalate ", " $ map (("\""<>).(<>"\"")) fulls diff --git a/hledger/test/check-uniqueleafnames.test b/hledger/test/check-uniqueleafnames.test index 41d6cc373..1165f77e2 100644 --- a/hledger/test/check-uniqueleafnames.test +++ b/hledger/test/check-uniqueleafnames.test @@ -11,5 +11,5 @@ $ hledger -f- check uniqueleafnames (a) 1 (b:a) 1 $ hledger -f- check uniqueleafnames ->2 /account leaf names are not unique/ +>2 /account leaf name .* is not unique/ >=1 diff --git a/hledger/test/errors/uniqueleafnames.test b/hledger/test/errors/uniqueleafnames.test index f9c41a6ed..7020eb974 100644 --- a/hledger/test/errors/uniqueleafnames.test +++ b/hledger/test/errors/uniqueleafnames.test @@ -1,10 +1,9 @@ $$$ hledger check uniqueleafnames -f uniqueleafnames.j >>>2 -hledger: Error: account leaf names are not unique -leaf name "c" appears in account names: "a:c", "b:c" -seen in "a:c" in transaction at: /Users/simon/src/hledger/hledger/test/errors/uniqueleafnames.j:8-9 - -> 2022-01-01 p -> (a:c) 1 - +hledger: Error: /Users/simon/src/hledger/hledger/test/errors/uniqueleafnames.j:9:8-8: + | 2022-01-01 p +9 | (a:c) 1 + | ^ +account leaf name "c" is not unique +it is used in account names: "a:c", "b:c" >>>=1