Skip to content

Commit

Permalink
Simplify & test format_rust_expression (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored and mitsuhiko committed Jul 28, 2019
1 parent 52cc472 commit bb07664
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ doc:
test: cargotest

cargotest:
@rustup component add rustfmt 2> /dev/null
@cargo test
@cargo test --all-features

Expand Down
46 changes: 25 additions & 21 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::borrow::Cow;
use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str;
use std::sync::Mutex;
use std::thread;

Expand Down Expand Up @@ -42,6 +43,8 @@ fn path_to_storage<P: AsRef<Path>>(path: P) -> String {
}

fn format_rust_expression(value: &str) -> Cow<'_, str> {
const PREFIX: &str = "const x:() = ";
const SUFFIX: &str = ";\n";
if let Ok(mut proc) = Command::new("rustfmt")
.arg("--emit=stdout")
.arg("--edition=2018")
Expand All @@ -52,34 +55,36 @@ fn format_rust_expression(value: &str) -> Cow<'_, str> {
{
{
let stdin = proc.stdin.as_mut().unwrap();
stdin.write_all(b"const x:() = ").unwrap();
stdin.write_all(PREFIX.as_bytes()).unwrap();
stdin.write_all(value.as_bytes()).unwrap();
stdin.write_all(b";").unwrap();
stdin.write_all(SUFFIX.as_bytes()).unwrap();
}
if let Ok(output) = proc.wait_with_output() {
if output.status.success() {
let mut buf = String::new();
let mut rv = String::new();
let mut reader = BufReader::new(&output.stdout[..]);
reader.read_line(&mut buf).unwrap();

rv.push_str(&buf[14..]);
loop {
buf.clear();
let read = reader.read_line(&mut buf).unwrap();
if read == 0 {
break;
}
rv.push_str(&buf);
}
rv.truncate(rv.trim_end().len() - 1);
return Cow::Owned(rv);
// slice between after the prefix and before the suffix
// (currently 14 from the start and 2 before the end, respectively)
let start = PREFIX.len() + 1;
let end = output.stdout.len() - SUFFIX.len();
return str::from_utf8(&output.stdout[start..end])
.unwrap()
.to_owned()
.into();
}
}
}
Cow::Borrowed(value)
}

#[test]
fn test_format_rust_expression() {
use crate::assert_snapshot_matches;
assert_snapshot_matches!(format_rust_expression("vec![1,2,3]"), @"vec![1, 2, 3]");
assert_snapshot_matches!(format_rust_expression("vec![1,2,3].iter()"), @"vec![1, 2, 3].iter()");
assert_snapshot_matches!(format_rust_expression(r#" "aoeu""#), @r###""aoeu""###);
assert_snapshot_matches!(format_rust_expression(r#" "aoeπŸ˜„""#), @r###""aoeπŸ˜„""###);
assert_snapshot_matches!(format_rust_expression("πŸ˜„πŸ˜„πŸ˜„πŸ˜„πŸ˜„"), @"πŸ˜„πŸ˜„πŸ˜„πŸ˜„πŸ˜„")
}

fn update_snapshot_behavior() -> UpdateBehavior {
match env::var("INSTA_UPDATE").ok().as_ref().map(|x| x.as_str()) {
None | Some("") | Some("auto") => {
Expand Down Expand Up @@ -557,8 +562,7 @@ pub fn assert_snapshot(
}

if should_fail_in_tests() {
assert!(
false,
panic!(
"snapshot assertion for '{}' failed in line {}",
snapshot_name.unwrap_or(Cow::Borrowed("inline snapshot")),
line
Expand Down

0 comments on commit bb07664

Please sign in to comment.