From 07442ee78170d019c5b0273c3b95d27163af7803 Mon Sep 17 00:00:00 2001
From: Pietro Peterlongo
Date: Sun, 6 Mar 2022 16:28:08 +0100
Subject: [PATCH] Refactor nb blocks (fix #24, fix #52) (#78)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
his PR changes completely the `NbBlock` type that is at the basis of Nimib and makes it easier to create custom blocks.
A block will now be created using `newNbBlock` template (in `nimib / blocks`, exported) where we assign a `command` to the block (e.g. `"nbText", "nCode", "nbSource"`) and in order for rendering to be correct, `partials` and a sequence of `renderProc`s need to be defined associated to the above command. "Native" blocks and custom blocks are defined in the same way, the only difference is that for native blocks the block is defined in `src/nimib.nim` and the `partial`s and `renderProc`s are defined in `src/nimib/render` while in custom blocks they will likely be defined next to one other.
along with the above changes there are a few welcome add-ons:
- a better logging of what happens while running blocks
- some tests, sorely needed since ptest was (rightfully) removed
- a new partial `main`
some other accidental or not so welcome changes:
- `sugar` is now exported _(accidental)_
- `nb: NbDoc` is mutated when rendered _(unwelcome, will be changed later)_
- cannot use both Html and Md backend at the same time _(unwelcome, will be changed later)_
this feature will be the main highlight of next release (0.3) but in order to merge this PR some stuff needed for release is left out. Among stuff left out:
- `nbFile` is likely broken, should be fixed and tested inside Nimib (and examples should be added)
- documentation has not improved and it should
- I need to check that (some) projects depending on nimib are not broken (e.g. nimibook, scinim, ...)
after that a release of 0.3 should be in order. Release name will be **Block Maker**.
During work on this release I have also identified next milestone for 0.4: better handling of render backends (release name backendMaker). In particular:
> a big next change would be to use a json data in NbBlock instead of context and generate context during save. In this way you could go back to use multiple render backends at once, document could be made not mutable during save, ...
----
commits
* new newBlock (with test)
* code is stripped as default
* imporve tests
* add dev notes (to be removed at the end)
* moved templates outside of nbInit
* update dev notes
* preliminaries to change rendering of block
* newNbBlock refactored (and renamed)
* nbNormalize
* add note \c == \r and \l == \n
* change name to tests
* add new nbText, nbCode with tests
* only log output if there is output
* use always check
* new renderBlock working with (minimal) test
* nim r hello works! πΎ
* mostaccio ok
* numerical ok
* nolan ok
* pythno ok
* remove ptest
* general cleanup
* update nbImage to refactored blocks
* minimal plan to just merge
* cheatsheet fixed
* update nimble docs
* markdown backend + index
* update nimble docs and rerun
* more cleanup and remove NbKind
* finalize notes
* remove refactor notes
---
.github/workflows/test.yml | 2 +-
.gitignore | 1 +
README.md | 3 +-
docs/cheatsheet.html | 263 ++++++++++---------
docs/cheatsheet.nim | 34 +--
docs/far/from/home.html | 7 +-
docs/far/from/home.nim | 7 +-
docs/hello.html | 9 +-
docs/index.html | 33 ++-
docs/index.nim | 28 +-
docs/mostaccio.html | 65 ++---
docs/nolan.html | 1 -
docs/nolan_no_source.html | 1 -
docs/numerical.html | 21 +-
docs/numerical_default_style.html | 21 +-
docs/ptest.html | 418 ------------------------------
docs/ptest.nim | 167 ------------
docs/pythno.html | 6 +-
docs/pythno.nim | 2 +-
nimib.nimble | 20 +-
src/nimib.nim | 162 ++++++------
src/nimib/blocks.nim | 66 ++---
src/nimib/docs.nim | 5 +-
src/nimib/renders.nim | 149 +++++------
src/nimib/themes.nim | 17 +-
src/nimib/types.nim | 10 +-
tests/tblocks.nim | 35 +++
tests/tnimib.nim | 49 ++++
tests/trenders.nim | 19 ++
tests/tsources.nim | 8 +-
30 files changed, 556 insertions(+), 1073 deletions(-)
delete mode 100644 docs/ptest.html
delete mode 100644 docs/ptest.nim
create mode 100644 tests/tblocks.nim
create mode 100644 tests/tnimib.nim
create mode 100644 tests/trenders.nim
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 1e370c74..44f377cf 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -14,5 +14,5 @@ jobs:
nim-version: '1.6.x'
- run: nimble deps
- run: nimble tdeps
- #- run: nimble ptest
- run: nimble test
+ - run: nimble docs # not to publish but to test it works
diff --git a/.gitignore b/.gitignore
index fe0923cd..c239b2fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
*
!/**/
!*.*
+.DS_store
*.exe
*.tmp.html
diff --git a/README.md b/README.md
index 4e3ce61b..eb9628d4 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
+
# nimib π³ - nim π driven β΅ publishing β
Nimib provides an API to convert your Nim code and its outputs to html documents.
@@ -108,7 +109,6 @@ in this repo:
* [numerical](https://pietroppeter.github.io/nimib/numerical.html): example usage of NumericalNim (example of custom style, usage of latex)
* [cheatsheet](https://pietroppeter.github.io/nimib/cheatsheet.html): markdown cheatsheet (example of a custom block, custom highlighting and a simple TOC)
* [mostaccio](https://pietroppeter.github.io/nimib/mostaccio.html): examples of usage of nim-mustache and of dark mode.
-* [ptest](https://pietroppeter.github.io/nimib/ptest.html): print testing for nimib
elsewhere:
@@ -292,4 +292,3 @@ because I made a [package](https://github.com/pietroppeter/nimoji) for that and
because [someone made it into an art form](https://github.com/oakes/vim_cubed#q--a)
and they tell me [imitation is the sincerest form of flattery](https://www.goodreads.com/quotes/558084-imitation-is-the-sincerest-form-of-flattery-that-mediocrity-can)
-
diff --git a/docs/cheatsheet.html b/docs/cheatsheet.html
index cdccb6b9..99d3ca80 100644
--- a/docs/cheatsheet.html
+++ b/docs/cheatsheet.html
@@ -72,7 +72,16 @@
Table of Contents:
Headers
-
# H1
+
H1
+
H2
+
H3
+
H4
+
H5
+
H6
+
Alternatively, for H1 and H2, an underline-ish style:
+
Alt-H1
+
Alt-H2
+
# H1
## H2
### H3
#### H4
@@ -87,19 +96,14 @@
Headers
Alt-H2
------
-
H1
-
H2
-
H3
-
H4
-
H5
-
H6
-
Alternatively, for H1 and H2, an underline-ish style:
-
Alt-H1
-
Alt-H2
Emphasis
-
Emphasis, aka italics, with *asterisks* or _underscores_.
+
Emphasis, aka italics, with asterisks or underscores.
+
Strong emphasis, aka bold, with asterisks or underscores.
+
Combined emphasis with asterisks and underscores.
+
Strikethrough uses two tildes. Scratch this.
+
Emphasis, aka italics, with *asterisks* or _underscores_.
Strong emphasis, aka bold, with **asterisks** or __underscores__.
@@ -107,30 +111,10 @@
Emphasis
Strikethrough uses two tildes. ~~Scratch this.~~
-
Emphasis, aka italics, with asterisks or underscores.
-
Strong emphasis, aka bold, with asterisks or underscores.
-
Combined emphasis with asterisks and underscores.
-
Strikethrough uses two tildes. Scratch this.
Lists
(In this example, leading and trailing spaces are shown with with dots: β )
-
1. First ordered list item
-2. Another item
-β β β β * Unordered sub-list.
-1. Actual numbers don't matter, just that it's a number
-β β β β 1. Ordered sub-list
-4. And another item.
-
-β β β You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
-
-β β β To have a line break without a paragraph, you will need to use two trailing spaces.β β
-β β β Note that this line is separate, but within the same paragraph.β β
-β β β (This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)
-
-* Unordered list can use asterisks
-- Or minuses
-+ Or pluses
First ordered list item
@@ -164,6 +148,22 @@
Lists
Or pluses
+
1. First ordered list item
+2. Another item
+β β β β * Unordered sub-list.
+1. Actual numbers don't matter, just that it's a number
+β β β β 1. Ordered sub-list
+4. And another item.
+
+β β β You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
+
+β β β To have a line break without a paragraph, you will need to use two trailing spaces.β β
+β β β Note that this line is separate, but within the same paragraph.β β
+β β β (This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)
+
+* Unordered list can use asterisks
+- Or minuses
++ Or pluses
in ordered to have the ordered sublist work correctly
(it is not working on linked original) I increased indent from 2 to 4,
@@ -173,9 +173,19 @@
Lists
Links
There are two ways to create links, inline and reference.
-
[I'm an inline-style link](https://www.google.com)
+
URLs and URLs in angle brackets will automatically get turned into links.
+http://www.example.com or http://www.example.com and sometimes
+example.com (but not on Github, for example).
+
Some text to show that the reference links can follow later.
+
[I'm an inline-style link](https://www.google.com)
-[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
+[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
[I'm a reference-style link][Arbitrary case-insensitive reference text]
@@ -186,7 +196,7 @@
Links
Or leave it empty and use the [link text itself].
URLs and URLs in angle brackets will automatically get turned into links.
-http://www.example.com or and sometimes
+http://www.example.com or <http://www.example.com> and sometimes
example.com (but not on Github, for example).
Some text to show that the reference links can follow later.
@@ -195,37 +205,27 @@
Links
[1]: http://slashdot.org
[link text itself]: http://www.reddit.com
URLs and URLs in angle brackets will automatically get turned into links.
-http://www.example.com or http://www.example.com and sometimes
-example.com (but not on Github, for example).
-
Some text to show that the reference links can follow later.
only URL with angle brackets are turned into links here
Images
-
Here's our logo (hover to see the title text):
+
Here's our logo (hover to see the title text):
+
Inline-style:
+
+
Reference-style:
+
+
Here's our logo (hover to see the title text):
Inline-style:
-![alt text](https://nim-lang.org/assets/img/logo.svg "Logo Title Text 1")
+![alt text](https://nim-lang.org/assets/img/logo.svg "Logo Title Text 1")
Reference-style:
![alt text][logo]
-[logo]: https://nim-lang.org/assets/img/logo.svg "Logo Title Text 2"
+[logo]: https://nim-lang.org/assets/img/logo.svg "Logo Title Text 2"
-
Here's our logo (hover to see the title text):
-
Inline-style:
-
-
Reference-style:
-
Code and Syntax Highlighting
@@ -236,35 +236,35 @@
Code and Syntax Highlighting
for dozens of languages (and not-really-languages, like diffs and HTTP headers);
to see the complete list, and how to write the language names,
see the highlight.js demo page.
-
Inline `code` has `back-ticks around` it.
Inline code has back-ticks around it.
+
Inline `code` has `back-ticks around` it.
Blocks of code are either fenced by lines with three back-ticks ```,
or are indented with four spaces.
I recommend only using the fenced code blocks -- they're easier
and only they support syntax highlighting.
-
```javascript
-var s = "JavaScript syntax highlighting";
+
var s = "JavaScript syntax highlighting";
+alert(s);
+
+
s = "Python syntax highlighting"
+print s
+
+
No language indicated, so no syntax highlighting.
+But let's throw in a <b>tag</b>.
+
+
```javascript
+var s = "JavaScript syntax highlighting";
alert(s);
```
```python
-s = "Python syntax highlighting"
+s = "Python syntax highlighting"
print s
```
```
No language indicated, so no syntax highlighting.
-But let's throw in a tag.
-```
-
-
var s = "JavaScript syntax highlighting";
-alert(s);
-
-
s = "Python syntax highlighting"
-print s
-
-
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
+```
default syntax highlighting in nimib is for nim code only.
@@ -278,23 +278,6 @@
Tables
but they are part of GFM and Markdown Here supports them.
They are an easy way of adding tables to your email -- a task
that would otherwise require copy-pasting from another application.
-
Colons can be used to align columns.
-
-| Tables | Are | Cool |
-| ------------- |:-------------:| -----:|
-| col 3 is | right-aligned | $1600 |
-| col 2 is | centered | $12 |
-| zebra stripes | are neat | $1 |
-
-There must be at least 3 dashes separating each header cell.
-The outer pipes (|) are optional, and you don't need to make the
-raw Markdown line up prettily. You can also use inline Markdown.
-
-Markdown | Less | Pretty
---- | --- | ---
-*Still* | `renders` | **nicely**
-1 | 2 | 3
-
Colons can be used to align columns.
@@ -342,15 +325,26 @@
Tables
2
3
+
Colons can be used to align columns.
+
+| Tables | Are | Cool |
+| ------------- |:-------------:| -----:|
+| col 3 is | right-aligned | $1600 |
+| col 2 is | centered | $12 |
+| zebra stripes | are neat | $1 |
+
+There must be at least 3 dashes separating each header cell.
+The outer pipes (|) are optional, and you don't need to make the
+raw Markdown line up prettily. You can also use inline Markdown.
+
+Markdown | Less | Pretty
+--- | --- | ---
+*Still* | `renders` | **nicely**
+1 | 2 | 3
+
Blockquotes
-
> Blockquotes are very handy in email to emulate reply text.
-> This line is part of the same quote.
-
-Quote break.
-
-> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
Blockquotes are very handy in email to emulate reply text.
This line is part of the same quote.
@@ -359,27 +353,40 @@
Blockquotes
This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can putMarkdown into a blockquote.
+
> Blockquotes are very handy in email to emulate reply text.
+> This line is part of the same quote.
+
+Quote break.
+
+> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
Inline HTML
You can also use raw HTML in your Markdown, and it'll mostly work pretty well.
-
<dl>
- <dt>Definition list</dt>
- <dd>Is something people use sometimes.</dd>
-
- <dt>Markdown in HTML</dt>
- <dd>Does *not* work **very** well. Use HTML <em>tags</em>.</dd>
-</dl>
Definition list
Is something people use sometimes.
Markdown in HTML
Does *not* work **very** well. Use HTML tags.
+
<dl>
+ <dt>Definition list</dt>
+ <dd>Is something people use sometimes.</dd>
+
+ <dt>Markdown in HTML</dt>
+ <dd>Does *not* work **very** well. Use HTML <em>tags</em>.</dd>
+</dl>
Horizontal Rule
-
Three or more...
+
Three or more...
+
+
Hyphens
+
+
Asterisks
+
+
Underscores
+
Three or more...
---
@@ -392,13 +399,6 @@
Horizontal Rule
___
Underscores
-
Three or more...
-
-
Hyphens
-
-
Asterisks
-
-
Underscores
Line Breaks
@@ -408,16 +408,16 @@
Line Breaks
see what happens. You'll soon learn to get what you want.
"Markdown Toggle" is your friend.
Here are some things to try out:
-
Here's a line for us to start with.
+
Here's a line for us to start with.
+
This line is separated from the one above by two newlines, so it will be a separate paragraph.
+
This line is also a separate paragraph, but...
+This line is only separated by a single newline, so it's a separate line in the same paragraph.
+
Here's a line for us to start with.
This line is separated from the one above by two newlines, so it will be a *separate paragraph*.
This line is also a separate paragraph, but...
This line is only separated by a single newline, so it's a separate line in the *same paragraph*.
-
Here's a line for us to start with.
-
This line is separated from the one above by two newlines, so it will be a separate paragraph.
-
This line is also a separate paragraph, but...
-This line is only separated by a single newline, so it's a separate line in the same paragraph.
note that last two lines do not have a line break in between as the original
@@ -425,14 +425,13 @@
Line Breaks
YouTube Videos
They can't be added directly but you can add an image with a link to the video like this:
This was produced with `nim r docs/hello`, where [docs/hello.nim]({repo}/blob/main/docs/hello.nim) is:
""".emojize
-nbCode: discard
-nb.blk.code = "\n" & hello # "\n" should not be needed here (fix required in rendering)
+
+
+whennotdefined(useMdBackend):
+ nbCode: discard
+ nb.blk.code = hello # "\n" should not be needed here (fix required in rendering)
+else:
+ nbText &"""
+```nim
+{hello}
+```"""
+
+
nbText: fmd"""
### Other examples of usage
@@ -380,7 +389,6 @@
why the Q & A?
* [numerical]({docs}/numerical.html): example usage of NumericalNim (example of custom style, usage of latex)
* [cheatsheet]({docs}/cheatsheet.html): markdown cheatsheet (example of a custom block, custom highlighting and a simple TOC)
* [mostaccio]({docs}/mostaccio.html): examples of usage of nim-mustache and of dark mode.
-* [ptest]({docs}/ptest.html): print testing for nimib
elsewhere:
@@ -565,11 +573,12 @@
why the Q & A?
and they tell me [imitation is the sincerest form of flattery](https://www.goodreads.com/quotes/558084-imitation-is-the-sincerest-form-of-flattery-that-mediocrity-can)
""".emojize
-nbSave
-
-nbDoc.render = renderMark
-nbDoc.filename = "../README.md"
-nbSave
+whennotdefined(useMdBackend):
+ nbSave
+else:
+ nb.useMdBackend
+ nb.filename = "../README.md"
+ nbSave
The following is an adaption of treeform's ptest.nim
-taking into account specifics of nimib files: they already produce an output (html file).
-
Imports and global variables:
-
import
- os, osproc, strformat, strutils, nimib / paths, nimib / gits
-
-type
- SkipReason* = enum
- srNotSkipped, srNotTrackedInGit, srNoHtmlFound, srChangedInGit
- TestCase* = refobject## ref to make it easy to mutate while in a container
- file*: AbsoluteFile
- skip*: bool
- skipReason*: SkipReason
- fail*: bool
-
-func find*(s: seq[TestCase]; file: AbsoluteFile; test: varTestCase): bool =
- for t in s:
- if t.file == file:
- test = t
- returntrue
-
-func spanColor*(text, color: string): string =
- "<span style=\"color:" & color & "\">" & text & "</span>"
-
-func aLink*(text, link: string): string =
- "<a href=\"" & link & "\">" & text & "</a>"
-
-func stats*(tests: seq[TestCase]): tuple[skip, fail, pass: int] =
- for test in tests:
- if test.skip:
- inc result.skip
- continue
- elif test.fail:
- inc result.fail
- else:
- inc result.pass
-
-var
- test: TestCase
- tests: seq[TestCase]
-
test cases
-
for every file ending in nim (and not starting with ptest) in nbHomeDir (test cases)
-
-
a test case will be skipped if it is not tracked in git
-
a test case will be skipped if it does not have a corresponding html
-
a test case will be skipped if it is modified/added in git
-
-
let skipList = @["penguins.nim"]
-for file in walkDirRec(nbHomeDir):
- ## note that file is an AbsoluteFile (since nbHomeDir is an AbsoluteDir)
- if file.endsWith(".nim") andnot file.name.startsWith("ptest") and
- file.relPath notin skipList:
- test = TestCase(file: file)
- stdout.write "added test candidate: ", file.relPath
- else:
- continue
- let html = changeFileExt(file, ".html")
- ifnot file.isGitTracked:
- test.skip = true
- test.skipReason = srNotTrackedInGit
- echo" -> skipped since it is not tracked in git"
- elifnot html.fileExists ornot html.isGitTracked:
- test.skip = true
- test.skipReason = srNoHtmlFound
- echo" -> skipped since it does not have a corresponding (git tracked) html file"
- elif file.isGitChanged:
- test.skip = true
- test.skipReason = srChangedInGit
- echo" -> skipped since it is changed in git"
- else:
- echo""
- tests.add test
-
added test candidate: numerical.nim
-added test candidate: nolan.nim
-added test candidate: pythno.nim
-added test candidate: mostaccio.nim
-added test candidate: index.nim
-added test candidate: cheatsheet.nim
-added test candidate: hello.nim
-added test candidate: far/from/home.nim
-
-
performing test
-
for every non-skipped test case:
-
-
if the corresponding html output is changed in git the test is failed
-
copy the corresponding html to a tmp file
-
find and execute the corresponding nim file
-
if an error is found while compiling/executing, report a failure
-
if a difference is reported between the corresponding html file and the temporary copy a failure is reported
-
if no failure has been reported the test is reported as passed (and the temporary file is cleaned up)
-
-
for test in tests:
- var
- output: string
- err: int
- let
- html = test.file.changeFileExt(".html")
- tmp = test.file.changeFileExt(".tmp.html")
- fileWithLink = test.file.relPath.aLink html.relPath
- if test.skip:
- stdout.write "[SKIP]".spanColor "blue"
- echo" " & fileWithLink
- continue
- if html.isGitChanged:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"html file is changed in git. commit or revert and rerun test"
- continue
- copyFile(source = html, dest = tmp)
- (output, err) = execCmdEx &"nim --verbosity:0 --hints:off -d:nimibHomeDir=. r {test.file}"
- if err != 0:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"Error during compile/run"
- echo"output\n:", output
- echo"err\n:", err
- continue
- (output, err) = execCmdEx(&"git diff -w --ignore-space-change --no-index {html} {tmp}")
- if err != 0:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"Differences found in html file"
- echo"output\n:", output
- echo"err\n:", err
- continue
- stdout.write "[OK]".spanColor "green"
- echo" " & fileWithLink
- removeFile(tmp)
import nimib
-nbInit # this will also put me in docs folder
-
-nbText: """# Print-testing for nimib
-
-What is print-testing? Testing by comparing printed output of a file with a versioned reference (see [treeform/ptest](https://github.com/treeform/ptest)).
-"""
-nbText: """## Test Results"""
-var resultsBlock = nbBlock # I will add to it at the end
-
-nbText: """---
-# Implementation
-
-The following is an adaption of [treeform's ptest.nim](https://github.com/treeform/ptest/blob/master/src/ptest.nim)
-taking into account specifics of nimib files: they already produce an output (html file).
-
-Imports and global variables:
-"""
-nbCode:
- import os, osproc, strformat, strutils, nimib / paths, nimib / gits
-
- type
- SkipReason* = enum
- srNotSkipped, srNotTrackedInGit, srNoHtmlFound, srChangedInGit
- TestCase* = refobject## ref to make it easy to mutate while in a container
- file*: AbsoluteFile
- skip*: bool
- skipReason*: SkipReason
- fail*: bool
-
- func find*(s: seq[TestCase], file: AbsoluteFile, test: varTestCase): bool =
- for t in s:
- if t.file == file:
- test = t
- returntrue
-
- # this do not work anymore with the new code output escape. will need to fix
- func spanColor*(text, color: string): string = "<span style=\"color:" & color & "\">" & text & "</span>"
- func aLink*(text, link: string): string = "<a href=\"" & link & "\">" & text & "</a>"
-
- func stats*(tests: seq[TestCase]): tuple[skip, fail, pass: int] =
- for test in tests:
- if test.skip:
- inc result.skip
- continue
- elif test.fail:
- inc result.fail
- else:
- inc result.pass
-
- var
- test: TestCase
- tests: seq[TestCase]
-
-whendefined(nbDebug):
- nbText: "Current directory as results of calling nbInit:"
- nbCode: echo getCurrentDir()
-
-nbText: """### test cases
-for every file ending in nim (and not starting with ptest) in nbHomeDir (test cases)
-- a test case will be skipped if it is not tracked in git
-- a test case will be skipped if it does not have a corresponding html
-- a test case will be skipped if it is modified/added in git
-"""
-nbCode:
- let skipList = @["penguins.nim"]
- for file in walkDirRec(nbHomeDir):
- ## note that file is an AbsoluteFile (since nbHomeDir is an AbsoluteDir)
- if file.endsWith(".nim") andnot file.name.startsWith("ptest") and file.relPath notin skipList:
- test = TestCase(file: file)
- stdout.write "added test candidate: ", file.relPath
- else:
- continue
- let html = changeFileExt(file, ".html")
- ifnot file.isGitTracked:
- test.skip = true
- test.skipReason = srNotTrackedInGit
- echo" -> skipped since it is not tracked in git"
- elifnot html.fileExists ornot html.isGitTracked:
- test.skip = true
- test.skipReason = srNoHtmlFound
- echo" -> skipped since it does not have a corresponding (git tracked) html file"
- elif file.isGitChanged:
- test.skip = true
- test.skipReason = srChangedInGit
- echo" -> skipped since it is changed in git"
- else:
- echo""
- tests.add test
-
- # for file in gitChangedFiles():
- # if tests.find(file, test) and not test.skip:
- # test.skip = true
- # test.skipReason = srChangedInGit
- # echo " -> skipped since it is changed in git", file.relPath
-
-nbText: """### performing test
-for every non-skipped test case:
-- if the corresponding html output is changed in git the test is failed
-- copy the corresponding html to a tmp file
-- find and execute the corresponding nim file
-- if an error is found while compiling/executing, report a failure
-- if a difference is reported between the corresponding html file and the temporary copy a failure is reported
-- if no failure has been reported the test is reported as passed (and the temporary file is cleaned up)
-"""
-nbCode:
- for test in tests:
- var
- output: string
- err: int
- let
- html = test.file.changeFileExt(".html")
- tmp = test.file.changeFileExt(".tmp.html")
- fileWithLink = test.file.relPath.aLink html.relPath
- if test.skip:
- stdout.write "[SKIP]".spanColor "blue"
- echo" " & fileWithLink
- continue
- if html.isGitChanged:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"html file is changed in git. commit or revert and rerun test"
- continue
- copyFile(source=html, dest=tmp)
- (output, err) = execCmdEx &"nim --verbosity:0 --hints:off -d:nimibHomeDir=. r {test.file}"
- if err != 0:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"Error during compile/run"
- echo"output\n:", output
- echo"err\n:", err
- continue
- (output, err) = execCmdEx(&"git diff -w --ignore-space-change --no-index {html} {tmp}")
- if err != 0:
- test.fail = true
- stdout.write "[FAIL]".spanColor "red"
- echo" " & fileWithLink
- echo"Differences found in html file"
- echo"output\n:", output
- echo"err\n:", err
- continue
- stdout.write "[OK]".spanColor "green"
- echo" " & fileWithLink
- removeFile(tmp)
-
-let details = nbBlock.output
-
-nbText: "results are appended to previously save resultsBlock"
-nbCode:
- let (skip, fail, pass) = tests.stats
- if fail > 0:
- resultsBlock.output.add " β"# slug for this? now emoji.muan is not helpful anymore...
- else:
- resultsBlock.output.add " β "
- let
- skipHeader = "skip".spanColor "blue"
- failHeader = "fail".spanColor "red"
- passHeader = "pass".spanColor "green"
- resultsBlock.output.add &"\n|{skipHeader}|{failHeader}|{passHeader}|total|"
- resultsBlock.output.add &"\n|---|---|---|---|"
- resultsBlock.output.add &"\n|{skip}|{fail}|{pass}|{skip + fail + pass}|"
- resultsBlock.output.add &"\n\n{details.renderHtmlCodeOutput}"
-
-nbSave
-quit(fail)
-