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

CLI Changes Sub-PR: Integration tests #601

Merged
merged 6 commits into from
Aug 25, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: export GC_DONT_GC=1; nix -L flake check

- name: Build and test executable
run: 'echo \{ \"foo\": \"bar\" \} | nix run . -- -l json'
run: 'echo \{ \"foo\": \"bar\" \} | nix run . -- fmt -l json'

- name: Verify that usage in README.md matches CLI output
run: ./verify-documented-usage.sh
Expand Down
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ futures = "0.3.28"
indoc = "2.0"
itertools = "0.11"
log = "0.4"
predicates = "3.0.3"
pretty_assertions = "1.3"
prettydiff = { version = "0.6.4", default-features = false }
regex = "1.8.2"
Expand Down
1 change: 1 addition & 0 deletions topiary-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ tryvial = { workspace = true }

[dev-dependencies]
assert_cmd = { workspace = true }
predicates = { workspace = true }
200 changes: 150 additions & 50 deletions topiary-cli/tests/cli-tester.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,210 @@
use std::{fmt, fs, fs::File, io::Write, path::PathBuf};

use assert_cmd::Command;
use std::{
fs::File,
io::{Read, Write},
path::Path,
use predicates::{
prelude::PredicateBooleanExt,
str::{ends_with, starts_with},
};
use tempfile::NamedTempFile;
use tempfile::TempDir;

// Simple exemplar JSON state, to verify the formatter
// Simple exemplar JSON and TOML state, to verify the formatter
// is doing something... and hopefully the right thing
const JSON_INPUT: &str = r#"{ "test" :123}"#;
const JSON_EXPECTED: &str = r#"{ "test": 123 }"#;
const JSON_EXPECTED: &str = r#"{ "test": 123 }
"#;

const TOML_INPUT: &str = r#" test= 123"#;
const TOML_EXPECTED: &str = r#"test = 123
"#;

struct State(NamedTempFile);
struct State(TempDir, PathBuf);

impl State {
fn new(payload: &str) -> Self {
let mut state = NamedTempFile::new().unwrap();
fn new(payload: &str, extension: &str) -> Self {
let tmp_dir = TempDir::new().unwrap();
let tmp_file = tmp_dir.path().join(format!("state.{extension}"));

let mut state = File::create(&tmp_file).unwrap();
write!(state, "{payload}").unwrap();

Self(state)
Self(tmp_dir, tmp_file)
}

fn path(&self) -> &Path {
self.0.path()
fn path(&self) -> &PathBuf {
&self.1
}

fn read(&self) -> String {
// For an in place edit, Topiary will remove the original file. As such, we can't use
// NamedTempFile::reopen, as the original no longer exists; we have to "reopen" it by path.
let mut file = File::open(self.path()).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();

contents
fs::read_to_string(self.path()).unwrap()
}
}

#[test]
fn test_file_output() {
let output = State::new("");
fn test_fmt_stdin() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("fmt")
.arg("--language")
.arg("json")
.write_stdin(JSON_INPUT)
.assert()
.success()
.stdout(JSON_EXPECTED);
}

#[test]
fn test_fmt_stdin_query() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("fmt")
.arg("--language")
.arg("json")
.arg("--output-file")
.arg(output.path())
.arg("--query")
.arg("../languages/json.scm")
.write_stdin(JSON_INPUT)
.assert()
.success()
.stdout(JSON_EXPECTED);
}

#[test]
fn test_fmt_files() {
let json = State::new(JSON_INPUT, "json");
let toml = State::new(TOML_INPUT, "toml");

let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("fmt")
.arg(json.path())
.arg(toml.path())
.assert()
.success();

assert_eq!(output.read().trim(), JSON_EXPECTED);
assert_eq!(json.read(), JSON_EXPECTED);
assert_eq!(toml.read(), TOML_EXPECTED);
}

#[test]
fn test_no_clobber() {
let json = State::new(JSON_INPUT);
let input_path = json.path();
fn test_fmt_dir() {
let json = State::new(JSON_INPUT, "json");

let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("--language")
.arg("json")
.arg("--input-files")
.arg(input_path)
.arg("--output-file")
.arg(input_path)
.arg("fmt")
.arg(json.path().parent().unwrap())
.assert()
.success();

// NOTE We only assume, here, that the state has been modified by the call to Topiary. It may
// be worthwhile asserting (e.g., change in mtime, etc.).
assert_eq!(json.read().trim(), JSON_EXPECTED);
assert_eq!(json.read(), JSON_EXPECTED);
}

#[test]
fn test_in_place() {
let json = State::new(JSON_INPUT);
let input_path = json.path();

fn test_fmt_invalid() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Can't specify --language with input files
topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("fmt")
.arg("--language")
.arg("json")
.arg("--input-files")
.arg(input_path)
.arg("--in-place")
.arg("/path/to/some/input")
.assert()
.success();
.failure();

// Can't specify --query without --language
topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("fmt")
.arg("--query")
.arg("/path/to/query")
.assert()
.failure();
}

// NOTE We only assume, here, that the state has been modified by the call to Topiary. It may
// be worthwhile asserting (e.g., change in mtime, etc.).
assert_eq!(json.read().trim(), JSON_EXPECTED);
#[test]
fn test_vis() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Sanity check output is a valid DOT graph
let is_graph = starts_with("graph {").and(ends_with("}\n"));

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("vis")
.arg("--language")
.arg("json")
.write_stdin(JSON_INPUT)
.assert()
.success()
.stdout(is_graph);
}

#[test]
fn test_in_place_no_input() {
fn test_vis_invalid() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

// Can't specify --language with input file
topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("vis")
.arg("--language")
.arg("json")
.arg("--in-place")
.arg("/path/to/some/input")
.assert()
.failure();

// Can't specify --query without --language
topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("vis")
.arg("--query")
.arg("/path/to/query")
.assert()
.failure();

// Can't specify multiple input files
topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("vis")
.arg("/path/to/some/input")
.arg("/path/to/another/input")
.assert()
.failure();
}

#[test]
fn test_cfg() {
let mut topiary = Command::cargo_bin("topiary").unwrap();

topiary
.env("TOPIARY_LANGUAGE_DIR", "../languages")
.arg("cfg")
.assert()
.success()
.stdout(IsToml);
}

struct IsToml;

impl predicates::Predicate<str> for IsToml {
fn eval(&self, variable: &str) -> bool {
toml::Value::try_from(variable).is_ok()
}
}

impl predicates::reflection::PredicateReflection for IsToml {}

impl fmt::Display for IsToml {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "is_toml")
}
}
4 changes: 3 additions & 1 deletion verify-documented-usage.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
#!/usr/bin/env nix-shell
#!nix-shell -i bash --packages diffutils gnused
#shellcheck shell=bash

set -euo pipefail

Expand Down
2 changes: 1 addition & 1 deletion web-playground/e2e/sample-tester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FrameWaitForFunctionOptions, Page } from "puppeteer";
import * as fs from 'fs';
import * as path from 'path';

const TimeoutMs = 20000;
const TimeoutMs = 30000;

// Generated by hand from topiary/src/language.rs
// TODO: read from config file, when we have that.
Expand Down
Loading