From 6511b9883e81b92d544c02260f8192936f37f067 Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Tue, 1 Sep 2020 13:49:24 +0200 Subject: [PATCH 1/3] yaml parser, load empty files --- lib/buchungsstreber/parser/yaml_timesheet.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/buchungsstreber/parser/yaml_timesheet.rb b/lib/buchungsstreber/parser/yaml_timesheet.rb index a2f6508..f348757 100644 --- a/lib/buchungsstreber/parser/yaml_timesheet.rb +++ b/lib/buchungsstreber/parser/yaml_timesheet.rb @@ -14,7 +14,12 @@ def self.parses?(file) end def parse(file_path) - timesheet = YAML.load_file(file_path) + timesheet = + if File.size(file_path) == 0 + {} + else + YAML.load_file(file_path) + end throw 'invalid line: file should contain map' unless timesheet.is_a?(Hash) result = [] From 388d071894a14bbfa6c4702b35878708e740f560 Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Tue, 1 Sep 2020 13:49:53 +0200 Subject: [PATCH 2/3] yaml parser, add format to be able to fill a file via generate --- lib/buchungsstreber/parser/yaml_timesheet.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/buchungsstreber/parser/yaml_timesheet.rb b/lib/buchungsstreber/parser/yaml_timesheet.rb index f348757..5489c16 100644 --- a/lib/buchungsstreber/parser/yaml_timesheet.rb +++ b/lib/buchungsstreber/parser/yaml_timesheet.rb @@ -49,6 +49,19 @@ def archive(file_path, archive_path, date) File.write(file_path, "#{next_monday}:\n\n\n---\n# Letzte Woche\n" + old_timesheet) end + def format(entries) + buf = "" + days = entries.group_by {|e| e[:date] }.to_a.sort_by { |x| x[0] } + days.each do |date, day| + buf << "#{date}:\n" + day.each do |e| + buf << " # #{e[:comment]}\n" if e[:comment] + buf << " - #{qarter_time(e[:time] || 0.0)}\t#{e[:activity]}\t#{e[:redmine]}#{e[:issue]}\t#{e[:text]}\n" + end + end + buf + end + private def parse_entry(entry, date) From 509ce9dd92bc83db9934ba10f76a90965eb157de Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Tue, 1 Sep 2020 13:50:07 +0200 Subject: [PATCH 3/3] Add test for generating time entries --- spec/timesheet_examples.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/timesheet_examples.rb b/spec/timesheet_examples.rb index 8c7de3a..76c80a0 100644 --- a/spec/timesheet_examples.rb +++ b/spec/timesheet_examples.rb @@ -1,3 +1,5 @@ +require 'tempfile' + require 'buchungsstreber/parser' RSpec.shared_examples 'a timesheet parser' do |extension, templates| @@ -39,4 +41,29 @@ expect { described_class.new(templates || {}).parse("spec/examples/invalid_time#{extension}") }.to raise_exception(/invalid time/) end end + + context 'generating' do + it 'can render time entries' do + e = [{ issue: '1234', activity: 'Dev', text: 'asdf', time: 0.5, date: Date.parse('1970-01-01') }] + expect(described_class.new(templates || {}).format(e)).to include('asdf') + end + + it 'produces something the same parser can parse' do + parser = described_class.new(templates || {}) + e1 = [{ issue: '1234', activity: 'Dev', text: 'asdf', time: 0.5, date: Date.parse('1970-01-01') }] + str = parser.format(e1) + + file = Tempfile.new('foo') + begin + file.write(str) + file.close + e2 = parser.parse(file.path) + e1[0].each do |k,v| + expect(e2[0][k]).to eq(v) + end + ensure + file.unlink + end + end + end end