Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial integration tests #321

Merged
merged 7 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 208 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ tempfile = "3"
toml = "0.5.9"
whoami = "1.2"
xdg = "2.4"

[dev-dependencies]
assert_cmd = "2"
22 changes: 22 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::env;
use std::process::Command;

fn main() {
let mut version = String::from(env!("CARGO_PKG_VERSION"));
if let Some(commit_hash) = commit_hash() {
version = format!("{} ({})", version, commit_hash);
}
println!("cargo:rustc-env=KBS2_BUILD_VERSION={}", version);
}

// Cribbed from Alacritty:
// https://github.com/alacritty/alacritty/blob/8ea6c3b/alacritty/build.rs
fn commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn app() -> Command<'static> {
// The latter probably won't work with env!, though.
Command::new(env!("CARGO_PKG_NAME"))
.allow_external_subcommands(true)
.version(env!("CARGO_PKG_VERSION"))
.version(env!("KBS2_BUILD_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(
Arg::new("config-dir")
Expand All @@ -22,6 +22,7 @@ fn app() -> Command<'static> {
.long("config-dir")
.value_name("DIR")
.takes_value(true)
.allow_invalid_utf8(true)
.env("KBS2_CONFIG_DIR")
.default_value_os(kbs2::config::DEFAULT_CONFIG_DIR.as_ref())
.value_hint(ValueHint::DirPath),
Expand Down Expand Up @@ -78,6 +79,7 @@ fn app() -> Command<'static> {
.long("store-dir")
.value_name("DIR")
.takes_value(true)
.allow_invalid_utf8(true)
.default_value_os(kbs2::config::DEFAULT_STORE_DIR.as_ref())
.value_hint(ValueHint::DirPath),
)
Expand Down
62 changes: 62 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// NOTE(ww): Dead code allowed because of this `cargo test` bug:
// https://github.com/rust-lang/rust/issues/46379
#![allow(dead_code)]

use std::process::Output;

use assert_cmd::Command;
use serde_json::Value;
use tempfile::TempDir;

#[derive(Debug)]
pub struct CliSession {
pub config_dir: TempDir,
pub store_dir: TempDir,
}

impl CliSession {
pub fn new() -> Self {
let config_dir = TempDir::new().unwrap();
let store_dir = TempDir::new().unwrap();

// Run `kbs2 init` to configure the config and session directories.
{
kbs2()
.arg("--config-dir")
.arg(config_dir.path())
.arg("init")
.arg("--insecure-not-wrapped")
.arg("--store-dir")
.arg(store_dir.path())
.assert()
.success();
}

Self {
config_dir,
store_dir,
}
}

pub fn command(&self) -> Command {
let mut kbs2 = kbs2();

kbs2.arg("--config-dir").arg(self.config_dir.path());

kbs2
}
}

pub fn kbs2() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap()
}

pub trait ToJson {
fn json(&self) -> Value;
}

impl ToJson for Output {
fn json(&self) -> Value {
serde_json::from_slice(&self.stdout).unwrap()
}
}
46 changes: 46 additions & 0 deletions tests/test_kbs2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
mod common;

use clap::ArgEnum;
use clap_complete::Shell;
use common::kbs2;

#[test]
fn test_kbs2_help() {
// `help`, `--help`, and `-h` all produce the same output

let reference_output = kbs2().arg("help").output().unwrap();
assert!(reference_output.status.success());

for help in &["--help", "-h"] {
let output = kbs2().arg(help).output().unwrap();
assert!(output.status.success());
assert_eq!(reference_output.stdout, output.stdout);
}
}

#[test]
fn test_kbs2_completions() {
// Tab completion generation works

for shell in Shell::value_variants() {
let output = kbs2()
.args(&["--completions", &shell.to_string()])
.output()
.unwrap();
assert!(output.status.success());
assert!(!output.stdout.is_empty());
}
}

#[test]
fn test_kbs2_version() {
// kbs2 --version works and outputs a string starting with `kbs2 X.Y.Z`

let version = format!("kbs2 {}", env!("CARGO_PKG_VERSION"));

let output = kbs2().arg("--version").output().unwrap();
assert!(output.status.success());
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with(&version));
}
17 changes: 17 additions & 0 deletions tests/test_kbs2_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod common;

use common::CliSession;

#[test]
fn test_kbs2_init() {
let session = CliSession::new();

let config_dir = session.config_dir.path();
let store_dir = session.store_dir.path();

// Our config dir, etc. all exist; the store dir is empty.
assert!(config_dir.is_dir());
assert!(store_dir.is_dir());
assert!(config_dir.join("config.toml").is_file());
assert!(store_dir.read_dir().unwrap().next().is_none());
}
87 changes: 87 additions & 0 deletions tests/test_kbs2_new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
mod common;

use common::{CliSession, ToJson};
use serde_json::json;

// TODO: Figure out how to test prompts instead of terse inputs.

#[test]
fn test_kbs2_new_login() {
let session = CliSession::new();

session
.command()
.args(&["new", "-k", "login", "test-record"])
.write_stdin("fakeuser\x01fakepass")
.assert()
.success();

let dump = session
.command()
.args(&["dump", "--json", "test-record"])
.output()
.unwrap()
.json();

let fields = dump.get("body").unwrap().get("fields").unwrap();

assert_eq!(
fields,
// https://github.com/serde-rs/json/issues/867
&json!({ "username": "fakeuser", "password": "fakepass" }),
);
}

#[test]
fn test_kbs2_new_environment() {
let session = CliSession::new();

session
.command()
.args(&["new", "-k", "environment", "test-record"])
.write_stdin("fakevariable\x01fakevalue")
.assert()
.success();

let dump = session
.command()
.args(&["dump", "--json", "test-record"])
.output()
.unwrap()
.json();

let fields = dump.get("body").unwrap().get("fields").unwrap();

assert_eq!(
fields,
// https://github.com/serde-rs/json/issues/867
&json!({ "variable": "fakevariable", "value": "fakevalue" }),
);
}

#[test]
fn test_kbs2_new_unstructured() {
let session = CliSession::new();

session
.command()
.args(&["new", "-k", "unstructured", "test-record"])
.write_stdin("fakevalue")
.assert()
.success();

let dump = session
.command()
.args(&["dump", "--json", "test-record"])
.output()
.unwrap()
.json();

let fields = dump.get("body").unwrap().get("fields").unwrap();

assert_eq!(
fields,
// https://github.com/serde-rs/json/issues/867
&json!({ "contents": "fakevalue" }),
);
}