Skip to content

Commit

Permalink
Remove input file as argument, bump versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sevagh committed May 3, 2017
1 parent 2f46d70 commit 50cf379
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 92 deletions.
6 changes: 3 additions & 3 deletions 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,6 +1,6 @@
[package]
name = "pq"
version = "0.4.0"
version = "0.4.1"
authors = ["Sevag Hanssian <[email protected]>"]
description = "jq for protobuf"
repository = "https://github.com/sevagh/pqrs"
Expand All @@ -17,7 +17,7 @@ serde-value = "0.4.0"
serde_json = "0.9.9"
serde-protobuf = "0.5"
protobuf = "1.2.1"
stream_delimit = { path = "stream_delimit", version = "0.1.0" }
stream_delimit = { path = "stream_delimit", version = "0.1.2" }

[workspace]
members = ["stream_delimit"]
Expand Down
15 changes: 6 additions & 9 deletions docs/_pqrs.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ pq \- jq for protobuf - parse protobuf generically
[\fB\-\-msgtype=STRING\fR]
[\fB\-\-stream=DELIMITER\fR]
[\fB\-\-trail=DELIMITER\fR]
.IR [file]
.SH DESCRIPTION
.B pq
parses a compiled protobuf message with .fdset files located in '~/.pq/'. see more at https://github.com/sevagh/pqrs
.SH POSITIONAL ARGS
.TP
.BR (OPTIONAL)file\fR
.br
Read message from a file. Default is stdin.
parses a compiled protobuf message with .fdset files located in '~/.pq/'. See more at https://github.com/sevagh/pqrs.

.B pq
operates on stdin by default. Use files with the <filename pipe.
.SH OPTIONS
.TP
.BR \-\-msgtype=STRING\fR
Expand All @@ -31,9 +28,9 @@ guessing cannot distinguish between user-specified nulls and empty fields. E.g.,
.br
Parse a delimited stream. The delimiter is specified as a string. For now the only supported delimiter is "varint" (this is the way it's done by Google in delimited_message_util).
.TP
.BR \-\-trail=DELIMITER\fR
.BR \-\-trail=NUM\fR
.br
Delete trailing delimiter characters between messages, e.g. "\\r\\n". Must be used in conjunction with '--stream'.
Delete NUM number of trailing characters between streamed messages.
.TP
.SH ENVIRONMENT VARIABLES
.TP
Expand Down
18 changes: 6 additions & 12 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ <h1 class="Sh">NAME</h1>
pq - jq for protobuf - parse protobuf generically
<h1 class="Sh">SYNOPSIS</h1>
<b>pq</b> [ <b>--msgtype=STRING</b>] [ <b>--stream=DELIMITER</b>] [
<b>--trail=DELIMITER</b>] <i>[file]</i>
<b>--trail=DELIMITER</b>]
<h1 class="Sh">DESCRIPTION</h1>
<b>pq</b> parses a compiled protobuf message with .fdset files located in
'~/.pq/'. see more at https://github.com/sevagh/pqrs
<h1 class="Sh">POSITIONAL ARGS</h1>
<dl class="Bl-tag">
<dt class="It-tag"><b>(OPTIONAL)file</b></dt>
<dd class="It-tag">
<div style="height: 0.00em;">&#160;</div>
Read message from a file. Default is stdin.</dd>
</dl>
'~/.pq/'. See more at https://github.com/sevagh/pqrs.
<div style="height: 1.00em;">&#160;</div>
<b>pq</b> operates on stdin by default. Use files with the &lt;filename pipe.
<h1 class="Sh">OPTIONS</h1>
<dl class="Bl-tag">
<dt class="It-tag"><b>--msgtype=STRING</b></dt>
Expand Down Expand Up @@ -61,11 +56,10 @@ <h1 class="Sh">OPTIONS</h1>
done by Google in delimited_message_util).</dd>
</dl>
<dl class="Bl-tag">
<dt class="It-tag"><b>--trail=DELIMITER</b></dt>
<dt class="It-tag"><b>--trail=NUM</b></dt>
<dd class="It-tag">
<div style="height: 0.00em;">&#160;</div>
Delete trailing delimiter characters between messages, e.g.
&quot;\r\n&quot;. Must be used in conjunction with '--stream'.</dd>
Delete NUM number of trailing characters between streamed messages.</dd>
</dl>
<h1 class="Sh">ENVIRONMENT VARIABLES</h1>
<dl class="Bl-tag">
Expand Down
17 changes: 3 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ mod macros;
use docopt::Docopt;
use decode::PqrsDecoder;
use stream_delimit::StreamDelimiter;
use std::fs::File;
use std::io::{self, Read, BufReader, Write, stderr};
use std::io::{self, Read, Write, stderr};
use std::process;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand All @@ -28,7 +27,7 @@ const USAGE: &'static str = "
pq - Protobuf to json
Usage:
pq [--msgtype=<msgtype>] [<infile>] [--stream=<delim>] [--trail=<delim>]
pq [--msgtype=<msgtype>] [--stream=<delim>] [--trail=<delim>]
pq (--help | --version)
Options:
Expand All @@ -41,7 +40,6 @@ Options:

#[derive(Debug, RustcDecodable)]
struct Args {
pub arg_infile: Option<String>,
pub flag_msgtype: Option<String>,
pub flag_stream: Option<String>,
pub flag_trail: Option<usize>,
Expand All @@ -61,16 +59,7 @@ fn main() {
Err(e) => errexit!(e),
};

let mut infile: Box<Read> = match args.arg_infile {
Some(x) => {
let file = match File::open(&x) {
Ok(x) => x,
Err(e) => errexit!(e),
};
Box::new(BufReader::new(file))
}
None => Box::new(stdin.lock()),
};
let mut infile: Box<Read> = Box::new(stdin.lock());

if let Some(lead_delim) = args.flag_stream {
let delim = StreamDelimiter::new(&lead_delim, &mut infile, args.flag_trail);
Expand Down
2 changes: 1 addition & 1 deletion stream_delimit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stream_delimit"
version = "0.1.1"
version = "0.1.2"
authors = ["Sevag Hanssian <[email protected]>"]
description = "length delimited protobuf stream separator"
repository = "https://github.com/sevagh/pqrs/tree/master/stream_delimit"
Expand Down
12 changes: 10 additions & 2 deletions tests/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use std::env;
use std::path::PathBuf;
use std::io::Write;
use std::io::{Read, Write};
use std::process::{Command, Output, Child, Stdio};
use std::fs::File;

pub struct Runner {
pub cmd: Command,
Expand Down Expand Up @@ -33,7 +34,7 @@ impl Runner {
}
}

pub fn with_stdin(&mut self, contents: &[u8]) {
fn with_stdin(&mut self, contents: &[u8]) {
self.cmd.stdin(Stdio::piped());
let mut chld = self._spawn();
chld.stdin
Expand All @@ -44,6 +45,13 @@ impl Runner {
self.chld = Some(chld);
}

pub fn stdin_from_file(&mut self, path: &str) {
let mut file = File::open(&self.tests_path.join(path)).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
self.with_stdin(&buf);
}

pub fn _spawn(&mut self) -> Child {
self.cmd.spawn().unwrap()
}
Expand Down
68 changes: 19 additions & 49 deletions tests/test_pqrs_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,39 @@ extern crate protobuf;
mod runner;

use std::process::Output;
use std::io::Read;
use std::fs::File;
use runner::Runner;

fn for_dog_stream_with_trail(work: &mut Runner) {
work.cmd.arg("--stream=varint");
work.cmd.arg("--trail=1");
work.stdin_from_file("samples/dog_stream_trail");
}

fn for_person(work: &mut Runner) {
work.stdin_from_file("samples/person");
}

fn for_dog(work: &mut Runner) {
work.stdin_from_file("samples/dog");
}

fn for_nonexistent_fdset_dir(work: &mut Runner) {
work.cmd.env("FDSET_PATH", "fdset-doesnt-exist");
work.cmd.arg(&work.tests_path.join("samples/dog"));
work.stdin_from_file("samples/dog");
}

fn for_no_valid_fdsets(work: &mut Runner) {
work.cmd
.env("FDSET_PATH", &work.tests_path.join("fdsets-invalid"));
}

fn for_nonexistent_file(work: &mut Runner) {
work.cmd.arg("file-doesnt-exist");
}

fn for_bad_input(work: &mut Runner) {
work.cmd.arg(&work.tests_path.join("samples/bad"));
}

fn for_dog_file(work: &mut Runner) {
work.cmd.arg(&work.tests_path.join("samples/dog"));
work.stdin_from_file("samples/bad");
}

fn for_dog_stream(work: &mut Runner) {
work.cmd.arg("--stream=varint");
work.cmd.arg(&work.tests_path.join("samples/dog_stream"));
}

fn for_dog_stream_with_trail(work: &mut Runner) {
work.cmd.arg("--stream=varint");
work.cmd.arg("--trail=1");
work.cmd.arg(&work.tests_path.join("samples/dog_stream_trail"));
}

fn for_person(work: &mut Runner) {
work.cmd.arg(&work.tests_path.join("samples/person"));
}

fn for_dog_stdin(work: &mut Runner) {
let mut file = File::open(&work.tests_path.join("samples/dog")).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
work.with_stdin(&buf);
work.stdin_from_file("samples/dog_stream");
}

fn run_pqrs<F>(modify_in: F) -> Output
Expand All @@ -66,16 +53,8 @@ fn run_pqrs<F>(modify_in: F) -> Output
}

#[test]
fn test_dog_decode_from_file() {
let out = run_pqrs(for_dog_file);
assert!(out.status.success());
assert_eq!(String::from_utf8_lossy(&out.stdout),
"{\"age\":3,\"breed\":\"gsd\",\"temperament\":\"excited\"}");
}

#[test]
fn test_dog_decode_from_stdin() {
let out = run_pqrs(for_dog_stdin);
fn test_dog_decode() {
let out = run_pqrs(for_dog);
assert!(out.status.success());
assert_eq!(String::from_utf8_lossy(&out.stdout),
"{\"age\":3,\"breed\":\"gsd\",\"temperament\":\"excited\"}");
Expand Down Expand Up @@ -122,15 +101,6 @@ fn test_person_decode() {
"{\"id\":0,\"name\":\"khosrov\"}");
}

#[test]
fn test_nonexistent_file() {
let out = run_pqrs(for_nonexistent_file);
assert_eq!(out.status.code().unwrap(), 255);
assert_eq!(String::from_utf8_lossy(&out.stdout), "");
assert!(String::from_utf8_lossy(&out.stderr)
.contains("No such file or directory (os error 2)"));
}

#[test]
fn test_bad_input() {
let out = run_pqrs(for_bad_input);
Expand Down

0 comments on commit 50cf379

Please sign in to comment.