Skip to content

Commit

Permalink
added wafl tests with trycmd, added ability for wafl query to accept …
Browse files Browse the repository at this point in the history
…from STDIN (#76)
  • Loading branch information
jsoverson authored Aug 15, 2022
1 parent c3c40c5 commit 4e6e6d1
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/bins/wafl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ oci-distribution = { version = "0.9", features = [
], default-features = false }
wasmflow-oci = { path = "../../wasmflow/wasmflow-oci" }
anyhow = "1.0"
markup-converter = "0.1"
markup-converter = "0.2"
jaq-core = "0.7"

[dev-dependencies]
Expand Down
58 changes: 54 additions & 4 deletions crates/bins/wafl/src/commands/query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::io::{self, BufRead};
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use clap::Args;
use jaq_core::{parse, Ctx, Definitions, Val};
use markup_converter::Transcoder;
use markup_converter::{Format, Transcoder};

#[derive(Debug, Clone, Args)]
#[clap(rename_all = "kebab-case")]
Expand All @@ -15,18 +17,66 @@ pub(crate) struct Options {
#[clap(short = 'r', long = "raw", action)]
raw_output: bool,

/// Option to print raw output.
#[clap(short = 't', long = "type", action)]
kind: Option<MarkupKind>,

/// Path to JSON, YAML, or TOML file.
#[clap(action)]
path: PathBuf,
#[clap(short = 'f', long = "file", action)]
path: Option<PathBuf>,

/// The query.
#[clap(action)]
query: String,
}

#[derive(Debug, Clone)]
enum MarkupKind {
Json,
Toml,
Yaml,
}

impl FromStr for MarkupKind {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"json" => Ok(MarkupKind::Json),
"toml" => Ok(MarkupKind::Toml),
"yaml" => Ok(MarkupKind::Yaml),
_ => Err(anyhow!("Markup kind '{}' not supported", s)),
}
}
}

#[allow(clippy::unused_async)]
pub(crate) async fn handle(opts: Options) -> Result<()> {
let input = Transcoder::new(&opts.path)?.to_json()?;
let input = if let Some(path) = opts.path {
match opts.kind {
None => Transcoder::from_path(&path)?.to_json()?,
Some(kind) => {
let source = crate::io::read_to_string(&path).await?;
match kind {
MarkupKind::Json => Transcoder::new(Format::json(&source)?)?.to_json()?,
MarkupKind::Toml => Transcoder::new(Format::toml(&source)?)?.to_json()?,
MarkupKind::Yaml => Transcoder::new(Format::yaml(&source)?)?.to_json()?,
}
}
}
} else {
if atty::is(atty::Stream::Stdin) {
eprintln!("No file path passed, reading from <STDIN>. Use -f/--file to pass a file as an argument.");
}
let reader = io::BufReader::new(io::stdin());
let lines = reader.lines();
let markup = lines.collect::<Result<String, _>>()?;
match opts.kind {
Some(MarkupKind::Json) | None => Transcoder::new(Format::json(&markup)?)?.to_json()?,
Some(MarkupKind::Toml) => Transcoder::new(Format::toml(&markup)?)?.to_json()?,
Some(MarkupKind::Yaml) => Transcoder::new(Format::yaml(&markup)?)?.to_json()?,
}
};

let filter = &opts.query;

Expand Down
21 changes: 21 additions & 0 deletions crates/bins/wafl/tests/cmd/query/_sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "test-data",
"version": "0.0.0",
"description": "Wasmflow internal scripts",
"directories": {
"example": "examples",
"test": "tests"
},
"scripts": {
"update-lint": "ts-node ./scripts/update-lints.ts"
},
"dependencies": {
"@types/node": "^16.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
},
"devDependencies": {},
"author": "",
"license": "ISC",
"main": "index.js"
}
57 changes: 57 additions & 0 deletions crates/bins/wafl/tests/cmd/query/_sample.random
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: test-data
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 8080 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: wasmflow_service
timeout: 0s
max_stream_duration:
grpc_timeout_header_max: 0s
cors:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: custom-header-1,grpc-status,grpc-message
http_filters:
- name: envoy.filters.http.grpc_web
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
- name: envoy.filters.http.cors
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: wasmflow_service
connect_timeout: 0.25s
type: logical_dns
http2_protocol_options: {}
lb_policy: round_robin
load_assignment:
cluster_name: cluster_0
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 8060
57 changes: 57 additions & 0 deletions crates/bins/wafl/tests/cmd/query/_sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: test-data
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 8080 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: wasmflow_service
timeout: 0s
max_stream_duration:
grpc_timeout_header_max: 0s
cors:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: custom-header-1,grpc-status,grpc-message
http_filters:
- name: envoy.filters.http.grpc_web
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
- name: envoy.filters.http.cors
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: wasmflow_service
connect_timeout: 0.25s
type: logical_dns
http2_protocol_options: {}
lb_policy: round_robin
load_assignment:
cluster_name: cluster_0
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 8060
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/json-raw.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -r -f tests/cmd/query/_sample.json .name"
stdout = "test-data\n"
name = "wat"
5 changes: 5 additions & 0 deletions crates/bins/wafl/tests/cmd/query/json-stdin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin.name = "wafl"
args = "query .name"
stdin = "{\"name\":\"test-data\"}"
stdout = "\"test-data\"\n"
name = "wat"
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/json.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -f tests/cmd/query/_sample.json .name"
stdout = "\"test-data\"\n"
name = "wat"
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/random.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -f tests/cmd/query/_sample.random -t yaml .name"
stdout = "\"test-data\"\n"
name = "wat"
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/raw-toml.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -r -f Cargo.toml .package.name"
stdout = "wafl\n"
name = "wat"
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/toml.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -f Cargo.toml .package.name"
stdout = "\"wafl\"\n"
name = "wat"
5 changes: 5 additions & 0 deletions crates/bins/wafl/tests/cmd/query/yaml-stdin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin.name = "wafl"
args = "query -t yaml .name"
stdin = "name: test-data\n"
stdout = "\"test-data\"\n"
name = "wat"
4 changes: 4 additions & 0 deletions crates/bins/wafl/tests/cmd/query/yaml.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin.name = "wafl"
args = "query -f tests/cmd/query/_sample.yaml .name"
stdout = "\"test-data\"\n"
name = "wat"
6 changes: 3 additions & 3 deletions crates/bins/wafl/tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[tokio::test]
async fn wafl_invoke() {
tracing::warn!("TODO");
#[test]
fn cli_tests() {
trycmd::TestCases::new().case("tests/cmd/**/*.toml");
}
5 changes: 2 additions & 3 deletions crates/misc/tap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ impl TestRunner {
total_tests += block.num_tests();
}

let name = format!("# {}", description);
let plan_line = format!("1..{}", total_tests);
let mut all_lines = vec![name, plan_line];
let plan_line = format!("1..{} # {}", total_tests, description);
let mut all_lines = vec![plan_line];

let mut test_num = 0;
for block in self.blocks.iter_mut() {
Expand Down
3 changes: 1 addition & 2 deletions crates/misc/tap/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ fn basics() -> anyhow::Result<()> {
runner.run();

let expected = vec![
"# My test",
"1..2",
"1..2 # My test",
"# My block",
"ok 1 three is greater than two",
"not ok 2 three is less than two",
Expand Down

0 comments on commit 4e6e6d1

Please sign in to comment.