Skip to content

Commit

Permalink
Work on a temporary database
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarrk committed May 21, 2022
1 parent bfd3884 commit 9ffe7d1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "gibcite"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
authors = ["Marek S. Łukasiewicz <[email protected]>"]
description = "Give details of citation from local Zotero database"
readme = "README.md"
repository = "https://github.com/Maarrk/gibcite"
license = "AGPL-3.0-or-later"
license-file = "LICENSE.txt"
keywords = ["bibliography", "zotero"]
categories = ["command-line-utilities"]

Expand All @@ -21,6 +20,7 @@ regex = "1"
rusqlite = { version = "0.27.0", features = ["backup", "bundled"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tempfile = "3"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
26 changes: 23 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use regex::Regex;
use rusqlite::Connection;
use serde::Deserialize;
use serde_json::Value;
use std::{error::Error, path::PathBuf};
use std::{error::Error, fs::copy, path::PathBuf};
use tempfile::{tempdir, TempDir};

pub mod output;

pub fn get_database_path(input: &Option<PathBuf>) -> Result<Box<PathBuf>, String> {
/// Copies the SQLite database files into a temporary directory
///
/// This allows to read them while they are locked by Zotero
pub fn get_database_path(input: &Option<PathBuf>) -> Result<TempDir, String> {
let path_buf = match input {
Some(path) => path.to_owned(),
None => {
Expand All @@ -32,7 +36,23 @@ pub fn get_database_path(input: &Option<PathBuf>) -> Result<Box<PathBuf>, String
));
}

Ok(Box::new(path_buf))
if let Ok(dir) = tempdir() {
if let Err(err) = copy(
path_buf.join("zotero.sqlite"),
dir.path().join("zotero.sqlite"),
) {
return Err(format!("copy error: {:?}", err));
}
if let Err(err) = copy(
path_buf.join("better-bibtex.sqlite"),
dir.path().join("better-bibtex.sqlite"),
) {
return Err(format!("copy error: {:?}", err));
}
Ok(dir)
} else {
Err("could not open temporary dir".into())
}
}

pub fn count_items(conn: &Connection) -> Result<usize, Box<dyn Error>> {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ fn main() {
}

let conn = Connection::open_with_flags(
database_path.join("zotero.sqlite"),
database_path.path().join("zotero.sqlite"),
OpenFlags::SQLITE_OPEN_READ_ONLY,
)
.unwrap_or_else(|err| {
eprintln!("could not open Zotero database: {}", err);
exit(exitcode::UNAVAILABLE);
});
let bibtex_conn = Connection::open_with_flags(
database_path.join("better-bibtex.sqlite"),
database_path.path().join("better-bibtex.sqlite"),
OpenFlags::SQLITE_OPEN_READ_ONLY,
)
.unwrap_or_else(|err| {
Expand Down
25 changes: 25 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ fn mock_key() -> Result<(), Box<dyn Error>> {

Ok(())
}

#[test]
fn missing_key() -> Result<(), Box<dyn Error>> {
let (mut cmd, _dir) = setup_command().unwrap();
cmd.arg("thisisnotacitationkey");

cmd.assert().failure();

Ok(())
}

#[test]
fn json_output() -> Result<(), Box<dyn Error>> {
let (mut cmd, _dir) = setup_command().unwrap();
cmd.arg("kowalskiSampleTitle2022");

let output = cmd.output().unwrap().stdout;
let val: serde_json::Value = serde_json::from_slice(&output).unwrap();
let object = val[0].as_object().unwrap();

assert_eq!("A sample title", object["title"]);
assert_eq!("Abstract for a sample article", object["abstract"]);

Ok(())
}

0 comments on commit 9ffe7d1

Please sign in to comment.