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

docs(ci): automate parsing and running of docs test #254

Draft
wants to merge 3 commits into
base: release/v0.50
Choose a base branch
from
Draft
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
105 changes: 105 additions & 0 deletions docs/scripts/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import os
import re
import subprocess
import time

import requests

# current file location
curr_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(curr_dir)

# Commands which block the process
BLOCKING_START_COMMANDS = ["local-ic start", "make testnet", "make sh-testnet"]
ignore_commands = ["gh repo create"]

START_PID = -1
DEBUGGING = False

def main():
demos = os.path.join(parent_dir, "versioned_docs", "version-v0.50.x", "03-demos")
with open(os.path.join(demos, "03-tokenfactory.md"), "r") as f:
parse_docs(f.read())

def poll_for_start(API_URL: str, pid=-1, waitSeconds=300):
for i in range(waitSeconds + 1):
try:
requests.get(API_URL)
return
except Exception:
# if i % 5 == 0:
print(f"waiting for server to start (iter:{i}) ({API_URL})")
time.sleep(1)

if pid != -1:
print(f"Killing process with pid for failing to start: {pid}")
os.system(f"kill {pid}")

raise Exception("Server did not start")

def clean_lines(text: str) -> list[str]:
# remove comment lines
sec = [line for line in text.split("\n") if not line.startswith("#")]

for l in sec:
l = l.strip()
# if l starts with any in ignore-commands, remove the line from the section
if any([l.startswith(cmd) for cmd in ignore_commands]):
sec.remove(l)

# remove blank lines
return [line for line in sec if line.strip() != ""]

def get_env_variables_from_lines(lines: list[str]) -> dict[str, str]:
env_vars = {}
for line in lines:
# DENOM=factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
match = re.match(r"([A-Z_]+)=(.*)", line)
if match:
env_vars[match.group(1)] = match.group(2)

return env_vars

def parse_docs(text: str):
global START_PID

# split the text by ```bash
sections = text.split("```bash")
for section in sections[1:]: # first section has nothing we want
end = section.find("```")
sec = clean_lines(section[:end])

if len(sec) == 0:
# print("debugging: empty (commands are ignored / there is nothing here)")
continue

secStr = "\n".join(sec)

print("="*20, "\n", secStr, "\n", "="*20)

# run sec as the os terminal
if not DEBUGGING:
# if sec contains anything from within BLOCKING_START_COMMANDS, it should run in its
# own terminal

# TODO: keep up with these globally and unset at end? so we dont polute test
envs = get_env_variables_from_lines(sec)
for k, v in envs.items():
os.environ[k] = v
print(f"{k}={v}")

if any([cmd in sec for cmd in BLOCKING_START_COMMANDS]):
START_PID = subprocess.Popen(secStr, shell=True).pid
print(f"Started process with pid: {START_PID}")
poll_for_start("http://127.0.0.1:26657", START_PID, waitSeconds=60)
else:
os.system(secStr)
time.sleep(2)

# input("--- Press Enter to continue...")

# TODO: verification logic here
# Kill / cleanup the instance (direct from pid would be nice, or just a killall <binary>)

if __name__ == '__main__':
main()
12 changes: 6 additions & 6 deletions docs/versioned_docs/version-v0.50.x/03-demos/03-tokenfactory.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The `denom_creation_fee` is a cost the application can set for creating new toke
The `denom_creation_gas_consume` is the amount of indirect resource cost to consume for creating a new token.
It is a more indirect approach to charging and is a better experience overall for developers on a network.

```bash
```text
params:
denom_creation_fee: []
denom_creation_gas_consume: "100000"
Expand All @@ -92,7 +92,7 @@ rolld q tokenfactory denoms-from-creator $(rolld keys show acc0 -a)

<details>
<summary>denoms-from-creator output</summary>
```bash
```text
denoms:
- factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
```
Expand Down Expand Up @@ -129,7 +129,7 @@ rolld q bank denom-metadata $DENOM

<details>
<summary>bank denom-metadata output</summary>
```bash
```text
metadata:
base: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
denom_units:
Expand Down Expand Up @@ -162,7 +162,7 @@ rolld q bank balances $(rolld keys show acc0 -a)

<details>
<summary>bank balances output</summary>
```bash
```text
balances:
- amount: "5000000"
denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
Expand All @@ -189,7 +189,7 @@ rolld q bank balances $(rolld keys show acc1 -a)

<details>
<summary>mint-to output</summary>
```bash
```text
balances:
- amount: "1000000"
denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
Expand All @@ -206,7 +206,7 @@ note, you can check for just a specific token balance with
rolld q bank balance $(rolld keys show acc0 -a) $DENOM
```

```bash
```text
balance:
amount: "5000000"
denom: factory/roll1hj5fveer5cjtn4wd6wstzugjfdxzl0xpg2te87/mytoken
Expand Down
Loading