2021-08-03 20:58:26 -05:00
|
|
|
use std::convert::TryFrom;
|
2021-08-25 14:30:27 -05:00
|
|
|
use std::io::{BufRead, Write};
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
|
|
|
use crate::database::{Database, DBVersion};
|
|
|
|
use crate::error::{Error, Result};
|
2021-08-25 14:30:27 -05:00
|
|
|
use crate::commands::{Facts, Command};
|
2021-08-03 20:58:26 -05:00
|
|
|
use crate::timeparse::parse_time;
|
2021-08-04 18:27:08 -05:00
|
|
|
use crate::old::{entries_or_warning, time_or_warning, warn_if_needed};
|
2021-08-03 20:58:26 -05:00
|
|
|
use crate::formatters::text;
|
2021-08-04 18:27:08 -05:00
|
|
|
use crate::editor;
|
2021-08-25 14:30:27 -05:00
|
|
|
use crate::io::Streams;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Args {
|
|
|
|
id: Option<u64>,
|
|
|
|
start: Option<DateTime<Utc>>,
|
|
|
|
end: Option<DateTime<Utc>>,
|
|
|
|
append: bool,
|
|
|
|
r#move: Option<String>,
|
|
|
|
note: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:27:08 -05:00
|
|
|
impl Args {
|
|
|
|
/// returns true only if no argument was passed other that possibly --id.
|
|
|
|
/// This means that an edit was requested without specifying what to edit,
|
|
|
|
/// therefore let's edit the note because why not
|
|
|
|
fn none_given(&self) -> bool {
|
|
|
|
!(self.start.is_some() || self.end.is_some() || self.r#move.is_some() || self.note.is_some())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 20:58:26 -05:00
|
|
|
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(matches: &'a ArgMatches) -> Result<Self> {
|
|
|
|
Ok(Args {
|
|
|
|
id: matches.value_of("id").map(|i| i.parse().unwrap()),
|
2021-12-13 14:20:56 -06:00
|
|
|
start: matches.value_of("start").map(parse_time).transpose()?,
|
|
|
|
end: matches.value_of("end").map(parse_time).transpose()?,
|
2021-08-03 20:58:26 -05:00
|
|
|
append: matches.is_present("append"),
|
|
|
|
r#move: matches.value_of("move").map(|s| s.into()),
|
|
|
|
note: matches.value_of("note").map(|s| s.into()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EditCommand {}
|
|
|
|
|
|
|
|
impl<'a> Command<'a> for EditCommand {
|
|
|
|
type Args = Args;
|
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
fn handle<D, I, O, E>(args: Args, streams: &mut Streams<D, I, O, E>, facts: &Facts) -> Result<()>
|
2021-08-03 20:58:26 -05:00
|
|
|
where
|
|
|
|
D: Database,
|
2021-08-25 14:30:27 -05:00
|
|
|
I: BufRead,
|
2021-08-03 20:58:26 -05:00
|
|
|
O: Write,
|
|
|
|
E: Write,
|
|
|
|
{
|
2021-08-25 14:30:27 -05:00
|
|
|
let current_sheet = streams.db.current_sheet()?.unwrap_or_else(|| "default".to_owned());
|
|
|
|
let needs_warning = streams.db.version()? == DBVersion::Timetrap;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
let entry = if let Some(id) = args.id {
|
2021-08-25 14:30:27 -05:00
|
|
|
if let Some(entry) = streams.db.entry_by_id(id)? {
|
2021-08-03 20:58:26 -05:00
|
|
|
entry
|
|
|
|
} else {
|
2021-08-25 14:30:27 -05:00
|
|
|
writeln!(streams.out, "Entry with id \"{}\" does not exist. Perhaps it was deleted", id)?;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
2021-08-25 14:30:27 -05:00
|
|
|
} else if let Some(entry) = streams.db.last_entry_of_sheet(¤t_sheet)? {
|
2021-08-03 20:58:26 -05:00
|
|
|
entry
|
|
|
|
} else {
|
2021-08-25 14:30:27 -05:00
|
|
|
writeln!(streams.out, "No entries to edit in sheet \"{}\".", current_sheet)?;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
2021-08-04 18:27:08 -05:00
|
|
|
// normalize the entry in case it comes from an old database
|
2021-08-25 14:30:27 -05:00
|
|
|
let entry = entries_or_warning(vec![entry], &streams.db)?.0.into_iter().next().unwrap();
|
2021-08-04 18:27:08 -05:00
|
|
|
|
|
|
|
// try really hard to obtain the note
|
2021-08-03 20:58:26 -05:00
|
|
|
let note = if let Some(new_note) = args.note {
|
2021-08-04 18:27:08 -05:00
|
|
|
// either from the command's args
|
2021-08-03 20:58:26 -05:00
|
|
|
if args.append {
|
2021-08-25 14:30:27 -05:00
|
|
|
Some(entry.note.unwrap_or_else(|| "".to_owned()) + &facts.config.append_notes_delimiter + &new_note)
|
2021-08-03 20:58:26 -05:00
|
|
|
} else {
|
|
|
|
Some(new_note)
|
|
|
|
}
|
2021-08-04 18:27:08 -05:00
|
|
|
} else if args.none_given() {
|
|
|
|
// or from the editor if no arguments where given
|
2021-08-25 14:30:27 -05:00
|
|
|
Some(editor::get_string(facts.config.note_editor.as_deref(), entry.note)?)
|
2021-08-03 20:58:26 -05:00
|
|
|
} else {
|
2021-08-04 18:27:08 -05:00
|
|
|
// or just use watever was previously there is the user is editing
|
|
|
|
// something else
|
2021-08-03 20:58:26 -05:00
|
|
|
entry.note
|
|
|
|
};
|
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_update(
|
2021-08-03 20:58:26 -05:00
|
|
|
entry.id,
|
2021-08-25 14:30:27 -05:00
|
|
|
time_or_warning(args.start.unwrap_or(entry.start), &streams.db)?.0,
|
|
|
|
args.end.or(entry.end).map(|e| time_or_warning(e, &streams.db)).transpose()?.map(|o| o.0),
|
2021-08-03 20:58:26 -05:00
|
|
|
note,
|
|
|
|
&args.r#move.unwrap_or(entry.sheet),
|
|
|
|
)?;
|
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
let updated_entry = entries_or_warning(vec![streams.db.entry_by_id(entry.id)?.unwrap()], &streams.db)?.0;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
text::print_formatted(
|
2021-08-04 18:27:08 -05:00
|
|
|
updated_entry,
|
2021-08-25 14:30:27 -05:00
|
|
|
&mut streams.out,
|
2021-08-25 14:43:50 -05:00
|
|
|
facts,
|
2021-08-03 20:58:26 -05:00
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
warn_if_needed(&mut streams.err, needs_warning, &facts.env)?;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-08-04 18:40:35 -05:00
|
|
|
use std::fs;
|
|
|
|
|
2021-08-03 20:58:26 -05:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use chrono::{Duration, TimeZone};
|
2021-08-04 18:40:35 -05:00
|
|
|
use tempfile::NamedTempFile;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
use crate::database::SqliteDatabase;
|
|
|
|
use crate::test_utils::Ps;
|
2021-08-25 14:30:27 -05:00
|
|
|
use crate::config::Config;
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_last_note() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let args = Args {
|
|
|
|
note: Some("new note".into()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("should be left intact".into()), "sheet1").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
let entry2 = streams.db.entry_by_id(2).unwrap().unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
|
|
|
assert_eq!(entry2.note, Some("should be left intact".into()));
|
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:29:00 - 14:29:00 1:00:00 new note
|
2021-08-03 20:58:26 -05:00
|
|
|
1:00:00
|
|
|
|
--------------------------------------------------------------
|
|
|
|
Total 1:00:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_with_id() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-04 18:27:08 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let args = Args {
|
|
|
|
id: Some(2),
|
|
|
|
note: Some("new note".into()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("should be left intact".into()), "sheet1").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: sheet1
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
2 Tue Aug 03, 2021 13:29:00 - 14:29:00 1:00:00 new note
|
2021-08-03 20:58:26 -05:00
|
|
|
1:00:00
|
|
|
|
--------------------------------------------------------------
|
|
|
|
Total 1:00:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_start() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let args = Args {
|
|
|
|
start: Some(now - Duration::minutes(30)),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:59:00 - 14:29:00 0:30:00 a note
|
2021-08-03 20:58:26 -05:00
|
|
|
0:30:00
|
|
|
|
------------------------------------------------------------
|
|
|
|
Total 0:30:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_end() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let args = Args {
|
|
|
|
end: Some(now - Duration::minutes(30)),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:29:00 - 13:59:00 0:30:00 a note
|
2021-08-03 20:58:26 -05:00
|
|
|
0:30:00
|
|
|
|
------------------------------------------------------------
|
|
|
|
Total 0:30:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_append() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let args = Args {
|
|
|
|
note: Some("new note".into()),
|
|
|
|
append: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:29:00 - 14:29:00 1:00:00 a note new note
|
2021-08-03 20:58:26 -05:00
|
|
|
1:00:00
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
Total 1:00:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn edit_move() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let args = Args {
|
|
|
|
r#move: Some("new sheet".to_owned()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: new sheet
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:29:00 - 14:29:00 1:00:00 a note
|
2021-08-03 20:58:26 -05:00
|
|
|
1:00:00
|
|
|
|
------------------------------------------------------------
|
|
|
|
Total 1:00:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn non_default_delimiter() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-04 18:27:08 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"");
|
2021-08-03 20:58:26 -05:00
|
|
|
let args = Args {
|
|
|
|
note: Some("new note".into()),
|
|
|
|
append: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
|
|
|
let an_hour_ago = now - Duration::hours(1);
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now).with_config(Config {
|
2021-08-03 20:58:26 -05:00
|
|
|
append_notes_delimiter: ";".to_owned(),
|
|
|
|
..Default::default()
|
2021-08-25 14:30:27 -05:00
|
|
|
});
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
streams.db.entry_insert(an_hour_ago, Some(now), Some("a note".into()), "default").unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-03 20:58:26 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-03 20:58:26 -05:00
|
|
|
ID Day Start End Duration Notes
|
2021-08-11 17:54:37 -05:00
|
|
|
1 Tue Aug 03, 2021 13:29:00 - 14:29:00 1:00:00 a note;new note
|
2021-08-03 20:58:26 -05:00
|
|
|
1:00:00
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
Total 1:00:00
|
|
|
|
"));
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:40:35 -05:00
|
|
|
fn copy_db(path: &str) -> NamedTempFile {
|
|
|
|
let tmpfile = NamedTempFile::new().unwrap();
|
|
|
|
|
|
|
|
fs::copy(path, &tmpfile).unwrap();
|
|
|
|
|
|
|
|
tmpfile
|
|
|
|
}
|
|
|
|
|
2021-08-03 20:58:26 -05:00
|
|
|
#[test]
|
2021-08-04 18:27:08 -05:00
|
|
|
fn warn_old() {
|
2021-08-11 17:54:37 -05:00
|
|
|
std::env::set_var("TZ", "CST+6");
|
2021-08-04 18:27:08 -05:00
|
|
|
|
2021-08-04 18:40:35 -05:00
|
|
|
let database_file = copy_db("assets/test_old_db.db");
|
2021-08-25 14:30:27 -05:00
|
|
|
let mut streams = Streams::fake(b"").with_db(
|
|
|
|
SqliteDatabase::from_path(&database_file).unwrap()
|
|
|
|
);
|
2021-08-04 18:27:08 -05:00
|
|
|
let now = Utc.ymd(2021, 8, 3).and_hms(20, 29, 0);
|
2021-12-13 14:20:56 -06:00
|
|
|
let new_end = Utc.ymd(2021, 6, 29).and_hms(14, 26, 52);
|
2021-08-04 18:27:08 -05:00
|
|
|
let args = Args {
|
|
|
|
end: Some(new_end),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-08-25 14:30:27 -05:00
|
|
|
let facts = Facts::new().with_now(now);
|
2021-08-04 18:27:08 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
EditCommand::handle(args, &mut streams, &facts).unwrap();
|
2021-08-04 18:27:08 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Timesheet: default
|
2021-08-04 18:27:08 -05:00
|
|
|
ID Day Start End Duration Notes
|
|
|
|
1 Tue Jun 29, 2021 06:26:49 - 08:26:52 2:00:02 lets do some rust
|
|
|
|
2:00:02
|
|
|
|
-----------------------------------------------------------------------
|
|
|
|
Total 2:00:02
|
|
|
|
"));
|
|
|
|
assert_eq!(
|
2021-08-25 14:30:27 -05:00
|
|
|
String::from_utf8_lossy(&streams.err),
|
|
|
|
"[WARNING] You are using the old timetrap format, it is advised that you update your database using t migrate\n"
|
2021-08-04 18:27:08 -05:00
|
|
|
);
|
2021-08-04 18:40:35 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
std::mem::drop(streams.db);
|
2021-08-04 18:40:35 -05:00
|
|
|
std::mem::drop(database_file);
|
2021-08-03 20:58:26 -05:00
|
|
|
}
|
|
|
|
}
|