Skip to content

Commit

Permalink
refactor readme generation and testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Marmare314 committed Sep 29, 2023
1 parent 4ef45c5 commit d673580
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 228 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: python tests/tools/run_tests.py
- run: python tools/check_tests.py
- run: python tools/check_readme.py
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
tests/tools/__pycache__/
tests/outputs/
docs/tmp/
tools/__pycache__/
tools/tmp/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ features, feel free to open an issue on

The result should now look something like this:

![image](docs/images/image_0.png)
![image](docs/images/basic-usage.png)

## Examples

Expand All @@ -93,7 +93,7 @@ This example shows how corollaries can be numbered after the last theorem.
#corollary(lorem(5))
```

![image](docs/images/image_1.png)
![image](docs/images/corollary-numbering-example.png)

## Custom style example

Expand Down Expand Up @@ -137,6 +137,6 @@ This example shows how corollaries can be numbered after the last theorem.
]
```

![image](docs/images/image_2.png)
![image](docs/images/custom-style-example.png)

For a full documentation of all functions check [readme.pdf](docs/readme.pdf)
18 changes: 0 additions & 18 deletions docs/generate_pdf.py

This file was deleted.

86 changes: 0 additions & 86 deletions docs/generate_readme.py

This file was deleted.

File renamed without changes
File renamed without changes
File renamed without changes
Binary file modified docs/readme.pdf
Binary file not shown.
13 changes: 8 additions & 5 deletions docs/readme.typ
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
reset-counter: reset-counter
)

#let export-code(code, setup, imports) = {
#let export-code(name, code, setup, imports) = {
imports = "#import \"../../src/export-lib.typ\": " + imports.join(", ")
code = imports + "\n" + setup + "\n" + code

[#metadata((code: code)) <generate-image>]
[#metadata((code: code, name: name)) <generate-image>]
}

#let eval-raws(..raws, scope: (:), export-setup: "") = {
#let eval-raws(..raws, name, scope: (:), export-setup: "") = {
assert(raws.named().len() == 0)
let code = raws.pos().map(x => x.text + "\n").sum()
[
Expand All @@ -28,7 +28,7 @@
eval(code, mode: "markup", scope: scope)
) <ignore-content>
]
export-code(code, export-setup, scope.keys())
export-code(name, code, export-setup, scope.keys())
}

#let code-with-import(..imports, code: raw("")) = {
Expand Down Expand Up @@ -97,7 +97,7 @@
}
} else if con.func() == metadata {
if con.has("label") and con.label == <generate-image> {
"<GENERATE-IMAGE>" + con.value.code + "</GENERATE-IMAGE>"
"<GENERATE-IMAGE:" + con.value.name + ">" + con.value.code + "</GENERATE-IMAGE>"
} else {
panic("metadata without generate-image label")
}
Expand Down Expand Up @@ -189,6 +189,7 @@
#eval-raws(
step2, step3, step4, step5,
"basic-usage",
scope: basic-usage-scope,
export-setup: "#set page(width: 300pt, height: auto, margin: 10pt)"
)
Expand Down Expand Up @@ -219,6 +220,7 @@
#eval-raws(
example1,
"corollary-numbering-example",
scope: example1-scope,
export-setup: "#set page(width: 300pt, height: auto, margin: 10pt)"
)
Expand Down Expand Up @@ -268,6 +270,7 @@
#eval-raws(
example2,
"custom-style-example",
scope: example2-scope,
export-setup: "#set page(width: 500pt, height: auto, margin: 10pt)"
)
Expand Down
34 changes: 0 additions & 34 deletions tests/tools/common.py

This file was deleted.

45 changes: 0 additions & 45 deletions tests/tools/run_tests.py

This file was deleted.

33 changes: 0 additions & 33 deletions tests/tools/update_reference.py

This file was deleted.

45 changes: 45 additions & 0 deletions tools/check_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from generate_readme import split_images, combine_markdown, IMAGE_FOLDER, query_to_markdown, README_TEMPLATE_PATH, README_PATH, README_PDF_PATH
from common import TypstRunner, ToolStatus, compare_files
import os

def check_images(split_markdown, runner, status):
if type(split_markdown) == dict:
name, code, after = split_markdown["name"], split_markdown["code"], split_markdown["after"]
image_path = os.path.join(IMAGE_FOLDER, name + ".png")

status.start_action("Checking " + image_path)
output_path = runner.tmp_file_path(name + ".png")
runner.compile_code(code, name + ".typ", output_path)
if status.check_runner(runner):
status.end_action(compare_files(image_path, output_path))
else:
status.end_action(False)

check_images(split_markdown["after"], runner, status)

def main():
with TypstRunner() as runner, ToolStatus("Readme is up to date", "Readme is not up to date") as status:
status.start_action("Compiling " + README_TEMPLATE_PATH)
output_path = runner.tmp_file_path("readme.pdf")
runner.compile_file(README_TEMPLATE_PATH, output_path)
if status.check_runner(runner):
status.end_action(compare_files(README_PDF_PATH, output_path))
else:
status.end_action(False)

status.start_action("Querying " + README_TEMPLATE_PATH)
query_result, query_success = runner.query_file(README_TEMPLATE_PATH, "<export>")
status.end_action(query_success)

markdown_with_tags = query_to_markdown(query_result)
split_markdown = split_images(markdown_with_tags)

check_images(split_markdown, runner, status)

status.start_action("Checking " + README_PATH)
markdown = combine_markdown(split_markdown)
with open(README_PATH) as file:
status.end_action(file.read() == markdown)

if __name__ == "__main__":
main()
19 changes: 19 additions & 0 deletions tools/check_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from common import ToolStatus, TypstRunner, compare_files
from generate_references import list_tests, input_file_path, reference_file_path, has_reference_check

def main():
with TypstRunner() as runner, ToolStatus("All tests passed", "Some tests failed") as status:
for test in list_tests():
status.start_action("Checking " + input_file_path(test))
output_path = runner.tmp_file_path(test + ".png")
runner.compile_file(input_file_path(test), output_path)
if status.check_runner(runner):
if has_reference_check(test):
status.end_action(compare_files(output_path, reference_file_path(test)))
else:
status.end_action(True)
else:
status.end_action(False)

if __name__ == "__main__":
main()
Loading

0 comments on commit d673580

Please sign in to comment.