From 8bdb28a991dba3ffeacd9b22743f924f14ecace4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 10 Nov 2020 08:40:08 -0800 Subject: [PATCH] csv, journal: infer style from first amount, as documented, not last This has been broken since hledger 1.12 (!) --- hledger-lib/Hledger/Read/Common.hs | 22 ++++++++++------------ tests/journal/amounts-and-commodities.test | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index adbcbbb60..6a9072ff2 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -312,21 +312,19 @@ journalFinalise :: InputOpts -> FilePath -> Text -> ParsedJournal -> ExceptT Str journalFinalise InputOpts{auto_,ignore_assertions_,commoditystyles_} f txt pj = do t <- liftIO getClockTime d <- liftIO getCurrentDay - -- Set any global commodity styles that have been provided via InputOpts - let pj' = pj{jglobalcommoditystyles=fromMaybe M.empty commoditystyles_} + let pj' = + pj{jglobalcommoditystyles=fromMaybe M.empty commoditystyles_} -- save any global commodity styles + & journalAddFile (f, txt) -- save the main file's info + & journalSetLastReadTime t -- save the last read time + & journalReverse -- convert all lists to the order they were parsed + -- Infer and apply canonical styles for each commodity (or throw an error). -- This affects transaction balancing/assertions/assignments, so needs to be done early. - -- (TODO: since #903's refactoring for hledger 1.12, - -- journalApplyCommodityStyles here is seeing the - -- transactions before they get reversesd to normal order.) case journalApplyCommodityStyles pj' of - Left e -> throwError e - Right pj' -> either throwError return $ - pj' - & journalAddFile (f, txt) -- save the file path and content - & journalSetLastReadTime t -- save the last read time - & journalReverse -- convert all lists to parse order - & (if not auto_ || null (jtxnmodifiers pj) + Left e -> throwError e + Right pj'' -> either throwError return $ + pj'' + & (if not auto_ || null (jtxnmodifiers pj') then -- Auto postings are not active. -- Balance all transactions and maybe check balance assertions. diff --git a/tests/journal/amounts-and-commodities.test b/tests/journal/amounts-and-commodities.test index cab81295f..3268139d8 100644 --- a/tests/journal/amounts-and-commodities.test +++ b/tests/journal/amounts-and-commodities.test @@ -178,3 +178,20 @@ $ hledger -f- bal a # 2000-01-01 # a $-10,000 # b $10,000,000 + +# 13. A commodity's display style should come from the first-parsed amount +< +2020-01-01 + (a) 1.00 $ + +2020-01-02 + (b) $2,00 + +$ hledger -f - print +2020-01-01 + (a) 1.00 $ + +2020-01-02 + (b) 2.00 $ + +>=