2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-08-26 10:52:57 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2016-01-23 15:29:04 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-09-06 09:27:42 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2016-08-26 10:52:57 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-09-06 09:27:42 +00:00
|
|
|
use libimagutil::warn_result::*;
|
2016-01-23 15:29:04 +00:00
|
|
|
|
|
|
|
pub fn delete(rt: &Runtime) {
|
2017-09-04 14:51:18 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("delete").unwrap();
|
|
|
|
let id = scmd.value_of("id").unwrap(); // safe by clap
|
|
|
|
let path = PathBuf::from(id);
|
|
|
|
let store = Some(rt.store().path().clone());
|
2018-02-11 18:37:52 +00:00
|
|
|
let path = StoreId::new(store, path).map_err_trace_exit_unwrap(1);
|
2017-09-04 14:51:18 +00:00
|
|
|
debug!("Deleting file at {:?}", id);
|
2016-03-13 20:04:06 +00:00
|
|
|
|
2017-09-04 14:51:18 +00:00
|
|
|
let _ = rt.store()
|
|
|
|
.delete(path)
|
|
|
|
.map_warn_err(|e| format!("Error: {:?}", e))
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2016-01-23 15:29:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:08:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use create::create;
|
|
|
|
use super::delete;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
make_mock_app! {
|
2017-09-05 15:38:18 +00:00
|
|
|
app "imag-store";
|
2017-09-03 20:08:01 +00:00
|
|
|
modulename mock;
|
2018-03-03 12:51:55 +00:00
|
|
|
version env!("CARGO_PKG_VERSION");
|
2017-09-05 15:38:18 +00:00
|
|
|
with help "imag-store mocking app";
|
2017-09-03 20:08:01 +00:00
|
|
|
}
|
|
|
|
use self::mock::generate_test_runtime;
|
|
|
|
use self::mock::reset_test_runtime;
|
|
|
|
|
|
|
|
#[test]
|
2017-09-05 15:38:18 +00:00
|
|
|
fn test_delete_simple() {
|
2017-09-03 20:08:01 +00:00
|
|
|
let test_name = "test_create_simple";
|
2017-09-05 15:38:18 +00:00
|
|
|
let rt = generate_test_runtime(vec!["create", "test_create_simple"]).unwrap();
|
2017-09-03 20:08:01 +00:00
|
|
|
|
|
|
|
create(&rt);
|
|
|
|
|
2017-09-05 15:38:18 +00:00
|
|
|
let rt = reset_test_runtime(vec!["delete", "test_create_simple"], rt).unwrap();
|
2017-09-03 20:08:01 +00:00
|
|
|
|
|
|
|
delete(&rt);
|
|
|
|
|
|
|
|
let e = rt.store().get(PathBuf::from(test_name));
|
|
|
|
assert!(e.is_ok());
|
|
|
|
let e = e.unwrap();
|
|
|
|
assert!(e.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|