Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from dantuck/feature/file-env
Browse files Browse the repository at this point in the history
Allow for setting of file location by env
  • Loading branch information
ebcrowder authored May 13, 2020
2 parents 942902c + 4396d0d commit 511241e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ fn main() -> Result<(), std::io::Error> {

let pargs_options = pargs_result.option_args;
let pargs_commands = pargs_result.command_args;

let ledger_file = match pargs_options.get("-l") {
Some(value) => value,
None => "",
Some(value) => value.to_string(),
None => {
let ledger_file_env = match std::env::var("RLEDGER_FILE") {
Ok(p) => format!("{}", p),
Err(_) => {
format!("{}", "")
}
};

ledger_file_env.to_string()
}
};

let options_arg = match pargs_options.get("-f") {
Expand Down
45 changes: 45 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::io::Write;
use std::process::Command;
use tempfile;

use std::env;

#[test]
fn file_does_not_exist() -> Result<(), std::io::Error> {
let mut cmd = Command::new("./target/debug/rust_ledger");
Expand All @@ -17,6 +19,49 @@ fn file_does_not_exist() -> Result<(), std::io::Error> {
Ok(())
}

#[test]
fn file_path_found_as_env() -> Result<(), Box<dyn std::error::Error>> {
let mut file = tempfile::Builder::new().suffix(".yaml").tempfile().unwrap();

let account_yml = br#"
owner: test
currencies:
id: $
name: US Dollar
alias: USD
note: US Currency
accounts:
- id: 0
acct_name: operating
acct_type: asset
debit_credit: 1500
- id: 1
acct_name: equity
acct_type: equity
debit_credit: -1500
transactions:
- acct_name: expense-test-acct
debit_credit: 1
acct_type: expense
date: 2019-01-01
acct_offset_name: credit_card
name: 'expense transaction'
"#;

file.write_all(account_yml).unwrap();
file.flush().unwrap();

env::set_var("RLEDGER_FILE", file.path());

let mut cmd = Command::new("./target/debug/rust_ledger");
cmd.arg("accounts");

cmd.assert()
.success();

Ok(())
}

#[test]
fn accounts_test() -> Result<(), Box<dyn std::error::Error>> {
let mut file = tempfile::Builder::new().suffix(".yaml").tempfile().unwrap();
Expand Down

0 comments on commit 511241e

Please sign in to comment.