hledger/tests/journal/parse-errors.test
Simon Michael b1f3880c3d lib: drop the file format auto-detection feature
For a long time hledger has auto-detected the file format when it's
not known, eg when reading from a file with unusual extension (like
.dat or .txt), or from standard input (-f-), or when using the include
directive (which currently ignores file extensions).

Auto-detecting has been done by trying all readers until one succeeds.
This could guess wrong in some cases, but it was so rare that it has
been working fine.

Recently, more conveniences have been added to timedot format,
increasing its overlap with journal format, which makes this kind of
auto-detection unreliable.

Auto-detection and auto-detection failures are (probably) still pretty
rare in practice. But when it does happen it's confusing, giving
misleading errors or false successes (eg printing timedot entries
instead of a journal error).

For predictability and to minimise confusion, hledger no longer tries
to guess; when there's no file extension or reader prefix, it assumes
journal format. To specify one of the other formats, you must use a
standard file extension (.timeclock, .timedot, .csv, .ssv, .tsv), or a
reader prefix (-f csv:foo.txt, -f timedot:-).

For now, the include directive still tries to autodetect
(journal/timeclock/timedot), and this can't be overridden; it will be
fixed later.

Experimental; testing and feedback welcome.
2020-03-01 14:06:29 -08:00

78 lines
1.6 KiB
Plaintext

# Testing parse error messages for accuracy and usefulness.
# A journal with an incomplete date.
<
2018
# XXX depends on the executable being named "hledger" (prepended by GHC.Err.error)
# 1.
$ hledger -f - print
>2
hledger: -:1:5:
|
1 | 2018
| ^
unexpected newline
expecting date separator or digit
>=1
# A journal with an unbalanced transaction.
<
2018/1/1
a 1
# 2. When read from stdin with no reader prefix, the journal reader is used,
# and fails here. (Before 1.17, all readers were tried and the timedot reader
# would succeed.)
$ hledger -f - print
>2 /could not balance/
>=1
# 3. So in these tests we must sometimes force the desired format, like so.
# Now we see the error from the journal reader.
$ hledger -f journal:- print
>2 /could not balance this transaction \(real postings are off by 1\)/
>=1
# 4. We expect to have reference to line number with last posting
$ hledger -f journal:- print
>2 /\<2\>/
>=1
# A posting without two spaces between account and amount.
<
2018/1/1
(a) 1
# 5. hledger doesn't detect this as an error directly, it parses account name "(a) 1" and
# amount 0 here.
$ hledger -f - print -x
2018-01-01
(a) 1 0
>=
# 6. Two (or more) postings with implicit amount cannot be balanced.
<
2018/1/1
a 1
b
c
$ hledger -f journal:- print
>2 /could not balance this transaction - can't have more than one real posting with no amount/
>=1
# 7. Two (or more) virtual postings with implicit amount cannot be balanced.
# (And the error message contains line numbers).
<
2018/1/1
[a] 1
[b]
[c]
$ hledger -f journal:- print
>2 /lines 1-4/
>=1