Skip to content

Commit

Permalink
Add ledger-date-format option to specify ledger file date format.
Browse files Browse the repository at this point in the history
Fixes #111
  • Loading branch information
benprew committed Aug 8, 2021
1 parent abbe948 commit 05e045a
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 32 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ Learn more:
--encoding 'UTF-8'
Specify an encoding for the CSV file
-c, --currency '$' Currency symbol to use - default $ (ex £, EUR)
--date-format '%d/%m/%Y'
Force the date format (see Ruby DateTime strftime)
--date-format FORMAT
CSV file date format (see `date` for format)
--ledger-date-format FORMAT
Ledger date format (see `date` for format)
-u, --unattended Don't ask questions and guess all the accounts automatically. Use with --learn-from or --account-tokens options.
-t, --account-tokens FILE YAML file with manually-assigned tokens for each account (see README)
--table-output-file FILE
--default-into-account NAME
Default into account
--default-outof-account NAME
Expand Down
5 changes: 3 additions & 2 deletions lib/reckon/date_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module Reckon
class DateColumn < Array
attr_accessor :endian_precedence
def initialize( arr = [], options = {} )
@options = options
arr.each do |value|
if options[:date_format]
begin
value = Date.strptime(value, options[:date_format])
rescue
puts "I'm having trouble parsing #{value} with the desired format: #{options[:date_format]}"
puts "I'm having trouble parsing '#{value}' with the desired format: #{options[:date_format]}"
exit 1
end
else
Expand Down Expand Up @@ -53,7 +54,7 @@ def pretty_for(index)
date = self.for(index)
return "" if date.nil?

date.iso8601
date.strftime(@options[:ledger_date_format] || '%Y-%m-%d')
end

def self.likelihood(entry)
Expand Down
2 changes: 1 addition & 1 deletion lib/reckon/ledger_parser.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class LedgerParser

def initialize(ledger, options = {})
@options = options
@date_format = options[:date_format] || '%Y-%m-%d'
@date_format = options[:ledger_date_format] || options[:date_format] || '%Y-%m-%d'
parse(ledger)
end

Expand Down
6 changes: 5 additions & 1 deletion lib/reckon/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ def self.parse(args = ARGV, stdin = $stdin)
options[:currency] = e
end

opts.on("", "--date-format '%d/%m/%Y'", "Force the date format (see Ruby DateTime strftime)") do |d|
opts.on("", "--date-format FORMAT", "CSV file date format (see `date` for format)") do |d|
options[:date_format] = d
end

opts.on("", "--ledger-date-format FORMAT", "Ledger date format (see `date` for format)") do |d|
options[:ledger_date_format] = d
end

opts.on("-u", "--unattended", "Don't ask questions and guess all the accounts automatically. Use with --learn-from or --account-tokens options.") do |n|
options[:unattended] = n
end
Expand Down
1 change: 1 addition & 0 deletions spec/integration/ledger_date_format/compare_cmds
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ledger --input-date-format '%d/%m/%Y'
3 changes: 3 additions & 0 deletions spec/integration/ledger_date_format/input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
02/12/2009,Check - 0000000122,122,-$76.00,"","$1,750.06"
02/12/2009,BLARG R SH 456930,"","",+$327.49,"$1,826.06"
02/12/2009,Check - 0000000112,112,-$800.00,"","$1,498.57"
12 changes: 12 additions & 0 deletions spec/integration/ledger_date_format/output.ledger
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
02/12/2009 BLARG R SH 456930; $1,826.06
Assets:Bank:Checking $327.49
Income:Unknown

02/12/2009 Check - 0000000122; 122; $1,750.06
Income:Unknown
Assets:Bank:Checking -$76.00

02/12/2009 Check - 0000000112; 112; $1,498.57
Income:Unknown
Assets:Bank:Checking -$800.00

1 change: 1 addition & 0 deletions spec/integration/ledger_date_format/test_args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-f input.csv --unattended --account Assets:Bank:Checking --date-format '%d/%m/%Y' --ledger-date-format '%d/%m/%Y'
62 changes: 36 additions & 26 deletions spec/integration/test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

# set -x

set -Euo pipefail


Expand All @@ -23,25 +21,27 @@ main () {

echo > test.log

NUM_TESTS=$(echo "$TESTS" |wc -l)
NUM_TESTS=$(echo "$TESTS" |wc -l |awk '{print $1}')

echo "1..$NUM_TESTS"

I=1

for t in $TESTS; do
TEST_DIR=$(dirname "$t")
TEST_LOG=$(mktemp)
pushd "$TEST_DIR" >/dev/null || exit 1
if [[ -e "cli_input.exp" ]]; then
cli_test
cli_test >$TEST_LOG 2>&1
else
unattended_test
unattended_test >$TEST_LOG 2>&1
fi

popd >/dev/null || exit 1
# have to save output after popd
echo -e "\n\n======>$TEST_DIR" >> test.log
echo -e "TEST_CMD\n$TEST_LOG" >> test.log
echo -e "TEST_CMD: $TEST_CMD" >> test.log
cat $TEST_LOG >> test.log

if [[ $ERROR -ne 0 ]]; then
echo -e "not ok $I - $TEST_DIR"
Expand All @@ -56,11 +56,11 @@ main () {

cli_test () {
OUTPUT_FILE=$(mktemp)
TEST_CMD="$RECKON_CMD --table-output-file $OUTPUT_FILE $(cat test_args)"
TEST_CMD="expect -d -c 'spawn $TEST_CMD' cli_input.exp"
TEST_LOG=$(eval "$TEST_CMD" 2>&1)
CLI_CMD="$RECKON_CMD --table-output-file $OUTPUT_FILE $(cat test_args)"
TEST_CMD="expect -d -c 'spawn $CLI_CMD' cli_input.exp"
eval $TEST_CMD 2>&1
ERROR=0
TEST_DIFF=$(diff -u "$OUTPUT_FILE" "expected_output")
TEST_DIFF=$(diff -u "$OUTPUT_FILE" expected_output)

# ${#} is character length, test that there was no output from diff
if [ ${#TEST_DIFF} -eq 0 ]; then
Expand All @@ -73,7 +73,7 @@ cli_test () {
unattended_test() {
OUTPUT_FILE=$(mktemp)
TEST_CMD="$RECKON_CMD -o $OUTPUT_FILE $(cat test_args)"
TEST_LOG=$(eval "$TEST_CMD" 2>&1)
eval $TEST_CMD 2>&1
ERROR=0

compare_output "$OUTPUT_FILE"
Expand All @@ -87,15 +87,38 @@ test_fail () {
fi
}

compare_output () {
OUTPUT_FILE=$1
pwd
if [[ -e compare_cmds ]]; then
COMPARE_CMDS=$(cat compare_cmds)
else
COMPARE_CMDS=$'ledger\nhledger'
fi

ERROR=1
while IFS= read -r n; do
if compare_output_for "$OUTPUT_FILE" "$n"; then
ERROR=0
else
ERROR=1
break
fi
done <<< "$COMPARE_CMDS"
}

compare_output_for () {
OUTPUT_FILE=$1
LEDGER=$2

EXPECTED_FILE=$(mktemp)
ACTUAL_FILE=$(mktemp)

$LEDGER -f output.ledger r >"$EXPECTED_FILE" 2>&1 || return 1
$LEDGER -f output.ledger r >"$ACTUAL_FILE" 2>&1 || return 1
echo $LEDGER -f output.ledger r "$EXPECTED_FILE"
eval $LEDGER -f output.ledger r >"$EXPECTED_FILE" || return 1

echo $LEDGER -f output.ledger r "$ACTUAL_FILE"
eval $LEDGER -f output.ledger r >"$ACTUAL_FILE" || return 1

TEST_DIFF=$(diff -u "$EXPECTED_FILE" "$ACTUAL_FILE")

Expand All @@ -107,17 +130,4 @@ compare_output_for () {
fi
}

compare_output () {
OUTPUT_FILE=$1

for n in {ledger,hledger}; do
if compare_output_for "$OUTPUT_FILE" "$n"; then
ERROR=0
else
ERROR=1
return 0
fi
done
}

main "$@"
12 changes: 12 additions & 0 deletions spec/reckon/date_column_spec.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@
.to eq(Date.new(2013, 2, 1))
end
end

describe "#pretty_for" do
it 'should use ledger_date_format' do
expect(Reckon::DateColumn.new(%w[13/02/2013], {ledger_date_format: '%d/%m/%Y'}).pretty_for(0))
.to eq('13/02/2013')
end

it 'should default to is' do
expect(Reckon::DateColumn.new(%w[13/12/2013]).pretty_for(0))
.to eq('2013-12-13')
end
end
end

0 comments on commit 05e045a

Please sign in to comment.