-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from apollographql/jake/stdin
use stdin as sdl input
- Loading branch information
Showing
6 changed files
with
147 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub(crate) mod client; | |
pub mod command; | ||
mod stringify; | ||
mod telemetry; | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::utils::parsers::SchemaSource; | ||
use anyhow::{Context, Result}; | ||
use std::io::Read; | ||
use std::path::Path; | ||
|
||
/// this fn takes 2 args: the first, an enum describing where to look to load | ||
/// a schema - from stdin or a file's PathBuf, and the second, the reference to | ||
/// stdin to load from, should it be needed. | ||
pub fn load_schema_from_flag(loc: &SchemaSource, mut stdin: impl Read) -> Result<String> { | ||
match loc { | ||
SchemaSource::Stdin => { | ||
let mut buffer = String::new(); | ||
stdin | ||
.read_to_string(&mut buffer) | ||
.context("Failed while attempting to read SDL from stdin")?; | ||
Ok(buffer) | ||
} | ||
SchemaSource::File(path) => { | ||
if Path::exists(&path) { | ||
let contents = std::fs::read_to_string(path)?; | ||
Ok(contents) | ||
} else { | ||
Err(anyhow::anyhow!( | ||
"Invalid path. No file found at {}", | ||
path.display() | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{load_schema_from_flag, SchemaSource}; | ||
use assert_fs::prelude::*; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn load_schema_from_flag_loads() { | ||
let fixture = assert_fs::TempDir::new().unwrap(); | ||
|
||
let test_file = fixture.child("schema.graphql"); | ||
test_file | ||
.write_str("type Query { hello: String! }") | ||
.unwrap(); | ||
|
||
let test_path = test_file.path().to_path_buf(); | ||
let loc = SchemaSource::File(test_path); | ||
|
||
let schema = load_schema_from_flag(&loc, std::io::stdin()).unwrap(); | ||
assert_eq!(schema, "type Query { hello: String! }".to_string()); | ||
} | ||
|
||
#[test] | ||
fn load_schema_from_flag_errs_on_bad_path() { | ||
let empty_path = "./wow.graphql"; | ||
let loc = SchemaSource::File(PathBuf::from(empty_path)); | ||
|
||
let schema = load_schema_from_flag(&loc, std::io::stdin()); | ||
assert_eq!(schema.is_err(), true); | ||
} | ||
|
||
#[test] | ||
fn load_schema_from_stdin_works() { | ||
// input implements std::io::Read, so it should be a suitable | ||
// replacement for stdin | ||
let input = b"type Query { hello: String! }"; | ||
let loc = SchemaSource::Stdin; | ||
|
||
let schema = load_schema_from_flag(&loc, &input[..]).unwrap(); | ||
assert_eq!(schema, std::str::from_utf8(input).unwrap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod loaders; | ||
pub mod parsers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use anyhow::Result; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum SchemaSource { | ||
Stdin, | ||
File(PathBuf), | ||
} | ||
|
||
pub fn parse_schema_source(loc: &str) -> Result<SchemaSource> { | ||
if loc == "-" { | ||
Ok(SchemaSource::Stdin) | ||
} else if loc.is_empty() { | ||
Err(anyhow::anyhow!( | ||
"The path provided to find a schema is empty" | ||
)) | ||
} else { | ||
let path = PathBuf::from(loc); | ||
Ok(SchemaSource::File(path)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{parse_schema_source, SchemaSource}; | ||
|
||
#[test] | ||
fn it_correctly_parses_stdin_flag() { | ||
assert_eq!(parse_schema_source("-").unwrap(), SchemaSource::Stdin); | ||
} | ||
|
||
#[test] | ||
fn it_correctly_parses_path_option() { | ||
let loc = parse_schema_source("./schema.graphql").unwrap(); | ||
match loc { | ||
SchemaSource::File(buf) => { | ||
assert_eq!(buf.to_str().unwrap(), "./schema.graphql"); | ||
} | ||
_ => panic!("parsed incorrectly as stdin"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn it_errs_with_empty_path() { | ||
let loc = parse_schema_source(""); | ||
assert!(loc.is_err()); | ||
} | ||
} |