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

Implement function newUuid #3426

Merged
merged 3 commits into from
Nov 22, 2024
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
4 changes: 2 additions & 2 deletions integration/hurl/tests_failed/runner_errors.err.pattern
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ error: Unauthorized file access
| ^^^^^^^^^^ unauthorized access to file /root/file, check --file-root option
|

error: Unrenderable variable
error: Unrenderable expression
--> tests_failed/runner_errors.hurl:165:4
|
| GET http://localhost:8000/runner_errors
165 | `{{list}}`
| ^^^^ variable <list> with value [1,2,3] can not be rendered
| ^^^^ expression with value [1,2,3] can not be rendered
|

error: Decompression error
Expand Down
4 changes: 2 additions & 2 deletions integration/hurl/tests_failed/runner_errors_color.err.pattern
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@
 | ^^^^^^^^^^ unauthorized access to file /root/file, check --file-root option
 |

error: Unrenderable variable
error: Unrenderable expression
--> tests_failed/runner_errors.hurl:165:4
 |
 | GET http://localhost:8000/runner_errors
165 | `{{list}}`
 | ^^^^ variable <list> with value [1,2,3] can not be rendered
 | ^^^^ expression with value [1,2,3] can not be rendered
 |

error: Decompression error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
error: Unrenderable variable
error: Unrenderable expression
--> tests_failed/template_variable_not_renderable.hurl:12:9
|
| GET http://localhost:8000/undefined
| ...
12 | list: {{list}}
| ^^^^ variable <list> with value [1,2,3] can not be rendered
| ^^^^ expression with value [1,2,3] can not be rendered
|

error: Unrenderable variable
error: Unrenderable expression
--> tests_failed/template_variable_not_renderable.hurl:17:11
|
| GET http://localhost:8000/undefined
| ...
17 | object: {{object}}
| ^^^^^^ variable <object> with value Object() can not be rendered
| ^^^^^^ expression with value Object() can not be rendered
|

error: Unrenderable variable
error: Unrenderable expression
--> tests_failed/template_variable_not_renderable.hurl:22:12
|
| GET http://localhost:8000/undefined
| ...
22 | nodeset: {{nodeset}}
| ^^^^^^^ variable <nodeset> with value Nodeset(size=1) can not be rendered
| ^^^^^^^ expression with value Nodeset(size=1) can not be rendered
|

4 changes: 4 additions & 0 deletions integration/hurl/tests_ok/function.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GET http://localhost:8000/function
[Query]
uuid: {{newUuid}}
HTTP 200
3 changes: 3 additions & 0 deletions integration/hurl/tests_ok/function.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_ok/function.hurl
11 changes: 11 additions & 0 deletions integration/hurl/tests_ok/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import re

from app import app
from flask import request


@app.route("/function")
def function():
uuid_pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
assert re.match(uuid_pattern, request.args.get("uuid"))
return ""
3 changes: 3 additions & 0 deletions integration/hurl/tests_ok/function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_ok/function.hurl
4 changes: 4 additions & 0 deletions integration/hurlfmt/tests_export/function.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/hello</span></span>
<span class="line"><span class="section-header">[Query]</span></span>
<span class="line"><span class="string">uuid</span>: <span class="string">{{newUuid}}</span></span>
</span></span></code></pre>
3 changes: 3 additions & 0 deletions integration/hurlfmt/tests_export/function.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET http://localhost:8000/hello
[Query]
uuid: {{newUuid}}
1 change: 1 addition & 0 deletions integration/hurlfmt/tests_export/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"entries":[{"request":{"method":"GET","url":"http://localhost:8000/hello","query_string_params":[{"name":"uuid","value":"{{newUuid}}"}]}}]}
3 changes: 3 additions & 0 deletions integration/hurlfmt/tests_export/function.lint.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET http://localhost:8000/hello
[Query]
uuid: {{newUuid}}
10 changes: 4 additions & 6 deletions packages/hurl/src/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ pub enum RunnerErrorKind {
TemplateVariableNotDefined {
name: String,
},

UnrenderableVariable {
name: String,
UnrenderableExpression {
value: String,
},
/// Unauthorized file access, check `--file-root` option.
Expand Down Expand Up @@ -157,7 +155,7 @@ impl DisplaySourceError for RunnerError {
RunnerErrorKind::UnauthorizedFileAccess { .. } => {
"Unauthorized file access".to_string()
}
RunnerErrorKind::UnrenderableVariable { .. } => "Unrenderable variable".to_string(),
RunnerErrorKind::UnrenderableExpression { .. } => "Unrenderable expression".to_string(),
}
}

Expand Down Expand Up @@ -306,8 +304,8 @@ impl DisplaySourceError for RunnerError {
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
RunnerErrorKind::UnrenderableVariable { name, value } => {
let message = &format!("variable <{name}> with value {value} can not be rendered");
RunnerErrorKind::UnrenderableExpression { value } => {
let message = &format!("expression with value {value} can not be rendered");
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
Expand Down
29 changes: 12 additions & 17 deletions packages/hurl/src/runner/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::runner::error::{RunnerError, RunnerErrorKind};
use crate::runner::value::Value;
use crate::runner::VariableSet;

use super::function;

/// Evaluates the expression `expr` with `variables` map, returns a [`Value`] on success or an [`RunnerError`] .
pub fn eval(expr: &Expr, variables: &VariableSet) -> Result<Value, RunnerError> {
match &expr.kind {
Expand All @@ -34,28 +36,21 @@ pub fn eval(expr: &Expr, variables: &VariableSet) -> Result<Value, RunnerError>
Err(RunnerError::new(variable.source_info, kind, false))
}
}
ExprKind::Function(_function) => todo!(),
ExprKind::Function(fct) => function::eval(fct),
}
}

/// Render the expression `expr` with `variables` map, returns a [`String`] on success or an [`RunnerError`] .
pub fn render(expr: &Expr, variables: &VariableSet) -> Result<String, RunnerError> {
match &expr.kind {
ExprKind::Variable(variable) => {
let source_info = variable.source_info;
let name = &variable.name;
let value = eval(expr, variables)?;
if value.is_renderable() {
Ok(value.to_string())
} else {
let kind = RunnerErrorKind::UnrenderableVariable {
name: name.to_string(),
value: value.to_string(),
};
Err(RunnerError::new(source_info, kind, false))
}
}
ExprKind::Function(_) => todo!(),
let source_info = expr.source_info;
let value = eval(expr, variables)?;
if value.is_renderable() {
Ok(value.to_string())
} else {
let kind = RunnerErrorKind::UnrenderableExpression {
value: value.to_string(),
};
Err(RunnerError::new(source_info, kind, false))
}
}

Expand Down
32 changes: 32 additions & 0 deletions packages/hurl/src/runner/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2024 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use hurl_core::ast::Function;
use uuid::Uuid;

use crate::runner::error::RunnerError;
use crate::runner::value::Value;

/// Evaluates the function `function`, returns a [`Value`] on success or an [`RunnerError`] .
pub fn eval(function: &Function) -> Result<Value, RunnerError> {
match &function {
Function::NewUuid => {
let uuid = Uuid::new_v4();
Ok(Value::String(uuid.to_string()))
}
}
}
1 change: 1 addition & 0 deletions packages/hurl/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod error;
mod event;
mod expr;
mod filter;
mod function;
mod hurl_file;
mod json;
mod multiline;
Expand Down
3 changes: 1 addition & 2 deletions packages/hurl/src/runner/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ mod tests {
);
assert_eq!(
error.kind,
RunnerErrorKind::UnrenderableVariable {
name: "name".to_string(),
RunnerErrorKind::UnrenderableExpression {
value: "[1,2]".to_string()
}
);
Expand Down
Loading