mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-11-23 00:44:55 +03:00
Check that variables do not conflict with existing functions
This commit is contained in:
parent
d8dd240acf
commit
298d0eea0c
7
integration/hurl/tests_error_parser/invalid_variable.err
Normal file
7
integration/hurl/tests_error_parser/invalid_variable.err
Normal 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
|
||||
|
|
||||
|
@ -0,0 +1,2 @@
|
||||
2
|
||||
|
@ -0,0 +1,6 @@
|
||||
GET http://localhost:8000
|
||||
[Options]
|
||||
variable: newFoo=Bar
|
||||
variable: newUuid=123
|
||||
|
||||
|
3
integration/hurl/tests_error_parser/invalid_variable.ps1
Normal file
3
integration/hurl/tests_error_parser/invalid_variable.ps1
Normal file
@ -0,0 +1,3 @@
|
||||
Set-StrictMode -Version latest
|
||||
$ErrorActionPreference = 'Stop'
|
||||
hurl tests_error_parser/invalid_variable.hurl
|
3
integration/hurl/tests_error_parser/invalid_variable.sh
Executable file
3
integration/hurl/tests_error_parser/invalid_variable.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
set -Eeuo pipefail
|
||||
hurl tests_error_parser/invalid_variable.hurl
|
1
integration/hurl/tests_failed/invalid_variable.err
Normal file
1
integration/hurl/tests_failed/invalid_variable.err
Normal file
@ -0,0 +1 @@
|
||||
error: Variable newUuid conflicts with the newUuid function, use a different name.
|
1
integration/hurl/tests_failed/invalid_variable.exit
Normal file
1
integration/hurl/tests_failed/invalid_variable.exit
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
integration/hurl/tests_failed/invalid_variable.hurl
Normal file
1
integration/hurl/tests_failed/invalid_variable.hurl
Normal file
@ -0,0 +1 @@
|
||||
|
6
integration/hurl/tests_failed/invalid_variable.ps1
Normal file
6
integration/hurl/tests_failed/invalid_variable.ps1
Normal 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
|
||||
|
5
integration/hurl/tests_failed/invalid_variable.sh
Executable file
5
integration/hurl/tests_failed/invalid_variable.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -Eeuo pipefail
|
||||
set +e
|
||||
hurl --variable newFoo=Bar --variable newUuid=123 tests_failed/invalid_variable.hurl
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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>,
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user