Skip to content

Commit cdc4398

Browse files
committed
Tests: Make $_path support dots in paths (#28917)
`$_path` is used by documentation tests to ignore a value from a response, for example: ``` [source,js] ---- { "count": 1, "datafeeds": [ { "datafeed_id": "datafeed-total-requests", "state": "started", "node": { ... "attributes": { "ml.machine_memory": "17179869184", "ml.max_open_jobs": "20", "ml.enabled": "true" } }, "assignment_explanation": "" } ] } ---- // TESTRESPONSE[s/"17179869184"/$body.$_path/] ``` That example shows `17179869184` in the compiled docs but when it runs the tests generated by that doc it ignores `17179869184` and asserts instead that there is a value in that field. This is required because we can't predict things like "how many milliseconds will this take?" and "how much memory will this take?". Before this change it was impossible to use `$_path` when any component of the path contained a `.`. This fixes the `$_path` evaluator to properly escape `.`. Closes #28770
1 parent 7a764d8 commit cdc4398

File tree

2 files changed

+24
-13
lines changed
  • test/framework/src

2 files changed

+24
-13
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Stash.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ private Object getValue(List<Object> path, String key) throws IOException {
184184
StringBuilder pathBuilder = new StringBuilder();
185185
Iterator<Object> element = path.iterator();
186186
if (element.hasNext()) {
187-
pathBuilder.append(element.next());
187+
pathBuilder.append(element.next().toString().replace(".", "\\."));
188188
while (element.hasNext()) {
189189
pathBuilder.append('.');
190-
pathBuilder.append(element.next());
190+
pathBuilder.append(element.next().toString().replace(".", "\\."));
191191
}
192192
}
193193
String builtPath = Matcher.quoteReplacement(pathBuilder.toString());

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/StashTests.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,22 @@ public void testReplaceStashedValuesStashKeyInList() throws IOException {
119119

120120
public void testPathInList() throws IOException {
121121
Stash stash = new Stash();
122-
stash.stashValue("body", singletonMap("foo", Arrays.asList("a", "b")));
122+
String topLevelKey;
123+
if (randomBoolean()) {
124+
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
125+
} else {
126+
topLevelKey = randomAlphaOfLength(5);
127+
}
128+
stash.stashValue("body", singletonMap(topLevelKey, Arrays.asList("a", "b")));
123129

124130
Map<String, Object> expected;
125131
Map<String, Object> map;
126132
if (randomBoolean()) {
127-
expected = singletonMap("foo", Arrays.asList("test", "boooooh!"));
128-
map = singletonMap("foo", Arrays.asList("test", "${body.$_path}oooooh!"));
133+
expected = singletonMap(topLevelKey, Arrays.asList("test", "boooooh!"));
134+
map = singletonMap(topLevelKey, Arrays.asList("test", "${body.$_path}oooooh!"));
129135
} else {
130-
expected = singletonMap("foo", Arrays.asList("test", "b"));
131-
map = singletonMap("foo", Arrays.asList("test", "$body.$_path"));
136+
expected = singletonMap(topLevelKey, Arrays.asList("test", "b"));
137+
map = singletonMap(topLevelKey, Arrays.asList("test", "$body.$_path"));
132138
}
133139

134140
Map<String, Object> actual = stash.replaceStashedValues(map);
@@ -138,21 +144,26 @@ public void testPathInList() throws IOException {
138144

139145
public void testPathInMapValue() throws IOException {
140146
Stash stash = new Stash();
141-
stash.stashValue("body", singletonMap("foo", singletonMap("a", "b")));
147+
String topLevelKey;
148+
if (randomBoolean()) {
149+
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
150+
} else {
151+
topLevelKey = randomAlphaOfLength(5);
152+
}
153+
stash.stashValue("body", singletonMap(topLevelKey, singletonMap("a", "b")));
142154

143155
Map<String, Object> expected;
144156
Map<String, Object> map;
145157
if (randomBoolean()) {
146-
expected = singletonMap("foo", singletonMap("a", "boooooh!"));
147-
map = singletonMap("foo", singletonMap("a", "${body.$_path}oooooh!"));
158+
expected = singletonMap(topLevelKey, singletonMap("a", "boooooh!"));
159+
map = singletonMap(topLevelKey, singletonMap("a", "${body.$_path}oooooh!"));
148160
} else {
149-
expected = singletonMap("foo", singletonMap("a", "b"));
150-
map = singletonMap("foo", singletonMap("a", "$body.$_path"));
161+
expected = singletonMap(topLevelKey, singletonMap("a", "b"));
162+
map = singletonMap(topLevelKey, singletonMap("a", "$body.$_path"));
151163
}
152164

153165
Map<String, Object> actual = stash.replaceStashedValues(map);
154166
assertEquals(expected, actual);
155167
assertThat(actual, not(sameInstance(map)));
156168
}
157-
158169
}

0 commit comments

Comments
 (0)