Check that variables do not conflict with existing functions

This commit is contained in:
Fabrice Reix 2024-09-15 08:35:56 +02:00
parent d8dd240acf
commit 298d0eea0c
No known key found for this signature in database
GPG Key ID: BF5213154B2E7155
14 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,7 @@
error: Parsing variable
--> tests_error_parser/invalid_variable.hurl:4:11
|
4 | variable: newUuid=123
| ^ conflicts with the newUuid function, use a different name
|

View File

@ -0,0 +1,2 @@
2

View File

@ -0,0 +1,6 @@
GET http://localhost:8000
[Options]
variable: newFoo=Bar
variable: newUuid=123

View File

@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/invalid_variable.hurl

View File

@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/invalid_variable.hurl

View File

@ -0,0 +1 @@
error: Variable newUuid conflicts with the newUuid function, use a different name.

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,6 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
$ErrorActionPreference = 'Continue'
hurl --variable newFoo=Bar --variable newUuid=123 tests_failed/invalid_variable.hurl

View File

@ -0,0 +1,5 @@
#!/bin/bash
set -Eeuo pipefail
set +e
hurl --variable newFoo=Bar --variable newUuid=123 tests_failed/invalid_variable.hurl

View File

@ -18,6 +18,7 @@
use super::CliOptionsError;
use crate::runner::{Number, Value};
use hurl_core::ast::is_variable_reserved;
pub fn parse(s: &str) -> Result<(String, Value), CliOptionsError> {
match s.find('=') {
@ -26,6 +27,11 @@ pub fn parse(s: &str) -> Result<(String, Value), CliOptionsError> {
))),
Some(index) => {
let (name, value) = s.split_at(index);
if is_variable_reserved(name) {
return Err(CliOptionsError::Error(format!(
"Variable {name} conflicts with the {name} function, use a different name."
)));
}
let value = parse_value(&value[1..])?;
Ok((name.to_string(), value))
}

View File

@ -700,6 +700,12 @@ pub struct Variable {
pub source_info: SourceInfo,
}
/// Check that variable name is not reserved
/// (would conflicts with an existing function)
pub fn is_variable_reserved(name: &str) -> bool {
["getEnv", "newDate", "newUuid"].contains(&name)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EntryOption {
pub line_terminators: Vec<LineTerminator>,

View File

@ -62,6 +62,7 @@ pub enum ParseErrorKind {
Unicode,
UrlIllegalCharacter(char),
UrlInvalidStart,
Variable(String),
Version,
XPathExpr,
Xml,
@ -126,6 +127,7 @@ impl DisplaySourceError for ParseError {
ParseErrorKind::Unicode => "Parsing unicode literal".to_string(),
ParseErrorKind::UrlIllegalCharacter(_) => "Parsing URL".to_string(),
ParseErrorKind::UrlInvalidStart => "Parsing URL".to_string(),
ParseErrorKind::Variable(_) => "Parsing variable".to_string(),
ParseErrorKind::Version => "Parsing version".to_string(),
ParseErrorKind::XPathExpr => "Parsing XPath expression".to_string(),
ParseErrorKind::Xml => "Parsing XML".to_string(),
@ -242,6 +244,7 @@ impl DisplaySourceError for ParseError {
ParseErrorKind::Unicode => "Invalid unicode literal".to_string(),
ParseErrorKind::UrlIllegalCharacter(c) => format!("illegal character <{c}>"),
ParseErrorKind::UrlInvalidStart => "expecting http://, https:// or {{".to_string(),
ParseErrorKind::Variable(message) => message.clone(),
ParseErrorKind::Version => {
"HTTP version must be HTTP, HTTP/1.0, HTTP/1.1 or HTTP/2".to_string()
}

View File

@ -350,6 +350,11 @@ fn variable_name(reader: &mut Reader) -> ParseResult<String> {
value: "variable name".to_string(),
};
return Err(ParseError::new(start.pos, false, kind));
} else if is_variable_reserved(&name) {
let kind = ParseErrorKind::Variable(format!(
"conflicts with the {name} function, use a different name"
));
return Err(ParseError::new(start.pos, false, kind));
}
Ok(name)
}