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

Update doctest code for more consistent runs #3053

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions doctest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ def path = project(':').projectDir
def plugin_path = project(':doctest').projectDir

task cloneSqlCli(type: Exec) {
// clone the sql-cli repo locally
commandLine 'git', 'clone', 'https://github.com/opensearch-project/sql-cli.git'
def repoDir = new File("${project.projectDir}/sql-cli")

if (repoDir.exists()) {
// Repository already exists, fetch and checkout latest
commandLine 'git', '-C', repoDir.absolutePath, 'fetch', 'origin', 'main'
commandLine 'git', '-C', repoDir.absolutePath, 'checkout', 'origin/main'
} else {
// Repository doesn't exist, clone it
commandLine 'git', 'clone', 'https://github.com/opensearch-project/sql-cli.git', repoDir.absolutePath
}
}

task bootstrap(type: Exec, dependsOn: ['cloneSqlCli', 'spotlessJava']) {
Expand Down
28 changes: 26 additions & 2 deletions doctest/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,34 @@ def process(self, statement):
click.echo(output)


"""
For _explain requests, there are several additional request fields that will inconsistently
appear/change depending on underlying cluster state. This method normalizes these responses in-place
to make _explain doctests more consistent.

If the passed response is not an _explain response, the input is left unmodified.
"""
def normalize_explain_response(data):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems all query reponse are impacted, not only the explain query. do u know why the format is different? does it impact rest api customer?

Copy link
Collaborator Author

@Swiddis Swiddis Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting changes because of cli-helpers updating from v1 to v2 in sql-cli, which removes some extra whitespace from most responses. It's separate from the _explain issue.

if "root" in data:
data = data["root"]

if (request := data.get("description", {}).get("request", None)) and request.startswith("OpenSearchQueryRequest("):
for filter_field in ["needClean", "pitId", "cursorKeepAlive", "searchAfter", "searchResponse"]:
request = re.sub(f", {filter_field}=\\w+", "", request)
data["description"]["request"] = request

for child in data.get("children", []):
normalize_explain_response(child)

return data


def pretty_print(s):
try:
d = json.loads(s)
print(json.dumps(d, indent=2))
data = json.loads(s)
normalize_explain_response(data)

print(json.dumps(data, indent=2))
except json.decoder.JSONDecodeError:
print(s)

Expand Down
Loading