Add option --variables-file

This commit is contained in:
Fabrice Reix 2021-01-09 10:25:01 +01:00
parent a52ea2f570
commit d10045e320
11 changed files with 313 additions and 176 deletions

View File

@ -271,6 +271,14 @@ Define variable (name/value) to be used in Hurl templates.
Only string values can be defined.
### --variables-file <file> {#variables-file}
Set properties file in which your define your variables.
Each variable is defined as name=value exactly as with [--variable](#variable) option.
Note that defining a variable twice produces an error.
### -v, --verbose {#verbose}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
<div class="hurl-file"><div class="hurl-entry"><div class="request"><span class="line"><span class="method">POST</span> <span class="url">http://localhost:8000/variables</span></span></div><span class="line">{<span class="line"> "name": "{{name}}",</span><span class="line"> "age": {{age}},</span><span class="line"> "female": {{female}}</span><span class="line">}</span></div><span class="line"> </span></div>

View File

@ -0,0 +1,7 @@
POST http://localhost:8000/variables
{
"name": "{{name}}",
"age": {{age}},
"female": {{female}}
}

View File

@ -0,0 +1 @@
{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/variables","body":{"type":"json","value":{"name":"{{name}}","age":"{{age}}","female":"{{female}}"}}}}]}

View File

@ -0,0 +1 @@
--variables-file tests/variables.properties --variable female=true

View File

@ -0,0 +1,4 @@
# Variables for hurl
name=Jennifer
age=30

View File

@ -0,0 +1,17 @@
from flask import request
from tests import app
import json
@app.route('/variables', methods=['POST'])
def variables():
assert request.headers['Content-Type'] == 'application/json'
s = request.data.decode("utf-8")
data = json.loads(s)
assert data['name'] == 'Jennifer'
assert data['age'] == 30
assert data['female'] == True
return ''

View File

@ -20,7 +20,7 @@ use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::prelude::*;
use std::io::{self, Read};
use std::io::{self, BufReader, Read};
use std::path::{Path, PathBuf};
use std::time::Duration;
@ -36,6 +36,7 @@ use hurl::runner::{HurlResult, RunnerOptions};
use hurl_core::ast::{Pos, SourceInfo};
use hurl_core::error::Error;
use hurl_core::parser;
use std::fs::File;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CLIOptions {
@ -266,30 +267,69 @@ fn html_report(matches: ArgMatches) -> Result<Option<std::path::PathBuf>, CLIErr
fn variables(matches: ArgMatches) -> Result<HashMap<String, String>, CLIError> {
let mut variables = HashMap::new();
if let Some(filename) = matches.value_of("variables_file") {
let path = std::path::Path::new(filename);
if !path.exists() {
return Err(CLIError {
message: format!("Properties file {} does not exist", path.display()),
});
}
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
for (index, line) in reader.lines().enumerate() {
let line = match line {
Ok(s) => s,
Err(_) => {
return Err(CLIError {
message: format!("Can not parse line {} of {}", index + 1, path.display()),
})
}
};
let line = line.trim();
if line.starts_with('#') || line.is_empty() {
continue;
}
let (name, value) = parse_variable(line)?;
if variables.contains_key(name.as_str()) {
return Err(CLIError {
message: format!("Variable {} defined twice!", name),
});
}
variables.insert(name.to_string(), value[1..].to_string());
}
}
if matches.is_present("variable") {
let input: Vec<_> = matches.values_of("variable").unwrap().collect();
for s in input {
match s.find('=') {
None => {
return Err(CLIError {
message: format!("Missing variable value for {}!", s),
});
}
Some(index) => {
let (name, value) = s.split_at(index);
if variables.contains_key(name) {
return Err(CLIError {
message: format!("Variable {} defined twice!", name),
});
}
variables.insert(name.to_string(), value[1..].to_string());
}
};
let (name, value) = parse_variable(s)?;
if variables.contains_key(name.as_str()) {
return Err(CLIError {
message: format!("Variable {} defined twice!", name),
});
}
variables.insert(name.to_string(), value[1..].to_string());
}
}
Ok(variables)
}
fn parse_variable(s: &str) -> Result<(String, String), CLIError> {
match s.find('=') {
None => Err(CLIError {
message: format!("Missing variable value for {}!", s),
}),
Some(index) => {
let (name, value) = s.split_at(index);
Ok((name.to_string(), value.to_string()))
}
}
}
fn app() -> clap::App<'static, 'static> {
clap::App::new("hurl")
//.author(clap::crate_authors!())
@ -450,6 +490,13 @@ fn app() -> clap::App<'static, 'static> {
.help("Define a variable")
.takes_value(true),
)
.arg(
clap::Arg::with_name("variables_file")
.long("variables-file")
.value_name("FILE")
.help("Define a properties file in which you define your variables")
.takes_value(true),
)
.arg(
clap::Arg::with_name("verbose")
.short("v")